ronaldkwandy

Untitled

Sep 14th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.85 KB | None | 0 0
  1. package id.co.sofcograha.gajiidapi.domain.hris.mst.workplace.services;
  2.  
  3. import java.util.List;
  4.  
  5. import id.co.sofcograha.gajiidapi.base.exceptions.BusinessException;
  6. import id.co.sofcograha.gajiidapi.base.extendables.FreeFormService;
  7. import id.co.sofcograha.gajiidapi.base.mst.systemconfig.entities.SystemConfig;
  8. import id.co.sofcograha.gajiidapi.base.mst.systemconfig.services.SystemConfigService;
  9. import id.co.sofcograha.gajiidapi.base.utils.VersionUtil;
  10. import id.co.sofcograha.gajiidapi.domain.hris.mst.attendanceGroup.entities.AttendanceGroup;
  11. import id.co.sofcograha.gajiidapi.domain.hris.mst.attendanceGroup.pojos.AttendanceGroupPojo;
  12. import id.co.sofcograha.gajiidapi.domain.hris.mst.attendanceGroup.services.AttendanceGroupService;
  13. import id.co.sofcograha.gajiidapi.domain.hris.mst.payrollGroup.entities.PayrollGroup;
  14. import id.co.sofcograha.gajiidapi.domain.hris.mst.payrollGroup.pojos.PayrollGroupPojo;
  15. import id.co.sofcograha.gajiidapi.domain.hris.mst.payrollGroup.services.PayrollGroupService;
  16. import id.co.sofcograha.gajiidapi.domain.hris.mst.userAttendanceGroupAuthorization.entities.UserAttendanceGroupAuthorization;
  17. import id.co.sofcograha.gajiidapi.domain.hris.mst.userAttendanceGroupAuthorization.services.UserAttendanceGroupAuthorizationService;
  18. import id.co.sofcograha.gajiidapi.domain.hris.mst.userPayrollGroupAuthorization.entities.UserPayrollGroupAuthorization;
  19. import id.co.sofcograha.gajiidapi.domain.hris.mst.userPayrollGroupAuthorization.services.UserPayrollGroupAuthorizationService;
  20. import id.co.sofcograha.gajiidapi.domain.hris.mst.workplace.entities.Workplace;
  21. import id.co.sofcograha.gajiidapi.domain.hris.mst.workplace.pojos.WorkplacePojo;
  22.  
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26.  
  27. @Service
  28. public class CompleteWorkplaceService extends FreeFormService {
  29.  
  30.     @Autowired SystemConfigService systemConfigService;
  31.     @Autowired WorkplaceService workplaceService;
  32.     @Autowired PayrollGroupService payrollGroupService;
  33.     @Autowired AttendanceGroupService attendanceGroupService;
  34.     @Autowired UserPayrollGroupAuthorizationService userPayrollGroupAuthorizationService;
  35.     @Autowired UserAttendanceGroupAuthorizationService userAttendanceGroupAuthorizationService;
  36.    
  37.     @Transactional
  38.     public Workplace add(Workplace workplace, Boolean isWorkplaceAsPayrollGroup, Boolean isWorkplaceAsAttendanceGroup) {
  39.         valWorkplaceRequired(workplace);
  40.         valRequiredBoolean(isWorkplaceAsPayrollGroup, "completeWorkplace.workplaceAsPayrollGroup.required");
  41.         valRequiredBoolean(isWorkplaceAsAttendanceGroup, "completeWorkplace.workplaceAsAttendanceGroup.required");
  42.         throwBatchError();
  43.        
  44.         workplace = workplaceService.add(workplace);
  45.        
  46.         addPayrollGroup(workplace, isWorkplaceAsPayrollGroup);
  47.         addAttendanceGroup(workplace, isWorkplaceAsAttendanceGroup);
  48.        
  49.         return workplace;
  50.     }
  51.    
  52.     @Transactional
  53.     public Workplace edit(Workplace workplace, Boolean isWorkplaceAsPayrollGroup, Boolean isWorkplaceAsAttendanceGroup) {
  54.         valWorkplaceRequired(workplace);
  55.         valRequiredBoolean(isWorkplaceAsPayrollGroup, "completeWorkplace.workplaceAsPayrollGroup.required");
  56.         valRequiredBoolean(isWorkplaceAsAttendanceGroup, "completeWorkplace.workplaceAsAttendanceGroup.required");
  57.         throwBatchError();
  58.        
  59.         valIdVersionRequired(workplace.getId(), workplace.getVersion());
  60.         valWorkplaceVersion(workplace.getId(), workplace.getVersion());
  61.         throwBatchError();
  62.        
  63.         Workplace oldWorkplace = workplaceService.find(workplace.getId()).get();
  64.         WorkplacePojo oldWorkplacePojo = WorkplacePojo.fromEntity(oldWorkplace);
  65.         PayrollGroup oldPayrollGroup = payrollGroupService.findByBk(oldWorkplacePojo.name).orElse(null);
  66.         AttendanceGroup oldAttendanceGroup = attendanceGroupService.findByBk(oldWorkplacePojo.code).orElse(null);
  67.        
  68.         editPayrollGroup(workplace, oldPayrollGroup, isWorkplaceAsPayrollGroup);
  69.         editAttendanceGroup(workplace, oldAttendanceGroup, isWorkplaceAsAttendanceGroup);
  70.         workplace = workplaceService.edit(workplace);
  71.        
  72.         return workplace;
  73.     }
  74.    
  75.     @Transactional
  76.     public void delete(String id, Long version) {
  77.         valIdVersionRequired(id, version);
  78.         valWorkplaceVersion(id, version);
  79.         throwBatchError();
  80.        
  81.         Workplace workplace = workplaceService.find(id).get();
  82.         SystemConfig workplaceAsPayrollGroup = systemConfigService.findByBk("WORKPLACE_IS_PAYROLL_GRP").orElse(null);
  83.         if (workplaceAsPayrollGroup != null && workplaceAsPayrollGroup.getDataValue() == "Yes") {
  84.             deleteUserPayrollGroupAuthorization(workplace);
  85.             deletePayrollGroup(workplace);         
  86.         }
  87.        
  88.         SystemConfig workplaceAsAttendanceGroup = systemConfigService.findByBk("WORKPLACE_IS_ATTENDANCE_GRP").orElse(null);
  89.         if (workplaceAsAttendanceGroup != null && workplaceAsAttendanceGroup.getDataValue() == "Yes") {
  90.             deleteUserAttendanceGroupAuthorization(workplace);
  91.             deleteAttendanceGroup(workplace);          
  92.         }
  93.        
  94.         workplaceService.delete(id, version, Workplace.class);
  95.     }
  96.    
  97.     private void deleteUserPayrollGroupAuthorization(Workplace workplace) {
  98.         PayrollGroup payrollGroup = payrollGroupService.findByBk(workplace.getName()).orElse(null);
  99.         if (payrollGroup != null) {
  100.             List<UserPayrollGroupAuthorization> entities = userPayrollGroupAuthorizationService.findByPayrollGroup(payrollGroup);
  101.             for (UserPayrollGroupAuthorization entity: entities) {
  102.                 userPayrollGroupAuthorizationService.delete(entity.getId(), entity.getVersion(), UserPayrollGroupAuthorization.class);
  103.             }
  104.         }
  105.     }
  106.    
  107.     private void deleteUserAttendanceGroupAuthorization(Workplace workplace) {
  108.         AttendanceGroup attendanceGroup = attendanceGroupService.findByBk(workplace.getCode()).orElse(null);
  109.         if (attendanceGroup != null) {
  110.             List<UserAttendanceGroupAuthorization> entities = userAttendanceGroupAuthorizationService.findByAttendanceGroup(attendanceGroup);
  111.             for (UserAttendanceGroupAuthorization entity: entities) {
  112.                 userAttendanceGroupAuthorizationService.delete(entity.getId(), entity.getVersion(), UserAttendanceGroupAuthorization.class);
  113.             }
  114.         }
  115.     }
  116.    
  117.     private void deletePayrollGroup(Workplace workplace) {
  118.         PayrollGroup payrollGroup = payrollGroupService.findByBk(workplace.getName()).orElse(null);
  119.         if (payrollGroup != null) {
  120.             payrollGroupService.delete(payrollGroup.getId(), payrollGroup.getVersion(), PayrollGroup.class);
  121.         }
  122.     }
  123.    
  124.     private void deleteAttendanceGroup(Workplace workplace) {
  125.         AttendanceGroup attendanceGroup = attendanceGroupService.findByBk(workplace.getCode()).orElse(null);
  126.         if (attendanceGroup != null) {
  127.             attendanceGroupService.delete(attendanceGroup.getId(), attendanceGroup.getVersion(), AttendanceGroup.class);
  128.         }
  129.     }
  130.    
  131.     private void addPayrollGroup(Workplace workplace, Boolean isWorkplaceAsPayrollGroup) {
  132.         if (isWorkplaceAsPayrollGroup) {
  133.             PayrollGroup entity = new PayrollGroup();
  134.             entity.setPayrollGroupName(workplace.getName());
  135.             payrollGroupService.add(entity);
  136.         }
  137.     }
  138.    
  139.     private void editPayrollGroup(Workplace workplace, PayrollGroup payrollGroup, Boolean isWorkplaceAsPayrollGroup) {
  140.         if (isWorkplaceAsPayrollGroup) {
  141.             if (payrollGroup != null) {
  142.                 PayrollGroupPojo pojo = PayrollGroupPojo.fromEntity(payrollGroup);
  143.                 pojo.payrollGroupName = workplace.getName();
  144.                 pojo.isActive = workplace.isActive();
  145.                
  146.                 PayrollGroup entity = pojo.toEntity();
  147.                 payrollGroupService.edit(entity);
  148.             }
  149.         }
  150.     }
  151.    
  152.     private void addAttendanceGroup(Workplace workplace, Boolean isWorkplaceAsAttendanceGroup) {
  153.         if (isWorkplaceAsAttendanceGroup) {
  154.             AttendanceGroup entity = new AttendanceGroup();
  155.             entity.setCode(workplace.getCode());
  156.             entity.setName(workplace.getName());
  157.             attendanceGroupService.add(entity);
  158.         }
  159.     }
  160.    
  161.     private void editAttendanceGroup(Workplace workplace, AttendanceGroup attendanceGroup, Boolean isWorkplaceAsAttendanceGroup) {
  162.         if (isWorkplaceAsAttendanceGroup) {
  163.             if (attendanceGroup != null) {
  164.                 AttendanceGroupPojo pojo = AttendanceGroupPojo.fromEntity(attendanceGroup);
  165.                 pojo.code = workplace.getCode();
  166.                 pojo.name = workplace.getName();
  167.                 pojo.isActive = workplace.isActive();
  168.                
  169.                 AttendanceGroup entity = pojo.toEntity();
  170.                 attendanceGroupService.edit(entity);
  171.             }
  172.         }
  173.     }
  174.    
  175.     protected void valWorkplaceRequired(Workplace workplace) {
  176.         if (workplace == null) {
  177.             batchError("completeWorkplace.workplace.required");
  178.         }
  179.     }
  180.  
  181.     protected void valIdVersionRequired(String id, Long version) {
  182.         if (id == null || version == null) {
  183.             throw new BusinessException("data.require.id.and.version");
  184.         }
  185.     }
  186.    
  187.     protected void valWorkplaceVersion(String id, Long version) {
  188.         valEntityExists(id);
  189.         Workplace dbEntity = workplaceService.get(id);
  190.         VersionUtil.check(version, dbEntity.getVersion());
  191.     }
  192.    
  193.     protected void valEntityExists(String id) {
  194.         if (!workplaceService.exists(id)) {
  195.             throw new BusinessException("data.not.found", WorkplaceService.class.getSimpleName(), id);
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment