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.apache.commons.lang.StringUtils;
26  import org.kuali.ole.coa.businessobject.OrganizationReversion;
27  import org.kuali.ole.coa.businessobject.OrganizationReversionCategory;
28  import org.kuali.ole.coa.businessobject.OrganizationReversionGlobal;
29  import org.kuali.ole.coa.businessobject.OrganizationReversionGlobalDetail;
30  import org.kuali.ole.coa.businessobject.OrganizationReversionGlobalOrganization;
31  import org.kuali.ole.coa.service.OrganizationReversionService;
32  import org.kuali.ole.sys.OLEConstants;
33  import org.kuali.ole.sys.OLEPropertyConstants;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.ole.sys.document.FinancialSystemGlobalMaintainable;
36  import org.kuali.rice.kns.document.MaintenanceDocument;
37  import org.kuali.rice.kns.lookup.LookupResultsService;
38  import org.kuali.rice.kns.web.ui.Column;
39  import org.kuali.rice.kns.web.ui.ResultRow;
40  import org.kuali.rice.krad.bo.PersistableBusinessObject;
41  import org.kuali.rice.krad.maintenance.MaintenanceLock;
42  import org.kuali.rice.krad.util.GlobalVariables;
43  
44  /**
45   * This class provides some specific functionality for the {@link OrganizationReversionGlobal} maintenance document inner class for
46   * doing comparisons on {@link OrganizationReversionCategory} generateMaintenanceLocks - generates the appropriate maintenance locks
47   * on {@link OrganizationReversion} setBusinessObject - populates the {@link OrganizationReversionGlobalDetail}s
48   * isRelationshipRefreshable - makes sure that {@code organizationReversionGlobalDetails} isn't wiped out accidentally
49   * processGlobalsAfterRetrieve - provides special handling for the details (which aren't a true collection)
50   */
51  public class OrganizationReversionGlobalMaintainableImpl extends FinancialSystemGlobalMaintainable {
52      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionGlobalMaintainableImpl.class);
53  
54      private static transient OrganizationReversionService organizationReversionService;
55  
56      /**
57       * This class is an inner class for comparing two {@link OrganizationReversionCategory}s
58       */
59      private class CategoryComparator implements Comparator<OrganizationReversionGlobalDetail> {
60          @Override
61          public int compare(OrganizationReversionGlobalDetail detailA, OrganizationReversionGlobalDetail detailB) {
62              OrganizationReversionCategory categoryA = detailA.getOrganizationReversionCategory();
63              OrganizationReversionCategory categoryB = detailB.getOrganizationReversionCategory();
64  
65              String code0 = categoryA.getOrganizationReversionCategoryCode();
66              String code1 = categoryB.getOrganizationReversionCategoryCode();
67  
68              return code0.compareTo(code1);
69          }
70      }
71  
72      /**
73       * This implementation locks all organization reversions that would be accessed by this global organization reversion. It does
74       * not lock any OrganizationReversionDetail objects, as we expect that those will be inaccessible
75       * 
76       * @see org.kuali.rice.kns.maintenance.KualiGlobalMaintainableImpl#generateMaintenaceLocks()
77       */
78      @Override
79      public List<MaintenanceLock> generateMaintenanceLocks() {
80          List<MaintenanceLock> locks = new ArrayList<MaintenanceLock>();
81          OrganizationReversionGlobal globalOrgRev = (OrganizationReversionGlobal) this.getBusinessObject();
82          if (globalOrgRev.getUniversityFiscalYear() != null && globalOrgRev.getOrganizationReversionGlobalOrganizations() != null && globalOrgRev.getOrganizationReversionGlobalOrganizations().size() > 0) { // only generate locks if we're going to have primary keys
83              for (OrganizationReversionGlobalOrganization orgRevOrg : globalOrgRev.getOrganizationReversionGlobalOrganizations()) {
84                  MaintenanceLock maintenanceLock = new MaintenanceLock();
85                  maintenanceLock.setDocumentNumber(globalOrgRev.getDocumentNumber());
86  
87                  StringBuilder lockRep = new StringBuilder();
88                  lockRep.append(OrganizationReversion.class.getName());
89                  lockRep.append(OLEConstants.Maintenance.AFTER_CLASS_DELIM);
90                  lockRep.append(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
91                  lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
92                  lockRep.append(orgRevOrg.getChartOfAccountsCode());
93                  lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
94                  lockRep.append(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
95                  lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
96                  lockRep.append(globalOrgRev.getUniversityFiscalYear().toString());
97                  lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
98                  lockRep.append(OLEPropertyConstants.ORGANIZATION_CODE);
99                  lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
100                 lockRep.append(orgRevOrg.getOrganizationCode());
101                 lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
102 
103                 maintenanceLock.setLockingRepresentation(lockRep.toString());
104                 locks.add(maintenanceLock);
105             }
106         }
107 
108         return locks;
109     }
110 
111     /**
112      * Just like OrganizationReversionMaintainableImpl's setBusinessObject method populates the list of details so there is one
113      * detail per active Organization Reversion Category, this method populates a list of Organization Reversion Change details.
114      * 
115      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#setBusinessObject(org.kuali.rice.krad.bo.PersistableBusinessObject)
116      */
117     @Override
118     public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
119         super.processAfterNew(document, requestParameters);
120         OrganizationReversionGlobal globalOrgRev = (OrganizationReversionGlobal) getBusinessObject();
121         List<OrganizationReversionGlobalDetail> details = globalOrgRev.getOrganizationReversionGlobalDetails();
122         if (LOG.isDebugEnabled()) {
123             LOG.debug("Details size before adding categories = " + details.size());    
124         }
125 
126         if (details == null) {
127             details = new ArrayList<OrganizationReversionGlobalDetail>();
128             globalOrgRev.setOrganizationReversionGlobalDetails(details);
129         }
130 
131         if (details.size() == 0) {
132 
133             Collection<OrganizationReversionCategory> categories = getOrganizationReversionService().getCategoryList();
134             for (OrganizationReversionCategory category : categories) {
135                 if (category.isActive()) {
136                     OrganizationReversionGlobalDetail detail = new OrganizationReversionGlobalDetail();
137                     detail.setOrganizationReversionCategoryCode(category.getOrganizationReversionCategoryCode());
138                     detail.setOrganizationReversionCategory(category);
139                     detail.setParentGlobalOrganizationReversion(globalOrgRev);
140                     details.add(detail);
141                 }
142             }
143             if (LOG.isDebugEnabled()) {
144                 LOG.debug("Details size after adding categories = " + details.size());
145             }
146             Collections.sort(details, new CategoryComparator());
147         }
148     }
149 
150     /**
151      * Prevents Organization Reversion Change Details from being refreshed by a look up (because doing that refresh before a save
152      * would wipe out the list of organization reversion change details).
153      * 
154      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#isRelationshipRefreshable(java.lang.Class, java.lang.String)
155      */
156     @SuppressWarnings("unchecked")
157     @Override
158     protected boolean isRelationshipRefreshable(Class boClass, String relationshipName) {
159         if (relationshipName.equals("organizationReversionGlobalDetails")) {
160             return false;
161         }
162         else {
163             return super.isRelationshipRefreshable(boClass, relationshipName);
164         }
165     }
166 
167     /**
168      * The org reversion detail collection does not behave like a true collection (no add lines). The records on the collection
169      * should not have the delete option.
170      * 
171      * @see org.kuali.rice.kns.maintenance.KualiGlobalMaintainableImpl#processGlobalsAfterRetrieve()
172      */
173     @Override
174     protected void processGlobalsAfterRetrieve() {
175         super.processGlobalsAfterRetrieve();
176         for (OrganizationReversionGlobalDetail changeDetail : ((OrganizationReversionGlobal) businessObject).getOrganizationReversionGlobalDetails()) {
177             changeDetail.setNewCollectionRecord(false);
178         }
179     }
180 
181     /**
182      *
183      * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refresh(java.lang.String, java.util.Map, org.kuali.rice.kns.document.MaintenanceDocument)
184      */
185     @SuppressWarnings("unchecked")
186     @Override
187     public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
188         super.refresh(refreshCaller, fieldValues, document);
189         final String lookupBusinessObjectClassName = (String)fieldValues.get(OLEConstants.LOOKUP_RESULTS_BO_CLASS_NAME);
190         if (!StringUtils.isBlank(lookupBusinessObjectClassName)) {
191             if (lookupBusinessObjectClassName.equals(OrganizationReversion.class.getName())) {
192                 final String lookupResultsSequenceNumber = (String)fieldValues.get(OLEConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER);
193                 if (!StringUtils.isBlank(lookupResultsSequenceNumber)) {
194                     final LookupResultsService lookupResultsService = SpringContext.getBean(LookupResultsService.class);
195                     try {
196                         final List<ResultRow> resultRows = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPrincipalId());
197                         if (!resultRows.isEmpty()) {
198                             final ResultRow topRow = resultRows.get(0);
199                             for (Column column : topRow.getColumns()) {
200                                 if (column.getPropertyName().equals(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
201                                     final String universityFiscalYearAsString = column.getPropertyValue();
202                                     final Integer universityFiscalYear = Integer.parseInt(universityFiscalYearAsString);
203 
204                                     final OrganizationReversionGlobal orgRevGlobal = ((OrganizationReversionGlobal)document.getNewMaintainableObject().getBusinessObject());
205                                     orgRevGlobal.setUniversityFiscalYear(universityFiscalYear);
206                                 }
207                             }
208                         }
209                     }
210                     catch (Exception ex) {
211                         throw new RuntimeException("Could not retrieve lookup results to populate fiscal year", ex);
212                     }
213 
214 
215                 }
216             }
217         }
218 
219     }
220 
221     @Override
222     public Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass() {
223         return OrganizationReversion.class;
224     }
225 
226     protected OrganizationReversionService getOrganizationReversionService() {
227         if ( organizationReversionService == null ) {
228             organizationReversionService = SpringContext.getBean(OrganizationReversionService.class);
229         }
230         return organizationReversionService;
231     }
232 }