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