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.roles.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.time.roles.TkRole;
20  import org.kuali.hr.time.roles.TkRoleGroup;
21  import org.kuali.hr.time.util.TkConstants;
22  import org.kuali.rice.kns.document.MaintenanceDocument;
23  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
24  import org.kuali.rice.krad.bo.PersistableBusinessObject;
25  
26  public class TkRoleValidation extends MaintenanceDocumentRuleBase{
27  
28      private static final String ADD_LINE_LOCATION = "add.roles.";
29  
30      protected boolean validateTkRole(TkRole role, String fieldPrefix) {
31          boolean valid = true;
32  
33          if (fieldPrefix == null)
34              fieldPrefix = "";
35  
36         String rname = role.getRoleName();
37         if (StringUtils.equalsIgnoreCase(rname, TkConstants.ROLE_TK_LOCATION_ADMIN) || StringUtils.equalsIgnoreCase(rname, TkConstants.ROLE_TK_LOCATION_VO)) {
38      	   if (StringUtils.isBlank(role.getChart())) {
39  	    	   this.putFieldError(fieldPrefix + "chart", "error.role.chart.required");
40  	           valid = false;
41      	   }
42          } else {
43          	if (!StringUtils.isBlank(role.getChart())) {
44   	    	   this.putFieldError(fieldPrefix + "chart", "error.role.chart.disallowed");
45   	           valid = false;
46       	   }
47          }
48         
49          return valid;
50      }
51  
52      /**
53       * Currently, the prefix will not work when the attributes of the collection
54       * items are set to readOnlyAfterAdd. This seems to be a built in function
55       * of rice. Errors will still appear but no marker will be placed next to
56       * the field.
57       */
58      boolean validateTkRoleGroup(TkRoleGroup roleGroup) {
59          boolean valid = true;
60  
61          int pos = 0;
62          for (TkRole role: roleGroup.getRoles()) {
63              StringBuffer prefix = new StringBuffer("roles[");
64              prefix.append(pos).append("].");
65              validateTkRole(role, prefix.toString());
66              pos++;
67          }
68  
69          return valid;
70      }
71  
72  	@Override
73  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
74  		boolean valid = true;
75  
76  		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
77  		if (pbo instanceof TkRole) {
78  			TkRole role = (TkRole)pbo;
79              valid = validateTkRole(role, null);
80  		} else if (pbo instanceof TkRoleGroup) {
81              TkRoleGroup roleGroup = (TkRoleGroup)pbo;
82              valid = validateTkRoleGroup(roleGroup);
83          }
84  
85  		return valid;
86  	}
87  
88      @Override
89      /**
90       * Validation for the 'add' role in the collection manipulator.
91       */
92      public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
93          boolean valid = true;
94  
95          if (line instanceof TkRole) {
96              TkRole role = (TkRole)line;
97              valid = validateTkRole(role, ADD_LINE_LOCATION);
98          }
99  
100         return valid;
101     }
102 }