View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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, List<Task> tasks) {
106 		boolean valid = true;
107 		
108 		for (Task t : tasks) {
109 			if (t.getTask().equals(task.getTask()) ) {
110 				this.putGlobalError("error.duplicate.entry", "task '" + task.getTask() + "'");
111 				valid = false;
112 			}
113 		}
114 		return valid;
115 	}
116 	
117 	boolean validateDefaultOTEarnCode(String earnCode, Date asOfDate) {
118 		// defaultOvertimeEarnCode is a nullable field. 
119 		if (earnCode != null
120 				&& !ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
121 			this.putFieldError("defaultOvertimeEarnCode", "error.existence", "earnCode '"
122 					+ earnCode + "'");
123 			return false;
124 		} else {
125 			if (earnCode != null
126 					&& !ValidationUtils.validateEarnCode(earnCode, true, asOfDate)) {
127 				this.putFieldError("defaultOvertimeEarnCode", "earncode.ovt.required",
128 						earnCode);
129 				return false;
130 			}
131 			return true;
132 		}
133 	}
134 
135 	@Override
136 	protected boolean processCustomRouteDocumentBusinessRules(
137 			MaintenanceDocument document) {
138 		boolean valid = false;
139 
140 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
141 		if (pbo instanceof WorkArea) {
142 			WorkArea wa = (WorkArea) pbo;
143 			valid = validateDepartment(wa.getDept(), wa.getEffectiveDate());
144 			if(!DepartmentalRuleAuthorizer.hasAccessToWrite((DepartmentalRule)pbo)) {
145 				String[] params = new String[]{GlobalVariables.getUserSession().getPrincipalName(), wa.getDept()};
146 				this.putFieldError("dept", "dept.user.unauthorized", params);
147 			}
148 			valid &= validateRoles(wa.getRoles(), wa.getEffectiveDate());
149 			// defaultOvertimeEarnCode is a nullable field. 
150 			if ( wa.getDefaultOvertimeEarnCode() != null ){
151 				valid &= validateDefaultOTEarnCode(wa.getDefaultOvertimeEarnCode(),
152 						wa.getEffectiveDate());
153 			}
154 			
155 			if(!wa.isActive()){
156 				List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(wa.getWorkArea(), wa.getEffectiveDate());
157 				for(Assignment assignment: assignments){
158 					if(assignment.getWorkArea().equals(wa.getWorkArea())){
159 						this.putGlobalError("workarea.active.required");
160 						valid = false;
161 						break;
162 					}
163 				}
164 			}else{
165 				List<Long> inactiveTasks = new ArrayList<Long>();
166 				for (Task task : wa.getTasks()) {
167 					if(!task.isActive()){
168 						inactiveTasks.add(task.getTask());
169 					}
170 				}
171 				
172 				if(!inactiveTasks.isEmpty()){
173 					List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(wa.getWorkArea(), wa.getEffectiveDate());
174 					for(Assignment assignment : assignments){
175 						for(Long inactiveTask : inactiveTasks){
176 							if(inactiveTask.equals(assignment.getTask())){
177 								this.putGlobalError("task.active.required", inactiveTask.toString());
178 								valid = false;
179 							}
180 						}
181 					}
182 				}
183 				
184 			}
185 		}
186 
187 		return valid;
188 	}
189 
190 	@Override
191 	public boolean processCustomAddCollectionLineBusinessRules(
192 			MaintenanceDocument document, String collectionName,
193 			PersistableBusinessObject line) {
194 		boolean valid = false;
195 		LOG.debug("entering custom validation for Task");
196 		PersistableBusinessObject pboWorkArea = document.getDocumentBusinessObject();
197 		PersistableBusinessObject pbo = line;
198 		if (pbo instanceof Task && pboWorkArea instanceof WorkArea) {
199 			WorkArea wa = (WorkArea) pboWorkArea;
200 			
201 			Task task = (Task) pbo;
202 			
203 			if (task != null && wa.getTasks() != null) {
204 				valid = true;
205 				valid &= this.validateTask(task, wa.getTasks());
206 				// KPME-870
207 				if ( valid ){
208 					if (task.getTask() == null){
209 						Long maxTaskNumberInTable = this.getTaskNumber(wa);
210 						Long maxTaskNumberOnPage = 0L;
211 						if(wa.getTasks().size() > 0){
212 							maxTaskNumberOnPage = wa.getTasks().get((wa.getTasks().size()) -1 ).getTask();
213 						}
214 						
215 						if ( maxTaskNumberOnPage.compareTo(maxTaskNumberInTable) >= 0){
216 							task.setTask(maxTaskNumberOnPage + 1);
217 						} else {
218 							task.setTask(maxTaskNumberInTable);
219 						}
220 						task.setWorkArea(wa.getWorkArea());
221 						task.setTkWorkAreaId(wa.getTkWorkAreaId());
222 					}
223 				}
224 			}
225 		} else if ((pbo instanceof TkRole && pboWorkArea instanceof WorkArea)) {
226 			TkRole tkRole = (TkRole)pbo;
227 			valid = true;
228 			if(StringUtils.isEmpty(tkRole.getPrincipalId())) {
229 				if (tkRole.getPositionNumber() == null || StringUtils.isEmpty(tkRole.getPositionNumber().toString())){
230 					this.putGlobalError("principal.position.required");
231 				}
232 			}
233 		}
234 		
235 		return valid;
236 	}
237 
238 	public Long getTaskNumber(WorkArea workArea) {
239 		Long task = new Long("0");
240 		
241 		Task maxTaskInTable = TkServiceLocator.getTaskService().getMaxTask();
242 		if(maxTaskInTable != null) {
243 			// get the max of task number of the collection
244 			task = maxTaskInTable.getTask() +1;
245 		} else {
246 			task = new Long("100");
247 		}
248 		
249 		return task;
250 	}
251 }