View Javadoc
1   /**
2    * Copyright 2004-2014 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.bo.validation;
17  
18  import java.util.ListIterator;
19  
20  import org.apache.commons.collections.CollectionUtils;
21  import org.joda.time.LocalDate;
22  import org.kuali.kpme.core.bo.HrKeyedSetBusinessObject;
23  import org.kuali.kpme.core.bo.derived.HrBusinessObjectKey;
24  import org.kuali.kpme.core.util.ValidationUtils;
25  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
26  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
27  
28  public abstract class HrKeyedSetBusinessObjectValidation<O extends HrKeyedSetBusinessObject<O, K>, K extends HrBusinessObjectKey<O, K>> extends MaintenanceDocumentRuleBase {
29  	
30  	private static final String EFFECTIVE_KEY_LIST_PROPERTY_PATH = "dataObject.effectiveKeyList[%d].groupKeyCode";
31  	private static final String EMPTY_KEY_LIST_ERROR_KEY = "keyedSet.keyList.empty";
32  	private static final String DUPLICATE_GROUP_KEY_CODE_ERROR_KEY = "keyedSet.keyList.duplicates";
33  	private static final String ERROR_EXISTENCE_KEY = "error.existence";
34  	private static final String EFFECTIVE_KEY_LIST = "dataObject.effectiveKeyList";
35  	
36  	@SuppressWarnings("unchecked")
37  	@Override
38  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
39  		boolean retVal = false;
40  		LOG.debug("entering common validation logic for abstract HrKeyedSetBusinessObject");
41  		if((this.getNewDataObject() != null) && (this.getNewDataObject() instanceof HrKeyedSetBusinessObject)) {
42  			retVal = true;
43  			HrKeyedSetBusinessObject<O,K> newKeyOwner = (HrKeyedSetBusinessObject<O,K>) this.getNewDataObject();
44  			retVal &= this.checkGroupKeyListNotEmpty(newKeyOwner);
45  			retVal &= this.checkThatAllGroupKeysExist(newKeyOwner);
46  			retVal &= this.checkNoDuplicateGroupKeyCodes(newKeyOwner);
47  		}		
48  		return retVal;
49  	}
50  
51  
52  	private boolean checkNoDuplicateGroupKeyCodes(HrKeyedSetBusinessObject<O, K> keyOwnerObj) {
53  		if(CollectionUtils.isNotEmpty(keyOwnerObj.getEffectiveKeyList()) && (keyOwnerObj.getEffectiveKeyList().size() != keyOwnerObj.getEffectiveKeySet().size())) {
54  			this.putFieldError(EFFECTIVE_KEY_LIST, DUPLICATE_GROUP_KEY_CODE_ERROR_KEY);
55  			return false;
56  		}
57  		else {
58  			return true;
59  		}
60  	}
61  
62  
63  	private boolean checkThatAllGroupKeysExist(HrKeyedSetBusinessObject<O, K> keyOwnerObj) {
64  		boolean retVal = true;
65  		LocalDate asOfDate = LocalDate.fromDateFields(keyOwnerObj.getRelativeEffectiveDate());		
66  		if(CollectionUtils.isNotEmpty(keyOwnerObj.getEffectiveKeyList())) {
67  	    	for (ListIterator<K> iterator = keyOwnerObj.getEffectiveKeyList().listIterator(); iterator.hasNext(); ) {
68  				int index = iterator.nextIndex();
69  				K key = iterator.next();
70  				if(!ValidationUtils.validateGroupKey(key.getGroupKeyCode(), asOfDate)) {
71  					// put error message
72  					this.putFieldError( String.format(EFFECTIVE_KEY_LIST_PROPERTY_PATH, index), ERROR_EXISTENCE_KEY, "Group Key '"+ key.getGroupKeyCode() + "'");
73  					retVal = false;
74  				}
75  			}
76      	}		
77  		return retVal;
78  	}
79  	
80  
81  
82  	private boolean checkGroupKeyListNotEmpty(HrKeyedSetBusinessObject<?,?> keyOwnerObj) {
83  		if(CollectionUtils.isEmpty(keyOwnerObj.getEffectiveKeyList())) {
84  			this.putFieldError(EFFECTIVE_KEY_LIST, EMPTY_KEY_LIST_ERROR_KEY);
85  			return false;
86  		}
87  		else {
88  			return true;
89  		}
90  	}
91  
92  }