001: /*
002: * Copyright 2006-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.service.impl;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.kuali.core.service.BusinessObjectService;
025: import org.kuali.core.service.KualiRuleService;
026: import org.kuali.core.service.LookupService;
027: import org.kuali.core.util.GeneralLedgerPendingEntrySequenceHelper;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.kfs.bo.AccountingLine;
030: import org.kuali.kfs.context.SpringContext;
031: import org.kuali.module.chart.bo.Account;
032: import org.kuali.module.labor.bo.LaborLedgerPendingEntry;
033: import org.kuali.module.labor.dao.LaborLedgerPendingEntryDao;
034: import org.kuali.module.labor.document.LaborLedgerPostingDocument;
035: import org.kuali.module.labor.rule.event.GenerateLaborLedgerBenefitClearingPendingEntriesEvent;
036: import org.kuali.module.labor.rule.event.GenerateLaborLedgerPendingEntriesEvent;
037: import org.kuali.module.labor.service.LaborLedgerPendingEntryService;
038: import org.springframework.transaction.annotation.Transactional;
039:
040: /**
041: * Service implementation of LaborLedgerPendingEntryService.
042: */
043: @Transactional
044: public class LaborLedgerPendingEntryServiceImpl implements
045: LaborLedgerPendingEntryService {
046: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(LaborLedgerPendingEntryServiceImpl.class);
048:
049: private LaborLedgerPendingEntryDao laborLedgerPendingEntryDao;
050: private KualiRuleService kualiRuleService;
051: private BusinessObjectService businessObjectService;
052:
053: /**
054: * @see org.kuali.module.labor.service.LaborLedgerPendingEntryService#hasPendingLaborLedgerEntry(org.kuali.module.chart.bo.Account)
055: */
056: public boolean hasPendingLaborLedgerEntry(Account account) {
057: Map fieldValues = new HashMap();
058: fieldValues.put("chartOfAccountsCode", account
059: .getChartOfAccountsCode());
060: fieldValues.put("accountNumber", account.getAccountNumber());
061:
062: return businessObjectService.countMatching(
063: LaborLedgerPendingEntry.class, fieldValues) > 0;
064: }
065:
066: /**
067: * @see org.kuali.module.labor.service.LaborLedgerPendingEntryService#hasPendingLaborLedgerEntry(java.util.Map)
068: */
069: public boolean hasPendingLaborLedgerEntry(Map fieldValues) {
070: LOG.info("hasPendingLaborLedgerEntry(Map fieldValues) started");
071:
072: Collection<LaborLedgerPendingEntry> pendingEntries = SpringContext
073: .getBean(LookupService.class).findCollectionBySearch(
074: LaborLedgerPendingEntry.class, fieldValues);
075:
076: // exclude the pending labor ledger transaction has been processed
077: for (LaborLedgerPendingEntry pendingLedgerEntry : pendingEntries) {
078: String approvedCode = pendingLedgerEntry
079: .getFinancialDocumentApprovedCode();
080: if (!KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED
081: .equals(approvedCode)) {
082: return true;
083: }
084: }
085: return false;
086: }
087:
088: /**
089: * Invokes generateEntries method on the salary expense transfer document.
090: *
091: * @param document - document whose pending entries need generated
092: * @return whether the business rules succeeded
093: */
094: public boolean generateLaborLedgerPendingEntries(
095: LaborLedgerPostingDocument document) {
096: LOG.info("generateLaborLedgerPendingEntries() started");
097: boolean success = true;
098:
099: // we must clear them first before creating new ones
100: document.getLaborLedgerPendingEntries().clear();
101:
102: LOG
103: .info("deleting existing labor ledger pending ledger entries for document "
104: + document.getDocumentNumber());
105: delete(document.getDocumentNumber());
106:
107: LOG
108: .info("generating labor ledger pending ledger entries for document "
109: + document.getDocumentNumber());
110: GeneralLedgerPendingEntrySequenceHelper sequenceHelper = new GeneralLedgerPendingEntrySequenceHelper();
111:
112: // process accounting lines, generate labor ledger pending entries
113: List<AccountingLine> sourceAccountingLines = getSourceLines(document);
114: if (sourceAccountingLines != null) {
115: for (AccountingLine line : sourceAccountingLines) {
116: success &= processLaborLedgerPendingEntryForAccountingLine(
117: document, sequenceHelper, line);
118: }
119: }
120:
121: List<AccountingLine> targetAccountingLines = getTargetLines(document);
122: if (targetAccountingLines != null) {
123: for (AccountingLine line : targetAccountingLines) {
124: success &= processLaborLedgerPendingEntryForAccountingLine(
125: document, sequenceHelper, line);
126: }
127: }
128:
129: // compare source and target accounting lines, and generate benefit clearing lines as needed
130: success &= processGenerateLaborLedgerBenefitClearingEntries(
131: document, sequenceHelper);
132:
133: return success;
134: }
135:
136: private List<AccountingLine> getSourceLines(
137: LaborLedgerPostingDocument document) {
138: return (List<AccountingLine>) document
139: .getSourceAccountingLines();
140: }
141:
142: private List<AccountingLine> getTargetLines(
143: LaborLedgerPostingDocument document) {
144: return (List<AccountingLine>) document
145: .getTargetAccountingLines();
146: }
147:
148: /**
149: * This method handles generically taking an accounting line, doing a deep copy on it so that we have a new instance without
150: * reference to the original (won't affect the tran doc's acct lines), performing a retrieveNonKeyFields on the line to make
151: * sure it's populated properly, and then calling the rule framework driven GLPE generation code.
152: *
153: * @param document
154: * @param sequenceHelper
155: * @param iter
156: * @return whether the business rules succeeded
157: */
158: private boolean processLaborLedgerPendingEntryForAccountingLine(
159: LaborLedgerPostingDocument document,
160: GeneralLedgerPendingEntrySequenceHelper sequenceHelper,
161: AccountingLine line) {
162: LOG
163: .debug("processLaborLedgerPendingEntryForAccountingLine() started");
164: boolean success = true;
165:
166: GenerateLaborLedgerPendingEntriesEvent event = new GenerateLaborLedgerPendingEntriesEvent(
167: document, line, sequenceHelper);
168: success &= kualiRuleService.applyRules(event);
169:
170: return success;
171: }
172:
173: private boolean processGenerateLaborLedgerBenefitClearingEntries(
174: LaborLedgerPostingDocument document,
175: GeneralLedgerPendingEntrySequenceHelper sequenceHelper) {
176: LOG
177: .debug("processLaborLedgerPendingEntryForAccountingLine() started");
178: boolean success = true;
179:
180: GenerateLaborLedgerBenefitClearingPendingEntriesEvent event = new GenerateLaborLedgerBenefitClearingPendingEntriesEvent(
181: document, sequenceHelper);
182: success &= kualiRuleService.applyRules(event);
183:
184: return success;
185: }
186:
187: public void delete(String documentHeaderId) {
188: LOG.debug("delete() started");
189:
190: laborLedgerPendingEntryDao.delete(documentHeaderId);
191: }
192:
193: public Collection findPendingEntries(Map fieldValues,
194: boolean isApproved) {
195: LOG.debug("findPendingEntries() started");
196:
197: return laborLedgerPendingEntryDao.findPendingEntries(
198: fieldValues, isApproved);
199: }
200:
201: /**
202: * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntriesForAccountBalance(java.util.Map,
203: * boolean, boolean)
204: */
205: public Iterator findPendingLedgerEntriesForLedgerBalance(
206: Map fieldValues, boolean isApproved) {
207: LOG
208: .debug("findPendingLedgerEntriesForAccountBalance() started");
209: return laborLedgerPendingEntryDao
210: .findPendingLedgerEntriesForLedgerBalance(fieldValues,
211: isApproved);
212: }
213:
214: /**
215: * @see org.kuali.module.labor.service.LaborLedgerPendingEntryService#findApprovedPendingLedgerEntries()
216: */
217: public Iterator<LaborLedgerPendingEntry> findApprovedPendingLedgerEntries() {
218: return laborLedgerPendingEntryDao
219: .findApprovedPendingLedgerEntries();
220: }
221:
222: /**
223: * @see org.kuali.module.labor.service.LaborLedgerPendingEntryService#deleteByFinancialDocumentApprovedCode(java.lang.String)
224: */
225: public void deleteByFinancialDocumentApprovedCode(
226: String financialDocumentApprovedCode) {
227: laborLedgerPendingEntryDao
228: .deleteByFinancialDocumentApprovedCode(financialDocumentApprovedCode);
229: }
230:
231: public void setLaborLedgerPendingEntryDao(
232: LaborLedgerPendingEntryDao laborLedgerPendingEntryDao) {
233: this .laborLedgerPendingEntryDao = laborLedgerPendingEntryDao;
234: }
235:
236: public void setKualiRuleService(KualiRuleService kualiRuleService) {
237: this .kualiRuleService = kualiRuleService;
238: }
239:
240: public void setBusinessObjectService(
241: BusinessObjectService businessObjectService) {
242: this.businessObjectService = businessObjectService;
243: }
244: }
|