View Javadoc
1   /**
2    * Copyright 2004-2015 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.pm.positiondepartment.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.kpme.core.department.Department;
20  import org.kuali.kpme.core.service.HrServiceLocator;
21  import org.kuali.kpme.core.util.ValidationUtils;
22  import org.kuali.kpme.pm.positiondepartment.PositionDepartment;
23  import org.kuali.kpme.pm.util.PmValidationUtils;
24  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
25  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
26  
27  @SuppressWarnings("deprecation")
28  public class PositionDepartmentValidation extends MaintenanceDocumentRuleBase  {
29  	@Override
30  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
31  		boolean valid = false;
32  		LOG.debug("entering custom validation for Position Department");
33  		PositionDepartment positionDepartment = (PositionDepartment) this.getNewDataObject();
34  		
35  		if (positionDepartment != null) {
36  			valid = true;
37  			valid &= this.validateInstitution(positionDepartment);
38  			valid &= this.validateLocation(positionDepartment);
39  			valid &= this.validateDepartment(positionDepartment);
40  			valid &= this.validateAffiliation(positionDepartment);
41  		}
42  		return valid;
43  	}
44  	
45  	private boolean validateInstitution(PositionDepartment positionDepartment) {
46  		if (StringUtils.isNotEmpty(positionDepartment.getInstitution())
47  				&& !ValidationUtils.validateInstitution(positionDepartment.getInstitution(), positionDepartment.getEffectiveLocalDate())) {
48  			this.putFieldError("institution", "error.existence", "Institution '"
49  					+ positionDepartment.getInstitution() + "'");
50  			return false;
51  		} else {
52  			return true;
53  		}
54  	}
55  	
56  	private boolean validateLocation(PositionDepartment positionDepartment) {
57  		if (StringUtils.isNotEmpty(positionDepartment.getLocation())
58  				&& !ValidationUtils.validateLocation(positionDepartment.getLocation(), positionDepartment.getEffectiveLocalDate())) {
59  			this.putFieldError("location", "error.existence", "Location '"
60  					+ positionDepartment.getLocation() + "'");
61  			return false;
62  		} else {
63  			return true;
64  		}
65  	}
66  	
67  	private boolean validateDepartment(PositionDepartment positionDepartment) {
68  		if (StringUtils.isNotEmpty(positionDepartment.getDepartment())
69  				&& !ValidationUtils.validateDepartment(positionDepartment.getDepartment(), positionDepartment.getEffectiveLocalDate())) {
70  			this.putFieldError("department", "error.existence", "Department '"
71  					+ positionDepartment.getDepartment() + "'");
72  			return false;
73  		}
74  		Department dep = HrServiceLocator.getDepartmentService().getDepartmentWithoutRoles(positionDepartment.getDepartment(), positionDepartment.getEffectiveLocalDate());
75  		if(dep == null ) {
76  			this.putFieldError("department", "error.existence", "Department '"
77  					+ positionDepartment.getDepartment() + "'");
78  			return false;
79  		} else {
80  			if(!ValidationUtils.wildCardMatch(dep.getLocation(), positionDepartment.getLocation())) {
81  				String[] params = new String[3];
82  				params[0] = positionDepartment.getLocation();
83  				params[1] = dep.getLocation();
84  				params[2] = "Department '" + positionDepartment.getDepartment() + "'";
85  				this.putFieldError("department", "location.inconsistent", params);
86  				return false;
87  			}
88  		}
89  		
90  		return true;
91  	}
92  	
93  	private boolean validateAffiliation(PositionDepartment positionDepartment) {
94  		if (StringUtils.isNotEmpty(positionDepartment.getPositionDeptAffl())
95  				&& !PmValidationUtils.validateAffiliation(positionDepartment.getPositionDeptAffl(), positionDepartment.getEffectiveLocalDate())) {
96  			this.putFieldError("positionDeptAffl", "error.existence", "Affiliation '"
97  					+ positionDepartment.getPositionDeptAffl() + "'");
98  			return false;
99  		} else {
100 			return true;
101 		}
102 	}
103 }
104