View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ar.document;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Map;
24  
25  import org.apache.commons.collections.CollectionUtils;
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.kfs.module.ar.ArPropertyConstants;
28  import org.kuali.kfs.module.ar.businessobject.CostCategory;
29  import org.kuali.kfs.module.ar.businessobject.CostCategoryObjectCode;
30  import org.kuali.kfs.module.ar.businessobject.CostCategoryObjectConsolidation;
31  import org.kuali.kfs.module.ar.businessobject.CostCategoryObjectLevel;
32  import org.kuali.kfs.sys.KFSConstants;
33  import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
34  import org.kuali.rice.kns.document.MaintenanceDocument;
35  import org.kuali.rice.krad.bo.BusinessObject;
36  import org.kuali.rice.krad.bo.PersistableBusinessObject;
37  import org.kuali.rice.krad.util.ObjectUtils;
38  
39  /**
40   * Overridden to add informative help message
41   */
42  public class CostCategoryMaintainableImpl extends FinancialSystemMaintainable {
43      public CostCategoryMaintainableImpl() {
44          super();
45          initiateInactiveRecordDisplay();
46      }
47  
48      public CostCategoryMaintainableImpl(PersistableBusinessObject businessObject) {
49          super(businessObject);
50          initiateInactiveRecordDisplay();
51      }
52  
53      /**
54       * By default, the children record collections will hide inactive records
55       */
56      protected void initiateInactiveRecordDisplay() {
57          inactiveRecordDisplay.put(ArPropertyConstants.OBJECT_CODES, Boolean.FALSE);
58          inactiveRecordDisplay.put(ArPropertyConstants.OBJECT_LEVELS, Boolean.FALSE);
59          inactiveRecordDisplay.put(ArPropertyConstants.OBJECT_CONSOLIDATIONS, Boolean.FALSE);
60      }
61  
62      /**
63       * Overridden to push the cost category code to all child objects
64       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refresh(java.lang.String, java.util.Map, org.kuali.rice.kns.document.MaintenanceDocument)
65       */
66      @Override
67      public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
68          super.refresh(refreshCaller, fieldValues, document);
69          if (StringUtils.equals(KFSConstants.MULTIPLE_VALUE, refreshCaller)) {
70              final String collectionName = (String)fieldValues.get(KFSConstants.LOOKED_UP_COLLECTION_NAME);
71              CostCategory costCategory = (CostCategory)document.getNewMaintainableObject().getBusinessObject();
72  
73              if (StringUtils.equals(collectionName, ArPropertyConstants.OBJECT_CODES) &&!CollectionUtils.isEmpty(costCategory.getObjectCodes())) {
74                  for (CostCategoryObjectCode objectCode : costCategory.getObjectCodes()) {
75                      if (StringUtils.isBlank(objectCode.getCategoryCode())) {
76                          objectCode.setCategoryCode(costCategory.getCategoryCode());
77                      }
78                  }
79              }
80              if (StringUtils.equals(collectionName, ArPropertyConstants.OBJECT_LEVELS) && !CollectionUtils.isEmpty(costCategory.getObjectLevels())) {
81                  for (CostCategoryObjectLevel objectLevel : costCategory.getObjectLevels()) {
82                      if (StringUtils.isBlank(objectLevel.getCategoryCode())) {
83                          objectLevel.setCategoryCode(costCategory.getCategoryCode());
84                      }
85                  }
86              }
87              if (StringUtils.equals(collectionName, ArPropertyConstants.OBJECT_CONSOLIDATIONS) && !CollectionUtils.isEmpty(costCategory.getObjectConsolidations())) {
88                  for (CostCategoryObjectConsolidation objectConsolidation : costCategory.getObjectConsolidations()) {
89                      if (StringUtils.isBlank(objectConsolidation.getCategoryCode())) {
90                          objectConsolidation.setCategoryCode(costCategory.getCategoryCode());
91                      }
92                  }
93              }
94  
95              if (!ObjectUtils.isNull(document.getOldMaintainableObject()) && !ObjectUtils.isNull(document.getOldMaintainableObject().getBusinessObject())) {
96                  CostCategory oldCostCategory = (CostCategory)document.getOldMaintainableObject().getBusinessObject();
97                  if (!sameSize(oldCostCategory.getObjectCodes(), costCategory.getObjectCodes())) {
98                      if (oldCostCategory.getObjectCodes() == null) {
99                          oldCostCategory.setObjectCodes(new ArrayList<CostCategoryObjectCode>());
100                     }
101                     while (oldCostCategory.getObjectCodes().size() < costCategory.getObjectCodes().size()) {
102                         CostCategoryObjectCode paddingObjectCode = new CostCategoryObjectCode();
103                         paddingObjectCode.setCategoryCode(oldCostCategory.getCategoryCode());
104                         oldCostCategory.getObjectCodes().add(paddingObjectCode);
105                     }
106                 }
107 
108                 if (!sameSize(oldCostCategory.getObjectLevels(), costCategory.getObjectLevels())) {
109                     if (oldCostCategory.getObjectLevels() == null) {
110                         oldCostCategory.setObjectLevels(new ArrayList<CostCategoryObjectLevel>());
111                     }
112                     while (oldCostCategory.getObjectLevels().size() < costCategory.getObjectLevels().size()) {
113                         CostCategoryObjectLevel paddingObjectLevel = new CostCategoryObjectLevel();
114                         paddingObjectLevel.setCategoryCode(oldCostCategory.getCategoryCode());
115                         oldCostCategory.getObjectLevels().add(paddingObjectLevel);
116                     }
117                 }
118 
119                 if (!sameSize(oldCostCategory.getObjectConsolidations(), costCategory.getObjectConsolidations())) {
120                     if (oldCostCategory.getObjectConsolidations() == null) {
121                         oldCostCategory.setObjectConsolidations(new ArrayList<CostCategoryObjectConsolidation>());
122                     }
123                     while (oldCostCategory.getObjectConsolidations().size() < costCategory.getObjectConsolidations().size()) {
124                         CostCategoryObjectConsolidation paddingConsolidation = new CostCategoryObjectConsolidation();
125                         paddingConsolidation.setCategoryCode(oldCostCategory.getCategoryCode());
126                         oldCostCategory.getObjectConsolidations().add(paddingConsolidation);
127                     }
128                 }
129             }
130         }
131     }
132 
133     /**
134      * Determines if two collections are the same size.  Being null and having no elements are treated as equivalent
135      * @param a the first collection to check
136      * @param b the second collection to check
137      * @return true if the two Collections are the same size, false otherwise
138      */
139     protected boolean sameSize(Collection<?> a, Collection<?> b) {
140         if (CollectionUtils.isEmpty(a)) {
141             return CollectionUtils.isEmpty(b);
142         }
143         if (CollectionUtils.isEmpty(b)) {
144             return false; // a isn't empty or we would have returned; therefore, a and b can't be the same size if we're here
145         }
146         return a.size() == b.size();
147     }
148 
149     /**
150      * Overridden to filter down category code before cost category detail makes it to rules checking
151      * @see org.kuali.kfs.sys.document.FinancialSystemMaintainable#processBeforeAddLine(java.lang.String, java.lang.Class, org.kuali.rice.krad.bo.BusinessObject)
152      */
153     @Override
154     public void processBeforeAddLine(String colName, Class colClass, BusinessObject bo) {
155         super.processBeforeAddLine(colName, colClass, bo);
156         if (bo instanceof CostCategoryObjectCode) {
157             ((CostCategoryObjectCode)bo).setCategoryCode(((CostCategory)getBusinessObject()).getCategoryCode());
158         } else if (bo instanceof CostCategoryObjectLevel) {
159             ((CostCategoryObjectLevel)bo).setCategoryCode(((CostCategory)getBusinessObject()).getCategoryCode());
160         } else if (bo instanceof CostCategoryObjectConsolidation) {
161             ((CostCategoryObjectConsolidation)bo).setCategoryCode(((CostCategory)getBusinessObject()).getCategoryCode());
162         }
163     }
164 }