1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.adjustment.validation;
17
18 import java.math.BigDecimal;
19 import java.util.List;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.joda.time.DateTime;
23 import org.joda.time.LocalDate;
24 import org.kuali.kpme.core.KPMENamespace;
25 import org.kuali.kpme.core.earncode.EarnCode;
26 import org.kuali.kpme.core.job.Job;
27 import org.kuali.kpme.core.role.KPMERole;
28 import org.kuali.kpme.core.service.HrServiceLocator;
29 import org.kuali.kpme.core.util.HrContext;
30 import org.kuali.kpme.core.util.ValidationUtils;
31 import org.kuali.kpme.tklm.leave.adjustment.LeaveAdjustment;
32 import org.kuali.rice.kns.document.MaintenanceDocument;
33 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
34 import org.kuali.rice.krad.bo.PersistableBusinessObject;
35 import org.kuali.rice.krad.util.GlobalVariables;
36
37 public class LeaveAdjustmentValidation extends MaintenanceDocumentRuleBase{
38
39 boolean validateLeavePlan(String leavePlan, LocalDate asOfDate) {
40 boolean valid = true;
41 if (!ValidationUtils.validateLeavePlan(leavePlan, asOfDate)) {
42 this.putFieldError("leavePlan", "error.existence", "leavePlan '"
43 + leavePlan + "'");
44 valid = false;
45 }
46 return valid;
47 }
48
49 boolean validateEarnCode(String earnCode, String accrualCategory, LocalDate asOfDate) {
50 boolean valid = true;
51 if (!ValidationUtils.validateEarnCodeOfAccrualCategory(earnCode, accrualCategory, asOfDate)) {
52 this.putFieldError("earnCode", "error.earnCode.accrualCategory.mismatch",
53 earnCode);
54 valid = false;
55 }
56 return valid;
57 }
58
59 boolean validatePrincipal(String principalId) {
60 boolean valid = true;
61 if(principalId != null && StringUtils.isNotEmpty(principalId)) {
62 if(StringUtils.equals(principalId, GlobalVariables.getUserSession().getPrincipalId())){
63 this.putFieldError("principalId", "error.cannot.modify.own.balance",
64 "principalId '" + principalId + "'");
65 valid = false;
66 }
67 if(valid){
68 if (!ValidationUtils.validatePrincipalId(principalId)) {
69 this.putFieldError("principalId", "error.existence",
70 "principalId '" + principalId + "'");
71 valid = false;
72 }
73 }
74 }
75 return valid;
76 }
77
78 boolean validateAccrualCategory(String accrualCategory, LocalDate asOfDate, String principalId) {
79 boolean valid = true;
80 if (!ValidationUtils.validateAccCategory(accrualCategory, principalId, asOfDate)) {
81 this.putFieldError("accrualCategory", "error.existence", "accrualCategory '"
82 + accrualCategory + "'");
83 valid = false;
84 }
85 return valid;
86 }
87
88 boolean validateDepartmentAdmin(String principalId) {
89
90 boolean valid = false;
91 String LoggedInPrincipalID = GlobalVariables.getUserSession().getPrincipalId();
92 LocalDate loggedInDay = LocalDate.now();
93 DateTime asOfDate = LocalDate.now().toDateTimeAtStartOfDay();
94
95 if(principalId != null && StringUtils.isNotEmpty(principalId)) {
96 List<Job> targetUserJob = HrServiceLocator.getJobService().getActiveLeaveJobs(principalId, loggedInDay);
97
98 if(!targetUserJob.isEmpty()) {
99
100 String targetUserDept = targetUserJob.get(0).getDept();
101 String targetUserLocation = targetUserJob.get(0).getLocation();
102
103 if(HrContext.isSystemAdmin()
104 || HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(LoggedInPrincipalID, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName(), targetUserDept, asOfDate)
105 || HrServiceLocator.getKPMERoleService().principalHasRoleInDepartment(LoggedInPrincipalID, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName(), targetUserDept, asOfDate)
106 || HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(LoggedInPrincipalID, KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), targetUserLocation, asOfDate)
107 || HrServiceLocator.getKPMERoleService().principalHasRoleInLocation(LoggedInPrincipalID, KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), targetUserLocation, asOfDate))
108 {
109 valid = true;
110 }
111 }
112 }
113 if(!valid) {
114 GlobalVariables.getMessageMap().putError("document.newMaintainableObject.principalId", "principal.is.not.dept.admin");
115 }
116 return valid;
117 }
118
119 private boolean validateFraction(String earnCode, BigDecimal amount, LocalDate asOfDate) {
120 boolean valid = true;
121 if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
122 EarnCode ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
123 if(ec != null && ec.getFractionalTimeAllowed() != null) {
124 BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
125 String[] parameters = new String[2];
126 parameters[0] = earnCode;
127 parameters[1] = Integer.toString(fracAllowed.scale());
128 this.putFieldError("adjustmentAmount", "error.amount.fraction", parameters);
129 valid = false;
130 }
131 }
132 return valid;
133 }
134
135 @Override
136 protected boolean processCustomRouteDocumentBusinessRules(
137 MaintenanceDocument document) {
138 boolean valid = false;
139
140 LOG.debug("entering custom validation for LeaveAdjustment");
141 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
142 if (pbo instanceof LeaveAdjustment) {
143 LeaveAdjustment leaveAdjustment = (LeaveAdjustment) pbo;
144
145 if (leaveAdjustment != null) {
146 valid = true;
147 if(leaveAdjustment.getPrincipalId() != null) {
148 valid &= this.validatePrincipal(leaveAdjustment.getPrincipalId());
149 valid &= this.validateDepartmentAdmin(leaveAdjustment.getPrincipalId());
150 }
151 if(leaveAdjustment.getAccrualCategory() != null) {
152 valid &= this.validateAccrualCategory(leaveAdjustment.getAccrualCategory(),leaveAdjustment.getEffectiveLocalDate(), leaveAdjustment.getPrincipalId());
153 }
154 if(leaveAdjustment.getLeavePlan() != null) {
155 valid &= this.validateLeavePlan(leaveAdjustment.getLeavePlan(), leaveAdjustment.getEffectiveLocalDate());
156 }
157 if(leaveAdjustment.getEarnCode() != null) {
158 valid &= this.validateEarnCode(leaveAdjustment.getEarnCode(), leaveAdjustment.getAccrualCategory(), leaveAdjustment.getEffectiveLocalDate());
159 if(leaveAdjustment.getAdjustmentAmount() != null) {
160 valid &= this.validateFraction(leaveAdjustment.getEarnCode(), leaveAdjustment.getAdjustmentAmount(), leaveAdjustment.getEffectiveLocalDate());
161 }
162 }
163 }
164 }
165
166 return valid;
167 }
168
169 }