Commit 5263cbac authored by sakoju-nisum-com's avatar sakoju-nisum-com Committed by rbonthala-nisum-com

MT-77 and MT-81: saving and fetching the account details (#19)

parent 85e930bd
package com.nisum.mytime.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.AccountInfo;
import com.nisum.mytime.service.AccountServiceImpl;
import com.nisum.mytime.utils.MyTimeUtils;
@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountServiceImpl accountServiceImpl;
@RequestMapping(value = "/addAccount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AccountInfo> addAccount(@RequestBody AccountInfo accountInfo) throws MyTimeException {
if (StringUtils.isEmpty(accountInfo.getAccountId())) {
accountInfo.setAccountId(getAccountId());
}
accountInfo.setStatus(MyTimeUtils.ACTIVE);
AccountInfo currentAccount = accountServiceImpl.addAccount(accountInfo);
return new ResponseEntity<>(currentAccount, HttpStatus.OK);
}
// getting the account id.
// accountid format is "Acc001"
private String getAccountId() throws MyTimeException {
return (MyTimeUtils.ACC_NAME + MyTimeUtils.ZERO_) + (accountServiceImpl.getAccounts().size() + MyTimeUtils.ONE);
}
@RequestMapping(value = "/getAccounts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Map<Object, Object>>> getAccounts() throws MyTimeException {
List<Map<Object, Object>> acounts = accountServiceImpl.getAccountsList();
return new ResponseEntity<>(acounts, HttpStatus.OK);
}
@RequestMapping(value = "/getAccountNames", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<String>> getAccountNames() throws MyTimeException {
List<AccountInfo> acounts = accountServiceImpl.getAccounts();
List<String> accountNames = new ArrayList<>();
for (AccountInfo account : acounts) {
accountNames.add(account.getAccountName());
}
return new ResponseEntity<>(accountNames, HttpStatus.OK);
}
@RequestMapping(value = "/deleteAccount", method = RequestMethod.DELETE, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> deleteAccount(@RequestParam(value = "accountId") String accountId)
throws MyTimeException {
accountServiceImpl.deleteAccount(accountId);
return new ResponseEntity<>("Success", HttpStatus.OK);
}
}
\ No newline at end of file
package com.nisum.mytime.model;
import java.io.Serializable;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document(collection = "AccountInfo")
public class AccountInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private ObjectId id;
private String accountId;
private String accountName;
private String status;
private String clientAddress;
private String industryType;
List<String> deliveryManagers;
}
package com.nisum.mytime.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.nisum.mytime.model.AccountInfo;
public interface AccountInfoRepo extends MongoRepository<AccountInfo, String> {
AccountInfo findByAccountName(String accontName);
}
\ No newline at end of file
package com.nisum.mytime.service;
import java.util.List;
import java.util.Map;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.AccountInfo;
public interface AccountService {
AccountInfo addAccount(AccountInfo account) throws MyTimeException;
List<AccountInfo> getAccounts() throws MyTimeException;
List<Map<Object, Object>> getAccountsList() throws MyTimeException;
}
package com.nisum.mytime.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.AccountInfo;
import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.repository.AccountInfoRepo;
import com.nisum.mytime.utils.MyTimeUtils;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private AccountInfoRepo accountRepo;
@Override
public AccountInfo addAccount(AccountInfo account) throws MyTimeException {
return accountRepo.save(account);
}
@Override
public List<AccountInfo> getAccounts() throws MyTimeException {
return accountRepo.findAll();
}
@Override
public List<Map<Object, Object>> getAccountsList() throws MyTimeException {
List<Map<Object, Object>> updatedAccountList = new ArrayList<>();
List<Map<String, String>> updatedEmployeeList = null;
for (AccountInfo account : getAccountInfo()) {
updatedEmployeeList = new ArrayList<>();
for (EmployeeRoles employeesRole : getEmployeeDetails(account)) {
updatedEmployeeList.add(getEmployeeDetails(employeesRole));
}
updatedAccountList.add(getAccuntDetails(account, updatedEmployeeList));
}
return updatedAccountList;
}
// fetching the employee details using employeeId.
private List<EmployeeRoles> getEmployeeDetails(AccountInfo account) {
List<EmployeeRoles> employeeRoles = mongoTemplate.find(
new Query(Criteria.where(MyTimeUtils.EMPLOYEE_ID).in(account.getDeliveryManagers())),
EmployeeRoles.class);
return employeeRoles;
}
// fetching the active account details.
private List<AccountInfo> getAccountInfo() {
List<AccountInfo> accountList = mongoTemplate
.find(new Query(Criteria.where(MyTimeUtils.STATUS).is(MyTimeUtils.ACTIVE)), AccountInfo.class);
return accountList;
}
private HashMap<String, String> getEmployeeDetails(EmployeeRoles employeesRole) {
HashMap<String, String> employeeDetails = new HashMap<>();
employeeDetails.put(MyTimeUtils.EMPLOYEE_ID, employeesRole.getEmployeeId());
employeeDetails.put(MyTimeUtils.EMPLOYEE_NAME, employeesRole.getEmployeeName());
return employeeDetails;
}
private Map<Object, Object> getAccuntDetails(AccountInfo account, List<Map<String, String>> updatedEmployeeList) {
Map<Object, Object> accountDetails = new HashMap<>();
accountDetails.put(MyTimeUtils.ACC_ID, account.getId());
accountDetails.put(MyTimeUtils.ACCOUNT_ID, account.getAccountId());
accountDetails.put(MyTimeUtils.ACCOUNT_NAME, account.getAccountName());
accountDetails.put(MyTimeUtils.STATUS, account.getStatus());
accountDetails.put(MyTimeUtils.CLIENT_ADDRESS, account.getClientAddress());
accountDetails.put(MyTimeUtils.INDUSTRY_TYPE, account.getIndustryType());
accountDetails.put(MyTimeUtils.DELIVERY_MANAGERS, updatedEmployeeList);
return accountDetails;
}
// updating the status to "InActive".
public AccountInfo deleteAccount(String accountId) throws MyTimeException {
Query query = new Query(Criteria.where(MyTimeUtils.ACCOUNT_ID).is(accountId));
Update update = new Update();
update.set(MyTimeUtils.STATUS, MyTimeUtils.IN_ACTIVE);
FindAndModifyOptions options = new FindAndModifyOptions();
options.upsert(true);
return mongoTemplate.findAndModify(query, update, options, AccountInfo.class);
}
}
...@@ -37,6 +37,19 @@ public class MyTimeUtils { ...@@ -37,6 +37,19 @@ public class MyTimeUtils {
public final static String ABESENT_QUERY1 = ") AND STATUS='Working' AND EMPLOYEECODE NOT LIKE 'del%' "; public final static String ABESENT_QUERY1 = ") AND STATUS='Working' AND EMPLOYEECODE NOT LIKE 'del%' ";
public final static String ABESENT ="Absent"; public final static String ABESENT ="Absent";
public final static String EMAIL_ID = "emailId"; public final static String EMAIL_ID = "emailId";
public final static String ACCOUNT_ID = "accountId";
public static final int ONE = 1;
public final static String ACC_NAME = "Acc";
public static final String ZERO_ = "00";
// Manage account details
public static final String ACC_ID = "id";
public static final String ACCOUNT_NAME = "accountName";
public static final String STATUS = "status";
public static final String CLIENT_ADDRESS = "clientAddress";
public static final String INDUSTRY_TYPE = "industryType";
public static final String DELIVERY_MANAGERS = "deliveryManagers";
public static final String ACTIVE = "Active";
public static final String IN_ACTIVE = "InActive";
public final static String TEAMDETAILS_COLLECTION_NAME = "TeamDetails"; public final static String TEAMDETAILS_COLLECTION_NAME = "TeamDetails";
public final static String BILLINGDETAILS_COLLECTION_NAME = "BillingDetails"; public final static String BILLINGDETAILS_COLLECTION_NAME = "BillingDetails";
public final static String ENDDATE_COLUMN = "endDate"; public final static String ENDDATE_COLUMN = "endDate";
...@@ -45,9 +58,6 @@ public class MyTimeUtils { ...@@ -45,9 +58,6 @@ public class MyTimeUtils {
public final static String PROJECT_NAME = "projectName"; public final static String PROJECT_NAME = "projectName";
public final static String SET = "$set"; public final static String SET = "$set";
public final static int MINUS_ONE = -1; public final static int MINUS_ONE = -1;
public final static int ONE = 1;
public final static String FREE_POLL="Free Pool"; public final static String FREE_POLL="Free Pool";
public final static String START_DATE="startDate"; public final static String START_DATE="startDate";
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment