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,9 +44,10 @@ public class AccountServiceImpl implements AccountService { ...@@ -45,9 +44,10 @@ public class AccountServiceImpl implements AccountService {
String roleId = roleInfoService.getRole(MyTimeUtils.ACCOUNT); String roleId = roleInfoService.getRole(MyTimeUtils.ACCOUNT);
if( action != null && action.equals(MyTimeUtils.STRING_N)) {//N means adding New Account
List<Account> accounts = validateAccounts(account.getAccountName()); List<Account> accounts = validateAccounts(account.getAccountName());
if( action != null && action.equals(MyTimeUtils.STRING_N)) {//N means adding New Account
if( !accounts.isEmpty() && accounts.size() > 0){ if( !accounts.isEmpty() && accounts.size() > 0){
response ="Account already exist"; response ="Account already exist";
}else { }else {
...@@ -67,27 +67,32 @@ public class AccountServiceImpl implements AccountService { ...@@ -67,27 +67,32 @@ 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
if( !accounts.isEmpty() && accounts.size() > 0){
response ="Account already exist with updated name";
} else {
List<String> employeeIds = null; List<String> employeeIds = null;
List<Account> accountList = null; List<Account> accountList = null;
List<String> dmsAddedByUser = null; List<String> dmsAddedByUser = null;
List<String> dmsDeletedByUser = null; List<String> dmsDeletedByUser = null;
List<String> accountEmpIds = new ArrayList<String>();
Map<String, Integer> dmsCount = new HashMap<String,Integer>(); Map<String, Integer> dmsCount = new HashMap<String,Integer>();
Account accountBeforeUpdate = accountRepo.findByAccountId(account.getAccountId());
List<String> updatedAccDms = account.getDeliveryManagers(); List<String> updatedAccDms = account.getDeliveryManagers();
List<String> beforeAccUpdateDms = accountRepo.findByAccountId(account.getAccountId()).getDeliveryManagers(); List<String> beforeAccUpdateDms = accountBeforeUpdate.getDeliveryManagers();
dmsAddedByUser = CommomUtil.getAddedManagersList(beforeAccUpdateDms, updatedAccDms); dmsAddedByUser = CommomUtil.getAddedManagersList(beforeAccUpdateDms, updatedAccDms);
dmsDeletedByUser = CommomUtil.getDeletedManagersList(beforeAccUpdateDms, updatedAccDms); dmsDeletedByUser = CommomUtil.getDeletedManagersList(beforeAccUpdateDms, updatedAccDms);
accountList = accountRepo.findAll(); accountList = accountRepo.findAll();
if(accountList !=null && !accountList.isEmpty() && accountList.size() >0) { if(accountList != null && !accountList.isEmpty() && accountList.size() >0) {
for (Account acc : accountList) { for (Account acc : accountList) {
employeeIds=acc.getDeliveryManagers(); employeeIds = acc.getDeliveryManagers();
if(employeeIds != null && !employeeIds.isEmpty() && employeeIds.size() >0) { if(employeeIds != null && !employeeIds.isEmpty() && employeeIds.size() >0) {
for(String eId:employeeIds) { for(String eId:employeeIds) {
if(dmsCount.get(eId)!=null) if(dmsCount.get(eId) != null)
dmsCount.put(eId, dmsCount.get(eId)+1); dmsCount.put(eId, dmsCount.get(eId)+1);
else else
dmsCount.put(eId, 1); dmsCount.put(eId, 1);
...@@ -98,17 +103,19 @@ public class AccountServiceImpl implements AccountService { ...@@ -98,17 +103,19 @@ public class AccountServiceImpl implements AccountService {
} }
for (String empId : dmsDeletedByUser) { for (String empId : dmsDeletedByUser) {
int occurrences = Collections.frequency(accountEmpIds, empId); if(dmsCount.get(empId)==1) {
if(occurrences==1) {
//Service call for RoleMapping //Service call for RoleMapping
roleMappingService.deleteRole(empId,roleId); roleMappingService.deleteRole(empId,roleId);
} }
} }
account.setStatus(MyTimeUtils.STRING_Y); //account.setStatus(MyTimeUtils.STRING_Y);
account.setStatus(accountBeforeUpdate.getStatus());
accountResult = accountRepo.save(account); accountResult = accountRepo.save(account);
roleMappingService.saveUniqueEmployeeAndRole(dmsAddedByUser, roleId);
response="Updated succesfully"; 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);
} }
...@@ -182,4 +218,12 @@ public class AccountServiceImpl implements AccountService { ...@@ -182,4 +218,12 @@ public class AccountServiceImpl implements AccountService {
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