View Javadoc
1   /*
2    * Copyright 2007 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.ole.coa.document.validation.impl;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.kuali.ole.coa.businessobject.AccountingPeriod;
22  import org.kuali.ole.sys.OLEKeyConstants;
23  import org.kuali.ole.sys.businessobject.SystemOptions;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.rice.kns.document.MaintenanceDocument;
26  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
27  import org.kuali.rice.krad.service.KeyValuesService;
28  
29  /**
30   * Business rule(s) applicable to AccountingPeriodMaintence documents.
31   */
32  public class AccountingPeriodRule extends MaintenanceDocumentRuleBase {
33  
34      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountingPeriodRule.class);
35  
36      protected static final String GENERAL_FUND_CD = "GF";
37      protected static final String RESTRICTED_FUND_CD = "RF";
38      protected static final String ENDOWMENT_FUND_CD = "EN";
39      protected static final String PLANT_FUND_CD = "PF";
40  
41      protected static final String RESTRICTED_CD_RESTRICTED = "R";
42      protected static final String RESTRICTED_CD_UNRESTRICTED = "U";
43      protected static final String RESTRICTED_CD_TEMPORARILY_RESTRICTED = "T";
44      protected static final String SUB_FUND_GROUP_MEDICAL_PRACTICE_FUNDS = "MPRACT";
45      protected static final String BUDGET_RECORDING_LEVEL_MIXED = "M";
46  
47      protected AccountingPeriod oldAccountingPeriod;
48      protected AccountingPeriod newAccountingPeriod;
49  
50      public AccountingPeriodRule() {
51      }
52  
53      /**
54       * This method sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and
55       * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
56       * all sub-objects from the DB by their primary keys, if available.
57       * 
58       * @param document - the maintenanceDocument being evaluated
59       */
60      public void setupConvenienceObjects() {
61  
62          // setup oldAccountingPeriod convenience objects, make sure all possible sub-objects are populated
63          oldAccountingPeriod = (AccountingPeriod) super.getOldBo();
64  
65          // setup newAccountingPeriod convenience objects, make sure all possible sub-objects are populated
66          newAccountingPeriod = (AccountingPeriod) super.getNewBo();
67      }
68  
69      /**
70       * This method checks the following rules: calls processCustomRouteDocumentBusinessRules but does not fail if any of them fail
71       * (this only happens on routing)
72       * 
73       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
74       */
75      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
76  
77          LOG.info("processCustomSaveDocumentBusinessRules called");
78          // call the route rules to report all of the messages, but ignore the result
79          processCustomRouteDocumentBusinessRules(document);
80  
81          // Save always succeeds, even if there are business rule failures
82          return true;
83      }
84  
85      /**
86       * This method checks to see if the fiscal year for any of {@link Options} is the same as the {@link AccountingPeriod}'s fiscal
87       * year
88       * 
89       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
90       */
91      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
92  
93          LOG.info("processCustomRouteDocumentBusinessRules called");
94          setupConvenienceObjects();
95  
96          Boolean foundYear = false;
97  
98          KeyValuesService boService = SpringContext.getBean(KeyValuesService.class);
99          List optionList = (List) boService.findAll(SystemOptions.class);
100         if ( newAccountingPeriod.getUniversityFiscalYear() != null) {
101             for (Iterator iter = optionList.iterator(); iter.hasNext();) {
102                 SystemOptions options = (SystemOptions) iter.next();
103                 if (options.getUniversityFiscalYear().compareTo(newAccountingPeriod.getUniversityFiscalYear()) == 0) {
104                     foundYear = true;
105                     break;
106                 }
107             }
108         }
109         if (!foundYear) {
110             // display an error
111             putFieldError("universityFiscalYear", OLEKeyConstants.ERROR_DOCUMENT_FISCAL_PERIOD_YEAR_DOESNT_EXIST);
112         }
113 
114         return foundYear;
115     }
116 
117 }