Commit 35ed81fb authored by Vijay Chaitanya's avatar Vijay Chaitanya Committed by rbonthala-nisum-com

update code changes (#44)

parent 6be98c57
......@@ -33,23 +33,12 @@ public class DomainController {
@Autowired
private DomainService domainService;
@RequestMapping(value = "/domains", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> addDomain(@RequestBody Domains domains)
throws MyTimeException {
String response=null;
if (StringUtils.isEmpty(domains.getDomainId())) {
domains.setDomainId(generateDomainId());
}
domains.setStatus(MyTimeUtils.ACTIVE);
if(domains.getId()==null){
response="saved Succesfully";
}else{
response="updated Succesfully";
}
domainService.addDomain(domains);
return new ResponseEntity<>(response, HttpStatus.OK);
response=domainService.addDomains(domains);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@RequestMapping(value = "/domains", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
......@@ -64,18 +53,9 @@ public class DomainController {
}
@RequestMapping(value = "/domains/{domainName}/{accountName}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> validateDomain(@PathVariable ("domainName") String domainName,@PathVariable ("accountName") String accountName) throws MyTimeException {
String response=null;
List<Domains> domainList=domainService.validateDomains(domainName,accountName);
if(domainList.size()>0) {
response="Record already exists";
}
return new ResponseEntity<>(response, HttpStatus.OK);
}
private String generateDomainId() throws MyTimeException {
return (MyTimeUtils.DOM + MyTimeUtils.ZERO_) + (domainService.getAllDomains().size() +1);
}
@RequestMapping(value = "/domains", method = RequestMethod.PUT, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> updateDomain(@RequestBody Domains domains) throws MyTimeException {
domainService.updateDomain(domains);
return new ResponseEntity<>("Domain updated successfully", HttpStatus.OK);
}
}
\ No newline at end of file
......@@ -13,12 +13,11 @@ import com.nisum.mytime.model.Domains;
*/
public interface DomainService {
Domains addDomain(Domains d) throws MyTimeException;
String addDomains(Domains d)throws MyTimeException;
List<HashMap<Object,Object>> getAllDomains() throws MyTimeException;
WriteResult deleteDomain(String id) throws MyTimeException;
String updateDomain(Domains d) throws MyTimeException;
List<Domains> validateDomains(String domainName,String accountName) throws MyTimeException;
WriteResult deleteDomain(String id) throws MyTimeException;
}
package com.nisum.mytime.service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
......@@ -10,6 +11,7 @@ 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 org.springframework.util.StringUtils;
import com.mongodb.WriteResult;
import com.nisum.mytime.exception.handler.MyTimeException;
......@@ -36,11 +38,39 @@ public class DomainServiceImpl implements DomainService {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private RoleInfoService roleInfoService;
@Autowired
private RoleMappingService roleMappingService;
@Override
public Domains addDomain(Domains d) throws MyTimeException{
return domainRepo.save(d);
}
public String addDomains(Domains d) throws MyTimeException {
String response=null;
List<Domains> domainList=domainRepo.findByDomainNameAndAccountName(d.getDomainName(),d.getAccountName());
if(domainList.size()>0) {
response="Domain already exists";
return response;
}
if (StringUtils.isEmpty(d.getDomainId()) && d.getId()==null) {
d.setDomainId((MyTimeUtils.DOM + MyTimeUtils.ZERO_) + (getAllDomains().size() +1));
}
d.setStatus(MyTimeUtils.ACTIVE);
if(d.getId()==null){
response="Domain saved succesfully";
}else{
response="Domain updated succesfully";
}
domainRepo.save(d);
String roleId = roleInfoService.getRole(MyTimeUtils.DOMAIN);
roleMappingService.saveUniqueEmployeeAndRole(d.getDeliveryManagers(), roleId);
return response;
}
@Override
public List<HashMap<Object, Object>> getAllDomains() throws MyTimeException {
......@@ -72,21 +102,57 @@ public class DomainServiceImpl implements DomainService {
}
return updatedDomainList;
}
@Override
public String updateDomain(Domains d) throws MyTimeException{
String response=null;
List<String> domEmpIds=new ArrayList<String>();
List<String> updatedEmpIds=new ArrayList<String>();
domainList=domainRepo.findAll();
String roleId = roleInfoService.getRole(MyTimeUtils.DOMAIN);
for (Domains domain : domainList) {
List<String> employeeIds=domain.getDeliveryManagers();
for(String eIds:employeeIds)
domEmpIds.add(eIds);
}
d.setStatus(MyTimeUtils.ACTIVE);
domainRepo.save(d);
Query selectedDomainQuery = new Query(Criteria.where(MyTimeUtils.DOMAIN_ID).in(d.getDomainId()).and(MyTimeUtils.STATUS).in(MyTimeUtils.ACTIVE) );
List<Domains> selectedDomain = mongoTemplate.find(selectedDomainQuery, Domains.class);
for (String empId : selectedDomain.get(0).getDeliveryManagers()) {
int occurrences = Collections.frequency(domEmpIds, empId);
if(occurrences==0) {
//roleMappingService.saveUniqueEmployeeAndRole(empId, roleId);
updatedEmpIds.add(empId);
}
}
roleMappingService.saveUniqueEmployeeAndRole(updatedEmpIds, roleId);
return response;
}
@Override
public WriteResult deleteDomain(String id) throws MyTimeException{
Query query = new Query(Criteria.where(MyTimeUtils.DOMAIN_ID).is(id));
Update update = new Update();
List<String> domEmpIds=new ArrayList<String>();
String roleId = roleInfoService.getRole(MyTimeUtils.DOMAIN);
domainList=domainRepo.findAll();
Query selectedDomainQuery = new Query(Criteria.where(MyTimeUtils.DOMAIN_ID).in(id).and(MyTimeUtils.STATUS).in(MyTimeUtils.ACTIVE) );
List<Domains> selectedDomain = mongoTemplate.find(selectedDomainQuery, Domains.class);
for (Domains domain : domainList) {
List<String> employeeIds=domain.getDeliveryManagers();
for(String eIds:employeeIds)
domEmpIds.add(eIds);
}
for (String empId : selectedDomain.get(0).getDeliveryManagers()) {
int occurrences = Collections.frequency(domEmpIds, empId);
if(occurrences==1) {
//Service call for RoleMapping
roleMappingService.deleteRole(empId,roleId);
}
}
Update update = new Update();
update.set(MyTimeUtils.STATUS,MyTimeUtils.IN_ACTIVE);
return mongoTemplate.upsert(query, update,Domains.class);
return mongoTemplate.upsert(selectedDomainQuery, update,Domains.class);
}
@Override
public List<Domains> validateDomains(String domainName, String accountName) throws MyTimeException {
List<Domains> domainList=domainRepo.findByDomainNameAndAccountName(domainName,accountName);
return domainList;
}
}
\ No newline at end of file
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