View Javadoc

1   /**
2    * Copyright 2004-2013 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.kpme.core.job.validation;
17  
18  import java.util.List;
19  
20  import org.joda.time.LocalDate;
21  import org.kuali.kpme.core.assignment.Assignment;
22  import org.kuali.kpme.core.department.Department;
23  import org.kuali.kpme.core.job.Job;
24  import org.kuali.kpme.core.location.Location;
25  import org.kuali.kpme.core.service.HrServiceLocator;
26  import org.kuali.kpme.core.util.ValidationUtils;
27  import org.kuali.rice.kns.document.MaintenanceDocument;
28  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
29  import org.kuali.rice.krad.bo.PersistableBusinessObject;
30  
31  public class JobValidation extends MaintenanceDocumentRuleBase {
32  
33  	private boolean validatePrincipalId(Job job) {
34  		if (job.getPrincipalId() != null
35  				&& !ValidationUtils.validatePrincipalId(job.getPrincipalId())) {
36  			this.putFieldError("principalId", "error.existence",
37  					"principalId '" + job.getPrincipalId() + "'");
38  			return false;
39  		} else {
40  			return true;
41  		}
42  	}
43  
44  	private boolean validateJobNumber(Job job) {
45  		if (job.getJobNumber() != null) {
46  			Job jobObj = HrServiceLocator.getJobService().getJob(
47  					job.getPrincipalId(), job.getJobNumber(),
48  					job.getEffectiveLocalDate(), false);
49  			if (jobObj != null) {
50  				String[] parameters = new String[2];
51  				parameters[0] = job.getJobNumber().toString();
52  				parameters[1] = job.getPrincipalId();
53  				this.putFieldError("jobNumber",
54  						"principal.jobnumber.already.exist", parameters);
55  				return false;
56  			}
57  		}
58  		return true;
59  	}
60  
61  	protected boolean validateDepartment(Job job) {
62  		if (job.getDept() != null
63  				&& !ValidationUtils.validateDepartment(job.getDept(), job
64  						.getEffectiveLocalDate())) {
65  			this.putFieldError("dept", "error.existence", "department '"
66  					+ job.getDept() + "'");
67  			return false;
68  		} else {
69  			return true;
70  		}
71  	}
72  
73  	boolean validateSalGroup(Job job) {
74  		if (!ValidationUtils.validateSalGroup(job.getHrSalGroup(), job
75  				.getEffectiveLocalDate())) {
76  			this.putFieldError("hrSalGroup", "error.existence", "Salgroup '"
77  					+ job.getHrSalGroup() + "'");
78  			return false;
79  		} else {
80  			return true;
81  		}
82  	}
83  
84  	boolean validateLocation(Job job) {
85  		if (job.getLocation() != null
86  				&& !ValidationUtils.validateLocation(job.getLocation(), job.getEffectiveLocalDate())) {
87  			this.putFieldError("location", "error.existence", "location '"
88  					+ job.getLocation() + "'");
89  			return false;
90  		} else {
91  			return true;
92  		}
93  	}
94  
95  	boolean validatePayType(Job job) {
96  		if (job.getHrPayType() != null
97  				&& !ValidationUtils.validatePayType(job.getHrPayType(), job
98  						.getEffectiveLocalDate())) {
99  			this.putFieldError("hrPayType", "error.existence", "pay type '"
100 					+ job.getHrPayType() + "'");
101 			return false;
102 		} else {
103 			return true;
104 		}
105 	}
106 
107 	boolean validatePayGrade(Job job) {
108 		if (job.getPayGrade() != null
109 				&& !ValidationUtils.validatePayGrade(job.getPayGrade(), job.getHrSalGroup(), job
110 						.getEffectiveLocalDate())) {
111 			this.putFieldError("payGrade", "error.existence", "pay grade '"
112 					+ job.getPayGrade() + "'");
113 			return false;
114 		} else {
115 			return true;
116 		}
117 	}
118 	
119 	boolean validatePrimaryIndicator(Job job, Job oldJob) {
120 		boolean valid = true;
121 		if (job.getPrimaryIndicator()) {
122 			//do not block editing of previous primary job
123 			if(oldJob!=null && oldJob.getPrimaryIndicator()!=null && oldJob.getPrimaryIndicator()){
124 				return valid;
125 			}
126 			Job existingJob = HrServiceLocator.getJobService().getPrimaryJob(job.getPrincipalId(), LocalDate.now());
127 			if (existingJob != null && existingJob.getPrimaryIndicator()) {
128 				this.putFieldError("primaryIndicator", "error.primary.job.already.exist", job.getPrincipalId());
129 				valid = false;
130 			}
131 		}
132 		return valid;
133 	}
134 	
135 	// KPME-1129 Kagata
136 	// This method determines if the job can be inactivated or not
137 	boolean validateInactivation(Job job){
138 		
139 		// Get a list of active assignments based on principalId, jobNumber and current date.
140 		// If the list is not null, there are active assignments and the job can't be inactivated, so return false, otherwise true
141 		if(!job.isActive()) {
142 			//this has to use the effective date of the job passed in
143 			List<Assignment> aList = HrServiceLocator.getAssignmentService().getActiveAssignmentsForJob(job.getPrincipalId(), job.getJobNumber(), job.getEffectiveLocalDate());
144 			if (aList != null && aList.size() > 0) {
145 				// error.job.inactivate=Can not inactivate job number {0}.  It is used in active assignments.
146 				this.putFieldError("active", "error.job.inactivate", job.getJobNumber().toString());
147 				return false;
148 			} 
149 		}
150 
151 		return true;
152 	}
153 
154 	private boolean validateConsistentLocation(Job job) {
155 		String department = job.getDept();
156 		Department departmentObj = HrServiceLocator.getDepartmentService().getDepartment(department, job.getEffectiveLocalDate());
157 		Location location = HrServiceLocator.getLocationService().getLocation(job.getLocation(), job.getEffectiveLocalDate());
158 		if(departmentObj != null && location != null) {
159 			if(departmentObj.getLocation().equals(location.getLocation())) {
160 				return true;
161 			}
162 			else {
163 				this.putFieldError("location", "job.location.inconsistent", departmentObj.getLocation());
164 			}
165 		}
166 		return false;
167 	}
168 	
169 	@Override
170 	protected boolean processCustomRouteDocumentBusinessRules(
171 			MaintenanceDocument document) {
172 		boolean valid = false;
173 		LOG.debug("entering custom validation for Job");
174 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
175 		if (pbo instanceof Job) {
176 			Job job = (Job) pbo;
177 			Job oldJob = (Job) this.getOldBo();
178 			if (job != null) {
179 				valid = true;
180 				valid &= this.validatePrincipalId(job);
181 				if(!document.isOldBusinessObjectInDocument()){
182 					valid &= this.validateJobNumber(job);
183 				}
184 				valid &= this.validateDepartment(job);
185 				valid &= this.validateSalGroup(job);
186 				valid &= this.validateLocation(job);
187 				valid &= this.validatePayType(job);
188 				valid &= this.validatePayGrade(job);
189 				valid &= this.validatePrimaryIndicator(job, oldJob);
190 				// KPME-1129 Kagata
191 				valid &= this.validateInactivation(job);
192 				valid &= this.validateConsistentLocation(job);
193 			}
194 		}
195 		return valid;
196 	}
197 }