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.labor.document;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.kuali.core.exceptions.ValidationException;
022: import org.kuali.core.rule.event.KualiDocumentEvent;
023: import org.kuali.kfs.KFSConstants;
024: import org.kuali.kfs.context.SpringContext;
025: import org.kuali.kfs.document.AccountingDocumentBase;
026: import org.kuali.module.labor.bo.LaborLedgerPendingEntry;
027: import org.kuali.module.labor.service.LaborLedgerPendingEntryService;
028:
029: /**
030: * Labor Document base class implementation for all labor eDocs that are transactional, meaning implementing
031: * TransactionalDocumentBase. Additional functionality for labor is provided by this class, suchc as retrieving labor ledger pending
032: * entries.
033: */
034: public abstract class LaborLedgerPostingDocumentBase extends
035: AccountingDocumentBase implements LaborLedgerPostingDocument {
036: protected List<LaborLedgerPendingEntry> laborLedgerPendingEntries;
037:
038: /**
039: * Initializes the pending entries.
040: */
041: public LaborLedgerPostingDocumentBase() {
042: super ();
043: setLaborLedgerPendingEntries(new ArrayList<LaborLedgerPendingEntry>());
044: }
045:
046: /**
047: * @see org.kuali.module.labor.document.LaborLedgerPostingDocument#getLaborLedgerPendingEntries()
048: */
049: public List<LaborLedgerPendingEntry> getLaborLedgerPendingEntries() {
050: return this .laborLedgerPendingEntries;
051: }
052:
053: /**
054: * @see org.kuali.module.labor.document.LaborLedgerPostingDocument#setLaborLedgerPendingEntries(java.util.List)
055: */
056: public void setLaborLedgerPendingEntries(
057: List<LaborLedgerPendingEntry> laborLedgerPendingEntries) {
058: this .laborLedgerPendingEntries = laborLedgerPendingEntries;
059: }
060:
061: /**
062: * Override to call super and then iterate over all GLPEs and update the approved code appropriately.
063: *
064: * @see Document#handleRouteStatusChange()
065: */
066: @Override
067: public void handleRouteStatusChange() {
068: super .handleRouteStatusChange();
069: if (getDocumentHeader().getWorkflowDocument()
070: .stateIsProcessed()) {
071: changeLedgerPendingEntriesApprovedStatusCode();
072: } else if (getDocumentHeader().getWorkflowDocument()
073: .stateIsCanceled()
074: || getDocumentHeader().getWorkflowDocument()
075: .stateIsDisapproved()) {
076: removeLedgerPendingEntries();
077: }
078: }
079:
080: /**
081: * This method iterates over all of the pending entries for a document and sets their approved status code to APPROVED "A".
082: */
083: private void changeLedgerPendingEntriesApprovedStatusCode() {
084: for (LaborLedgerPendingEntry pendingEntry : laborLedgerPendingEntries) {
085: pendingEntry
086: .setFinancialDocumentApprovedCode(KFSConstants.DocumentStatusCodes.APPROVED);
087: }
088: }
089:
090: /**
091: * This method calls the service to remove all of the pending entries associated with this document
092: */
093: private void removeLedgerPendingEntries() {
094: LaborLedgerPendingEntryService laborLedgerPendingEntryService = SpringContext
095: .getBean(LaborLedgerPendingEntryService.class);
096: laborLedgerPendingEntryService.delete(getDocumentHeader()
097: .getDocumentNumber());
098: }
099:
100: /**
101: * This implementation is coupled tightly with some underlying issues that the Struts PojoProcessor plugin has with how objects
102: * get instantiated within lists. The first three lines are required otherwise when the PojoProcessor tries to automatically
103: * inject values into the list, it will get an index out of bounds error if the instance at an index is being called and prior
104: * instances at indices before that one are not being instantiated. So changing the code below will cause things to break.
105: *
106: * @param index of Labor Ledger Pending Entry to retrieve
107: * @return LaborLedgerPendingEntry
108: */
109: public LaborLedgerPendingEntry getLaborLedgerPendingEntry(int index) {
110: while (laborLedgerPendingEntries.size() <= index) {
111: laborLedgerPendingEntries
112: .add(new LaborLedgerPendingEntry());
113: }
114: return laborLedgerPendingEntries.get(index);
115: }
116:
117: /**
118: * @see org.kuali.kfs.document.AccountingDocumentBase#prepareForSave(org.kuali.core.rule.event.KualiDocumentEvent)
119: */
120: @Override
121: public void prepareForSave(KualiDocumentEvent event) {
122: if (!SpringContext
123: .getBean(LaborLedgerPendingEntryService.class)
124: .generateLaborLedgerPendingEntries(this )) {
125: logErrors();
126: throw new ValidationException(
127: "labor ledger LLPE generation failed");
128: }
129: super.prepareForSave(event);
130: }
131: }
|