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;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.kuali.ole.coa.businessobject.OrganizationReversion;
26  import org.kuali.ole.coa.businessobject.OrganizationReversionCategory;
27  import org.kuali.ole.coa.businessobject.OrganizationReversionDetail;
28  import org.kuali.ole.coa.service.OrganizationReversionDetailTrickleDownInactivationService;
29  import org.kuali.ole.coa.service.OrganizationReversionService;
30  import org.kuali.ole.sys.context.SpringContext;
31  import org.kuali.ole.sys.document.FinancialSystemMaintainable;
32  import org.kuali.rice.kns.document.MaintenanceDocument;
33  import org.kuali.rice.kns.maintenance.Maintainable;
34  import org.kuali.rice.kns.web.ui.Field;
35  import org.kuali.rice.kns.web.ui.Row;
36  import org.kuali.rice.kns.web.ui.Section;
37  import org.kuali.rice.krad.util.KRADConstants;
38  import org.kuali.rice.krad.util.ObjectUtils;
39  
40  /**
41   * This class provides some specific functionality for the {@link OrganizationReversion} maintenance document inner class for doing
42   * comparisons on {@link OrganizationReversionCategory} populateBusinessObject setBusinessObject - pre-populate the static list of
43   * details with each category isRelationshipRefreshable - makes sure that {@code organizationReversionGlobalDetails} isn't wiped out
44   * accidentally
45   */
46  public class OrganizationReversionMaintainableImpl extends FinancialSystemMaintainable {
47      private static transient OrganizationReversionService organizationReversionService;
48      private static transient OrganizationReversionDetailTrickleDownInactivationService trickleDownInactivationService;
49  
50      /**
51       * This comparator is used internally for sorting the list of categories
52       */
53      protected static class CategoryComparator implements Comparator<OrganizationReversionDetail> {
54  
55          @Override
56          public int compare(OrganizationReversionDetail detail0, OrganizationReversionDetail detail1) {
57  
58              OrganizationReversionCategory category0 = detail0.getOrganizationReversionCategory();
59              OrganizationReversionCategory category1 = detail1.getOrganizationReversionCategory();
60  
61              String code0 = category0.getOrganizationReversionCategoryCode();
62              String code1 = category1.getOrganizationReversionCategoryCode();
63  
64              return code0.compareTo(code1);
65          }
66  
67      }
68  
69      @Override
70      public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
71          super.processAfterNew(document, requestParameters);
72  
73          OrganizationReversion organizationReversion = (OrganizationReversion) getBusinessObject();
74          List<OrganizationReversionDetail> details = organizationReversion.getOrganizationReversionDetail();
75  
76          if (details == null) {
77              details = new ArrayList<OrganizationReversionDetail>();
78              organizationReversion.setOrganizationReversionDetail(details);
79          }
80  
81          if (details.size() == 0) {
82  
83              Collection<OrganizationReversionCategory> categories = getOrganizationReversionService().getCategoryList();
84  
85              for (OrganizationReversionCategory category : categories) {
86                  if (category.isActive()) {
87                      OrganizationReversionDetail detail = new OrganizationReversionDetail();
88                      detail.setOrganizationReversionCategoryCode(category.getOrganizationReversionCategoryCode());
89                      detail.setOrganizationReversionCategory(category);
90                      details.add(detail);
91                  }
92              }
93  
94              Collections.sort(details, new CategoryComparator());
95          }
96      }
97  
98      /**
99       * A method that prevents lookups from refreshing the Organization Reversion Detail list (because, if it is refreshed before a
100      * save...it ends up destroying the list).
101      * 
102      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#isRelationshipRefreshable(java.lang.Class, java.lang.String)
103      */
104     @Override
105     protected boolean isRelationshipRefreshable(Class boClass, String relationshipName) {
106         if (relationshipName.equals("organizationReversionDetail")) {
107             return false;
108         } else {
109             return super.isRelationshipRefreshable(boClass, relationshipName);
110         }
111     }
112     
113     /**
114      * Determines if this maint doc is inactivating an organization reversion
115      * @return true if the document is inactivating an active organization reversion, false otherwise
116      */
117     protected boolean isInactivatingOrganizationReversion() {
118         // the account has to be closed on the new side when editing in order for it to be possible that we are closing the account
119         if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && !((OrganizationReversion) getBusinessObject()).isActive()) {
120             OrganizationReversion existingOrganizationReversionFromDB = retrieveExistingOrganizationReversion();
121             if (ObjectUtils.isNotNull(existingOrganizationReversionFromDB)) {
122                 // now see if the original account was not closed, in which case, we are closing the account
123                 if (existingOrganizationReversionFromDB.isActive()) {
124                     return true;
125                 }
126             }
127         }
128         return false;
129     }
130     
131     /**
132      * Determines if this maint doc is activating an organization reversion
133      * @return true if the document is activating an inactive organization reversion, false otherwise
134      */
135     protected boolean isActivatingOrganizationReversion() {
136         // the account has to be closed on the new side when editing in order for it to be possible that we are closing the account
137         if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && ((OrganizationReversion) getBusinessObject()).isActive()) {
138             OrganizationReversion existingOrganizationReversionFromDB = retrieveExistingOrganizationReversion();
139             if (ObjectUtils.isNotNull(existingOrganizationReversionFromDB)) {
140                 // now see if the original account was not closed, in which case, we are closing the account
141                 if (!existingOrganizationReversionFromDB.isActive()) {
142                     return true;
143                 }
144             }
145         }
146         return false;
147     }
148     
149     /**
150      * Grabs the old version of this org reversion from the database
151      * @return the old version of this organization reversion
152      */
153     protected OrganizationReversion retrieveExistingOrganizationReversion() {
154         OrganizationReversion orgRev = (OrganizationReversion)getBusinessObject();
155         OrganizationReversion oldOrgRev = getOrganizationReversionService().getByPrimaryId(orgRev.getUniversityFiscalYear(), orgRev.getChartOfAccountsCode(), orgRev.getOrganizationCode());
156         return oldOrgRev;
157     }
158 
159     /**
160      * Overridden to trickle down inactivation or activation to details
161      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveBusinessObject()
162      */
163     @Override
164     public void saveBusinessObject() {
165         super.saveBusinessObject();
166         
167         if (isActivatingOrganizationReversion()) {
168             getTrickleDownInactivationService().trickleDownActiveOrganizationReversionDetails((OrganizationReversion)getBusinessObject(), getDocumentNumber());
169         } else if (isInactivatingOrganizationReversion()) {
170             getTrickleDownInactivationService().trickleDownInactiveOrganizationReversionDetails((OrganizationReversion)getBusinessObject(), getDocumentNumber());
171         }
172     }
173 
174     /**
175      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getSections(org.kuali.rice.kns.document.MaintenanceDocument, org.kuali.rice.kns.maintenance.Maintainable)
176      * 
177      * KRAD Conversion: Inquirable performs conditional display/hiding of the fields/sections on the inquiry
178      * But all field/section definitions are in data dictionary for bo Organization.
179      */
180     @Override
181     public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
182         List<Section> sections = super.getSections(document, oldMaintainable);
183         for (Section section : sections) {
184             for (Row row : section.getRows()) {
185                 List<Field> updatedFields = new ArrayList<Field>();
186                 for (Field field : row.getFields()) {
187                     if (shouldIncludeField(field)) {
188                         updatedFields.add(field);
189                     }
190                 }
191                 row.setFields(updatedFields);
192             }
193         }
194         return sections;
195     }
196     
197     /**
198      * Determines if the given field should be included in the updated row, once we take out inactive categories
199      * @param field the field to check
200      * @return true if the field should be included (ie, it doesn't describe an organization reversion with an inactive category); false otherwise
201      * 
202      * KRAD Conversion: Determines if fields should be included in the section.
203      * But all field/section definitions are in data dictionary.
204      */
205     protected boolean shouldIncludeField(Field field) {
206         boolean includeField = true;
207         if (field.getContainerRows() != null) {
208             for (Row containerRow : field.getContainerRows()) {
209                 for (Field containedField : containerRow.getFields()) {
210                     if (containedField.getPropertyName().matches("organizationReversionDetail\\[\\d+\\]\\.organizationReversionCategory\\.organizationReversionCategoryName")) {
211                         final String categoryValue = containedField.getPropertyValue();
212                         includeField = getOrganizationReversionService().isCategoryActiveByName(categoryValue);
213                     }
214                 }
215             }
216         }
217         return includeField;
218     }
219 
220     protected OrganizationReversionService getOrganizationReversionService() {
221         if ( organizationReversionService == null ) {
222             organizationReversionService = SpringContext.getBean(OrganizationReversionService.class);
223         }
224         return organizationReversionService;
225     }
226 
227     protected static OrganizationReversionDetailTrickleDownInactivationService getTrickleDownInactivationService() {
228         if ( trickleDownInactivationService == null ) {
229             trickleDownInactivationService = SpringContext.getBean(OrganizationReversionDetailTrickleDownInactivationService.class);
230         }
231         return trickleDownInactivationService;
232     }
233 }