View Javadoc
1   /*
2    * Copyright 2006 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.List;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.coa.businessobject.OrganizationReversion;
22  import org.kuali.ole.coa.businessobject.OrganizationReversionDetail;
23  import org.kuali.ole.coa.service.OrganizationReversionService;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.rice.kns.document.MaintenanceDocument;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  /**
29   * PreRules checks for the {@link OrganizationReversion} that needs to occur while still in the Struts processing. This includes defaults
30   */
31  public class OrganizationReversionPreRules extends MaintenancePreRulesBase {
32  
33      private transient OrganizationReversionService organizationReversionService;
34      public OrganizationReversionPreRules() {
35  
36      }
37  
38      /**
39       * This calls the {@link OrganizationReversionPreRules#copyKeyAttributesToDetail(OrganizationReversion)}
40       * @see org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
41       */
42      @Override
43      protected boolean doCustomPreRules(MaintenanceDocument document) {
44  
45          OrganizationReversion orgRev = (OrganizationReversion) document.getNewMaintainableObject().getBusinessObject();
46          // copy year and chart to detail records
47          copyKeyAttributesToDetail(orgRev);
48          copyDefaultObjectcodeIfNoCarryForwardByObjectCode(orgRev);
49          return true;
50      }
51  
52      /**
53       * 
54       * This copies the chart of accounts, and the fiscal year from the parent {@link OrganizationReversion} to the 
55       * {@link OrganizationReversionDetail} objects and refreshes the reference object on them if the values have 
56       * been filled out
57       * @param orgRev
58       */
59      protected void copyKeyAttributesToDetail(OrganizationReversion orgRev) {
60          if (orgRev.getUniversityFiscalYear() != null && orgRev.getUniversityFiscalYear().intValue() != 0 && StringUtils.isNotBlank(orgRev.getChartOfAccountsCode())) {
61              // loop over detail records, copying their details
62              for (OrganizationReversionDetail dtl : orgRev.getOrganizationReversionDetail()) {
63                  dtl.setChartOfAccountsCode(orgRev.getChartOfAccountsCode());
64                  dtl.setUniversityFiscalYear(orgRev.getUniversityFiscalYear());
65                  // load the object, if possible
66                  if (StringUtils.isNotBlank(dtl.getOrganizationReversionObjectCode())) {
67                      dtl.refreshReferenceObject("organizationReversionObject");
68                  }
69              }
70          }
71  
72      }
73      
74      /**
75       * 
76       * This copies the object code from system parameter to the Organization Reversion object code field if the 
77       * "Carry Forward by Object Code" indicator is set to true
78       * @param orgRev
79       */
80      protected void copyDefaultObjectcodeIfNoCarryForwardByObjectCode(OrganizationReversion orgRev) {
81          if (orgRev.isCarryForwardByObjectCodeIndicator() == true) return; 
82          
83          if (organizationReversionService == null) {
84              organizationReversionService = SpringContext.getBean(OrganizationReversionService.class);
85          }
86          String objectCode = organizationReversionService.getOrganizationReversionDetaiFromSystemParameters();
87          
88          List<OrganizationReversionDetail> details = orgRev.getOrganizationReversionDetail();
89          for (OrganizationReversionDetail dtl : details) {
90              if (ObjectUtils.isNull(dtl.getOrganizationReversionObjectCode()))  {
91                  dtl.setOrganizationReversionObjectCode(objectCode);
92              }
93          }
94      }       
95  }