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.dao.ojb;
017:
018: import java.sql.Connection;
019: import java.sql.Date;
020: import java.sql.Statement;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.ojb.broker.query.Criteria;
028: import org.apache.ojb.broker.query.QueryByCriteria;
029: import org.apache.ojb.broker.query.QueryFactory;
030: import org.apache.ojb.broker.query.ReportQueryByCriteria;
031: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
032: import org.kuali.module.gl.bo.OriginEntryGroup;
033: import org.kuali.module.gl.bo.OriginEntrySource;
034: import org.kuali.module.gl.dao.OriginEntryGroupDao;
035:
036: /**
037: * An OJB specific implementation of OriginEntryGroupDao
038: */
039: public class OriginEntryGroupDaoOjb extends PlatformAwareDaoBaseOjb
040: implements OriginEntryGroupDao {
041: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(OriginEntryGroupDaoOjb.class);
043:
044: private static final String DATE = "date";
045: private static final String ID = "id";
046: private static final String SOURCE_CODE = "sourceCode";
047: private static final String PROCESS = "process";
048: private static final String VALID = "valid";
049: private static final String SCRUB = "scrub";
050: private static final String ORIGIN_ENTRY_GRP_ID = "ORIGIN_ENTRY_GRP_ID";
051: private static final String MAX_ORIGIN_ENTRY_GRP_ID = "max(ORIGIN_ENTRY_GRP_ID)";
052:
053: /**
054: * Given an origin entry group source type (defined in OriginEntrySource)
055: *
056: * @param sourceCode the source code of the groups to find
057: * @return a OriginEntryGroup with the given source code and max ORIGIN_ENTRY_GRP_ID
058: * @see org.kuali.module.gl.bo.OriginEntrySource
059: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getGroupWithMaxIdFromSource(java.lang.String)
060: */
061: public OriginEntryGroup getGroupWithMaxIdFromSource(
062: String sourceCode) {
063: LOG.debug("getGroupWithMaxIdFromSource() started");
064:
065: Criteria crit = new Criteria();
066:
067: Criteria subCrit = new Criteria();
068: subCrit.addEqualTo(SOURCE_CODE, sourceCode);
069: ReportQueryByCriteria subQuery = new ReportQueryByCriteria(
070: OriginEntryGroup.class, subCrit);
071: subQuery
072: .setAttributes(new String[] { MAX_ORIGIN_ENTRY_GRP_ID });
073:
074: crit.addGreaterOrEqualThan(ORIGIN_ENTRY_GRP_ID, subQuery);
075:
076: QueryByCriteria qbc = QueryFactory.newQuery(
077: OriginEntryGroup.class, crit);
078:
079: return (OriginEntryGroup) getPersistenceBrokerTemplate()
080: .getObjectByQuery(qbc);
081: }
082:
083: /**
084: * Get all the groups that are older than a date
085: *
086: * @param day the date groups returned should be older than
087: * @return a Collection of origin entry groups older than that date
088: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getOlderGroups(Date)
089: */
090: public Collection<OriginEntryGroup> getOlderGroups(Date day) {
091: LOG.debug("getOlderGroups() started");
092:
093: Criteria criteria = new Criteria();
094: criteria.addLessOrEqualThan(DATE, day);
095:
096: return getPersistenceBrokerTemplate()
097: .getCollectionByQuery(
098: QueryFactory.newQuery(OriginEntryGroup.class,
099: criteria));
100: }
101:
102: /**
103: * Delete all the groups in the list. Note...it doesn't delete the entries within them, you need
104: * OriginEntryDao.deleteGroups for that
105: *
106: * @params groups a Collection of origin entry groups to delete
107: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#deleteGroups(java.util.Collection)
108: */
109: public void deleteGroups(Collection<OriginEntryGroup> groups) {
110: LOG.debug("deleteGroups() started");
111:
112: List ids = new ArrayList();
113: for (Iterator iter = groups.iterator(); iter.hasNext();) {
114: OriginEntryGroup element = (OriginEntryGroup) iter.next();
115: ids.add(element.getId());
116: }
117: Criteria criteria = new Criteria();
118: criteria.addIn(ID, ids);
119:
120: getPersistenceBrokerTemplate()
121: .deleteByQuery(
122: QueryFactory.newQuery(OriginEntryGroup.class,
123: criteria));
124: getPersistenceBrokerTemplate().clearCache();
125: }
126:
127: /**
128: * Builds an OJB query out of the given map of criteria and fetches all the groups that match the criteria
129: *
130: * @param searchCriteria a Map of search criteria to form the query
131: * @return a Collection of Origin Entry Groups that match that criteria
132: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getMatchingGroups(java.util.Map)
133: */
134: public Collection getMatchingGroups(Map searchCriteria) {
135: LOG.debug("getMatchingGroups() started");
136:
137: Criteria criteria = new Criteria();
138: for (Iterator iterator = searchCriteria.keySet().iterator(); iterator
139: .hasNext();) {
140: String key = iterator.next().toString();
141: criteria.addEqualTo(key, searchCriteria.get(key));
142: }
143:
144: QueryByCriteria qbc = QueryFactory.newQuery(
145: OriginEntryGroup.class, criteria);
146: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
147: }
148:
149: /**
150: * Get all the groups for the poster (that is to say, Groups with "Process" being true)
151: *
152: * @param groupSourceCode the source code of origin entry groups to return
153: * @return a Collection of origin entry groups that should be processed by the poster
154: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getPosterGroups(java.lang.String)
155: */
156: public Collection getPosterGroups(String groupSourceCode) {
157: LOG.debug("getPosterGroups() started");
158:
159: Criteria criteria = new Criteria();
160: criteria.addEqualTo(SOURCE_CODE, groupSourceCode);
161: criteria.addEqualTo(PROCESS, Boolean.TRUE);
162: criteria.addEqualTo(VALID, Boolean.TRUE);
163:
164: QueryByCriteria qbc = QueryFactory.newQuery(
165: OriginEntryGroup.class, criteria);
166: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
167: }
168:
169: /**
170: * Gets a collection of all backup groups that are scrubbable (i.e. valid, process, scrub indicators all set to true)
171: *
172: * @return a Collection of scrubbable origin entry groups
173: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getAllScrubbableBackupGroups()
174: */
175: public Collection<OriginEntryGroup> getAllScrubbableBackupGroups() {
176: Criteria criteria = new Criteria();
177: criteria.addEqualTo(SOURCE_CODE, OriginEntrySource.BACKUP);
178: criteria.addEqualTo(SCRUB, Boolean.TRUE);
179: criteria.addEqualTo(PROCESS, Boolean.TRUE);
180: criteria.addEqualTo(VALID, Boolean.TRUE);
181:
182: QueryByCriteria qbc = QueryFactory.newQuery(
183: OriginEntryGroup.class, criteria);
184: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
185: }
186:
187: /**
188: * Get all the Labor backup groups to scrub (ie, origin entry groups with source OriginEntrySource.LABOR_BACKUP)
189: *
190: * @param groupDate this parameter isn't really used
191: * @return a Collection of Labor backup groups
192: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getLaborBackupGroups(java.sql.Date)
193: */
194: public Collection getLaborBackupGroups(Date groupDate) {
195: LOG.debug("getGroupsToBackup() started");
196:
197: Criteria criteria = new Criteria();
198: criteria
199: .addEqualTo(SOURCE_CODE, OriginEntrySource.LABOR_BACKUP);
200: criteria.addEqualTo(SCRUB, Boolean.TRUE);
201: criteria.addEqualTo(PROCESS, Boolean.TRUE);
202: criteria.addEqualTo(VALID, Boolean.TRUE);
203:
204: QueryByCriteria qbc = QueryFactory.newQuery(
205: OriginEntryGroup.class, criteria);
206: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
207: }
208:
209: /**
210: * Get all the groups to be copied into the backup group
211: *
212: * @param groupDate the date returned origin entry groups must have been created on or before
213: * @return a Collection of origin entry groups to backup
214: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getScrubberGroups(java.sql.Date)
215: */
216: public Collection getGroupsToBackup(Date groupDate) {
217: LOG.debug("getGroupsToBackup() started");
218:
219: Criteria criteria = new Criteria();
220: criteria.addLessOrEqualThan(DATE, groupDate);
221: criteria.addEqualTo(SCRUB, Boolean.TRUE);
222: criteria.addEqualTo(PROCESS, Boolean.TRUE);
223: criteria.addEqualTo(VALID, Boolean.TRUE);
224:
225: QueryByCriteria qbc = QueryFactory.newQuery(
226: OriginEntryGroup.class, criteria);
227: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
228: }
229:
230: /**
231: * Get all the groups to be copied into the backup group...though, notably, this method
232: * does nothing to differentiate labor groups from otherwise normal groups. One must assume
233: * that processing takes place somewhere else
234: *
235: * @param groupDate the date returned origin entry groups must have been created on or before
236: * @return a Collection of Labor Origin Entry Groups to backup
237: *
238: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getLaborScrubberGroups(java.sql.Date)
239: */
240: public Collection getLaborGroupsToBackup(Date groupDate) {
241: LOG.debug("getLaborGroupsToBackup() started");
242:
243: Criteria criteria = new Criteria();
244: criteria.addLessOrEqualThan(DATE, groupDate);
245: criteria.addEqualTo(SCRUB, Boolean.TRUE);
246: criteria.addEqualTo(PROCESS, Boolean.TRUE);
247: criteria.addEqualTo(VALID, Boolean.TRUE);
248:
249: QueryByCriteria qbc = QueryFactory.newQuery(
250: OriginEntryGroup.class, criteria);
251: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
252: }
253:
254: /**
255: * Saves an origin entry group
256: *
257: * @param group the group to save
258: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#save(org.kuali.module.gl.bo.OriginEntryGroup)
259: */
260: public void save(OriginEntryGroup group) {
261: LOG.debug("save() started");
262:
263: getPersistenceBrokerTemplate().store(group);
264: }
265:
266: /**
267: * We all know that computers aren't naturally exact. They like to fudge things. Databases especially.
268: * If you send a database a table name and a primary key on that table, you really never know what you're
269: * going to get, now do you? But this method makes sure that that rascally database returns the origin
270: * entry group with the primary key of the given group id. Or null if it can't find anything. It works
271: * by magic.
272: *
273: * @param id the id of the origin entry group to return
274: * @return an Origin Entry Group, or null if the exact one couldn't be found
275: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getExactMatchingEntryGroup(java.lang.Integer)
276: */
277: public OriginEntryGroup getExactMatchingEntryGroup(Integer id) {
278: LOG.debug("getMatchingEntries() started");
279: return (OriginEntryGroup) getPersistenceBrokerTemplate()
280: .getObjectById(OriginEntryGroup.class, id);
281: }
282:
283: /**
284: * Run a sql command. This method takes its connection from the given persistence broker.
285: *
286: * @param sql the sql to execute; it must be an update state
287: * @return the result of the execute update
288: */
289: private int sqlCommand(String sql) {
290: LOG.info("sqlCommand() started: " + sql);
291:
292: Statement stmt = null;
293:
294: try {
295: Connection c = getPersistenceBroker(true)
296: .serviceConnectionManager().getConnection();
297: stmt = c.createStatement();
298: return stmt.executeUpdate(sql);
299: } catch (Exception e) {
300: throw new RuntimeException("Unable to execute: "
301: + e.getMessage());
302: } finally {
303: try {
304: if (stmt != null) {
305: stmt.close();
306: }
307: } catch (Exception e) {
308: throw new RuntimeException(
309: "Unable to close connection: " + e.getMessage());
310: }
311: }
312: }
313:
314: /**
315: * Given a date, finds all origin entry groups that were created on or after that date
316: * @param day the date that defines recency - all qualifying origin entries groups will have been created on or after that day
317: * @return a Collection of OriginEntryGroup records
318: * @see org.kuali.module.gl.dao.OriginEntryGroupDao#getRecentGroups(Date)
319: */
320: public Collection<OriginEntryGroup> getRecentGroups(Date day) {
321: LOG.debug("getOlderGroups() started");
322:
323: Criteria criteria = new Criteria();
324: criteria.addGreaterOrEqualThan(DATE, day);
325:
326: return getPersistenceBrokerTemplate()
327: .getCollectionByQuery(
328: QueryFactory.newQuery(OriginEntryGroup.class,
329: criteria));
330: }
331:
332: }
|