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.bo;
017:
018: import java.sql.Timestamp;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.LinkedHashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.kuali.core.bo.GlobalBusinessObject;
029: import org.kuali.core.bo.GlobalBusinessObjectDetail;
030: import org.kuali.core.bo.PersistableBusinessObject;
031: import org.kuali.core.bo.PersistableBusinessObjectBase;
032: import org.kuali.core.exceptions.BusinessObjectNotFoundException;
033: import org.kuali.core.service.BusinessObjectService;
034: import org.kuali.core.service.PersistenceStructureService;
035: import org.kuali.core.util.KualiDecimal;
036: import org.kuali.core.util.TypedArrayList;
037: import org.kuali.kfs.KFSPropertyConstants;
038: import org.kuali.kfs.context.SpringContext;
039:
040: /**
041: * This class simply acts as a container to hold the List of Delegate Changes and the list of Account entries, for the Global
042: * Delegate Change Document.
043: */
044: public class DelegateGlobal extends PersistableBusinessObjectBase
045: implements GlobalBusinessObject {
046:
047: private String documentNumber;
048:
049: private String modelName;
050: private String modelChartOfAccountsCode;
051: private String modelOrganizationCode;
052:
053: private OrganizationRoutingModelName model;
054:
055: List<AccountGlobalDetail> accountGlobalDetails;
056: List<DelegateGlobalDetail> delegateGlobals;
057:
058: /**
059: * Constructs a DelegateGlobal.java.
060: */
061: public DelegateGlobal() {
062: super ();
063: accountGlobalDetails = new TypedArrayList(
064: AccountGlobalDetail.class);
065: delegateGlobals = new TypedArrayList(DelegateGlobalDetail.class);
066: }
067:
068: /**
069: * This method adds a single AccountGlobalDetail instance to the list. If one is already present in the list with the same
070: * chartCode and accountNumber, then this new one will not be added.
071: *
072: * @param accountGlobalDetail - populated AccountGlobalDetail instance
073: */
074: public void addAccount(AccountGlobalDetail accountGlobalDetail) {
075:
076: // validate the argument
077: if (accountGlobalDetail == null) {
078: throw new IllegalArgumentException(
079: "The accountGlobalDetail instanced passed in was null.");
080: } else if (StringUtils.isBlank(accountGlobalDetail
081: .getChartOfAccountsCode())) {
082: throw new IllegalArgumentException(
083: "The chartOfAccountsCode member of the accountGlobalDetail object was not populated.");
084: } else if (StringUtils.isBlank(accountGlobalDetail
085: .getAccountNumber())) {
086: throw new IllegalArgumentException(
087: "The accountNumber member of the accountGlobalDetail object was not populated.");
088: }
089:
090: // add the object if one doesnt already exist, otherwise silently do nothing
091: AccountGlobalDetail testObject = getAccount(accountGlobalDetail
092: .getChartOfAccountsCode(), accountGlobalDetail
093: .getAccountNumber());
094: if (testObject == null) {
095: this .accountGlobalDetails.add(accountGlobalDetail);
096: }
097: }
098:
099: /**
100: * This method retrieves the specific AccountGlobalDetail object that corresponds to your requested chartCode and accountNumber
101: * (or a null object if there is no match).
102: *
103: * @param chartCode
104: * @param accountNumber
105: * @return returns the AccountGlobalDetail instance matching the chartCode & accountNumber passed in, or Null if none match
106: */
107: public AccountGlobalDetail getAccount(String chartCode,
108: String accountNumber) {
109:
110: // validate the argument
111: if (StringUtils.isBlank(chartCode)) {
112: throw new IllegalArgumentException(
113: "The chartCode argument was null or empty.");
114: } else if (StringUtils.isBlank(accountNumber)) {
115: throw new IllegalArgumentException(
116: "The accountNumber argument was null or empty.");
117: }
118:
119: // walk the list of AccountGlobalDetail objects
120: for (Iterator iter = this .accountGlobalDetails.iterator(); iter
121: .hasNext();) {
122: AccountGlobalDetail accountGlobalDetail = (AccountGlobalDetail) iter
123: .next();
124:
125: // if this one is a match, then quit
126: if (chartCode.equalsIgnoreCase(accountGlobalDetail
127: .getChartOfAccountsCode())
128: && accountNumber
129: .equalsIgnoreCase(accountGlobalDetail
130: .getAccountNumber())) {
131: return accountGlobalDetail;
132: }
133: }
134:
135: // we return null if one is not found
136: return null;
137: }
138:
139: /**
140: * @see org.kuali.core.document.GlobalBusinessObject#getGlobalChangesToDelete()
141: */
142: public List<PersistableBusinessObject> generateDeactivationsToPersist() {
143: BusinessObjectService boService = SpringContext
144: .getBean(BusinessObjectService.class);
145:
146: // retreive all the existing delegates for these accounts
147: List<Delegate> bosToDeactivate = new ArrayList();
148: Map<String, Object> fieldValues;
149: Collection existingDelegates;
150: for (AccountGlobalDetail accountDetail : getAccountGlobalDetails()) {
151: fieldValues = new HashMap();
152: fieldValues.put("chartOfAccountsCode", accountDetail
153: .getChartOfAccountsCode());
154: fieldValues.put("accountNumber", accountDetail
155: .getAccountNumber());
156: fieldValues.put("accountDelegateActiveIndicator", true);
157: existingDelegates = boService.findMatching(Delegate.class,
158: fieldValues);
159: bosToDeactivate.addAll(existingDelegates);
160: }
161:
162: // mark all the delegates as inactive
163: for (Delegate accountDelegate : bosToDeactivate) {
164: accountDelegate.setAccountDelegateActiveIndicator(false);
165: }
166: return new ArrayList<PersistableBusinessObject>(bosToDeactivate);
167: }
168:
169: /**
170: * @see org.kuali.core.document.GlobalBusinessObject#applyGlobalChanges(org.kuali.core.bo.BusinessObject)
171: */
172: @SuppressWarnings("deprecation")
173: public List<PersistableBusinessObject> generateGlobalChangesToPersist() {
174:
175: BusinessObjectService boService = SpringContext
176: .getBean(BusinessObjectService.class);
177: List<Delegate> persistables = new ArrayList();
178:
179: List<DelegateGlobalDetail> changeDocuments = this
180: .getDelegateGlobals();
181: List<AccountGlobalDetail> accountDetails = this
182: .getAccountGlobalDetails();
183:
184: for (DelegateGlobalDetail changeDocument : changeDocuments) {
185: for (AccountGlobalDetail accountDetail : accountDetails) {
186:
187: Account account = (Account) boService.findByPrimaryKey(
188: Account.class, accountDetail.getPrimaryKeys());
189:
190: // if the account doesnt exist, fail fast, as this should never happen,
191: // the busines rules for this document should have caught this.
192: if (account == null) {
193: throw new BusinessObjectNotFoundException(
194: "Account ["
195: + accountDetail
196: .getChartOfAccountsCode()
197: + "-"
198: + accountDetail.getAccountNumber()
199: + "] was not present in the database. "
200: + "This should never happen under normal circumstances, as an invalid account should have "
201: + "been caught by the Business Rules infrastructure.");
202: }
203:
204: // attempt to load the existing Delegate from the DB, if it exists. we do this to avoid
205: // versionNumber conflicts if we tried to just insert a new record that already existed.
206: Map pkMap = new HashMap();
207: pkMap.putAll(accountDetail.getPrimaryKeys()); // chartOfAccountsCode & accountNumber
208: pkMap.put("financialDocumentTypeCode", changeDocument
209: .getFinancialDocumentTypeCode());
210: pkMap.put("accountDelegateSystemId", changeDocument
211: .getAccountDelegateUniversalId());
212: Delegate delegate = (Delegate) boService
213: .findByPrimaryKey(Delegate.class, pkMap);
214:
215: // if there is no existing Delegate with these primary keys, then we're creating a new one,
216: // so lets populate it with the primary keys
217: if (delegate == null) {
218: delegate = new Delegate();
219: delegate.setChartOfAccountsCode(accountDetail
220: .getChartOfAccountsCode());
221: delegate.setAccountNumber(accountDetail
222: .getAccountNumber());
223: delegate.setAccountDelegateSystemId(changeDocument
224: .getAccountDelegateUniversalId());
225: delegate
226: .setFinancialDocumentTypeCode(changeDocument
227: .getFinancialDocumentTypeCode());
228: delegate.setAccountDelegateActiveIndicator(true);
229: } else {
230: delegate.setAccountDelegateActiveIndicator(true);
231: }
232:
233: // APPROVAL FROM AMOUNT
234: if (changeDocument.getApprovalFromThisAmount() != null) {
235: if (!changeDocument.getApprovalFromThisAmount()
236: .equals(new KualiDecimal(0))) {
237: delegate
238: .setFinDocApprovalFromThisAmt(changeDocument
239: .getApprovalFromThisAmount());
240: }
241: }
242:
243: // APPROVAL TO AMOUNT
244: if (changeDocument.getApprovalToThisAmount() != null) {
245: if (!changeDocument.getApprovalToThisAmount()
246: .equals(new KualiDecimal(0))) {
247: delegate
248: .setFinDocApprovalToThisAmount(changeDocument
249: .getApprovalToThisAmount());
250: }
251: }
252:
253: // PRIMARY ROUTING
254: delegate
255: .setAccountsDelegatePrmrtIndicator(changeDocument
256: .getAccountDelegatePrimaryRoutingIndicator());
257:
258: // START DATE
259: if (changeDocument.getAccountDelegateStartDate() != null) {
260: delegate.setAccountDelegateStartDate(new Timestamp(
261: changeDocument
262: .getAccountDelegateStartDate()
263: .getTime()));
264: }
265:
266: persistables.add(delegate);
267:
268: }
269: }
270:
271: return new ArrayList<PersistableBusinessObject>(persistables);
272: }
273:
274: /**
275: * @see org.kuali.core.bo.BusinessObjectBase#toStringMapper()
276: */
277: @Override
278: protected LinkedHashMap toStringMapper() {
279:
280: LinkedHashMap m = new LinkedHashMap();
281:
282: m
283: .put(KFSPropertyConstants.DOCUMENT_NUMBER,
284: this .documentNumber);
285: return m;
286: }
287:
288: /**
289: * @see org.kuali.core.document.GlobalBusinessObject#getDocumentNumber()
290: */
291: public String getDocumentNumber() {
292: return documentNumber;
293: }
294:
295: /**
296: * @see org.kuali.core.document.GlobalBusinessObject#setDocumentNumber(java.lang.String)
297: */
298: public void setDocumentNumber(String documentNumber) {
299: this .documentNumber = documentNumber;
300:
301: }
302:
303: /**
304: * Gets the accountGlobalDetails attribute.
305: *
306: * @return Returns the accountGlobalDetails.
307: */
308: public final List<AccountGlobalDetail> getAccountGlobalDetails() {
309: return accountGlobalDetails;
310: }
311:
312: /**
313: * Sets the accountGlobalDetails attribute value.
314: *
315: * @param accountGlobalDetails The accountGlobalDetails to set.
316: */
317: public final void setAccountGlobalDetails(
318: List<AccountGlobalDetail> accountGlobalDetails) {
319: this .accountGlobalDetails = accountGlobalDetails;
320: }
321:
322: /**
323: * Gets the delegateGlobals attribute.
324: *
325: * @return Returns the delegateGlobals.
326: */
327: public final List<DelegateGlobalDetail> getDelegateGlobals() {
328: return delegateGlobals;
329: }
330:
331: /**
332: * Sets the delegateGlobals attribute value.
333: *
334: * @param delegateGlobals The delegateGlobals to set.
335: */
336: public final void setDelegateGlobals(
337: List<DelegateGlobalDetail> delegateGlobals) {
338: this .delegateGlobals = delegateGlobals;
339: }
340:
341: /**
342: * @see org.kuali.core.document.GlobalBusinessObject#isPersistable()
343: */
344: public boolean isPersistable() {
345: PersistenceStructureService persistenceStructureService = SpringContext
346: .getBean(PersistenceStructureService.class);
347:
348: // fail if the PK for this object is emtpy
349: if (StringUtils.isBlank(documentNumber)) {
350: return false;
351: }
352:
353: // fail if the PKs for any of the contained objects are empty
354: for (DelegateGlobalDetail delegateGlobals : getDelegateGlobals()) {
355: if (!persistenceStructureService
356: .hasPrimaryKeyFieldValues(delegateGlobals)) {
357: return false;
358: }
359: }
360: for (AccountGlobalDetail account : getAccountGlobalDetails()) {
361: if (!persistenceStructureService
362: .hasPrimaryKeyFieldValues(account)) {
363: return false;
364: }
365: }
366:
367: // otherwise, its all good
368: return true;
369: }
370:
371: public String getModelName() {
372: return modelName;
373: }
374:
375: public void setModelName(String loadModelName) {
376: this .modelName = loadModelName;
377: }
378:
379: public String getModelChartOfAccountsCode() {
380: return modelChartOfAccountsCode;
381: }
382:
383: public void setModelChartOfAccountsCode(
384: String loadModelChartOfAccountsCode) {
385: this .modelChartOfAccountsCode = loadModelChartOfAccountsCode;
386: }
387:
388: public String getModelOrganizationCode() {
389: return modelOrganizationCode;
390: }
391:
392: public void setModelOrganizationCode(
393: String loadModelOrganizationCode) {
394: this .modelOrganizationCode = loadModelOrganizationCode;
395: }
396:
397: public OrganizationRoutingModelName getModel() {
398: return model;
399: }
400:
401: public void setModel(OrganizationRoutingModelName loadModel) {
402: this .model = loadModel;
403: }
404:
405: public List<? extends GlobalBusinessObjectDetail> getAllDetailObjects() {
406: ArrayList<GlobalBusinessObjectDetail> details = new ArrayList<GlobalBusinessObjectDetail>(
407: accountGlobalDetails.size() + delegateGlobals.size());
408: details.addAll(accountGlobalDetails);
409: details.addAll(delegateGlobals);
410: return details;
411: }
412:
413: @Override
414: public void linkEditableUserFields() {
415: super .linkEditableUserFields();
416: if (this == null) {
417: throw new IllegalArgumentException(
418: "globalDelegate parameter passed in was null");
419: }
420: List bos = new ArrayList();
421: bos.addAll(getDelegateGlobals());
422: SpringContext.getBean(BusinessObjectService.class)
423: .linkUserFields(bos);
424: }
425:
426: /**
427: * @see org.kuali.core.bo.PersistableBusinessObjectBase#buildListOfDeletionAwareLists()
428: */
429: @Override
430: public List buildListOfDeletionAwareLists() {
431: List<List> managedLists = super.buildListOfDeletionAwareLists();
432:
433: managedLists.add(getAccountGlobalDetails());
434: managedLists.add(getDelegateGlobals());
435:
436: return managedLists;
437: }
438: }
|