001: /*
002: * Copyright 2005-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.gl.service.impl;
017:
018: import java.sql.Date;
019: import java.util.ArrayList;
020: import java.util.Calendar;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import org.kuali.core.service.DateTimeService;
027: import org.kuali.core.util.Guid;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.module.gl.bo.OriginEntryFull;
030: import org.kuali.module.gl.bo.OriginEntryGroup;
031: import org.kuali.module.gl.bo.OriginEntrySource;
032: import org.kuali.module.gl.dao.OriginEntryDao;
033: import org.kuali.module.gl.dao.OriginEntryGroupDao;
034: import org.kuali.module.gl.service.OriginEntryGroupService;
035: import org.kuali.module.labor.bo.LaborOriginEntry;
036: import org.kuali.module.labor.dao.LaborOriginEntryDao;
037: import org.springframework.transaction.annotation.Transactional;
038:
039: /**
040: * The default implementation of OriginEntryGroupService
041: */
042: @Transactional
043: public class OriginEntryGroupServiceImpl implements
044: OriginEntryGroupService {
045: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
046: .getLogger(OriginEntryGroupServiceImpl.class);
047:
048: private OriginEntryGroupDao originEntryGroupDao;
049: private OriginEntryDao originEntryDao;
050: private DateTimeService dateTimeService;
051: private LaborOriginEntryDao laborOriginEntryDao;
052:
053: /**
054: * Constructs a OriginEntryGroupServiceImpl instance
055: */
056: public OriginEntryGroupServiceImpl() {
057: super ();
058: }
059:
060: /**
061: * Finds the group by the given group id, and then sets it to not process
062: * @param groupId the id of the group to set
063: * @see org.kuali.module.gl.service.OriginEntryGroupService#dontProcessGroup(java.lang.Integer)
064: */
065: public void dontProcessGroup(Integer groupId) {
066: LOG.debug("dontProcessGroup() started");
067:
068: OriginEntryGroup oeg = getExactMatchingEntryGroup(groupId);
069: if (oeg != null) {
070: oeg.setProcess(false);
071: save(oeg);
072: }
073: }
074:
075: /**
076: * Sets all scrubbable backup groups's scrub attributes to false, so none will be scrubbed
077: * @see org.kuali.module.gl.service.OriginEntryGroupService#markBackupGroupsUnscrubbable()
078: */
079: public void markScrubbableBackupGroupsAsUnscrubbable() {
080: LOG.debug("markScrubbableBackupGroupsAsUnscrubbable() started");
081: for (OriginEntryGroup scrubbableBackupGroup : getAllScrubbableBackupGroups()) {
082: if (LOG.isInfoEnabled()) {
083: LOG
084: .info("marking backup origin entry group as don't process: "
085: + scrubbableBackupGroup.getId());
086: }
087: scrubbableBackupGroup.setProcess(Boolean.FALSE);
088: save(scrubbableBackupGroup);
089: }
090: }
091:
092: /**
093: * Sets all groups created by the scrubber and ready to be posted's process attribute to false, so they won't be posted
094: * @see org.kuali.module.gl.service.OriginEntryGroupService#markPostableScrubberValidGroupsAsUnpostable()
095: */
096: public void markPostableScrubberValidGroupsAsUnpostable() {
097: LOG
098: .debug("markPostableScrubberValidGroupsAsUnpostable() started");
099: Collection<OriginEntryGroup> postableGroups = getGroupsToPost();
100: for (OriginEntryGroup postableGroup : postableGroups) {
101: if (LOG.isInfoEnabled()) {
102: LOG
103: .info("marking postable SCV origin entry group as don't process: "
104: + postableGroup.getId());
105: }
106: postableGroup.setProcess(Boolean.FALSE);
107: save(postableGroup);
108: }
109: }
110:
111: /**
112: * Marks any postable ICR group's process attribute as false, so they won't be posted
113: * @see org.kuali.module.gl.service.OriginEntryGroupService#markPostableIcrGroupsAsUnpostable()
114: */
115: public void markPostableIcrGroupsAsUnpostable() {
116: LOG.debug("markPostableIcrGroupsAsUnpostable() started");
117: Collection<OriginEntryGroup> postableGroups = getIcrGroupsToPost();
118: for (OriginEntryGroup postableGroup : postableGroups) {
119: if (LOG.isInfoEnabled()) {
120: LOG
121: .info("marking postable ICR origin entry group as don't process: "
122: + postableGroup.getId());
123: }
124: postableGroup.setProcess(Boolean.FALSE);
125: save(postableGroup);
126: }
127: }
128:
129: /**
130: * Returns the most recently created scrubber error group in the database
131: * @return the most recently created scrubber error group
132: * @see org.kuali.module.gl.service.OriginEntryGroupService#getNewestScrubberErrorGroup()
133: */
134: public OriginEntryGroup getNewestScrubberErrorGroup() {
135: LOG.debug("getNewestScrubberErrorGroup() started");
136:
137: OriginEntryGroup newest = null;
138:
139: Map crit = new HashMap();
140: crit.put("sourceCode", OriginEntrySource.SCRUBBER_ERROR);
141:
142: Collection groups = originEntryGroupDao.getMatchingGroups(crit);
143: for (Iterator iter = groups.iterator(); iter.hasNext();) {
144: OriginEntryGroup element = (OriginEntryGroup) iter.next();
145:
146: if (newest == null) {
147: newest = element;
148: } else {
149: if (newest.getId().intValue() < element.getId()
150: .intValue()) {
151: newest = element;
152: }
153: }
154: }
155:
156: return newest;
157: }
158:
159: /**
160: * Returns all groups created by a given origin entry Source
161: * @param sourceCode the source of the origin entry group
162: * @return a OriginEntryGroup with the given source code and max ORIGIN_ENTRY_GRP_ID
163: * @see org.kuali.module.gl.bo.OriginEntrySource
164: * @see org.kuali.module.gl.service.OriginEntryGroupService#getGroupsFromSource(java.lang.String)
165: */
166: public OriginEntryGroup getGroupWithMaxIdFromSource(
167: String sourceCode) {
168: LOG.debug("getGroupWithMaxIdFromSource() started");
169:
170: return originEntryGroupDao
171: .getGroupWithMaxIdFromSource(sourceCode);
172: }
173:
174: /**
175: * Returns all groups created by the backup source that can be scrubbed
176: * @return a Collection of origin entry groups to scrub
177: * @see org.kuali.module.gl.service.OriginEntryGroupService#getAllScrubbableBackupGroups()
178: */
179: public Collection<OriginEntryGroup> getAllScrubbableBackupGroups() {
180: return originEntryGroupDao.getAllScrubbableBackupGroups();
181: }
182:
183: /**
184: * Returns all labor origin entry groups created on the given date to back them up
185: * @param backupDate the date to find labor origin entry groups created on
186: * @see org.kuali.module.gl.service.OriginEntryGroupService#getLaborBackupGroups(java.sql.Date)
187: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getLaborBackupGroups(java.sql.Date)
188: */
189: public Collection getLaborBackupGroups(Date backupDate) {
190: LOG.debug("getBackupGroups() started");
191:
192: return originEntryGroupDao.getLaborBackupGroups(backupDate);
193: }
194:
195: /**
196: * Retrieves all groups to be created today, and creates backup group versions of them
197: * @see org.kuali.module.gl.service.OriginEntryGroupService#createBackupGroup()
198: */
199: public void createBackupGroup() {
200: LOG.debug("createBackupGroup() started");
201:
202: // Get the groups that need to be added
203: Date today = dateTimeService.getCurrentSqlDate();
204: Collection groups = originEntryGroupDao
205: .getGroupsToBackup(today);
206:
207: // Create the new group
208: OriginEntryGroup backupGroup = this .createGroup(today,
209: OriginEntrySource.BACKUP, true, true, true);
210:
211: for (Iterator<OriginEntryGroup> iter = groups.iterator(); iter
212: .hasNext();) {
213: OriginEntryGroup group = iter.next();
214:
215: for (Iterator<OriginEntryFull> entry_iter = originEntryDao
216: .getEntriesByGroup(group, 0); entry_iter.hasNext();) {
217: OriginEntryFull entry = entry_iter.next();
218:
219: entry.setEntryId(null);
220: entry.setObjectId(new Guid().toString());
221: entry.setGroup(backupGroup);
222: originEntryDao.saveOriginEntry(entry);
223: }
224:
225: group.setProcess(false);
226: group.setScrub(false);
227: originEntryGroupDao.save(group);
228: }
229: }
230:
231: /**
232: * Retrieves all labor origin entry groups to be backed up today and creates backup versions of them
233: * @see org.kuali.module.gl.service.OriginEntryGroupService#createLaborBackupGroup()
234: */
235: public void createLaborBackupGroup() {
236: LOG.debug("createBackupGroup() started");
237:
238: // Get the groups that need to be added
239: Date today = dateTimeService.getCurrentSqlDate();
240: Collection groups = originEntryGroupDao
241: .getLaborGroupsToBackup(today);
242:
243: // Create the new group
244: OriginEntryGroup backupGroup = this .createGroup(today,
245: OriginEntrySource.LABOR_BACKUP, true, true, true);
246:
247: for (Iterator<OriginEntryGroup> iter = groups.iterator(); iter
248: .hasNext();) {
249: OriginEntryGroup group = iter.next();
250: // Get only LaborOriginEntryGroup
251: if (group.getSourceCode().startsWith("L")) {
252: Iterator entry_iter = laborOriginEntryDao
253: .getLaborEntriesByGroup(group, 0);
254:
255: while (entry_iter.hasNext()) {
256: LaborOriginEntry entry = (LaborOriginEntry) entry_iter
257: .next();
258:
259: entry.setEntryId(null);
260: entry.setObjectId(new Guid().toString());
261: entry.setGroup(backupGroup);
262: laborOriginEntryDao.saveOriginEntry(entry);
263: }
264:
265: group.setProcess(false);
266: group.setScrub(false);
267: originEntryGroupDao.save(group);
268: }
269: }
270: }
271:
272: /**
273: * Deletes all groups older than a given number of days
274: * @param days the number of days that groups older than should be deleted
275: * @see org.kuali.module.gl.service.OriginEntryGroupService#deleteOlderGroups(int)
276: */
277: public void deleteOlderGroups(int days) {
278: LOG.debug("deleteOlderGroups() started");
279:
280: Calendar today = dateTimeService.getCurrentCalendar();
281: today.add(Calendar.DAY_OF_MONTH, 0 - days);
282:
283: Collection groups = originEntryGroupDao
284: .getOlderGroups(new java.sql.Date(today.getTime()
285: .getTime()));
286:
287: if (groups.size() > 0) {
288: originEntryDao.deleteGroups(groups);
289: originEntryGroupDao.deleteGroups(groups);
290: }
291: }
292:
293: /**
294: * Deletes every origin entry group in the given collection. Note: this method deletes all the origin entries
295: * in each group and then deletes the group.
296: * @param groupsToDelete a Collection of groups to delete
297: * @see org.kuali.module.gl.service.OriginEntryGroupService#deleteGroups(java.util.Collection)
298: */
299: public void deleteGroups(Collection<OriginEntryGroup> groupsToDelete) {
300: for (OriginEntryGroup groupToDelete : groupsToDelete) {
301: if (groupToDelete.getId() == null) {
302: throw new NullPointerException(
303: "Received null group ID trying to delete groups");
304: }
305: }
306:
307: if (groupsToDelete.size() > 0) {
308: originEntryDao.deleteGroups(groupsToDelete);
309: originEntryGroupDao.deleteGroups(groupsToDelete);
310: }
311: }
312:
313: /**
314: * Return all the origin entry groups that have a process attribute of false.
315: * @return a Collection of all origin entry groups that have a process indicator of false. collection is returned read-only.
316: */
317: public Collection getOriginEntryGroupsPendingProcessing() {
318: LOG.debug("getOriginEntryGroupsPendingProcessing() started");
319:
320: Map criteria = new HashMap();
321: criteria.put("process", Boolean.FALSE);
322: Collection returnCollection = new ArrayList();
323: returnCollection = originEntryGroupDao
324: .getMatchingGroups(criteria);
325: return returnCollection;
326: }
327:
328: /**
329: * Returns all origin entry groups currently in the databse
330: * @return a Collection of all origin entry groups in the database
331: * @see org.kuali.module.gl.service.OriginEntryGroupService#getAllOriginEntryGroup()
332: */
333: public Collection getAllOriginEntryGroup() {
334: LOG.debug("getAllOriginEntryGroup() started");
335: Map criteria = new HashMap();
336:
337: Collection<OriginEntryGroup> c = originEntryGroupDao
338: .getMatchingGroups(criteria);
339:
340: // GLCP and LLCP group filter exception
341: String groupException = "";
342: for (int i = 0; i < KFSConstants.LLCP_GROUP_FILTER_EXCEPTION.length; i++) {
343: groupException += KFSConstants.LLCP_GROUP_FILTER_EXCEPTION[i]
344: + " ";
345: }
346:
347: // Get the row counts for each group
348:
349: for (OriginEntryGroup group : c) {
350:
351: if (group.getSourceCode().startsWith("L")
352: && !groupException.contains(group.getSourceCode())) {
353: group.setRows(laborOriginEntryDao.getGroupCount(group
354: .getId()));
355: } else {
356: group.setRows(originEntryDao.getGroupCount(group
357: .getId()));
358: }
359:
360: }
361: return c;
362: }
363:
364: /**
365: * Create a new OriginEntryGroup and persists it to the database.
366: * @param date the date this group should list as its creation date
367: * @param sourceCode the source of this origin entry group
368: * @param valid whether this group is valid - ie, all entries within it are valid
369: * @param process whether this group should be processed by the next step
370: * @param scrub whether this group should be input to the scrubber
371: * @return a new origin entry group to put origin entries into
372: */
373: public OriginEntryGroup createGroup(Date date, String sourceCode,
374: boolean valid, boolean process, boolean scrub) {
375: LOG.debug("createGroup() started");
376:
377: OriginEntryGroup oeg = new OriginEntryGroup();
378: oeg.setDate(date);
379: oeg.setProcess(Boolean.valueOf(process));
380: oeg.setScrub(Boolean.valueOf(scrub));
381: oeg.setSourceCode(sourceCode);
382: oeg.setValid(Boolean.valueOf(valid));
383:
384: originEntryGroupDao.save(oeg);
385:
386: return oeg;
387: }
388:
389: /**
390: * Get all non-ICR-related OriginEntryGroups waiting to be posted as of postDate.
391: * @return a Collection of origin entry groups to post
392: */
393: public Collection getGroupsToPost() {
394: LOG.debug("getGroupsToPost() started");
395:
396: return originEntryGroupDao
397: .getPosterGroups(OriginEntrySource.SCRUBBER_VALID);
398: }
399:
400: /**
401: * Get all ICR-related OriginEntryGroups waiting to be posted as of postDate.
402: * @return a Collection of origin entry groups with indirect cost recovery origin entries to post
403: */
404: public Collection getIcrGroupsToPost() {
405: LOG.debug("getIcrGroupsToPost() started");
406:
407: return originEntryGroupDao
408: .getPosterGroups(OriginEntrySource.ICR_TRANSACTIONS);
409: }
410:
411: /**
412: * An alias for OriginEntryGroupDao.getScrubberGroups().
413: *
414: * @param scrubDate the date to find backup groups for
415: * @return a Collection of groups to scrub
416: */
417: public Collection getGroupsToBackup(Date scrubDate) {
418: LOG.debug("getGroupsToScrub() started");
419:
420: return originEntryGroupDao.getGroupsToBackup(scrubDate);
421: }
422:
423: /**
424: * Returns all groups to post
425: * @param entryGroupSourceCode the source code of origin entry groups to post
426: * @return a Collection of groups to post
427: * @see org.kuali.module.gl.service.OriginEntryGroupService#getGroups(java.lang.String)
428: */
429: public Collection getGroupsToPost(String entryGroupSourceCode) {
430: return originEntryGroupDao
431: .getPosterGroups(entryGroupSourceCode);
432: }
433:
434: /**
435: * Retrieves all groups that match the given search criteria
436: * @param criteria a Map of criteria to build a query from
437: * @return a Collection of all qualifying origin entry groups
438: * @see org.kuali.module.gl.service.OriginEntryGroupService#getMatchingGroups(java.util.Map)
439: */
440: public Collection getMatchingGroups(Map criteria) {
441: LOG.debug("getMatchingGroups() started");
442:
443: return originEntryGroupDao.getMatchingGroups(criteria);
444: }
445:
446: /**
447: * Persist an OriginEntryGroup to the database.
448: *
449: * @param originEntryGroup
450: */
451: public void save(OriginEntryGroup originEntryGroup) {
452: LOG.debug("save() started");
453:
454: originEntryGroupDao.save(originEntryGroup);
455: }
456:
457: /**
458: * Returns the origin entry group with the given id
459: * @param id the id of the origin entry group to retreive
460: * @return the origin entry group with the given id if found, otherwise null
461: * @see org.kuali.module.gl.service.OriginEntryGroupService#getExactMatchingEntryGroup(java.lang.Integer)
462: */
463: public OriginEntryGroup getExactMatchingEntryGroup(Integer id) {
464: return originEntryGroupDao.getExactMatchingEntryGroup(id);
465: }
466:
467: /**
468: * Returns groups created within the past number of given days
469: * @param days the number of days returned groups must be younger than
470: * @return a Collection of qualifying groups
471: * @see org.kuali.module.gl.service.OriginEntryGroupService#getRecentGroupsByDays(int)
472: */
473: public Collection getRecentGroupsByDays(int days) {
474:
475: Calendar today = dateTimeService.getCurrentCalendar();
476: today.add(Calendar.DAY_OF_MONTH, 0 - days);
477:
478: Collection groups = originEntryGroupDao
479: .getRecentGroups(new java.sql.Date(today.getTime()
480: .getTime()));
481:
482: return groups;
483: }
484:
485: /**
486: * Returns whether or not a group with the given id exists in the database
487: * @param groupId the id of the group to check for existence
488: * @return true if such a group exists, false otherwise
489: * @see org.kuali.module.gl.service.OriginEntryGroupService#getGroupExists(java.lang.Integer)
490: */
491: public boolean getGroupExists(Integer groupId) {
492: Map<String, Integer> criteria = new HashMap<String, Integer>();
493: criteria.put("id", groupId);
494: Collection groups = getMatchingGroups(criteria);
495: return groups.size() > 0;
496: }
497:
498: public void setOriginEntryGroupDao(OriginEntryGroupDao oegd) {
499: originEntryGroupDao = oegd;
500: }
501:
502: public void setOriginEntryDao(OriginEntryDao oed) {
503: originEntryDao = oed;
504: }
505:
506: public void setLaborOriginEntryDao(LaborOriginEntryDao loed) {
507: laborOriginEntryDao = loed;
508: }
509:
510: public void setDateTimeService(DateTimeService dts) {
511: dateTimeService = dts;
512: }
513: }
|