View Javadoc
1   /*
2    * Copyright 2008 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;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.coa.businessobject.Chart;
23  import org.kuali.ole.coa.businessobject.ObjectCode;
24  import org.kuali.ole.coa.service.ObjectCodeService;
25  import org.kuali.ole.coa.service.SubObjectTrickleDownInactivationService;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.document.FinancialSystemMaintainable;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.krad.maintenance.MaintenanceLock;
30  import org.kuali.rice.krad.util.KRADConstants;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  public class ObjectCodeMaintainableImpl extends FinancialSystemMaintainable {
34  
35      @Override
36      public List<MaintenanceLock> generateMaintenanceLocks() {
37          List<MaintenanceLock> maintenanceLocks = super.generateMaintenanceLocks();
38          ObjectCode maintainedObjectCode = (ObjectCode) getBusinessObject();
39          if (isInactivatingObjectCode()) {
40              maintenanceLocks.addAll(SpringContext.getBean(SubObjectTrickleDownInactivationService.class).generateTrickleDownMaintenanceLocks((ObjectCode) getBusinessObject(), getDocumentNumber()));
41          }
42          return maintenanceLocks;
43      }
44  
45      
46      @Override
47      public void saveBusinessObject() {
48          boolean isInactivatingObjectCode = isInactivatingObjectCode();
49          
50          super.saveBusinessObject();
51          
52          if (isInactivatingObjectCode) {
53              SpringContext.getBean(SubObjectTrickleDownInactivationService.class).trickleDownInactivateSubObjects((ObjectCode) getBusinessObject(), getDocumentNumber());
54          }
55      }
56      
57      protected boolean isInactivatingObjectCode() {
58          // the object code has to be inactive on the new side during an edit for it to be possible that we are inactivating an object code
59          if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && !((ObjectCode) getBusinessObject()).isActive()) {
60              // then check if the object code was originally active.  If so, then we are inactivating.
61              ObjectCode objectCodeFromDB = retrieveObjectCodeFromDB();
62              if (ObjectUtils.isNotNull(objectCodeFromDB)) {
63                  if (objectCodeFromDB.isActive()) {
64                      return true;
65                  }
66              }
67          }
68          return false;
69      }
70      
71      protected ObjectCode retrieveObjectCodeFromDB() {
72          ObjectCode maintainedObjectCode = (ObjectCode) getBusinessObject();
73          ObjectCode oldObjectCode = SpringContext.getBean(ObjectCodeService.class).getByPrimaryId(maintainedObjectCode.getUniversityFiscalYear(), maintainedObjectCode.getChartOfAccountsCode(), maintainedObjectCode.getFinancialObjectCode());
74          return oldObjectCode;
75      }
76  
77      /**
78       * Refreshes the Reports to Chart of Accounts code if needed
79       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refresh(java.lang.String, java.util.Map, org.kuali.rice.kns.document.MaintenanceDocument)
80       */
81      @Override
82      public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
83          super.refresh(refreshCaller, fieldValues, document);
84          refreshReportsToChartOfAccountsCodeIfNecessary(document);
85      }
86      
87      /**
88       * Insures that the reports to chart of accounts code on the document is populated by the chosen chart of account's reports to chart code
89       * @param document the MaintenanceDocument to get the ObjectCode to update from
90       */
91      protected void refreshReportsToChartOfAccountsCodeIfNecessary(MaintenanceDocument document) {
92          final ObjectCode newObjectCode = (ObjectCode)document.getNewMaintainableObject().getBusinessObject();
93          if (!StringUtils.isBlank(newObjectCode.getChartOfAccountsCode())) {
94              newObjectCode.refreshReferenceObject("chartOfAccounts");
95              final Chart newChart = newObjectCode.getChartOfAccounts();
96              
97              if (!ObjectUtils.isNull(newChart) && (StringUtils.isBlank(newObjectCode.getReportsToChartOfAccountsCode()) || !newObjectCode.getReportsToChartOfAccountsCode().equalsIgnoreCase(newChart.getReportsToChartOfAccountsCode()))) {
98                  newObjectCode.setReportsToChartOfAccountsCode(newChart.getReportsToChartOfAccountsCode());
99              }
100         }
101     }
102 }