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.department;
17  
18  import java.sql.Date;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.hr.time.roles.TkRole;
25  import org.kuali.hr.time.service.base.TkServiceLocator;
26  import org.kuali.hr.time.util.TKUtils;
27  import org.kuali.hr.time.util.TkConstants;
28  import org.kuali.kfs.coa.businessobject.Chart;
29  import org.kuali.kfs.coa.businessobject.Organization;
30  import org.kuali.rice.kns.document.MaintenanceDocument;
31  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
32  import org.kuali.rice.krad.bo.PersistableBusinessObject;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  
35  public class DepartmentRule extends MaintenanceDocumentRuleBase {
36  	
37  	boolean validateDepartment(Department department) {
38  		boolean valid = true;
39  		Department existingDept = TkServiceLocator.getDepartmentService().getDepartment(department.getDept(), department.getEffectiveDate());
40  		
41  		if ( department.getHrDeptId() == null ){ //KPME-1456. xichen. Only go through this validation when create a new dept. 
42  			if (department.getEffectiveDate() != null) {	    
43  				if (existingDept != null){
44  					if ( existingDept.getDept().equalsIgnoreCase(department.getDept()) && 
45  						 existingDept.getLocation().equalsIgnoreCase(department.getLocation()) ){
46  						// error.department.duplicate.exists=There is an exact duplicate version of this Department.					
47  						this.putFieldError("dept", "error.department.duplicate.exists", department.getDept());
48  						valid = false;
49  					}
50  				}
51  			}
52  		} 
53  		
54  		return valid;
55  	}
56  
57  	boolean validateChart(String value) {
58  		boolean valid = true;
59  		if (value != null) {
60  			Chart chart = KRADServiceLocator.getBusinessObjectService()
61  					.findBySinglePrimaryKey(Chart.class, value);
62  			valid = (chart != null);
63  
64  			if (!valid) {
65  				this.putFieldError("chart", "dept.chart.notfound", value);
66  			}
67  		}
68  		return valid;
69  	}
70  
71  	boolean validateOrg(String value) {
72  		boolean valid = true;
73  		if (value != null) {
74  			Organization org = this.getOrganization(value);
75  			valid = (org != null);
76  
77  			if (!valid) {
78  				this.putFieldError("org", "dept.org.notfound", value);
79  			}
80  		}
81  		return valid;
82  	}
83  
84  	boolean validateChartAndOrg(String chartString, String orgString) {
85  		if(chartString != null && orgString != null) {
86  			Chart chart = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Chart.class, chartString);
87  			Organization org = this.getOrganization(orgString);
88  			if(chart != null && org != null) {
89  				Chart chartTemp = org.getChartOfAccounts();
90  				if(!chart.getChartOfAccountsCode().equals(chartTemp.getChartOfAccountsCode())) {
91  					String[] params = new String[]{orgString, chartString};
92  					this.putFieldError("org", "dept.org.chart.notmatch", params);
93  					return false;
94  				}
95  			}
96  		}
97  		return true;
98  	}
99  
100 	Organization getOrganization(String orgCode) {
101 		Map<String, String> primaryKeys = new HashMap<String, String>();
102 		primaryKeys.put("organizationCode", orgCode);
103 		return (Organization) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(Organization.class, primaryKeys);
104 	}
105 
106 	/**
107 	 * Checks for not null and size > 0, but follows the validation pattern
108 	 * given in this class for future expanding.
109 	 *
110 	 * @return true if there is a role, false otherwise.
111 	 */
112 	protected boolean validateRolePresent(List<TkRole> roles, Date effectiveDate) {
113 		boolean valid = false;
114 
115 		if (roles != null && roles.size() > 0) {
116 			int pos = 0;
117 			for (TkRole role : roles) {
118 				valid |= role.isActive()
119 						&& StringUtils.equals(role.getRoleName(),
120 								TkConstants.ROLE_TK_DEPT_ADMIN);
121 				if(role.getExpirationDate() != null){
122 					StringBuffer prefix = new StringBuffer("roles[");
123 					prefix.append(pos).append("].");
124 					if (effectiveDate.compareTo(role.getExpirationDate()) >= 0
125 						|| role.getEffectiveDate().compareTo(
126 								role.getExpirationDate()) >= 0) {
127 						this.putFieldError(prefix + "expirationDate",
128 							"error.role.expiration");
129 					} else if (TKUtils.getDaysBetween(role.getEffectiveDate(), role.getExpirationDate()) > 180) {
130 		        		   this.putFieldError(prefix + "expirationDate",
131 		     						"error.role.expiration.duration");
132 		     				valid = false;
133 		        	}
134 				}
135 				pos++;
136 			}
137 		}
138 
139 		if (!valid) {
140 			this.putGlobalError("role.required");
141 		}
142 
143 		return valid;
144 	}
145 
146 	/**
147 	 * The calling method doesn't seem to examine the return value.
148 	 */
149 	@Override
150 	protected boolean processCustomRouteDocumentBusinessRules(
151 			MaintenanceDocument document) {
152 		boolean valid = false;
153 
154 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
155 		if (pbo instanceof Department) {
156 			Department clr = (Department) pbo;
157 			valid = validateChart(clr.getChart());
158 			valid &= validateOrg(clr.getOrg());
159 			valid &= validateChartAndOrg(clr.getChart(), clr.getOrg());
160 			valid &= validateRolePresent(clr.getRoles(), clr.getEffectiveDate());
161 			valid &= validateDepartment(clr); // KPME1400
162 		}
163 
164 		return valid;
165 	}
166 
167 }