Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.co.sofcograha.gajiidapi.domain.hris.mst.workplace.services;
- import java.util.List;
- import id.co.sofcograha.gajiidapi.base.exceptions.BusinessException;
- import id.co.sofcograha.gajiidapi.base.extendables.FreeFormService;
- import id.co.sofcograha.gajiidapi.base.mst.systemconfig.entities.SystemConfig;
- import id.co.sofcograha.gajiidapi.base.mst.systemconfig.services.SystemConfigService;
- import id.co.sofcograha.gajiidapi.base.utils.VersionUtil;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.attendanceGroup.entities.AttendanceGroup;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.attendanceGroup.pojos.AttendanceGroupPojo;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.attendanceGroup.services.AttendanceGroupService;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.payrollGroup.entities.PayrollGroup;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.payrollGroup.pojos.PayrollGroupPojo;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.payrollGroup.services.PayrollGroupService;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.userAttendanceGroupAuthorization.entities.UserAttendanceGroupAuthorization;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.userAttendanceGroupAuthorization.services.UserAttendanceGroupAuthorizationService;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.userPayrollGroupAuthorization.entities.UserPayrollGroupAuthorization;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.userPayrollGroupAuthorization.services.UserPayrollGroupAuthorizationService;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.workplace.entities.Workplace;
- import id.co.sofcograha.gajiidapi.domain.hris.mst.workplace.pojos.WorkplacePojo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- @Service
- public class CompleteWorkplaceService extends FreeFormService {
- @Autowired SystemConfigService systemConfigService;
- @Autowired WorkplaceService workplaceService;
- @Autowired PayrollGroupService payrollGroupService;
- @Autowired AttendanceGroupService attendanceGroupService;
- @Autowired UserPayrollGroupAuthorizationService userPayrollGroupAuthorizationService;
- @Autowired UserAttendanceGroupAuthorizationService userAttendanceGroupAuthorizationService;
- @Transactional
- public Workplace add(Workplace workplace, Boolean isWorkplaceAsPayrollGroup, Boolean isWorkplaceAsAttendanceGroup) {
- valWorkplaceRequired(workplace);
- valRequiredBoolean(isWorkplaceAsPayrollGroup, "completeWorkplace.workplaceAsPayrollGroup.required");
- valRequiredBoolean(isWorkplaceAsAttendanceGroup, "completeWorkplace.workplaceAsAttendanceGroup.required");
- throwBatchError();
- workplace = workplaceService.add(workplace);
- addPayrollGroup(workplace, isWorkplaceAsPayrollGroup);
- addAttendanceGroup(workplace, isWorkplaceAsAttendanceGroup);
- return workplace;
- }
- @Transactional
- public Workplace edit(Workplace workplace, Boolean isWorkplaceAsPayrollGroup, Boolean isWorkplaceAsAttendanceGroup) {
- valWorkplaceRequired(workplace);
- valRequiredBoolean(isWorkplaceAsPayrollGroup, "completeWorkplace.workplaceAsPayrollGroup.required");
- valRequiredBoolean(isWorkplaceAsAttendanceGroup, "completeWorkplace.workplaceAsAttendanceGroup.required");
- throwBatchError();
- valIdVersionRequired(workplace.getId(), workplace.getVersion());
- valWorkplaceVersion(workplace.getId(), workplace.getVersion());
- throwBatchError();
- Workplace oldWorkplace = workplaceService.find(workplace.getId()).get();
- WorkplacePojo oldWorkplacePojo = WorkplacePojo.fromEntity(oldWorkplace);
- PayrollGroup oldPayrollGroup = payrollGroupService.findByBk(oldWorkplacePojo.name).orElse(null);
- AttendanceGroup oldAttendanceGroup = attendanceGroupService.findByBk(oldWorkplacePojo.code).orElse(null);
- editPayrollGroup(workplace, oldPayrollGroup, isWorkplaceAsPayrollGroup);
- editAttendanceGroup(workplace, oldAttendanceGroup, isWorkplaceAsAttendanceGroup);
- workplace = workplaceService.edit(workplace);
- return workplace;
- }
- @Transactional
- public void delete(String id, Long version) {
- valIdVersionRequired(id, version);
- valWorkplaceVersion(id, version);
- throwBatchError();
- Workplace workplace = workplaceService.find(id).get();
- SystemConfig workplaceAsPayrollGroup = systemConfigService.findByBk("WORKPLACE_IS_PAYROLL_GRP").orElse(null);
- if (workplaceAsPayrollGroup != null && workplaceAsPayrollGroup.getDataValue() == "Yes") {
- deleteUserPayrollGroupAuthorization(workplace);
- deletePayrollGroup(workplace);
- }
- SystemConfig workplaceAsAttendanceGroup = systemConfigService.findByBk("WORKPLACE_IS_ATTENDANCE_GRP").orElse(null);
- if (workplaceAsAttendanceGroup != null && workplaceAsAttendanceGroup.getDataValue() == "Yes") {
- deleteUserAttendanceGroupAuthorization(workplace);
- deleteAttendanceGroup(workplace);
- }
- workplaceService.delete(id, version, Workplace.class);
- }
- private void deleteUserPayrollGroupAuthorization(Workplace workplace) {
- PayrollGroup payrollGroup = payrollGroupService.findByBk(workplace.getName()).orElse(null);
- if (payrollGroup != null) {
- List<UserPayrollGroupAuthorization> entities = userPayrollGroupAuthorizationService.findByPayrollGroup(payrollGroup);
- for (UserPayrollGroupAuthorization entity: entities) {
- userPayrollGroupAuthorizationService.delete(entity.getId(), entity.getVersion(), UserPayrollGroupAuthorization.class);
- }
- }
- }
- private void deleteUserAttendanceGroupAuthorization(Workplace workplace) {
- AttendanceGroup attendanceGroup = attendanceGroupService.findByBk(workplace.getCode()).orElse(null);
- if (attendanceGroup != null) {
- List<UserAttendanceGroupAuthorization> entities = userAttendanceGroupAuthorizationService.findByAttendanceGroup(attendanceGroup);
- for (UserAttendanceGroupAuthorization entity: entities) {
- userAttendanceGroupAuthorizationService.delete(entity.getId(), entity.getVersion(), UserAttendanceGroupAuthorization.class);
- }
- }
- }
- private void deletePayrollGroup(Workplace workplace) {
- PayrollGroup payrollGroup = payrollGroupService.findByBk(workplace.getName()).orElse(null);
- if (payrollGroup != null) {
- payrollGroupService.delete(payrollGroup.getId(), payrollGroup.getVersion(), PayrollGroup.class);
- }
- }
- private void deleteAttendanceGroup(Workplace workplace) {
- AttendanceGroup attendanceGroup = attendanceGroupService.findByBk(workplace.getCode()).orElse(null);
- if (attendanceGroup != null) {
- attendanceGroupService.delete(attendanceGroup.getId(), attendanceGroup.getVersion(), AttendanceGroup.class);
- }
- }
- private void addPayrollGroup(Workplace workplace, Boolean isWorkplaceAsPayrollGroup) {
- if (isWorkplaceAsPayrollGroup) {
- PayrollGroup entity = new PayrollGroup();
- entity.setPayrollGroupName(workplace.getName());
- payrollGroupService.add(entity);
- }
- }
- private void editPayrollGroup(Workplace workplace, PayrollGroup payrollGroup, Boolean isWorkplaceAsPayrollGroup) {
- if (isWorkplaceAsPayrollGroup) {
- if (payrollGroup != null) {
- PayrollGroupPojo pojo = PayrollGroupPojo.fromEntity(payrollGroup);
- pojo.payrollGroupName = workplace.getName();
- pojo.isActive = workplace.isActive();
- PayrollGroup entity = pojo.toEntity();
- payrollGroupService.edit(entity);
- }
- }
- }
- private void addAttendanceGroup(Workplace workplace, Boolean isWorkplaceAsAttendanceGroup) {
- if (isWorkplaceAsAttendanceGroup) {
- AttendanceGroup entity = new AttendanceGroup();
- entity.setCode(workplace.getCode());
- entity.setName(workplace.getName());
- attendanceGroupService.add(entity);
- }
- }
- private void editAttendanceGroup(Workplace workplace, AttendanceGroup attendanceGroup, Boolean isWorkplaceAsAttendanceGroup) {
- if (isWorkplaceAsAttendanceGroup) {
- if (attendanceGroup != null) {
- AttendanceGroupPojo pojo = AttendanceGroupPojo.fromEntity(attendanceGroup);
- pojo.code = workplace.getCode();
- pojo.name = workplace.getName();
- pojo.isActive = workplace.isActive();
- AttendanceGroup entity = pojo.toEntity();
- attendanceGroupService.edit(entity);
- }
- }
- }
- protected void valWorkplaceRequired(Workplace workplace) {
- if (workplace == null) {
- batchError("completeWorkplace.workplace.required");
- }
- }
- protected void valIdVersionRequired(String id, Long version) {
- if (id == null || version == null) {
- throw new BusinessException("data.require.id.and.version");
- }
- }
- protected void valWorkplaceVersion(String id, Long version) {
- valEntityExists(id);
- Workplace dbEntity = workplaceService.get(id);
- VersionUtil.check(version, dbEntity.getVersion());
- }
- protected void valEntityExists(String id) {
- if (!workplaceService.exists(id)) {
- throw new BusinessException("data.not.found", WorkplaceService.class.getSimpleName(), id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment