Commit 88eb83a8 authored by bsatyanarayana-nisum-com's avatar bsatyanarayana-nisum-com Committed by rbonthala-nisum-com

MT-97_3 : SNS :: Update_RoleTable_While_Account_CRUD_operations (#74)

parent 913d2950
package com.nisum.mytime.repository; package com.nisum.mytime.repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import com.nisum.mytime.model.Account; import com.nisum.mytime.model.Account;
...@@ -9,4 +11,6 @@ public interface AccountRepo extends MongoRepository<Account, String> { ...@@ -9,4 +11,6 @@ public interface AccountRepo extends MongoRepository<Account, String> {
Account findByAccountName(String accontName); Account findByAccountName(String accontName);
Account findByAccountId(String accountId); Account findByAccountId(String accountId);
List<Account> findByaccountNameAndAccountId(String accountName,String accountId);
} }
\ No newline at end of file
...@@ -16,7 +16,6 @@ import org.springframework.stereotype.Service; ...@@ -16,7 +16,6 @@ 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.Account;
import com.nisum.mytime.model.AccountInfo;
import com.nisum.mytime.model.EmployeeRoles; import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.repository.AccountRepo; import com.nisum.mytime.repository.AccountRepo;
import com.nisum.mytime.utils.CommomUtil; import com.nisum.mytime.utils.CommomUtil;
...@@ -45,8 +44,9 @@ public class AccountServiceImpl implements AccountService { ...@@ -45,8 +44,9 @@ public class AccountServiceImpl implements AccountService {
String roleId = roleInfoService.getRole(MyTimeUtils.ACCOUNT); String roleId = roleInfoService.getRole(MyTimeUtils.ACCOUNT);
List<Account> accounts = validateAccounts(account.getAccountName());
if( action != null && action.equals(MyTimeUtils.STRING_N)) {//N means adding New Account if( action != null && action.equals(MyTimeUtils.STRING_N)) {//N means adding New Account
List<Account> accounts = validateAccounts(account.getAccountName());
if( !accounts.isEmpty() && accounts.size() > 0){ if( !accounts.isEmpty() && accounts.size() > 0){
response ="Account already exist"; response ="Account already exist";
...@@ -67,47 +67,54 @@ public class AccountServiceImpl implements AccountService { ...@@ -67,47 +67,54 @@ public class AccountServiceImpl implements AccountService {
} }
} }
}else if(action != null && action.equals("U")){//U means updating existing Account }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); if( !accounts.isEmpty() && accounts.size() > 0){
dmsDeletedByUser = CommomUtil.getDeletedManagersList(beforeAccUpdateDms, updatedAccDms); response ="Account already exist with updated name";
} else {
accountList = accountRepo.findAll(); List<String> employeeIds = null;
if(accountList !=null && !accountList.isEmpty() && accountList.size() >0) { List<Account> accountList = null;
for (Account acc : accountList) { List<String> dmsAddedByUser = null;
employeeIds=acc.getDeliveryManagers(); List<String> dmsDeletedByUser = null;
if(employeeIds != null && !employeeIds.isEmpty() && employeeIds.size() >0) {
for(String eId:employeeIds) { Map<String, Integer> dmsCount = new HashMap<String,Integer>();
if(dmsCount.get(eId)!=null)
dmsCount.put(eId, dmsCount.get(eId)+1); Account accountBeforeUpdate = accountRepo.findByAccountId(account.getAccountId());
else
dmsCount.put(eId, 1); List<String> updatedAccDms = account.getDeliveryManagers();
employeeIds = null; List<String> beforeAccUpdateDms = accountBeforeUpdate.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) {
if(dmsCount.get(empId)==1) {
//Service call for RoleMapping
roleMappingService.deleteRole(empId,roleId);
}
}
//account.setStatus(MyTimeUtils.STRING_Y);
account.setStatus(accountBeforeUpdate.getStatus());
accountResult = accountRepo.save(account);
roleMappingService.saveUniqueEmployeeAndRole(dmsAddedByUser, roleId);
response="Updated succesfully";
} }
for (String empId : dmsDeletedByUser) {
int occurrences = Collections.frequency(accountEmpIds, empId);
if(occurrences==1) {
//Service call for RoleMapping
roleMappingService.deleteRole(empId,roleId);
}
}
account.setStatus(MyTimeUtils.STRING_Y);
accountResult = accountRepo.save(account);
response="Updated succesfully";
} }
return response; return response;
} }
...@@ -168,11 +175,40 @@ public class AccountServiceImpl implements AccountService { ...@@ -168,11 +175,40 @@ public class AccountServiceImpl implements AccountService {
// updating the status to "InActive". // updating the status to "InActive".
public Account deleteAccount(String accountId) throws MyTimeException { public Account deleteAccount(String accountId) throws MyTimeException {
int occurrences = 0;
List<Account> accountsList = null;
List<String> accountDms = null;
List<String> accountsDms = new ArrayList<String>();
String roleId = roleInfoService.getRole(MyTimeUtils.ACCOUNT);
accountsList = accountRepo.findAll();
List<String> deletedAccountDms = accountRepo.findByAccountId(accountId).getDeliveryManagers();
for (Account account : accountsList) {
accountDms = account.getDeliveryManagers();
for(String accountDm:accountDms)
accountsDms.add(accountDm);
accountDms = null;
}
for (String dmId : deletedAccountDms) {
occurrences = Collections.frequency(accountsDms, dmId);
if(occurrences==1) {
//Service call for RoleMapping
roleMappingService.deleteRole(dmId,roleId);
}
}
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.STRING_N); update.set(MyTimeUtils.STATUS, MyTimeUtils.STRING_N);
FindAndModifyOptions options = new FindAndModifyOptions(); FindAndModifyOptions options = new FindAndModifyOptions();
options.upsert(true); options.upsert(true);
return mongoTemplate.findAndModify(query, update, options, Account.class); return mongoTemplate.findAndModify(query, update, options, Account.class);
} }
...@@ -181,5 +217,13 @@ public class AccountServiceImpl implements AccountService { ...@@ -181,5 +217,13 @@ public class AccountServiceImpl implements AccountService {
private String generateAccountId() throws MyTimeException { private String generateAccountId() throws MyTimeException {
return (MyTimeUtils.ACC + MyTimeUtils.ZERO_) + (getAccounts().size() + MyTimeUtils.ONE); return (MyTimeUtils.ACC + MyTimeUtils.ZERO_) + (getAccounts().size() + MyTimeUtils.ONE);
} }
private boolean duplicateCheck(String accountName,String accountId){
boolean check=false;
List<Account> accountList=accountRepo.findByaccountNameAndAccountId(accountName, accountId);
if(accountList.size()>0)
check= true;
return check;
}
} }
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