001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.gl.dataaccess.impl;
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.kfs.gl.businessobject.OriginEntryGroup;
032 import org.kuali.kfs.gl.businessobject.OriginEntrySource;
033 import org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao;
034 import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
035
036 /**
037 * An OJB specific implementation of OriginEntryGroupDao
038 */
039 public class OriginEntryGroupDaoOjb extends PlatformAwareDaoBaseOjb implements OriginEntryGroupDao {
040 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OriginEntryGroupDaoOjb.class);
041
042 private static final String DATE = "date";
043 private static final String ID = "id";
044 private static final String SOURCE_CODE = "sourceCode";
045 private static final String PROCESS = "process";
046 private static final String VALID = "valid";
047 private static final String SCRUB = "scrub";
048 private static final String ORIGIN_ENTRY_GRP_ID = "ORIGIN_ENTRY_GRP_ID";
049 private static final String MAX_ORIGIN_ENTRY_GRP_ID = "max(ORIGIN_ENTRY_GRP_ID)";
050
051 /**
052 * Given an origin entry group source type (defined in OriginEntrySource)
053 *
054 * @param sourceCode the source code of the groups to find
055 * @return a OriginEntryGroup with the given source code and max ORIGIN_ENTRY_GRP_ID
056 * @see org.kuali.kfs.gl.businessobject.OriginEntrySource
057 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getGroupWithMaxIdFromSource(java.lang.String)
058 */
059 public OriginEntryGroup getGroupWithMaxIdFromSource(String sourceCode) {
060 LOG.debug("getGroupWithMaxIdFromSource() started");
061
062 Criteria crit = new Criteria();
063
064 Criteria subCrit = new Criteria();
065 subCrit.addEqualTo(SOURCE_CODE, sourceCode);
066 ReportQueryByCriteria subQuery = new ReportQueryByCriteria(OriginEntryGroup.class, subCrit);
067 subQuery.setAttributes(new String[]{MAX_ORIGIN_ENTRY_GRP_ID});
068
069 crit.addGreaterOrEqualThan(ORIGIN_ENTRY_GRP_ID, subQuery);
070
071 QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, crit);
072
073 return (OriginEntryGroup) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
074 }
075
076 /**
077 * Get all the groups that are older than a date
078 *
079 * @param day the date groups returned should be older than
080 * @return a Collection of origin entry groups older than that date
081 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getOlderGroups(Date)
082 */
083 public Collection<OriginEntryGroup> getOlderGroups(Date day) {
084 LOG.debug("getOlderGroups() started");
085
086 Criteria criteria = new Criteria();
087 criteria.addLessOrEqualThan(DATE, day);
088
089 return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(OriginEntryGroup.class, criteria));
090 }
091
092 /**
093 * Delete all the groups in the list. Note...it doesn't delete the entries within them, you need
094 * OriginEntryDao.deleteGroups for that
095 *
096 * @params groups a Collection of origin entry groups to delete
097 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#deleteGroups(java.util.Collection)
098 */
099 public void deleteGroups(Collection<OriginEntryGroup> groups) {
100 LOG.debug("deleteGroups() started");
101
102 List ids = new ArrayList();
103 for (Iterator iter = groups.iterator(); iter.hasNext();) {
104 OriginEntryGroup element = (OriginEntryGroup) iter.next();
105 ids.add(element.getId());
106 }
107 Criteria criteria = new Criteria();
108 criteria.addIn(ID, ids);
109
110 getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(OriginEntryGroup.class, criteria));
111 getPersistenceBrokerTemplate().clearCache();
112 }
113
114 /**
115 * Builds an OJB query out of the given map of criteria and fetches all the groups that match the criteria
116 *
117 * @param searchCriteria a Map of search criteria to form the query
118 * @return a Collection of Origin Entry Groups that match that criteria
119 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getMatchingGroups(java.util.Map)
120 */
121 public Collection getMatchingGroups(Map searchCriteria) {
122 LOG.debug("getMatchingGroups() started");
123
124 Criteria criteria = new Criteria();
125 for (Iterator iterator = searchCriteria.keySet().iterator(); iterator.hasNext();) {
126 String key = iterator.next().toString();
127 criteria.addEqualTo(key, searchCriteria.get(key));
128 }
129
130 QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
131 return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
132 }
133
134 /**
135 * Get all the groups for the poster (that is to say, Groups with "Process" being true)
136 *
137 * @param groupSourceCode the source code of origin entry groups to return
138 * @return a Collection of origin entry groups that should be processed by the poster
139 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getPosterGroups(java.lang.String)
140 */
141 public Collection getPosterGroups(String groupSourceCode) {
142 LOG.debug("getPosterGroups() started");
143
144 Criteria criteria = new Criteria();
145 criteria.addEqualTo(SOURCE_CODE, groupSourceCode);
146 criteria.addEqualTo(PROCESS, Boolean.TRUE);
147 criteria.addEqualTo(VALID, Boolean.TRUE);
148
149 QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
150 return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
151 }
152
153 /**
154 * Gets a collection of all backup groups that are scrubbable (i.e. valid, process, scrub indicators all set to true)
155 *
156 * @return a Collection of scrubbable origin entry groups
157 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getAllScrubbableBackupGroups()
158 */
159 public Collection<OriginEntryGroup> getAllScrubbableBackupGroups() {
160 Criteria criteria = new Criteria();
161 criteria.addEqualTo(SOURCE_CODE, OriginEntrySource.BACKUP);
162 criteria.addEqualTo(SCRUB, Boolean.TRUE);
163 criteria.addEqualTo(PROCESS, Boolean.TRUE);
164 criteria.addEqualTo(VALID, Boolean.TRUE);
165
166 QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
167 return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
168 }
169
170 /**
171 * Get all the groups to be copied into the backup group
172 *
173 * @param groupDate the date returned origin entry groups must have been created on or before
174 * @return a Collection of origin entry groups to backup
175 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getScrubberGroups(java.sql.Date)
176 */
177 public Collection getGroupsToBackup(Date groupDate) {
178 LOG.debug("getGroupsToBackup() started");
179
180 Criteria criteria = new Criteria();
181 criteria.addLessOrEqualThan(DATE, groupDate);
182 criteria.addEqualTo(SCRUB, Boolean.TRUE);
183 criteria.addEqualTo(PROCESS, Boolean.TRUE);
184 criteria.addEqualTo(VALID, Boolean.TRUE);
185
186 QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
187 return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
188 }
189
190 /**
191 * Saves an origin entry group
192 *
193 * @param group the group to save
194 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#save(org.kuali.kfs.gl.businessobject.OriginEntryGroup)
195 */
196 public void save(OriginEntryGroup group) {
197 LOG.debug("save() started");
198
199 getPersistenceBrokerTemplate().store(group);
200 }
201
202 /**
203 * We all know that computers aren't naturally exact. They like to fudge things. Databases especially.
204 * If you send a database a table name and a primary key on that table, you really never know what you're
205 * going to get, now do you? But this method makes sure that that rascally database returns the origin
206 * entry group with the primary key of the given group id. Or null if it can't find anything. It works
207 * by magic.
208 *
209 * @param id the id of the origin entry group to return
210 * @return an Origin Entry Group, or null if the exact one couldn't be found
211 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getExactMatchingEntryGroup(java.lang.Integer)
212 */
213 public OriginEntryGroup getExactMatchingEntryGroup(Integer id) {
214 LOG.debug("getMatchingEntries() started");
215 return (OriginEntryGroup) getPersistenceBrokerTemplate().getObjectById(OriginEntryGroup.class, id);
216 }
217
218 /**
219 * Given a date, finds all origin entry groups that were created on or after that date
220 * @param day the date that defines recency - all qualifying origin entries groups will have been created on or after that day
221 * @return a Collection of OriginEntryGroup records
222 * @see org.kuali.kfs.gl.dataaccess.OriginEntryGroupDao#getRecentGroups(Date)
223 */
224 public Collection<OriginEntryGroup> getRecentGroups(Date day) {
225 LOG.debug("getOlderGroups() started");
226
227 Criteria criteria = new Criteria();
228 criteria.addGreaterOrEqualThan(DATE, day);
229
230 return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(OriginEntryGroup.class, criteria));
231 }
232
233
234 }