View Javadoc

1   /*
2    * Copyright 2005 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.gl.dataaccess.impl;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.ojb.broker.query.Criteria;
26  import org.apache.ojb.broker.query.QueryByCriteria;
27  import org.apache.ojb.broker.query.QueryFactory;
28  import org.apache.ojb.broker.query.ReportQueryByCriteria;
29  import org.kuali.ole.gl.businessobject.OriginEntryGroup;
30  import org.kuali.ole.gl.businessobject.OriginEntrySource;
31  import org.kuali.ole.gl.dataaccess.OriginEntryGroupDao;
32  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
33  
34  /**
35   * An OJB specific implementation of OriginEntryGroupDao
36   */
37  public class OriginEntryGroupDaoOjb extends PlatformAwareDaoBaseOjb implements OriginEntryGroupDao {
38      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OriginEntryGroupDaoOjb.class);
39  
40      private static final String DATE = "date";
41      private static final String ID = "id";
42      private static final String SOURCE_CODE = "sourceCode";
43      private static final String PROCESS = "process";
44      private static final String VALID = "valid";
45      private static final String SCRUB = "scrub";
46      private static final String ORIGIN_ENTRY_GRP_ID = "ORIGIN_ENTRY_GRP_ID";
47      private static final String MAX_ORIGIN_ENTRY_GRP_ID = "max(ORIGIN_ENTRY_GRP_ID)";
48  
49      /**
50       * Given an origin entry group source type (defined in OriginEntrySource)
51       * 
52       * @param sourceCode the source code of the groups to find
53       * @return a OriginEntryGroup with the given source code and max ORIGIN_ENTRY_GRP_ID
54       * @see org.kuali.ole.gl.businessobject.OriginEntrySource
55       * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getGroupWithMaxIdFromSource(java.lang.String)
56       */
57      public OriginEntryGroup getGroupWithMaxIdFromSource(String sourceCode) {
58          LOG.debug("getGroupWithMaxIdFromSource() started");
59          
60          Criteria crit = new Criteria();
61          
62          Criteria subCrit = new Criteria();
63          subCrit.addEqualTo(SOURCE_CODE, sourceCode);
64          ReportQueryByCriteria subQuery = new ReportQueryByCriteria(OriginEntryGroup.class, subCrit);
65          subQuery.setAttributes(new String[]{MAX_ORIGIN_ENTRY_GRP_ID});
66          
67          crit.addGreaterOrEqualThan(ORIGIN_ENTRY_GRP_ID, subQuery);
68          
69          QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, crit);
70          
71          return (OriginEntryGroup) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
72      }
73  
74      /**
75       * Get all the groups that are older than a date
76       * 
77       * @param day the date groups returned should be older than
78       * @return a Collection of origin entry groups older than that date
79       * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getOlderGroups(Date)
80       */
81      public Collection<OriginEntryGroup> getOlderGroups(Date day) {
82          LOG.debug("getOlderGroups() started");
83  
84          Criteria criteria = new Criteria();
85          criteria.addLessOrEqualThan(DATE, day);
86  
87          return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(OriginEntryGroup.class, criteria));
88      }
89  
90      /**
91       * Delete all the groups in the list.  Note...it doesn't delete the entries within them, you need
92       * OriginEntryDao.deleteGroups for that
93       * 
94       * @params groups a Collection of origin entry groups to delete
95       * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#deleteGroups(java.util.Collection)
96       */
97      public void deleteGroups(Collection<OriginEntryGroup> groups) {
98          LOG.debug("deleteGroups() started");
99  
100         List ids = new ArrayList();
101         for (Iterator iter = groups.iterator(); iter.hasNext();) {
102             OriginEntryGroup element = (OriginEntryGroup) iter.next();
103             ids.add(element.getId());
104         }
105         Criteria criteria = new Criteria();
106         criteria.addIn(ID, ids);
107 
108         getPersistenceBrokerTemplate().deleteByQuery(QueryFactory.newQuery(OriginEntryGroup.class, criteria));
109         getPersistenceBrokerTemplate().clearCache();
110     }
111 
112     /**
113      * Builds an OJB query out of the given map of criteria and fetches all the groups that match the criteria
114      * 
115      * @param searchCriteria a Map of search criteria to form the query
116      * @return a Collection of Origin Entry Groups that match that criteria
117      * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getMatchingGroups(java.util.Map)
118      */
119     public Collection getMatchingGroups(Map searchCriteria) {
120         LOG.debug("getMatchingGroups() started");
121 
122         Criteria criteria = new Criteria();
123         for (Iterator iterator = searchCriteria.keySet().iterator(); iterator.hasNext();) {
124             String key = iterator.next().toString();
125             criteria.addEqualTo(key, searchCriteria.get(key));
126         }
127 
128         QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
129         return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
130     }
131 
132     /**
133      * Get all the groups for the poster (that is to say, Groups with "Process" being true)
134      * 
135      * @param groupSourceCode the source code of origin entry groups to return
136      * @return a Collection of origin entry groups that should be processed by the poster
137      * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getPosterGroups(java.lang.String)
138      */
139     public Collection getPosterGroups(String groupSourceCode) {
140         LOG.debug("getPosterGroups() started");
141 
142         Criteria criteria = new Criteria();
143         criteria.addEqualTo(SOURCE_CODE, groupSourceCode);
144         criteria.addEqualTo(PROCESS, Boolean.TRUE);
145         criteria.addEqualTo(VALID, Boolean.TRUE);
146 
147         QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
148         return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
149     }
150 
151     /**
152      * Gets a collection of all backup groups that are scrubbable (i.e. valid, process, scrub indicators all set to true)
153      * 
154      * @return a Collection of scrubbable origin entry groups
155      * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getAllScrubbableBackupGroups()
156      */
157     public Collection<OriginEntryGroup> getAllScrubbableBackupGroups() {
158         Criteria criteria = new Criteria();
159         criteria.addEqualTo(SOURCE_CODE, OriginEntrySource.BACKUP);
160         criteria.addEqualTo(SCRUB, Boolean.TRUE);
161         criteria.addEqualTo(PROCESS, Boolean.TRUE);
162         criteria.addEqualTo(VALID, Boolean.TRUE);
163 
164         QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
165         return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
166     }
167 
168     /**
169      * Get all the groups to be copied into the backup group
170      * 
171      * @param groupDate the date returned origin entry groups must have been created on or before
172      * @return a Collection of origin entry groups to backup
173      * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getScrubberGroups(java.sql.Date)
174      */
175     public Collection getGroupsToBackup(Date groupDate) {
176         LOG.debug("getGroupsToBackup() started");
177 
178         Criteria criteria = new Criteria();
179         criteria.addLessOrEqualThan(DATE, groupDate);
180         criteria.addEqualTo(SCRUB, Boolean.TRUE);
181         criteria.addEqualTo(PROCESS, Boolean.TRUE);
182         criteria.addEqualTo(VALID, Boolean.TRUE);
183 
184         QueryByCriteria qbc = QueryFactory.newQuery(OriginEntryGroup.class, criteria);
185         return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
186     }
187 
188     /**
189      * We all know that computers aren't naturally exact.  They like to fudge things.  Databases especially.
190      * If you send a database a table name and a primary key on that table, you really never know what you're
191      * going to get, now do you?  But this method makes sure that that rascally database returns the origin
192      * entry group with the primary key of the given group id.  Or null if it can't find anything.  It works
193      * by magic.
194      * 
195      * @param id the id of the origin entry group to return
196      * @return an Origin Entry Group, or null if the exact one couldn't be found
197      * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getExactMatchingEntryGroup(java.lang.Integer)
198      */
199     public OriginEntryGroup getExactMatchingEntryGroup(Integer id) {
200         LOG.debug("getMatchingEntries() started");
201         return (OriginEntryGroup) getPersistenceBrokerTemplate().getObjectById(OriginEntryGroup.class, id);
202     }
203 
204     /**
205      * Given a date, finds all origin entry groups that were created on or after that date
206      * @param day the date that defines recency - all qualifying origin entries groups will have been created on or after that day
207      * @return a Collection of OriginEntryGroup records
208      * @see org.kuali.ole.gl.dataaccess.OriginEntryGroupDao#getRecentGroups(Date)
209      */
210     public Collection<OriginEntryGroup> getRecentGroups(Date day) {
211         LOG.debug("getOlderGroups() started");
212 
213         Criteria criteria = new Criteria();
214         criteria.addGreaterOrEqualThan(DATE, day);
215 
216         return getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(OriginEntryGroup.class, criteria));
217     }
218 
219 
220 }