001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.kfs.coa.document; 017 018 import java.util.List; 019 import java.util.Map; 020 021 import org.apache.commons.lang.StringUtils; 022 import org.kuali.kfs.coa.businessobject.Chart; 023 import org.kuali.kfs.coa.businessobject.ObjectCode; 024 import org.kuali.kfs.coa.service.ObjectCodeService; 025 import org.kuali.kfs.coa.service.SubObjectTrickleDownInactivationService; 026 import org.kuali.kfs.sys.context.SpringContext; 027 import org.kuali.kfs.sys.document.FinancialSystemMaintainable; 028 import org.kuali.rice.kns.document.MaintenanceDocument; 029 import org.kuali.rice.kns.document.MaintenanceLock; 030 import org.kuali.rice.kns.util.KNSConstants; 031 import org.kuali.rice.kns.util.ObjectUtils; 032 033 public class ObjectCodeMaintainableImpl extends FinancialSystemMaintainable { 034 035 @Override 036 public List<MaintenanceLock> generateMaintenanceLocks() { 037 List<MaintenanceLock> maintenanceLocks = super.generateMaintenanceLocks(); 038 ObjectCode maintainedObjectCode = (ObjectCode) getBusinessObject(); 039 if (isInactivatingObjectCode()) { 040 maintenanceLocks.addAll(SpringContext.getBean(SubObjectTrickleDownInactivationService.class).generateTrickleDownMaintenanceLocks((ObjectCode) getBusinessObject(), documentNumber)); 041 } 042 return maintenanceLocks; 043 } 044 045 046 @Override 047 public void saveBusinessObject() { 048 boolean isInactivatingObjectCode = isInactivatingObjectCode(); 049 050 super.saveBusinessObject(); 051 052 if (isInactivatingObjectCode) { 053 SpringContext.getBean(SubObjectTrickleDownInactivationService.class).trickleDownInactivateSubObjects((ObjectCode) getBusinessObject(), documentNumber); 054 } 055 } 056 057 protected boolean isInactivatingObjectCode() { 058 // 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 059 if (KNSConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && !((ObjectCode) getBusinessObject()).isActive()) { 060 // then check if the object code was originally active. If so, then we are inactivating. 061 ObjectCode objectCodeFromDB = retrieveObjectCodeFromDB(); 062 if (ObjectUtils.isNotNull(objectCodeFromDB)) { 063 if (objectCodeFromDB.isActive()) { 064 return true; 065 } 066 } 067 } 068 return false; 069 } 070 071 protected ObjectCode retrieveObjectCodeFromDB() { 072 ObjectCode maintainedObjectCode = (ObjectCode) getBusinessObject(); 073 ObjectCode oldObjectCode = SpringContext.getBean(ObjectCodeService.class).getByPrimaryId(maintainedObjectCode.getUniversityFiscalYear(), maintainedObjectCode.getChartOfAccountsCode(), maintainedObjectCode.getFinancialObjectCode()); 074 return oldObjectCode; 075 } 076 077 /** 078 * Refreshes the Reports to Chart of Accounts code if needed 079 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refresh(java.lang.String, java.util.Map, org.kuali.rice.kns.document.MaintenanceDocument) 080 */ 081 @Override 082 public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) { 083 super.refresh(refreshCaller, fieldValues, document); 084 refreshReportsToChartOfAccountsCodeIfNecessary(document); 085 } 086 087 /** 088 * 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 089 * @param document the MaintenanceDocument to get the ObjectCode to update from 090 */ 091 protected void refreshReportsToChartOfAccountsCodeIfNecessary(MaintenanceDocument document) { 092 final ObjectCode newObjectCode = (ObjectCode)document.getNewMaintainableObject().getBusinessObject(); 093 if (!StringUtils.isBlank(newObjectCode.getChartOfAccountsCode())) { 094 newObjectCode.refreshReferenceObject("chartOfAccounts"); 095 final Chart newChart = newObjectCode.getChartOfAccounts(); 096 097 if (!ObjectUtils.isNull(newChart) && (StringUtils.isBlank(newObjectCode.getReportsToChartOfAccountsCode()) || !newObjectCode.getReportsToChartOfAccountsCode().equalsIgnoreCase(newChart.getReportsToChartOfAccountsCode()))) { 098 newObjectCode.setReportsToChartOfAccountsCode(newChart.getReportsToChartOfAccountsCode()); 099 } 100 } 101 } 102 }