Commit b46b0f61 authored by bsatyanarayana-nisum-com's avatar bsatyanarayana-nisum-com Committed by tdutta-nisum-com

MT-97 : SNS :: Update_RoleTable_While_Account_CRUD_operations (#56)

parent 4a9f9aa5
...@@ -4,7 +4,6 @@ import java.util.ArrayList; ...@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -17,9 +16,8 @@ import org.springframework.web.bind.annotation.RequestParam; ...@@ -17,9 +16,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.nisum.mytime.exception.handler.MyTimeException; import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.AccountInfo; import com.nisum.mytime.model.Account;
import com.nisum.mytime.service.AccountServiceImpl; import com.nisum.mytime.service.AccountServiceImpl;
import com.nisum.mytime.utils.MyTimeUtils;
@RestController @RestController
@RequestMapping("/account") @RequestMapping("/account")
...@@ -30,43 +28,12 @@ public class AccountController { ...@@ -30,43 +28,12 @@ public class AccountController {
@RequestMapping(value = "/accounts", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/accounts", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> addAccount(@RequestBody AccountInfo accountInfo,@RequestParam(value = "action") String action) throws MyTimeException { public ResponseEntity<String> addAccount(@RequestBody Account account,@RequestParam(value = "action") String action) throws MyTimeException {
String response = accountServiceImpl.addAccount(account, action);
String response="";
AccountInfo account= null;
if( action != null && action.equals("N")) {
List<AccountInfo> acounts = accountServiceImpl.validateAccounts(accountInfo.getAccountName());
if( !acounts.isEmpty() && acounts.size() > 0){
response ="Account already exist";
}else {
accountInfo.setAccountId(generateAccountId());
accountInfo.setStatus(MyTimeUtils.ACTIVE);
account = accountServiceImpl.addAccount(accountInfo);
if(account.getId() != null) {
response="saved Succesfully";
}else {
response = "Error occured while Account creating";
}
}
}else if(action != null && action.equals("U")){
accountInfo.setStatus(MyTimeUtils.ACTIVE);
account = accountServiceImpl.addAccount(accountInfo);
response="updated Succesfully";
}
return new ResponseEntity<>(response, HttpStatus.OK); return new ResponseEntity<>(response, HttpStatus.OK);
} }
// generating the account id.
// accountId format is "Acc001"
private String generateAccountId() throws MyTimeException {
return (MyTimeUtils.ACC + MyTimeUtils.ZERO_) + (accountServiceImpl.getAccounts().size() + MyTimeUtils.ONE);
}
@RequestMapping(value = "/accounts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/accounts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Map<Object, Object>>> getAccounts() throws MyTimeException { public ResponseEntity<List<Map<Object, Object>>> getAccounts() throws MyTimeException {
List<Map<Object, Object>> acounts = accountServiceImpl.getAccountsList(); List<Map<Object, Object>> acounts = accountServiceImpl.getAccountsList();
...@@ -75,9 +42,9 @@ public class AccountController { ...@@ -75,9 +42,9 @@ public class AccountController {
@RequestMapping(value = "/accountNames", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/accountNames", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<String>> getAccountNames() throws MyTimeException { public ResponseEntity<List<String>> getAccountNames() throws MyTimeException {
List<AccountInfo> acounts = accountServiceImpl.getAccounts(); List<Account> acounts = accountServiceImpl.getAccounts();
List<String> accountNames = new ArrayList<>(); List<String> accountNames = new ArrayList<>();
for (AccountInfo account : acounts) { for (Account account : acounts) {
accountNames.add(account.getAccountName()); accountNames.add(account.getAccountName());
} }
return new ResponseEntity<>(accountNames, HttpStatus.OK); return new ResponseEntity<>(accountNames, HttpStatus.OK);
...@@ -93,7 +60,7 @@ public class AccountController { ...@@ -93,7 +60,7 @@ public class AccountController {
@RequestMapping(value = "/accounts/{accountName}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) @RequestMapping(value = "/accounts/{accountName}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> validateAccounts(@PathVariable("accountName") String accountName) throws MyTimeException { public ResponseEntity<String> validateAccounts(@PathVariable("accountName") String accountName) throws MyTimeException {
String response =""; String response ="";
List<AccountInfo> acounts = accountServiceImpl.validateAccounts(accountName); List<Account> acounts = accountServiceImpl.validateAccounts(accountName);
if(acounts.size()>0){ if(acounts.size()>0){
response ="Account already exist"; response ="Account already exist";
} }
......
...@@ -29,7 +29,11 @@ public class Account implements Serializable { ...@@ -29,7 +29,11 @@ public class Account implements Serializable {
private String accountName; private String accountName;
private int accountProjectSequence; private int accountProjectSequence;
private String status; private String status;
private String domain; //private String domain;
List<String> subDomains; //List<String> subDomains;
private String clientAddress;
private String industryType;
private List<String> deliveryManagers;
} }
...@@ -7,4 +7,5 @@ import com.nisum.mytime.model.Account; ...@@ -7,4 +7,5 @@ import com.nisum.mytime.model.Account;
public interface AccountRepo extends MongoRepository<Account, String> { public interface AccountRepo extends MongoRepository<Account, String> {
Account findByAccountName(String accontName); Account findByAccountName(String accontName);
Account findByAccountId(String accountId);
} }
\ No newline at end of file
...@@ -4,16 +4,16 @@ import java.util.List; ...@@ -4,16 +4,16 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.nisum.mytime.exception.handler.MyTimeException; import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.AccountInfo; import com.nisum.mytime.model.Account;
public interface AccountService { public interface AccountService {
AccountInfo addAccount(AccountInfo account) throws MyTimeException; String addAccount(Account account,String action) throws MyTimeException;
List<AccountInfo> getAccounts() throws MyTimeException; List<Account> getAccounts() throws MyTimeException;
List<Map<Object, Object>> getAccountsList() throws MyTimeException; List<Map<Object, Object>> getAccountsList() throws MyTimeException;
List<AccountInfo> validateAccounts(String accountName) throws MyTimeException; List<Account> validateAccounts(String accountName) throws MyTimeException;
} }
package com.nisum.mytime.service; package com.nisum.mytime.service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -14,9 +15,11 @@ import org.springframework.data.mongodb.core.query.Update; ...@@ -14,9 +15,11 @@ import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.nisum.mytime.exception.handler.MyTimeException; import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.Account;
import com.nisum.mytime.model.AccountInfo; import com.nisum.mytime.model.AccountInfo;
import com.nisum.mytime.model.EmployeeRoles; import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.repository.AccountInfoRepo; import com.nisum.mytime.repository.AccountRepo;
import com.nisum.mytime.utils.CommomUtil;
import com.nisum.mytime.utils.MyTimeUtils; import com.nisum.mytime.utils.MyTimeUtils;
@Service @Service
...@@ -26,15 +29,91 @@ public class AccountServiceImpl implements AccountService { ...@@ -26,15 +29,91 @@ public class AccountServiceImpl implements AccountService {
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
@Autowired @Autowired
private AccountInfoRepo accountRepo; private AccountRepo accountRepo;
@Autowired
private RoleInfoService roleInfoService;
@Autowired
private RoleMappingService roleMappingService;
@Override @Override
public AccountInfo addAccount(AccountInfo account) throws MyTimeException { public String addAccount(Account account,String action) throws MyTimeException {
return accountRepo.save(account); String response = "";
Account accountResult= null;
String roleId = roleInfoService.getRole(MyTimeUtils.ACCOUNT);
if( action != null && action.equals("N")) {//N means adding New Account
List<Account> accounts = validateAccounts(account.getAccountName());
if( !accounts.isEmpty() && accounts.size() > 0){
response ="Account already exist";
}else {
List<String> accDms = account.getDeliveryManagers();
account.setAccountId(generateAccountId());
account.setStatus(MyTimeUtils.ACTIVE);
accountResult = accountRepo.save(account);
if(accDms != null && !accDms.isEmpty() && accDms.size() > 0) {
roleMappingService.saveUniqueEmployeeAndRole(accDms, roleId);
}
if(accountResult.getId() != null) {
response="Saved succesfully";
}else {
response = "Error occured while account creating";
}
}
}else if(action != null && action.equals("U")){//U means updating existing Account
List<String> employeeIds = null;
List<Account> accountList = null;
List<String> dmsAddedByUser = null;
List<String> dmsDeletedByUser = null;
List<String> accountEmpIds = new ArrayList<String>();
Map<String, Integer> dmsCount = new HashMap<String,Integer>();
List<String> updatedAccDms = account.getDeliveryManagers();
List<String> beforeAccUpdateDms = accountRepo.findByAccountId(account.getAccountId()).getDeliveryManagers();
dmsAddedByUser = CommomUtil.getAddedManagersList(beforeAccUpdateDms, updatedAccDms);
dmsDeletedByUser = CommomUtil.getDeletedManagersList(beforeAccUpdateDms, updatedAccDms);
accountList = accountRepo.findAll();
if(accountList !=null && !accountList.isEmpty() && accountList.size() >0) {
for (Account acc : accountList) {
employeeIds=acc.getDeliveryManagers();
if(employeeIds != null && !employeeIds.isEmpty() && employeeIds.size() >0) {
for(String eId:employeeIds) {
if(dmsCount.get(eId)!=null)
dmsCount.put(eId, dmsCount.get(eId)+1);
else
dmsCount.put(eId, 1);
employeeIds = null;
}
}
}
}
for (String empId : dmsDeletedByUser) {
int occurrences = Collections.frequency(accountEmpIds, empId);
if(occurrences==1) {
//Service call for RoleMapping
roleMappingService.deleteRole(empId,roleId);
}
}
account.setStatus(MyTimeUtils.ACTIVE);
accountResult = accountRepo.save(account);
response="Updated succesfully";
}
return response;
} }
@Override @Override
public List<AccountInfo> getAccounts() throws MyTimeException { public List<Account> getAccounts() throws MyTimeException {
return accountRepo.findAll(); return accountRepo.findAll();
} }
...@@ -42,7 +121,7 @@ public class AccountServiceImpl implements AccountService { ...@@ -42,7 +121,7 @@ public class AccountServiceImpl implements AccountService {
public List<Map<Object, Object>> getAccountsList() throws MyTimeException { public List<Map<Object, Object>> getAccountsList() throws MyTimeException {
List<Map<Object, Object>> updatedAccountList = new ArrayList<>(); List<Map<Object, Object>> updatedAccountList = new ArrayList<>();
List<Map<String, String>> updatedEmployeeList = null; List<Map<String, String>> updatedEmployeeList = null;
for (AccountInfo account : accountRepo.findAll()) { for (Account account : accountRepo.findAll()) {
updatedEmployeeList = new ArrayList<>(); updatedEmployeeList = new ArrayList<>();
for (EmployeeRoles employeesRole : getEmployeeDetails(account)) { for (EmployeeRoles employeesRole : getEmployeeDetails(account)) {
updatedEmployeeList.add(getEmployeeDetails(employeesRole)); updatedEmployeeList.add(getEmployeeDetails(employeesRole));
...@@ -53,15 +132,15 @@ public class AccountServiceImpl implements AccountService { ...@@ -53,15 +132,15 @@ public class AccountServiceImpl implements AccountService {
} }
@Override @Override
public List<AccountInfo> validateAccounts(String accountName) throws MyTimeException { public List<Account> validateAccounts(String accountName) throws MyTimeException {
List<AccountInfo> accountList = mongoTemplate.find(new Query(Criteria.where(MyTimeUtils.ACCOUNT_NAME).is(accountName.trim())), AccountInfo.class); List<Account> accountList = mongoTemplate.find(new Query(Criteria.where(MyTimeUtils.ACCOUNT_NAME).is(accountName.trim())), Account.class);
return accountList; return accountList;
} }
// fetching the employee details using employeeId. // fetching the employee details using employeeId.
private List<EmployeeRoles> getEmployeeDetails(AccountInfo account) { private List<EmployeeRoles> getEmployeeDetails(Account account) {
List<EmployeeRoles> employeeRoles = mongoTemplate.find( List<EmployeeRoles> employeeRoles = mongoTemplate.find(
new Query(Criteria.where(MyTimeUtils.EMPLOYEE_ID).in(account.getDeliveryManagers())), new Query(Criteria.where(MyTimeUtils.EMPLOYEE_ID).in(account.getDeliveryManagers())),
EmployeeRoles.class); EmployeeRoles.class);
...@@ -75,7 +154,7 @@ public class AccountServiceImpl implements AccountService { ...@@ -75,7 +154,7 @@ public class AccountServiceImpl implements AccountService {
return employeeDetails; return employeeDetails;
} }
private Map<Object, Object> getAccuntDetails(AccountInfo account, List<Map<String, String>> updatedEmployeeList) { private Map<Object, Object> getAccuntDetails(Account account, List<Map<String, String>> updatedEmployeeList) {
Map<Object, Object> accountDetails = new HashMap<>(); Map<Object, Object> accountDetails = new HashMap<>();
accountDetails.put(MyTimeUtils.ID_, account.getId()); accountDetails.put(MyTimeUtils.ID_, account.getId());
accountDetails.put(MyTimeUtils.ACCOUNT_ID, account.getAccountId()); accountDetails.put(MyTimeUtils.ACCOUNT_ID, account.getAccountId());
...@@ -88,13 +167,19 @@ public class AccountServiceImpl implements AccountService { ...@@ -88,13 +167,19 @@ public class AccountServiceImpl implements AccountService {
} }
// updating the status to "InActive". // updating the status to "InActive".
public AccountInfo deleteAccount(String accountId) throws MyTimeException { public Account deleteAccount(String accountId) throws MyTimeException {
Query query = new Query(Criteria.where(MyTimeUtils.ACCOUNT_ID).is(accountId)); Query query = new Query(Criteria.where(MyTimeUtils.ACCOUNT_ID).is(accountId));
Update update = new Update(); Update update = new Update();
update.set(MyTimeUtils.STATUS, MyTimeUtils.IN_ACTIVE); update.set(MyTimeUtils.STATUS, MyTimeUtils.IN_ACTIVE);
FindAndModifyOptions options = new FindAndModifyOptions(); FindAndModifyOptions options = new FindAndModifyOptions();
options.upsert(true); options.upsert(true);
return mongoTemplate.findAndModify(query, update, options, AccountInfo.class); return mongoTemplate.findAndModify(query, update, options, Account.class);
}
// generating the account id.
// accountId format is "Acc001"
private String generateAccountId() throws MyTimeException {
return (MyTimeUtils.ACC + MyTimeUtils.ZERO_) + (getAccounts().size() + MyTimeUtils.ONE);
} }
} }
...@@ -20,7 +20,7 @@ import com.nisum.mytime.model.AccountInfo; ...@@ -20,7 +20,7 @@ import com.nisum.mytime.model.AccountInfo;
import com.nisum.mytime.model.Domains; import com.nisum.mytime.model.Domains;
import com.nisum.mytime.model.EmployeeRoles; import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.repository.DomainRepo; import com.nisum.mytime.repository.DomainRepo;
import com.nisum.mytime.utils.CommomUtil;
import com.nisum.mytime.utils.MyTimeUtils; import com.nisum.mytime.utils.MyTimeUtils;
...@@ -126,8 +126,8 @@ public class DomainServiceImpl implements DomainService { ...@@ -126,8 +126,8 @@ public class DomainServiceImpl implements DomainService {
if (null != domainDetailsFromDb) if (null != domainDetailsFromDb)
deliveryManagersListFromDb = domainDetailsFromDb.getDeliveryManagers(); deliveryManagersListFromDb = domainDetailsFromDb.getDeliveryManagers();
managersAddedByUser = getAddedManagersList(deliveryManagersListFromDb, deliveryManagersListFromUser); managersAddedByUser = CommomUtil.getAddedManagersList(deliveryManagersListFromDb, deliveryManagersListFromUser);
managersDeletedByUser = getDeletedManagersList(deliveryManagersListFromDb, deliveryManagersListFromUser); managersDeletedByUser = CommomUtil.getDeletedManagersList(deliveryManagersListFromDb, deliveryManagersListFromUser);
domainList = domainRepo.findAll(); domainList = domainRepo.findAll();
String roleId = roleInfoService.getRole(MyTimeUtils.DOMAIN); String roleId = roleInfoService.getRole(MyTimeUtils.DOMAIN);
...@@ -190,24 +190,4 @@ public class DomainServiceImpl implements DomainService { ...@@ -190,24 +190,4 @@ public class DomainServiceImpl implements DomainService {
return selectedDomain.get(0); return selectedDomain.get(0);
return null; return null;
} }
private List<String> getAddedManagersList(List<String> fromDb, List<String> fromUser) {
List<String> addedManagers = new ArrayList<String>();
if (fromDb != null)
for (String managerFromUser : fromUser) {
if (!fromDb.contains(managerFromUser))
addedManagers.add(managerFromUser);
}
return addedManagers;
}
private List<String> getDeletedManagersList(List<String> fromDb, List<String> fromUser) {
List<String> deletedManager = new ArrayList<String>();
if (fromDb != null)
for (String managerFromDb : fromDb) {
if (!fromUser.contains(managerFromDb))
deletedManager.add(managerFromDb);
}
return deletedManager;
}
} }
\ No newline at end of file
package com.nisum.mytime.utils;
import java.util.ArrayList;
import java.util.List;
public class CommomUtil {
public static List<String> getAddedManagersList(List<String> fromDb, List<String> fromUser) {
List<String> addedManagers = new ArrayList<String>();
if (fromDb != null)
for (String managerFromUser : fromUser) {
if (!fromDb.contains(managerFromUser))
addedManagers.add(managerFromUser);
}
return addedManagers;
}
public static List<String> getDeletedManagersList(List<String> fromDb, List<String> fromUser) {
List<String> deletedManager = new ArrayList<String>();
if (fromDb != null)
for (String managerFromDb : fromDb) {
if (!fromUser.contains(managerFromDb))
deletedManager.add(managerFromDb);
}
return deletedManager;
}
}
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