View Javadoc

1   /**
2    * Copyright 2004-2012 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.hr.time.earncode.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.hr.time.earncode.EarnCode;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class EarnCodeDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeDao {
32  
33  	@SuppressWarnings("unused")
34  	private static final Logger LOG = Logger.getLogger(EarnCodeDaoSpringOjbImpl.class);
35  
36  	public void saveOrUpdate(EarnCode earnCode) {
37  		this.getPersistenceBrokerTemplate().store(earnCode);
38  	}
39  
40  	public void saveOrUpdate(List<EarnCode> earnCodeList) {
41  		if (earnCodeList != null) {
42  			for (EarnCode earnCode : earnCodeList) {
43  				this.getPersistenceBrokerTemplate().store(earnCode);
44  			}
45  		}
46  	}
47  
48  	public EarnCode getEarnCodeById(String earnCodeId) {
49  		Criteria crit = new Criteria();
50  		crit.addEqualTo("hrEarnCodeId", earnCodeId);
51  		return (EarnCode) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(EarnCode.class, crit));
52  	}
53  
54  	@Override
55  	public EarnCode getEarnCode(String earnCode, Date asOfDate) {
56  		EarnCode ec = null;
57  
58  		Criteria root = new Criteria();
59  		Criteria effdt = new Criteria();
60  		Criteria timestamp = new Criteria();
61  
62  		// OJB's awesome sub query setup part 1
63  		effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
64  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
65  //		effdt.addEqualTo("active", true);
66  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCode.class, effdt);
67  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
68  
69  		// OJB's awesome sub query setup part 2
70  		timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
71  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
72  //		timestamp.addEqualTo("active", true);
73  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCode.class, timestamp);
74  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
75  
76  		root.addEqualTo("earnCode", earnCode);
77  		root.addEqualTo("effectiveDate", effdtSubQuery);
78  		root.addEqualTo("timestamp", timestampSubQuery);
79  //		root.addEqualTo("active", true);
80  		
81  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
82  		activeFilter.addEqualTo("active", true);
83  		root.addAndCriteria(activeFilter);
84  		
85  		
86  		Query query = QueryFactory.newQuery(EarnCode.class, root);
87  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
88  
89  		if (obj != null) {
90  			ec = (EarnCode) obj;
91  		}
92  
93  		return ec;
94  	}
95  
96  	@Override
97  	public List<EarnCode> getOvertimeEarnCodes(Date asOfDate) {
98  		Criteria root = new Criteria();
99  		Criteria effdt = new Criteria();
100 		Criteria timestamp = new Criteria();
101 
102 		// OJB's awesome sub query setup part 1
103 		effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
104 		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
105 //		effdt.addEqualTo("active", true);
106 		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCode.class, effdt);
107 		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
108 
109 		// OJB's awesome sub query setup part 2
110 		timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
111 		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
112 //		timestamp.addEqualTo("active", true);
113 		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCode.class, timestamp);
114 		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
115 
116 		root.addEqualTo("ovtEarnCode", "Y");
117 		root.addEqualTo("effectiveDate", effdtSubQuery);
118 		root.addEqualTo("timestamp", timestampSubQuery);
119 //		root.addEqualTo("active", true);
120 		
121 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
122 		activeFilter.addEqualTo("active", true);
123 		root.addAndCriteria(activeFilter);
124 		
125 		
126 		Query query = QueryFactory.newQuery(EarnCode.class, root);
127 		List<EarnCode> ovtEarnCodes = (List<EarnCode>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
128 		return ovtEarnCodes;
129 	}
130 	
131 	@Override
132 	public int getEarnCodeCount(String earnCode) {
133 		Criteria crit = new Criteria();
134 		crit.addEqualTo("earnCode", earnCode);
135 		Query query = QueryFactory.newQuery(EarnCode.class, crit);
136 		return this.getPersistenceBrokerTemplate().getCount(query);
137 	}
138 	
139 	@Override
140 	public int getNewerEarnCodeCount(String earnCode, Date effdt) {
141 		Criteria crit = new Criteria();
142 		crit.addEqualTo("earnCode", earnCode);
143 		crit.addEqualTo("active", "Y");
144 		crit.addGreaterThan("effectiveDate", effdt);
145 		Query query = QueryFactory.newQuery(EarnCode.class, crit);
146        	return this.getPersistenceBrokerTemplate().getCount(query);
147 	}
148 }