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.pm.position.validation;
17  
18  import java.math.BigDecimal;
19  
20  import org.apache.commons.collections.CollectionUtils;
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.kpme.core.util.ValidationUtils;
23  import org.kuali.kpme.pm.classification.Classification;
24  import org.kuali.kpme.pm.classification.duty.ClassificationDuty;
25  import org.kuali.kpme.pm.position.Position;
26  import org.kuali.kpme.pm.position.PositionDuty;
27  import org.kuali.kpme.pm.position.funding.PositionFunding;
28  import org.kuali.rice.krad.bo.PersistableBusinessObject;
29  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
30  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
34  
35  public class PositionValidation extends MaintenanceDocumentRuleBase {
36  	@Override
37  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
38  		boolean valid = false;
39  		LOG.debug("entering custom validation for Position");
40  		Position aPosition = (Position) this.getNewDataObject();
41  		
42  		if (aPosition != null) {
43  			valid = true;
44  			valid &= this.validateDutyListPercentage(aPosition);
45  		}
46  		return valid;
47  	}
48  	
49  	private boolean validateDutyListPercentage(Position aPosition) {
50  		if(CollectionUtils.isNotEmpty(aPosition.getDutyList())) {
51  			BigDecimal sum = BigDecimal.ZERO;
52  			for(PositionDuty aDuty : aPosition.getDutyList()) {
53  				if(aDuty != null && aDuty.getPercentage() != null) {
54  					sum = sum.add(aDuty.getPercentage());
55  				}
56  			}
57  			if(sum.compareTo(new BigDecimal(100)) > 0) {
58  				String[] parameters = new String[1];
59  				parameters[0] = sum.toString();
60  				this.putFieldError("dataObject.dutyList", "duty.percentage.exceedsMaximum", parameters);
61  				return false;
62  			}
63  		}		
64  		return true;
65  	}
66  		
67  }