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.assignment.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.kpme.core.assignment.AssignmentBo;
20  import org.kuali.kpme.core.assignment.AssignmentBo;
21  import org.kuali.kpme.core.assignment.account.AssignmentAccountBo;
22  import org.kuali.kpme.core.bo.HrBusinessObject;
23  import org.kuali.kpme.core.bo.HrDataObjectMaintainableImpl;
24  import org.kuali.kpme.core.service.HrServiceLocator;
25  import org.kuali.kpme.core.util.ValidationUtils;
26  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
27  import org.kuali.rice.krad.uif.container.CollectionGroup;
28  import org.kuali.rice.krad.uif.view.View;
29  import org.kuali.rice.krad.uif.view.ViewModel;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
32  
33  import java.util.ArrayList;
34  
35  /**
36   * Override the Maintenance page behavior for Assignment object
37   * 
38   * 
39   */
40  public class AssignmentMaintainableServiceImpl extends HrDataObjectMaintainableImpl {
41  
42  	/**
43  	 * 
44  	 */
45  	private static final long serialVersionUID = 1L;
46  
47  	@Override
48  	public HrBusinessObject getObjectById(String id) {
49  		return AssignmentBo.from(HrServiceLocator.getAssignmentService().getAssignment(id));
50  	}
51  
52  	@Override
53  	public void customSaveLogic(HrBusinessObject hrObj) {
54  		AssignmentBo assignment = (AssignmentBo)hrObj;
55  		for (AssignmentAccountBo assignAcct : assignment.getAssignmentAccounts()) {
56  			if(!isOldDataObjectInDocument()){ //prevents duplicate object on edit
57  				assignAcct.setTkAssignAcctId(null);
58  			}
59  			assignAcct.setTkAssignmentId(assignment.getTkAssignmentId());
60  
61  		}
62  	}
63  
64      //KPME-2624 added logic to save current logged in user to UserPrincipal id for collections
65      @Override
66      public void prepareForSave() {
67      AssignmentBo assignment = (AssignmentBo)this.getDataObject();
68          for (AssignmentAccountBo assignAcct : assignment.getAssignmentAccounts()) {
69              assignAcct.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
70          }
71          super.prepareForSave();
72      }
73  
74      @Override
75      public void customInactiveSaveLogicNewEffective(HrBusinessObject oldHrObj) {
76          AssignmentBo bo = (AssignmentBo)oldHrObj;
77          bo.setAssignmentAccounts(new ArrayList<AssignmentAccountBo>());
78      }
79  
80  	@Override
81  	protected boolean performAddLineValidation(ViewModel viewModel, Object newLine, String collectionId, String collectionPath) {
82          boolean isValid = super.performAddLineValidation(viewModel, newLine, collectionId, collectionPath);
83          if (viewModel instanceof MaintenanceDocumentForm) {
84  	        MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) viewModel;
85  	        MaintenanceDocument document = maintenanceForm.getDocument();
86  	        if (document.getNewMaintainableObject().getDataObject() instanceof AssignmentBo) {
87  	        	AssignmentBo assignment = (AssignmentBo) document.getNewMaintainableObject().getDataObject();
88  	        	// Duty line validation
89  		        if (newLine instanceof AssignmentAccountBo) {
90  		        	AssignmentAccountBo assignmentAccount = (AssignmentAccountBo) newLine;
91  		        	boolean results = this.validateAssignmentAccount(assignmentAccount, assignment);
92  		        	if(!results) {
93  		        		return false;
94  		        	}
95  		        }
96  	        }
97          }
98  		return isValid;
99  	}
100 	
101 	private boolean validateAssignmentAccount(AssignmentAccountBo assignmentAccount, AssignmentBo assignmentObj) {
102 		boolean valid = false;
103 		
104 		if(StringUtils.isNotEmpty(assignmentAccount.getEarnCode())) {
105 			valid = ValidationUtils.validateEarnCode(assignmentAccount.getEarnCode(), assignmentObj.getEffectiveLocalDate());
106 			if(!valid) {
107 				GlobalVariables.getMessageMap().putError("newCollectionLines['document.newMaintainableObject.dataObject.assignmentAccounts'].earnCode","error.existence", "earn code '"+ assignmentAccount.getEarnCode() + "'");
108 				return valid;
109 			}
110 		}
111 		if(StringUtils.isNotEmpty(assignmentAccount.getAccountNbr())) {
112 			valid = ValidationUtils.validateAccount(assignmentAccount.getFinCoaCd(),assignmentAccount.getAccountNbr());
113 			if(!valid) {
114 				GlobalVariables.getMessageMap().putError("newCollectionLines['document.newMaintainableObject.dataObject.assignmentAccounts'].accountNbr","error.existence", "Account Number '"+ assignmentAccount.getAccountNbr() + "'");
115 				return valid;
116 			}
117 		}
118 		if(StringUtils.isNotEmpty(assignmentAccount.getFinObjectCd())) {
119 			valid = ValidationUtils.validateObjectCode(assignmentAccount.getFinObjectCd(),assignmentAccount.getFinCoaCd(),null);
120 			if (!valid) {
121 				GlobalVariables.getMessageMap().putError("newCollectionLines['document.newMaintainableObject.dataObject.assignmentAccounts'].finObjectCd","error.existence", "Object Code '"+ assignmentAccount.getFinObjectCd() + "'");
122 				return valid;
123 			}			
124 		}
125 		if (StringUtils.isNotEmpty(assignmentAccount.getFinSubObjCd())) {
126 			valid = ValidationUtils.validateSubObjectCode(String.valueOf(assignmentObj.getEffectiveLocalDate().getYear()),assignmentAccount.getFinCoaCd(),
127 					assignmentAccount.getAccountNbr(), assignmentAccount.getFinObjectCd(), assignmentAccount.getFinSubObjCd());
128 			if (!valid) {
129 				GlobalVariables.getMessageMap().putError("newCollectionLines['document.newMaintainableObject.dataObject.assignmentAccounts'].finSubObjCd","error.existence", "SubObject Code '"+ assignmentAccount.getFinSubObjCd() + "'");
130 				return valid;
131 			}
132 		} 
133 		if(assignmentAccount.getSubAcctNbr() != null && StringUtils.isNotEmpty(assignmentAccount.getSubAcctNbr())) {
134 			valid = ValidationUtils.validateSubAccount(assignmentAccount.getSubAcctNbr(),assignmentAccount.getAccountNbr(), assignmentAccount.getFinCoaCd());
135 			if (!valid) {
136 				GlobalVariables.getMessageMap().putError("newCollectionLines['document.newMaintainableObject.dataObject.assignmentAccounts'].subAcctNbr", "error.existence", "Sub-Account Number '"+ assignmentAccount.getSubAcctNbr() + "'");
137 				return valid;
138 			}
139 		}
140 		
141 		return valid;
142 	}
143     
144 }