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.HashMap;
020: import java.util.HashSet;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.kuali.core.document.MaintenanceLock;
027: import org.kuali.core.maintenance.KualiGlobalMaintainableImpl;
028: import org.kuali.core.service.BusinessObjectService;
029: import org.kuali.kfs.KFSConstants;
030: import org.kuali.kfs.context.SpringContext;
031: import org.kuali.module.chart.bo.AccountGlobalDetail;
032: import org.kuali.module.chart.bo.Delegate;
033: import org.kuali.module.chart.bo.DelegateGlobal;
034: import org.kuali.module.chart.bo.DelegateGlobalDetail;
035: import org.kuali.module.chart.bo.OrganizationRoutingModel;
036: import org.kuali.module.chart.bo.OrganizationRoutingModelName;
037:
038: /**
039: * This class overrides the base {@link KualiGlobalMaintainableImpl} to generate the specific maintenance locks for Global delegates
040: * and to help with using delegate models
041: *
042: * @see OrganizationRoutingModelName
043: */
044: public class DelegateGlobalMaintainableImpl extends
045: KualiGlobalMaintainableImpl {
046:
047: /**
048: * This method is used for the creation of a delegate from a {@link OrganizationRoutingModelName}
049: *
050: * @see org.kuali.core.maintenance.KualiMaintainableImpl#setupNewFromExisting()
051: */
052: @Override
053: public void setupNewFromExisting() {
054: super .setupNewFromExisting();
055:
056: DelegateGlobal globalDelegate = (DelegateGlobal) this
057: .getBusinessObject();
058: // 1. if model name, chart of accounts, and org code are all present
059: // then let's see if we've actually got a model record
060: if (!StringUtils.isBlank(globalDelegate.getModelName())
061: && !StringUtils.isBlank(globalDelegate
062: .getModelChartOfAccountsCode())
063: && !StringUtils.isBlank(globalDelegate
064: .getModelOrganizationCode())) {
065: Map pkMap = new HashMap();
066: pkMap.put("organizationRoutingModelName", globalDelegate
067: .getModelName());
068: pkMap.put("chartOfAccountsCode", globalDelegate
069: .getModelChartOfAccountsCode());
070: pkMap.put("organizationCode", globalDelegate
071: .getModelOrganizationCode());
072:
073: OrganizationRoutingModelName globalDelegateTemplate = (OrganizationRoutingModelName) SpringContext
074: .getBean(BusinessObjectService.class)
075: .findByPrimaryKey(
076: OrganizationRoutingModelName.class, pkMap);
077: if (globalDelegateTemplate != null) {
078: // 2. if there is a model record, then let's populate the global delegate
079: // based on that
080: for (OrganizationRoutingModel model : globalDelegateTemplate
081: .getOrganizationRoutingModel()) {
082: if (model.isActive()) { // only populate with active models
083: DelegateGlobalDetail newDelegate = new DelegateGlobalDetail(
084: model);
085: // allow deletion of the new delegate from the global delegate
086: newDelegate.setNewCollectionRecord(true);
087: globalDelegate.getDelegateGlobals().add(
088: newDelegate);
089: }
090: }
091: }
092: }
093: }
094:
095: /**
096: * This creates the particular locking representation for this global document.
097: *
098: * @see org.kuali.core.maintenance.Maintainable#generateMaintenanceLocks()
099: */
100: @Override
101: public List<MaintenanceLock> generateMaintenanceLocks() {
102: // create locking rep for each combination of account and object code
103: List<MaintenanceLock> maintenanceLocks = new ArrayList();
104: DelegateGlobal delegateGlobal = (DelegateGlobal) getBusinessObject();
105:
106: // hold all the locking representations in a set to make sure we don't get any duplicates
107: Set<String> lockingRepresentations = new HashSet<String>();
108:
109: MaintenanceLock maintenanceLock;
110: for (AccountGlobalDetail accountGlobalDetail : delegateGlobal
111: .getAccountGlobalDetails()) {
112: for (DelegateGlobalDetail delegateGlobalDetail : delegateGlobal
113: .getDelegateGlobals()) {
114: StringBuilder lockRep = new StringBuilder();
115: lockRep.append(Delegate.class.getName());
116: lockRep
117: .append(KFSConstants.Maintenance.AFTER_CLASS_DELIM);
118: lockRep.append("chartOfAccountsCode");
119: lockRep
120: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
121: lockRep.append(accountGlobalDetail
122: .getChartOfAccountsCode());
123: lockRep
124: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
125: lockRep.append("accountNumber");
126: lockRep
127: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
128: lockRep.append(accountGlobalDetail.getAccountNumber());
129: lockRep
130: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
131: lockRep.append("financialDocumentTypeCode");
132: lockRep
133: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
134: lockRep.append(delegateGlobalDetail
135: .getFinancialDocumentTypeCode());
136: lockRep
137: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
138: lockRep.append("accountDelegateSystemId");
139: lockRep
140: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
141: lockRep.append(delegateGlobalDetail
142: .getAccountDelegateUniversalId());
143: // FIXME above is a bit dangerous b/c it hard codes the attribute names, which could change (particularly
144: // accountDelegateSystemId) - guess they should either be constants or obtained by looping through Delegate keys;
145: // however, I copied this from elsewhere which had them hard-coded, so I'm leaving it for now
146:
147: if (!lockingRepresentations
148: .contains(lockRep.toString())) {
149: maintenanceLock = new MaintenanceLock();
150: maintenanceLock.setDocumentNumber(delegateGlobal
151: .getDocumentNumber());
152: maintenanceLock.setLockingRepresentation(lockRep
153: .toString());
154: maintenanceLocks.add(maintenanceLock);
155: lockingRepresentations.add(lockRep.toString());
156: }
157:
158: lockRep = new StringBuilder();
159: lockRep.append(Delegate.class.getName());
160: lockRep
161: .append(KFSConstants.Maintenance.AFTER_CLASS_DELIM);
162: lockRep.append("chartOfAccountsCode");
163: lockRep
164: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
165: lockRep.append(accountGlobalDetail
166: .getChartOfAccountsCode());
167: lockRep
168: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
169: lockRep.append("accountNumber");
170: lockRep
171: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
172: lockRep.append(accountGlobalDetail.getAccountNumber());
173: lockRep
174: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
175: lockRep.append("financialDocumentTypeCode");
176: lockRep
177: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
178: lockRep.append(delegateGlobalDetail
179: .getFinancialDocumentTypeCode());
180: lockRep
181: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
182: lockRep.append("accountsDelegatePrmrtIndicator");
183: lockRep
184: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
185: lockRep.append("true");
186:
187: if (!lockingRepresentations
188: .contains(lockRep.toString())) {
189: maintenanceLock = new MaintenanceLock();
190: maintenanceLock.setDocumentNumber(delegateGlobal
191: .getDocumentNumber());
192: maintenanceLock.setLockingRepresentation(lockRep
193: .toString());
194: maintenanceLocks.add(maintenanceLock);
195: lockingRepresentations.add(lockRep.toString());
196: }
197: }
198: }
199: return maintenanceLocks;
200: }
201: }
|