1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.workarea.web;
17
18 import java.sql.Date;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.kuali.hr.time.assignment.Assignment;
25 import org.kuali.hr.time.authorization.DepartmentalRule;
26 import org.kuali.hr.time.authorization.DepartmentalRuleAuthorizer;
27 import org.kuali.hr.time.roles.TkRole;
28 import org.kuali.hr.time.service.base.TkServiceLocator;
29 import org.kuali.hr.time.task.Task;
30 import org.kuali.hr.time.util.TKContext;
31 import org.kuali.hr.time.util.TKUtils;
32 import org.kuali.hr.time.util.TkConstants;
33 import org.kuali.hr.time.util.ValidationUtils;
34 import org.kuali.hr.time.workarea.WorkArea;
35 import org.kuali.rice.kns.document.MaintenanceDocument;
36 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
37 import org.kuali.rice.krad.bo.PersistableBusinessObject;
38 import org.kuali.rice.krad.util.GlobalVariables;
39
40 public class WorkAreaMaintenanceDocumentRule extends
41 MaintenanceDocumentRuleBase {
42
43 private static Logger LOG = Logger
44 .getLogger(WorkAreaMaintenanceDocumentRule.class);
45
46 boolean validateDepartment(String dept, Date asOfDate) {
47 boolean valid = ValidationUtils.validateDepartment(dept, asOfDate);
48 if (!valid) {
49 this.putFieldError("dept", "dept.notfound");
50 }
51 return valid;
52 }
53
54 boolean validateRoles(List<TkRole> roles, Date effectiveDate) {
55 boolean valid = false;
56
57 if (roles != null && roles.size() > 0) {
58 int pos = 0;
59 for (TkRole role : roles) {
60 valid |= role.isActive();
61 if(role.getRoleName().equalsIgnoreCase(TkConstants.ROLE_TK_APPROVER_DELEGATE)){
62 StringBuffer prefix = new StringBuffer("roles[");
63 prefix.append(pos).append("].");
64 if (role.getExpirationDate() == null) {
65 this.putFieldError(prefix + "expirationDate",
66 "error.role.expiration.required");
67 } else if (role.getEffectiveDate().compareTo(role.getExpirationDate()) >= 0) {
68 this.putFieldError(prefix + "expirationDate",
69 "error.role.expiration");
70 } else if (TKUtils.getDaysBetween(role.getEffectiveDate(), role.getExpirationDate()) > 180) {
71 this.putFieldError(prefix + "expirationDate",
72 "error.role.expiration.duration");
73 valid = false;
74 }
75 }
76 pos++;
77 }
78 }
79
80 if (!valid) {
81 this.putGlobalError("role.required");
82 }
83
84 return valid;
85 }
86
87 boolean validateTask(List<TkRole> roles) {
88 boolean valid = false;
89
90 if (roles != null && roles.size() > 0) {
91 for (TkRole role : roles) {
92 valid |= role.isActive()
93 && StringUtils.equals(role.getRoleName(),
94 TkConstants.ROLE_TK_APPROVER);
95 }
96 }
97
98 if (!valid) {
99 this.putGlobalError("role.required");
100 }
101
102 return valid;
103 }
104
105 boolean validateTask(Task task, Long workArea) {
106 boolean valid = true;
107
108
109
110
111
112
113
114
115 if (task.getEffectiveDate().compareTo(TkServiceLocator.getWorkAreaService().getWorkArea(workArea,TKUtils.getCurrentDate()).getEffectiveDate()) < 0) {
116 this.putGlobalError("task.workarea.invalid.effdt", "effective date '" + task.getEffectiveDate().toString() +"'");
117 valid = false;
118 }
119 return valid;
120 }
121
122 boolean validateDefaultOTEarnCode(String earnCode, Date asOfDate) {
123
124 if (earnCode != null
125 && !ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
126 this.putFieldError("defaultOvertimeEarnCode", "error.existence", "earnCode '"
127 + earnCode + "'");
128 return false;
129 } else {
130 if (earnCode != null
131 && !ValidationUtils.validateEarnCode(earnCode, true, asOfDate)) {
132 this.putFieldError("defaultOvertimeEarnCode", "earncode.ovt.required",
133 earnCode);
134 return false;
135 }
136 return true;
137 }
138 }
139
140 @Override
141 protected boolean processCustomRouteDocumentBusinessRules(
142 MaintenanceDocument document) {
143 boolean valid = false;
144
145 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
146 if (pbo instanceof WorkArea) {
147 WorkArea wa = (WorkArea) pbo;
148 valid = validateDepartment(wa.getDept(), wa.getEffectiveDate());
149 if(!DepartmentalRuleAuthorizer.hasAccessToWrite((DepartmentalRule)pbo)) {
150 String[] params = new String[]{GlobalVariables.getUserSession().getPrincipalName(), wa.getDept()};
151 this.putFieldError("dept", "dept.user.unauthorized", params);
152 }
153 valid &= validateRoles(wa.getRoles(), wa.getEffectiveDate());
154
155 if ( wa.getDefaultOvertimeEarnCode() != null ){
156 valid &= validateDefaultOTEarnCode(wa.getDefaultOvertimeEarnCode(),
157 wa.getEffectiveDate());
158 }
159
160 if(!wa.isActive()){
161 List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(wa.getWorkArea(), wa.getEffectiveDate());
162 for(Assignment assignment: assignments){
163 if(assignment.getWorkArea().equals(wa.getWorkArea())){
164 this.putGlobalError("workarea.active.required");
165 valid = false;
166 break;
167 }
168 }
169 }else{
170 List<Long> inactiveTasks = new ArrayList<Long>();
171 for (Task task : wa.getTasks()) {
172 if(!task.isActive()){
173 inactiveTasks.add(task.getTask());
174 }
175 }
176
177 if(!inactiveTasks.isEmpty()){
178 List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(wa.getWorkArea(), wa.getEffectiveDate());
179 for(Assignment assignment : assignments){
180 for(Long inactiveTask : inactiveTasks){
181 if(inactiveTask.equals(assignment.getTask())){
182 this.putGlobalError("task.active.required", inactiveTask.toString());
183 valid = false;
184 }
185 }
186 }
187 }
188
189 }
190 }
191
192 return valid;
193 }
194
195 @Override
196 public boolean processCustomAddCollectionLineBusinessRules(
197 MaintenanceDocument document, String collectionName,
198 PersistableBusinessObject line) {
199 boolean valid = false;
200 LOG.debug("entering custom validation for Task");
201 PersistableBusinessObject pboWorkArea = document.getDocumentBusinessObject();
202 PersistableBusinessObject pbo = line;
203 if (pbo instanceof Task && pboWorkArea instanceof WorkArea) {
204 WorkArea wa = (WorkArea) pboWorkArea;
205
206 Task task = (Task) pbo;
207
208 if (task != null && wa.getTasks() != null) {
209 valid = true;
210 valid &= this.validateTask(task, wa.getWorkArea());
211
212 if ( valid ){
213 if (task.getTask() == null){
214 Long maxTaskNumberInTable = this.getTaskNumber(wa);
215 Long maxTaskNumberOnPage = 0L;
216 if(wa.getTasks().size() > 0){
217 maxTaskNumberOnPage = wa.getTasks().get((wa.getTasks().size()) -1 ).getTask();
218 }
219
220 if ( maxTaskNumberOnPage.compareTo(maxTaskNumberInTable) >= 0){
221 task.setTask(maxTaskNumberOnPage + 1);
222 } else {
223 task.setTask(maxTaskNumberInTable);
224 }
225 task.setWorkArea(wa.getWorkArea());
226 }
227 }
228 }
229 } else if ((pbo instanceof TkRole && pboWorkArea instanceof WorkArea)) {
230 TkRole tkRole = (TkRole)pbo;
231 valid = true;
232 if(StringUtils.isEmpty(tkRole.getPrincipalId())) {
233 if (tkRole.getPositionNumber() == null || StringUtils.isEmpty(tkRole.getPositionNumber().toString())){
234 this.putGlobalError("principal.position.required");
235 }
236 }
237 }
238
239 return valid;
240 }
241
242 public Long getTaskNumber(WorkArea workArea) {
243 Long task = new Long("0");
244
245 Task maxTaskInTable = TkServiceLocator.getTaskService().getMaxTask();
246 if(maxTaskInTable != null) {
247
248 task = maxTaskInTable.getTask() +1;
249 } else {
250 task = new Long("100");
251 }
252
253 return task;
254 }
255 }