001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.chart.maintenance;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.List;
023:
024: import org.kuali.core.bo.PersistableBusinessObject;
025: import org.kuali.core.document.MaintenanceLock;
026: import org.kuali.core.maintenance.KualiGlobalMaintainableImpl;
027: import org.kuali.core.util.TypedArrayList;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.chart.bo.OrganizationReversion;
031: import org.kuali.module.chart.bo.OrganizationReversionCategory;
032: import org.kuali.module.chart.bo.OrganizationReversionGlobal;
033: import org.kuali.module.chart.bo.OrganizationReversionGlobalDetail;
034: import org.kuali.module.chart.bo.OrganizationReversionGlobalOrganization;
035: import org.kuali.module.chart.service.OrganizationReversionService;
036:
037: /**
038: * This class provides some specific functionality for the {@link OrganizationReversionGlobal} maintenance document inner class for
039: * doing comparisons on {@link OrganizationReversionCategory} generateMaintenanceLocks - generates the appropriate maintenance locks
040: * on {@link OrganizationReversion} setBusinessObject - populates the {@link OrganizationReversionGlobalDetail}s
041: * isRelationshipRefreshable - makes sure that {@code organizationReversionGlobalDetails} isn't wiped out accidentally
042: * processGlobalsAfterRetrieve - provides special handling for the details (which aren't a true collection)
043: */
044: public class OrganizationReversionGlobalMaintainableImpl extends
045: KualiGlobalMaintainableImpl {
046: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(OrganizationReversionGlobalMaintainableImpl.class);
048:
049: /**
050: * This class is an inner class for comparing two {@link OrganizationReversionCategory}s
051: */
052: private class CategoryComparator implements
053: Comparator<OrganizationReversionGlobalDetail> {
054: public int compare(OrganizationReversionGlobalDetail detailA,
055: OrganizationReversionGlobalDetail detailB) {
056: OrganizationReversionCategory categoryA = detailA
057: .getOrganizationReversionCategory();
058: OrganizationReversionCategory categoryB = detailB
059: .getOrganizationReversionCategory();
060:
061: String code0 = categoryA
062: .getOrganizationReversionCategoryCode();
063: String code1 = categoryB
064: .getOrganizationReversionCategoryCode();
065:
066: return code0.compareTo(code1);
067: }
068: }
069:
070: /**
071: * This implementation locks all organization reversions that would be accessed by this global organization reversion. It does
072: * not lock any OrganizationReversionDetail objects, as we expect that those will be inaccessible
073: *
074: * @see org.kuali.core.maintenance.KualiGlobalMaintainableImpl#generateMaintenaceLocks()
075: */
076: @Override
077: public List<MaintenanceLock> generateMaintenanceLocks() {
078: List<MaintenanceLock> locks = new ArrayList<MaintenanceLock>();
079: OrganizationReversionGlobal globalOrgRev = (OrganizationReversionGlobal) this
080: .getBusinessObject();
081: if (globalOrgRev.getUniversityFiscalYear() != null
082: && globalOrgRev
083: .getOrganizationReversionGlobalOrganizations() != null
084: && globalOrgRev
085: .getOrganizationReversionGlobalOrganizations()
086: .size() > 0) { // only
087: // generate
088: // locks
089: // if
090: // we're
091: // going
092: // to
093: // have
094: // primary
095: // keys
096: for (OrganizationReversionGlobalOrganization orgRevOrg : globalOrgRev
097: .getOrganizationReversionGlobalOrganizations()) {
098: MaintenanceLock maintenanceLock = new MaintenanceLock();
099: maintenanceLock.setDocumentNumber(globalOrgRev
100: .getDocumentNumber());
101:
102: StringBuffer lockRep = new StringBuffer();
103: lockRep.append(OrganizationReversion.class.getName());
104: lockRep
105: .append(KFSConstants.Maintenance.AFTER_CLASS_DELIM);
106: lockRep.append("chartOfAccountsCode");
107: lockRep
108: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
109: lockRep.append(orgRevOrg.getChartOfAccountsCode());
110: lockRep
111: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
112: lockRep.append("universityFiscalYear");
113: lockRep
114: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
115: lockRep.append(globalOrgRev.getUniversityFiscalYear()
116: .toString());
117: lockRep
118: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
119: lockRep.append("organizationCode");
120: lockRep
121: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
122: lockRep.append(orgRevOrg.getOrganizationCode());
123: lockRep
124: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
125:
126: maintenanceLock.setLockingRepresentation(lockRep
127: .toString());
128: locks.add(maintenanceLock);
129: }
130: }
131:
132: return locks;
133: }
134:
135: /**
136: * Just like OrganizationReversionMaintainableImpl's setBusinessObject method populates the list of details so there is one
137: * detail per active Organization Reversion Category, this method populates a list of Organization Reversion Change details.
138: *
139: * @see org.kuali.core.maintenance.KualiMaintainableImpl#setBusinessObject(org.kuali.core.bo.PersistableBusinessObject)
140: */
141: @Override
142: public void setBusinessObject(
143: PersistableBusinessObject businessObject) {
144: super .setBusinessObject(businessObject);
145: OrganizationReversionService organizationReversionService = SpringContext
146: .getBean(OrganizationReversionService.class);
147: OrganizationReversionGlobal globalOrgRev = (OrganizationReversionGlobal) businessObject;
148: List<OrganizationReversionGlobalDetail> details = globalOrgRev
149: .getOrganizationReversionGlobalDetails();
150: LOG.debug("Details size before adding categories = "
151: + details.size());
152:
153: if (details == null) {
154: details = new TypedArrayList(
155: OrganizationReversionGlobalDetail.class);
156: globalOrgRev.setOrganizationReversionGlobalDetails(details);
157: }
158:
159: if (details.size() == 0) {
160:
161: Collection<OrganizationReversionCategory> categories = organizationReversionService
162: .getCategoryList();
163: for (OrganizationReversionCategory category : categories) {
164: if (category
165: .isOrganizationReversionCategoryActiveIndicator()) {
166: OrganizationReversionGlobalDetail detail = new OrganizationReversionGlobalDetail();
167: detail
168: .setOrganizationReversionCategoryCode(category
169: .getOrganizationReversionCategoryCode());
170: detail.setOrganizationReversionCategory(category);
171: detail
172: .setParentGlobalOrganizationReversion(globalOrgRev);
173: details.add(detail);
174: }
175: }
176: LOG.debug("Details size after adding categories = "
177: + details.size());
178: Collections.sort(details, new CategoryComparator());
179: }
180: super .setBusinessObject(businessObject);
181: }
182:
183: /**
184: * Prevents Organization Reversion Change Details from being refreshed by a look up (because doing that refresh before a save
185: * would wipe out the list of organization reversion change details).
186: *
187: * @see org.kuali.core.maintenance.KualiMaintainableImpl#isRelationshipRefreshable(java.lang.Class, java.lang.String)
188: */
189: @Override
190: protected boolean isRelationshipRefreshable(Class boClass,
191: String relationshipName) {
192: if (relationshipName
193: .equals("organizationReversionGlobalDetails")) {
194: return false;
195: } else {
196: return super .isRelationshipRefreshable(boClass,
197: relationshipName);
198: }
199: }
200:
201: /**
202: * The org reversion detail collection does not behave like a true collection (no add lines). The records on the collection
203: * should not have the delete option.
204: *
205: * @see org.kuali.core.maintenance.KualiGlobalMaintainableImpl#processGlobalsAfterRetrieve()
206: */
207: @Override
208: protected void processGlobalsAfterRetrieve() {
209: super .processGlobalsAfterRetrieve();
210: for (OrganizationReversionGlobalDetail changeDetail : ((OrganizationReversionGlobal) businessObject)
211: .getOrganizationReversionGlobalDetails()) {
212: changeDetail.setNewCollectionRecord(false);
213: }
214: }
215: }
|