View Javadoc

1   /**
2    * Copyright 2004-2013 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.web;
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.bo.HrBusinessObject;
23  import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
24  import org.kuali.kpme.core.util.ValidationUtils;
25  import org.kuali.kpme.pm.classification.Classification;
26  import org.kuali.kpme.pm.classification.duty.ClassificationDuty;
27  import org.kuali.kpme.pm.classification.flag.ClassificationFlag;
28  import org.kuali.kpme.pm.classification.qual.ClassificationQualification;
29  import org.kuali.kpme.pm.position.Position;
30  import org.kuali.kpme.pm.position.PositionDuty;
31  import org.kuali.kpme.pm.position.PositionQualification;
32  import org.kuali.kpme.pm.position.PstnFlag;
33  import org.kuali.kpme.pm.position.funding.PositionFunding;
34  import org.kuali.kpme.pm.positionflag.PositionFlag;
35  import org.kuali.kpme.pm.service.base.PmServiceLocator;
36  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
37  import org.kuali.rice.krad.uif.container.CollectionGroup;
38  import org.kuali.rice.krad.uif.view.View;
39  import org.kuali.rice.krad.util.GlobalVariables;
40  import org.kuali.rice.krad.util.KRADConstants;
41  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
42  
43  public class PositionMaintainableServiceImpl extends HrBusinessObjectMaintainableImpl {
44  
45  	private static final long serialVersionUID = 1L;
46  
47  	@Override
48  	public HrBusinessObject getObjectById(String id) {
49  		return PmServiceLocator.getPositionService().getPosition(id);
50  	}
51  	
52  	@Override
53  	public void customSaveLogic(HrBusinessObject hrObj){
54  		Position aPosition = (Position) hrObj;
55  		for(PositionQualification aQual : aPosition.getQualificationList()) {
56  			aQual.setHrPositionId(aPosition.getHrPositionId());
57  			aQual.setPmQualificationId(null);
58  		}
59  		for(PositionDuty aDuty : aPosition.getDutyList()) {
60  			aDuty.setHrPositionId(aPosition.getHrPositionId());
61  			aDuty.setPmDutyId(null);
62  		}
63  		for(PstnFlag aFlag : aPosition.getFlagList()) {
64  			aFlag.setHrPositionId(aPosition.getHrPositionId());
65  			aFlag.setPmFlagId(null);
66  		}
67  		for(PositionFunding aFunding : aPosition.getFundingList()) {
68  			aFunding.setHrPositionId(aPosition.getHrPositionId());
69  			aFunding.setPmPositionFunctionId(null);
70  		}
71  		
72  	}
73  	
74  	@Override
75      protected boolean performAddLineValidation(View view, CollectionGroup collectionGroup, Object model,
76              Object addLine) {
77          boolean isValid = super.performAddLineValidation(view, collectionGroup, model, addLine);
78          if (model instanceof MaintenanceDocumentForm) {
79  	        MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
80  	        MaintenanceDocument document = maintenanceForm.getDocument();
81  	        if (document.getNewMaintainableObject().getDataObject() instanceof Position) {
82  	        	Position aPosition = (Position) document.getNewMaintainableObject().getDataObject();
83  	        	// Duty line validation
84  		        if (addLine instanceof PositionDuty) {
85  		        	PositionDuty pd = (PositionDuty) addLine;
86  		        	boolean results = this.validateDutyListPercentage(pd, aPosition);
87  		        	if(!results) {
88  		        		return false;
89  		        	}
90  		        }
91  	        	// Funding line validation
92  		        if (addLine instanceof PositionFunding) {
93  		        	PositionFunding pf = (PositionFunding) addLine;
94  		        	boolean results = this.validateAddFundingLine(pf, aPosition);
95  		        	if(!results) {
96  		        		return false;
97  		        	}
98  		        }
99  	        }
100         }
101 
102         return isValid;
103     }
104 	
105 	private boolean validateDutyListPercentage(PositionDuty pd, Position aPosition) {
106 		if(CollectionUtils.isNotEmpty(aPosition.getDutyList()) && pd.getPercentage() != null) {
107 			BigDecimal sum = pd.getPercentage();
108 			for(PositionDuty aDuty : aPosition.getDutyList()) {
109 				if(aDuty != null && aDuty.getPercentage() != null) {
110 					sum = sum.add(aDuty.getPercentage());
111 				}
112 			}
113 			if(sum.compareTo(new BigDecimal(100)) > 0) {
114 				GlobalVariables.getMessageMap().putError("Position-duties", "duty.percentage.exceedsMaximum", sum.toString());
115 				return false;
116 			}
117 		}		
118 		return true;
119 	}
120 	
121 	protected boolean validateAddFundingLine(PositionFunding pf, Position aPosition) {
122     	if(pf.getEffectiveDate() != null && aPosition.getEffectiveDate() != null) {
123     		if(pf.getEffectiveDate().compareTo(aPosition.getEffectiveDate()) < 0) {
124     			String[] parameters = new String[2];
125     			parameters[0] = pf.getEffectiveDate().toString();
126     			parameters[1] = aPosition.getEffectiveDate().toString();
127     			// using section id as the error key because KRAD does not support error matching on property names for collections as in 2.3M2
128     			GlobalVariables.getMessageMap().putError("Position-fundings","error.funding.effdt.invalid", parameters);
129    			 	return false;
130     		}
131     	}
132     	if(StringUtils.isNotEmpty(pf.getAccount())) {
133     		boolean results = ValidationUtils.validateAccount(pf.getChart(), pf.getAccount());
134     		if(!results) {
135     			GlobalVariables.getMessageMap().putError("Position-fundings", "error.existence", "Account '" + pf.getAccount() + "'");
136     			return results;
137     		}
138     	}
139     	if(StringUtils.isNotEmpty(pf.getSubAccount())) {
140     		boolean results = ValidationUtils.validateSubAccount(pf.getSubAccount(), pf.getAccount(), pf.getChart());
141     		if(!results) {
142 	   			 GlobalVariables.getMessageMap().putError("Position-fundings","error.existence", "SubAccount '" + pf.getSubAccount() + "'");
143 	   			 return results;
144     		}
145     	}
146     	if(StringUtils.isNotEmpty(pf.getObjectCode())) {
147     		boolean results = ValidationUtils.validateObjectCode(pf.getObjectCode(), pf.getChart(), Integer.valueOf(pf.getEffectiveLocalDate().getYear()));
148     		if(!results) {
149       			 GlobalVariables.getMessageMap().putError("Position-fundings","error.existence", "ObjectCode '" + pf.getObjectCode() + "'");
150       			 return results;
151     		}
152     	}
153     	if(StringUtils.isNotEmpty(pf.getSubObjectCode())) {
154     		boolean results = ValidationUtils.validateSubObjectCode(String.valueOf(pf.getEffectiveLocalDate().getYear()),
155     				pf.getChart(),
156     				pf.getAccount(),
157     				pf.getObjectCode(),
158     				pf.getSubObjectCode());
159     		if(!results) {
160       			 GlobalVariables.getMessageMap().putError("Position-fundings","error.existence", "SubObjectCode '" + pf.getSubObjectCode() + "'");
161       			 return results;
162     		}
163     	}
164     	return true;
165     
166 	}
167 
168 }