1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.coa.document.validation.impl;
17  
18  import java.sql.Timestamp;
19  import java.util.Calendar;
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.lang.time.DateUtils;
26  import org.kuali.ole.coa.businessobject.Account;
27  import org.kuali.ole.coa.businessobject.AccountDelegate;
28  import org.kuali.ole.sys.OLEConstants;
29  import org.kuali.ole.sys.OLEKeyConstants;
30  import org.kuali.ole.sys.OLEPropertyConstants;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.ole.sys.document.service.FinancialSystemDocumentTypeService;
33  import org.kuali.ole.sys.document.validation.impl.KfsMaintenanceDocumentRuleBase;
34  import org.kuali.rice.core.api.util.type.KualiDecimal;
35  import org.kuali.rice.kim.api.identity.Person;
36  import org.kuali.rice.kns.document.MaintenanceDocument;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  
40  
41  
42  
43  public class DelegateRule extends KfsMaintenanceDocumentRuleBase {
44  
45      protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DelegateRule.class);
46  
47      protected AccountDelegate oldDelegate;
48      protected AccountDelegate newDelegate;
49  
50      
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61      @Override
62      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
63  
64          LOG.debug("Entering processCustomSaveDocumentBusinessRules()");
65          setupConvenienceObjects(document);
66  
67          
68          checkSimpleRules();
69  
70          
71          checkOnlyOnePrimaryRoute(document);
72  
73          
74          checkDelegateUserRules(document);
75  
76          return true;
77      }
78  
79      
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90      @Override
91      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
92          LOG.debug("Entering processCustomRouteDocumentBusinessRules()");
93  
94          setupConvenienceObjects(document);
95  
96          
97          boolean success = checkSimpleRules();
98          
99          
100         success &= checkOnlyOnePrimaryRoute(document);
101 
102         
103         success &= checkDelegateUserRules(document);
104         
105         return success;
106     }
107 
108     
109 
110 
111 
112 
113 
114 
115 
116 
117 
118     @Override
119     protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
120 
121         boolean success = true;
122 
123         LOG.debug("Entering processCustomApproveDocumentBusinessRules()");
124         setupConvenienceObjects(document);
125 
126         
127         success &= checkSimpleRules();
128 
129         success &= checkOnlyOnePrimaryRoute(document);
130 
131         
132         success &= checkDelegateUserRules(document);
133 
134         return success;
135     }
136 
137     
138 
139 
140 
141 
142 
143 
144     protected void setupConvenienceObjects(MaintenanceDocument document) {
145 
146         
147         oldDelegate = (AccountDelegate) super.getOldBo();
148 
149         
150         newDelegate = (AccountDelegate) super.getNewBo();
151     }
152 
153 
154     
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166     protected boolean checkSimpleRules() {
167         boolean success = true;
168 
169         Map<String, String> fieldValues = new HashMap<String, String>();
170         fieldValues.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, newDelegate.getChartOfAccountsCode());
171         fieldValues.put(OLEPropertyConstants.ACCOUNT_NUMBER, newDelegate.getAccountNumber());
172         
173         int accountExist = getBoService().countMatching(Account.class, fieldValues);
174         if (accountExist<=0) {
175             putFieldError(OLEPropertyConstants.ACCOUNT_NUMBER, OLEKeyConstants.ERROR_EXISTENCE, newDelegate.getAccountNumber());
176             success &= false;
177         }
178 
179         
180         boolean newActive = newDelegate.isActive();
181         if ((ObjectUtils.isNotNull(newDelegate.getAccountDelegateStartDate())) && newActive) {
182             Timestamp today = getDateTimeService().getCurrentTimestamp();
183             today.setTime(DateUtils.truncate(today, Calendar.DAY_OF_MONTH).getTime());
184             if (newDelegate.getAccountDelegateStartDate().before(today)) {
185                 putFieldError(OLEPropertyConstants.ACCOUNT_DELEGATE_START_DATE, OLEKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_STARTDATE_IN_PAST);
186                 success &= false;
187             }
188         }
189 
190         
191         KualiDecimal fromAmount = newDelegate.getFinDocApprovalFromThisAmt();
192         if (ObjectUtils.isNotNull(fromAmount)) {
193             if (fromAmount.isLessThan(KualiDecimal.ZERO)) {
194                 putFieldError(OLEPropertyConstants.FIN_DOC_APPROVAL_FROM_THIS_AMT, OLEKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_FROM_AMOUNT_NONNEGATIVE);
195                 success &= false;
196             }
197         }
198 
199         
200         KualiDecimal toAmount = newDelegate.getFinDocApprovalToThisAmount();
201         if (ObjectUtils.isNotNull(toAmount) && !toAmount.equals(KualiDecimal.ZERO)) {
202             
203             if (fromAmount != null && toAmount.isLessThan(fromAmount)) {
204                 putFieldError(OLEPropertyConstants.FIN_DOC_APPROVAL_TO_THIS_AMOUNT, OLEKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_TO_AMOUNT_MORE_THAN_FROM_OR_ZERO);
205                 success &= false;
206             } else if (toAmount.isLessThan(KualiDecimal.ZERO)) {
207                 putFieldError(OLEPropertyConstants.FIN_DOC_APPROVAL_TO_THIS_AMOUNT, OLEKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_TO_AMOUNT_MORE_THAN_FROM_OR_ZERO);
208                 success &= false;
209             }
210         }
211         
212         
213         final FinancialSystemDocumentTypeService documentService = SpringContext.getBean(FinancialSystemDocumentTypeService.class);
214         if (!documentService.isFinancialSystemDocumentType(newDelegate.getFinancialDocumentTypeCode())) {
215             putFieldError(OLEPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, OLEKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_INVALID_DOC_TYPE, new String[] { newDelegate.getFinancialDocumentTypeCode(), OLEConstants.ROOT_DOCUMENT_TYPE });
216             success = false;
217         }
218 
219         return success;
220     }
221 
222     
223 
224 
225 
226 
227 
228     protected boolean checkOnlyOnePrimaryRoute(MaintenanceDocument document) {
229 
230         boolean success = true;
231         boolean checkDb = false;
232         boolean newPrimary;
233         boolean newActive;
234         boolean blockingDocumentExists;
235 
236         
237         newPrimary = newDelegate.isAccountsDelegatePrmrtIndicator();
238         if (!newPrimary) {
239             return success;
240         }
241 
242         
243         newActive = newDelegate.isActive();
244         if (!newActive) {
245             return success;
246         }
247 
248         
249         
250         if (document.isNew()) {
251             if (newPrimary) {
252                 checkDb = true;
253             }
254         }
255 
256         
257         
258         if (document.isEdit()) {
259             boolean oldPrimary = oldDelegate.isAccountsDelegatePrmrtIndicator();
260             if (!oldPrimary && newPrimary) {
261                 checkDb = true;
262             }
263         }
264 
265         
266         if (!checkDb) {
267             return success;
268         }
269 
270         
271         
272 
273         Map whereMap = new HashMap();
274         whereMap.put("chartOfAccountsCode", newDelegate.getChartOfAccountsCode());
275         whereMap.put("accountNumber", newDelegate.getAccountNumber());
276         whereMap.put("accountsDelegatePrmrtIndicator", Boolean.TRUE);
277         whereMap.put("financialDocumentTypeCode", newDelegate.getFinancialDocumentTypeCode());
278         whereMap.put("active", Boolean.TRUE);
279 
280         
281         Collection primaryRoutes = getBoService().findMatching(AccountDelegate.class, whereMap);
282 
283         
284         if (primaryRoutes.size() > 0) {
285             putGlobalError(OLEKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_PRIMARY_ROUTE_ALREADY_EXISTS_FOR_DOCTYPE);
286             success &= false;
287         }
288 
289         return success;
290     }
291 
292     
293 
294 
295 
296 
297 
298     protected boolean checkDelegateUserRules(MaintenanceDocument document) {
299         boolean success = true;
300         final Person accountDelegate = newDelegate.getAccountDelegate();
301 
302         
303         if (ObjectUtils.isNull(accountDelegate)) {
304             return success;
305         }
306         
307         
308         if (document.getOldMaintainableObject() != null && document.getOldMaintainableObject().getBusinessObject() != null && ((AccountDelegate)document.getOldMaintainableObject().getBusinessObject()).isActive() && !((AccountDelegate)document.getNewMaintainableObject().getBusinessObject()).isActive()) {
309             return success;
310         }
311 
312         if (StringUtils.isBlank(accountDelegate.getEntityId()) || !getDocumentHelperService().getDocumentAuthorizer(document).isAuthorized(document, OLEConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.namespace, OLEConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.name, accountDelegate.getPrincipalId())) {
313             super.putFieldError("accountDelegate.principalName", OLEKeyConstants.ERROR_USER_MISSING_PERMISSION, new String[] {accountDelegate.getName(), OLEConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.namespace, OLEConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.name});
314             success = false;
315         }
316         return success;
317     }
318 }