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.service.impl;
017:
018: import java.io.BufferedOutputStream;
019: import java.io.BufferedReader;
020: import java.io.BufferedWriter;
021: import java.io.FileReader;
022: import java.io.FileWriter;
023: import java.io.IOException;
024: import java.sql.Date;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.kuali.core.service.DateTimeService;
033: import org.kuali.kfs.KFSPropertyConstants;
034: import org.kuali.module.gl.bo.OriginEntryGroup;
035: import org.kuali.module.gl.dao.OriginEntryDao;
036: import org.kuali.module.gl.service.OriginEntryGroupService;
037: import org.kuali.module.gl.service.impl.OriginEntryServiceImpl;
038: import org.kuali.module.gl.util.LedgerEntry;
039: import org.kuali.module.gl.util.LedgerEntryHolder;
040: import org.kuali.module.gl.util.OriginEntryStatistics;
041: import org.kuali.module.gl.util.PosterOutputSummaryEntry;
042: import org.kuali.module.labor.LaborConstants;
043: import org.kuali.module.labor.bo.LaborOriginEntry;
044: import org.kuali.module.labor.bo.LaborTransaction;
045: import org.kuali.module.labor.dao.LaborOriginEntryDao;
046: import org.kuali.module.labor.service.LaborOriginEntryService;
047: import org.kuali.module.labor.util.LaborLedgerUnitOfWork;
048: import org.kuali.module.labor.util.ObjectUtil;
049: import org.springframework.transaction.annotation.Transactional;
050:
051: /**
052: * Service implementation of LaborOriginEntryService.
053: */
054: @Transactional
055: public class LaborOriginEntryServiceImpl implements
056: LaborOriginEntryService {
057: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
058: .getLogger(OriginEntryServiceImpl.class);
059:
060: private LaborOriginEntryDao laborOriginEntryDao;
061: private OriginEntryDao originEntryDao;
062: private OriginEntryGroupService originEntryGroupService;
063: private DateTimeService dateTimeService;
064:
065: public OriginEntryStatistics getStatistics(Integer groupId) {
066: LOG.debug("getStatistics() started");
067:
068: OriginEntryStatistics oes = new OriginEntryStatistics();
069:
070: oes.setCreditTotalAmount(laborOriginEntryDao.getGroupTotal(
071: groupId, true));
072: oes.setDebitTotalAmount(laborOriginEntryDao.getGroupTotal(
073: groupId, false));
074: oes.setRowCount(laborOriginEntryDao.getGroupCount(groupId));
075:
076: return oes;
077: }
078:
079: /**
080: * @see org.kuali.module.labor.service.LaborOriginEntryService#copyEntries(java.util.Date, java.lang.String, boolean, boolean,
081: * boolean, java.util.Collection)
082: */
083: public OriginEntryGroup copyEntries(Date date, String sourceCode,
084: boolean valid, boolean process, boolean scrub,
085: Collection<LaborOriginEntry> entries) {
086: LOG.debug("copyEntries() started");
087:
088: OriginEntryGroup newOriginEntryGroup = originEntryGroupService
089: .createGroup(date, sourceCode, valid, process, scrub);
090:
091: // Create new Entries with newOriginEntryGroup
092: for (LaborOriginEntry oe : entries) {
093: oe.setEntryGroupId(newOriginEntryGroup.getId());
094: createEntry(oe, newOriginEntryGroup);
095: }
096:
097: return newOriginEntryGroup;
098: }
099:
100: /**
101: * @see org.kuali.module.labor.service.LaborOriginEntryService#copyEntries(java.sql.Date, java.lang.String, boolean, boolean,
102: * boolean, java.util.Iterator)
103: */
104: public OriginEntryGroup copyEntries(Date date, String sourceCode,
105: boolean valid, boolean process, boolean scrub,
106: Iterator<LaborOriginEntry> entries) {
107: LOG.debug("copyEntries() started");
108:
109: OriginEntryGroup newOriginEntryGroup = originEntryGroupService
110: .createGroup(date, sourceCode, valid, process, scrub);
111:
112: // Create new Entries with newOriginEntryGroup
113: while (entries.hasNext()) {
114: LaborOriginEntry oe = entries.next();
115: oe.setEntryGroupId(newOriginEntryGroup.getId());
116: createEntry(oe, newOriginEntryGroup);
117: }
118:
119: return newOriginEntryGroup;
120: }
121:
122: /**
123: * @see org.kuali.module.labor.service.LaborOriginEntryService#delete(org.kuali.module.labor.bo.LaborOriginEntry)
124: */
125: public void delete(LaborOriginEntry loe) {
126: LOG.debug("deleteEntry() started");
127:
128: originEntryDao.deleteEntry(loe);
129: }
130:
131: /**
132: * @see org.kuali.module.labor.service.LaborOriginEntryService#getDocumentsByGroup(org.kuali.module.gl.bo.originentrygroup)
133: */
134: public Collection<LaborOriginEntry> getDocumentsByGroup(
135: OriginEntryGroup oeg) {
136: LOG.debug("getDocumentsByGroup() started");
137:
138: Collection<LaborOriginEntry> results = new ArrayList<LaborOriginEntry>();
139: Iterator i = originEntryDao.getDocumentsByGroup(oeg);
140: while (i.hasNext()) {
141: Object[] data = (Object[]) i.next();
142: LaborOriginEntry oe = new LaborOriginEntry();
143: oe.setDocumentNumber((String) data[0]);
144: oe.setFinancialDocumentTypeCode((String) data[1]);
145: oe.setFinancialSystemOriginationCode((String) data[2]);
146: results.add(oe);
147: }
148:
149: return results;
150: }
151:
152: /**
153: * @see org.kuali.module.labor.service.LaborOriginEntryService#getBadBalanceEntries(org.kuali.module.gl.bo.originentrygroup)
154: */
155: public Iterator<LaborOriginEntry> getBadBalanceEntries(
156: Collection groups) {
157: LOG.debug("getBadBalanceEntries() started");
158: Iterator returnVal = laborOriginEntryDao
159: .getBadBalanceEntries(groups);
160:
161: return returnVal;
162: }
163:
164: /**
165: * @see org.kuali.module.labor.service.LaborOriginEntryService#getEntriesByGroupAccountOrder(org.kuali.module.gl.bo.originentrygroup)
166: */
167: public Iterator<LaborOriginEntry> getEntriesByGroupAccountOrder(
168: OriginEntryGroup oeg) {
169: LOG.debug("getEntriesByGroupAccountOrder() started");
170: Iterator returnVal = laborOriginEntryDao.getEntriesByGroup(oeg,
171: OriginEntryDao.SORT_ACCOUNT);
172:
173: return returnVal;
174: }
175:
176: /**
177: * @see org.kuali.module.labor.service.LaborOriginEntryService#getEntriesByGroupReportOrder(org.kuali.module.gl.bo.originentrygroup)
178: */
179: public Iterator<LaborOriginEntry> getEntriesByGroupReportOrder(
180: OriginEntryGroup oeg) {
181: LOG.debug("getEntriesByGroupAccountOrder() started");
182: Iterator returnVal = laborOriginEntryDao.getEntriesByGroup(oeg,
183: OriginEntryDao.SORT_REPORT);
184:
185: return returnVal;
186: }
187:
188: /**
189: * @see org.kuali.module.labor.service.LaborOriginEntryService#getEntriesByGroupListingReportOrder(org.kuali.module.gl.bo.originentrygroup)
190: */
191: public Iterator<LaborOriginEntry> getEntriesByGroupListingReportOrder(
192: OriginEntryGroup oeg) {
193: LOG.debug("getEntriesByGroupAccountOrder() started");
194:
195: Iterator returnVal = laborOriginEntryDao.getEntriesByGroup(oeg,
196: OriginEntryDao.SORT_LISTING_REPORT);
197: return returnVal;
198: }
199:
200: /**
201: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getEntriesByDocument(org.kuali.module.labor.bo.LaborOriginEntryGroup,
202: * java.lang.String, java.lang.String, java.lang.String)
203: */
204: public Collection<LaborOriginEntry> getEntriesByDocument(
205: OriginEntryGroup originEntryGroup, String documentNumber,
206: String documentTypeCode, String originCode) {
207: LOG.debug("getEntriesByGroup() started");
208:
209: Map criteria = new HashMap();
210: criteria.put(KFSPropertyConstants.ENTRY_GROUP_ID,
211: originEntryGroup.getId());
212: criteria.put(KFSPropertyConstants.DOCUMENT_NUMBER,
213: documentNumber);
214: criteria.put(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE,
215: documentTypeCode);
216: criteria.put(
217: KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE,
218: originCode);
219:
220: return laborOriginEntryDao
221: .getMatchingEntriesByCollection(criteria);
222: }
223:
224: /**
225: * @see org.kuali.module.labor.service.LaborOriginEntryService#createEntry(org.kuali.module.labor.bo.Transaction,
226: * org.kuali.module.gl.bo.originentrygroup)
227: */
228: public void createEntry(LaborTransaction laborTransaction,
229: OriginEntryGroup originEntryGroup) {
230: LOG.debug("createEntry() started");
231:
232: LaborOriginEntry e = new LaborOriginEntry(laborTransaction);
233: e.setGroup(originEntryGroup);
234:
235: laborOriginEntryDao.saveOriginEntry(e);
236: }
237:
238: /**
239: * @see org.kuali.module.labor.service.LaborOriginEntryService#save(org.kuali.module.labor.bo.LaborOriginEntry)
240: */
241: public void save(LaborOriginEntry entry) {
242: LOG.debug("save() started");
243:
244: laborOriginEntryDao.saveOriginEntry(entry);
245: }
246:
247: /**
248: * @see org.kuali.module.labor.service.LaborOriginEntryService#exportFlatFile(java.lang.String, java.lang.Integer)
249: */
250: public void exportFlatFile(String filename, Integer groupId) {
251: LOG.debug("exportFlatFile() started");
252:
253: BufferedWriter out = null;
254: try {
255: out = new BufferedWriter(new FileWriter(filename));
256:
257: OriginEntryGroup oeg = new OriginEntryGroup();
258: oeg.setId(groupId);
259: Iterator i = getEntriesByGroup(oeg);
260: while (i.hasNext()) {
261: LaborOriginEntry e = (LaborOriginEntry) i.next();
262: out.write(e.getLine() + "\n");
263: }
264: } catch (IOException e) {
265: LOG.error("exportFlatFile() Error writing to file", e);
266: } finally {
267: if (out != null) {
268: try {
269: out.close();
270: } catch (IOException ie) {
271: LOG
272: .error(
273: "exportFlatFile() Error closing file",
274: ie);
275: }
276: }
277: }
278: }
279:
280: /**
281: * @see org.kuali.module.labor.service.LaborOriginEntryService#loadFlatFile(java.lang.String, java.lang.String, boolean,
282: * boolean, boolean)
283: */
284: public void loadFlatFile(String filename, String groupSourceCode,
285: boolean isValid, boolean isProcessed, boolean isScrub) {
286: LOG.debug("loadFlatFile() started");
287:
288: java.sql.Date groupDate = new java.sql.Date(dateTimeService
289: .getCurrentDate().getTime());
290: OriginEntryGroup newGroup = originEntryGroupService
291: .createGroup(groupDate, groupSourceCode, isValid,
292: isProcessed, isScrub);
293:
294: BufferedReader input = null;
295: try {
296: input = new BufferedReader(new FileReader(filename));
297: String line = null;
298: while ((line = input.readLine()) != null) {
299: LaborOriginEntry entry = new LaborOriginEntry(line);
300: createEntry(entry, newGroup);
301: }
302: } catch (Exception ex) {
303: LOG.error("performStep() Error reading file", ex);
304: throw new IllegalArgumentException("Error reading file");
305: } finally {
306: try {
307: if (input != null) {
308: input.close();
309: }
310: } catch (IOException ex) {
311: LOG.error("loadFlatFile() error closing file.", ex);
312: }
313: }
314: }
315:
316: /**
317: * @see org.kuali.module.labor.service.LaborOriginEntryService#getMatchingEntriesByList(java.util.Map)
318: */
319: public List<LaborOriginEntry> getEntriesByGroupId(Integer groupId) {
320: if (groupId == null) {
321: throw new IllegalArgumentException("Group ID is null");
322: }
323: Map<String, Object> searchCriteria = new HashMap<String, Object>();
324: searchCriteria.put("entryGroupId", groupId);
325: Collection<LaborOriginEntry> searchResultAsCollection = getMatchingEntriesByCollection(searchCriteria);
326: if (searchResultAsCollection instanceof List) {
327: return (List<LaborOriginEntry>) searchResultAsCollection;
328: } else {
329: return new ArrayList<LaborOriginEntry>(
330: searchResultAsCollection);
331: }
332: }
333:
334: public LedgerEntryHolder getSummaryByGroupId(Collection groupIdList) {
335: LOG.debug("getSummaryByGroupId() started");
336:
337: LedgerEntryHolder ledgerEntryHolder = new LedgerEntryHolder();
338:
339: if (groupIdList.size() == 0) {
340: return ledgerEntryHolder;
341: }
342:
343: Iterator entrySummaryIterator = laborOriginEntryDao
344: .getSummaryByGroupId(groupIdList);
345: while (entrySummaryIterator.hasNext()) {
346: Object[] entrySummary = (Object[]) entrySummaryIterator
347: .next();
348: LedgerEntry ledgerEntry = LedgerEntry
349: .buildLedgerEntry(entrySummary);
350: ledgerEntryHolder.insertLedgerEntry(ledgerEntry, true);
351: }
352: return ledgerEntryHolder;
353: }
354:
355: /**
356: * @see org.kuali.module.labor.service.LaborOriginEntryService#flatFile(java.lang.Integer, java.io.BufferedOutputStream)
357: */
358: public void flatFile(Integer groupId, BufferedOutputStream bw) {
359: LOG.debug("flatFile() started");
360:
361: try {
362: OriginEntryGroup oeg = new OriginEntryGroup();
363: oeg.setId(groupId);
364: Iterator i = getEntriesByGroup(oeg);
365: while (i.hasNext()) {
366: LaborOriginEntry e = (LaborOriginEntry) i.next();
367: bw.write((e.getLine() + "\n").getBytes());
368: }
369: } catch (IOException e) {
370: LOG.error("flatFile() Error writing to file", e);
371: throw new RuntimeException("Error writing to file: "
372: + e.getMessage());
373: }
374: }
375:
376: /**
377: * @see org.kuali.module.labor.service.LaborOriginEntryService#getMatchingEntriesByCollection(java.util.Map)
378: */
379: public Collection getMatchingEntriesByCollection(Map searchCriteria) {
380: LOG.debug("getMatchingEntriesByCollection() started");
381:
382: return laborOriginEntryDao
383: .getMatchingEntriesByCollection(searchCriteria);
384: }
385:
386: /**
387: * @see org.kuali.module.labor.service.LaborOriginEntryService#getExactMatchingEntry(java.lang.Integer)
388: */
389: public LaborOriginEntry getExactMatchingEntry(Integer entryId) {
390: LOG.debug("getExactMatchingEntry() started");
391:
392: return (LaborOriginEntry) originEntryDao
393: .getExactMatchingEntry(entryId);
394: }
395:
396: /**
397: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getEntriesByGroup(org.kuali.module.gl.bo.OriginEntryGroup)
398: */
399: public Iterator<LaborOriginEntry> getEntriesByGroup(
400: OriginEntryGroup group) {
401: return laborOriginEntryDao.getLaborEntriesByGroup(group,
402: LaborOriginEntryDao.SORT_DOCUMENT);
403: }
404:
405: /**
406: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getEntriesByGroups(java.util.Collection)
407: */
408: public Iterator<LaborOriginEntry> getEntriesByGroups(
409: Collection<OriginEntryGroup> groups) {
410: return laborOriginEntryDao.getEntriesByGroups(groups);
411: }
412:
413: /**
414: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getEntriesByGroup(org.kuali.module.gl.bo.OriginEntryGroup,
415: * boolean)
416: */
417: public Iterator<LaborOriginEntry> getEntriesByGroup(
418: OriginEntryGroup group, boolean isConsolidated) {
419: if (!isConsolidated) {
420: return this .getEntriesByGroup(group);
421: }
422: return this .getConsolidatedEntryCollectionByGroup(group)
423: .iterator();
424: }
425:
426: /**
427: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getConsolidatedEntryCollectionByGroup(org.kuali.module.gl.bo.OriginEntryGroup,
428: * boolean)
429: */
430: public Collection<LaborOriginEntry> getConsolidatedEntryCollectionByGroup(
431: OriginEntryGroup group) {
432: Collection<LaborOriginEntry> entryCollection = new ArrayList<LaborOriginEntry>();
433: LaborLedgerUnitOfWork laborLedgerUnitOfWork = new LaborLedgerUnitOfWork();
434:
435: // the following iterator has been sorted
436: Iterator<Object[]> consolidatedEntries = laborOriginEntryDao
437: .getConsolidatedEntriesByGroup(group);
438:
439: while (consolidatedEntries.hasNext()) {
440: LaborOriginEntry laborOriginEntry = new LaborOriginEntry();
441: Object[] oneEntry = consolidatedEntries.next();
442: ObjectUtil.buildObject(laborOriginEntry, oneEntry,
443: LaborConstants
444: .consolidationAttributesOfOriginEntry());
445:
446: if (laborLedgerUnitOfWork.canContain(laborOriginEntry)) {
447: laborLedgerUnitOfWork
448: .addEntryIntoUnit(laborOriginEntry);
449: } else {
450: laborLedgerUnitOfWork
451: .resetLaborLedgerUnitOfWork(laborOriginEntry);
452: entryCollection.add(laborLedgerUnitOfWork
453: .getWorkingEntry());
454: }
455: }
456: return entryCollection;
457: }
458:
459: /**
460: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getSummariedEntriesByGroups(java.util.Collection)
461: */
462: public LedgerEntryHolder getSummariedEntriesByGroups(
463: Collection<OriginEntryGroup> groups) {
464: LedgerEntryHolder ledgerEntryHolder = new LedgerEntryHolder();
465:
466: if (groups.size() > 0) {
467: Iterator entrySummaryIterator = laborOriginEntryDao
468: .getSummaryByGroupId(groups);
469: while (entrySummaryIterator.hasNext()) {
470: Object[] entrySummary = (Object[]) entrySummaryIterator
471: .next();
472: ledgerEntryHolder.insertLedgerEntry(LedgerEntry
473: .buildLedgerEntry(entrySummary), true);
474: }
475: }
476: return ledgerEntryHolder;
477: }
478:
479: /**
480: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getPosterOutputSummaryByGroups(java.util.Collection)
481: */
482: public Map<String, PosterOutputSummaryEntry> getPosterOutputSummaryByGroups(
483: Collection<OriginEntryGroup> groups) {
484: Map<String, PosterOutputSummaryEntry> outputSummary = new HashMap<String, PosterOutputSummaryEntry>();
485:
486: if (groups.size() > 0) {
487: Iterator entrySummaryIterator = laborOriginEntryDao
488: .getPosterOutputSummaryByGroupId(groups);
489: while (entrySummaryIterator.hasNext()) {
490: Object[] entrySummary = (Object[]) entrySummaryIterator
491: .next();
492: PosterOutputSummaryEntry posterOutputSummaryEntry = PosterOutputSummaryEntry
493: .buildPosterOutputSummaryEntry(entrySummary);
494:
495: if (outputSummary.containsKey(posterOutputSummaryEntry
496: .getKey())) {
497: PosterOutputSummaryEntry tempEntry = outputSummary
498: .get(posterOutputSummaryEntry.getKey());
499: tempEntry.add(posterOutputSummaryEntry);
500: } else {
501: outputSummary.put(
502: posterOutputSummaryEntry.getKey(),
503: posterOutputSummaryEntry);
504: }
505: }
506: }
507: return outputSummary;
508: }
509:
510: /**
511: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getSizeOfEntriesInGroups(java.util.Collection)
512: */
513: public int getCountOfEntriesInGroups(
514: Collection<OriginEntryGroup> groups) {
515: return laborOriginEntryDao.getCountOfEntriesInGroups(groups);
516: }
517:
518: /**
519: * @see org.kuali.module.labor.service.LaborLaborOriginEntryService#getCountOfEntriesInSingleGroup(org.kuali.module.gl.bo.OriginEntryGroup)
520: */
521: public int getCountOfEntriesInSingleGroup(OriginEntryGroup group) {
522: List<OriginEntryGroup> groups = new ArrayList<OriginEntryGroup>();
523: groups.add(group);
524:
525: return this .getCountOfEntriesInGroups(groups);
526: }
527:
528: /**
529: * @see org.kuali.module.labor.service.LaborOriginEntryService#getEntryCollectionByGroup(org.kuali.module.gl.bo.OriginEntryGroup)
530: */
531: public Collection<LaborOriginEntry> getEntryCollectionByGroup(
532: OriginEntryGroup group) {
533: return laborOriginEntryDao.getEntryCollectionByGroup(group);
534: }
535:
536: /**
537: * Sets the dateTimeService attribute value.
538: *
539: * @param dateTimeService The dateTimeService to set.
540: */
541: public void setDateTimeService(DateTimeService dateTimeService) {
542: this .dateTimeService = dateTimeService;
543: }
544:
545: /**
546: * Sets the laborOriginEntryDao attribute value.
547: *
548: * @param laborOriginEntryDao The laborOriginEntryDao to set.
549: */
550: public void setLaborOriginEntryDao(
551: LaborOriginEntryDao laborOriginEntryDao) {
552: this .laborOriginEntryDao = laborOriginEntryDao;
553: }
554:
555: /**
556: * Sets the originEntryDao attribute value.
557: *
558: * @param originEntryDao The originEntryDao to set.
559: */
560: public void setOriginEntryDao(OriginEntryDao originEntryDao) {
561: this .originEntryDao = originEntryDao;
562: }
563:
564: /**
565: * Sets the originEntryGroupService attribute value.
566: *
567: * @param originEntryGroupService The originEntryGroupService to set.
568: */
569: public void setOriginEntryGroupService(
570: OriginEntryGroupService originEntryGroupService) {
571: this.originEntryGroupService = originEntryGroupService;
572: }
573: }
|