1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.shiftdiff.rule.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.commons.lang.StringUtils;
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.shiftdiff.rule.ShiftDifferentialRule;
29 import org.kuali.hr.time.util.TKUtils;
30 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31
32 public class ShiftDifferentialRuleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ShiftDifferentialRuleDao {
33
34 @Override
35 public ShiftDifferentialRule findShiftDifferentialRule(String id) {
36 Object o = this.getPersistenceBrokerTemplate().getObjectById(ShiftDifferentialRule.class, id);
37
38 return (ShiftDifferentialRule)o;
39 }
40
41 @SuppressWarnings("unchecked")
42 @Override
43 public List<ShiftDifferentialRule> findShiftDifferentialRules(String location, String hrSalGroup, String payGrade, String pyCalendarGroup, Date asOfDate) {
44 List<ShiftDifferentialRule> list = new ArrayList<ShiftDifferentialRule>();
45
46 Criteria root = new Criteria();
47 Criteria effdt = new Criteria();
48 Criteria timestamp = new Criteria();
49
50 effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
51 effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
52 effdt.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
53 effdt.addEqualToField("pyCalendarGroup", Criteria.PARENT_QUERY_PREFIX + "pyCalendarGroup");
54 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
55 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, effdt);
56 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
57
58 timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
59 timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
60 timestamp.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
61 timestamp.addEqualToField("pyCalendarGroup", Criteria.PARENT_QUERY_PREFIX + "pyCalendarGroup");
62 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
63 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, timestamp);
64 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
65
66 root.addEqualTo("location", location);
67 root.addEqualTo("hrSalGroup", hrSalGroup);
68 root.addEqualTo("payGrade", payGrade);
69 root.addEqualTo("pyCalendarGroup", pyCalendarGroup);
70 root.addEqualTo("effectiveDate", effdtSubQuery);
71 root.addEqualTo("timestamp", timestampSubQuery);
72
73 Criteria activeFilter = new Criteria();
74 activeFilter.addEqualTo("active", true);
75 root.addAndCriteria(activeFilter);
76
77 Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
78 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
79
80 if (c != null) {
81 list.addAll(c);
82 }
83
84 return list;
85 }
86
87 public void saveOrUpdate(ShiftDifferentialRule shiftDifferentialRule) {
88 this.getPersistenceBrokerTemplate().store(shiftDifferentialRule);
89 }
90
91 public void saveOrUpdate(List<ShiftDifferentialRule> shiftDifferentialRules) {
92 for (ShiftDifferentialRule sdr : shiftDifferentialRules) {
93 saveOrUpdate(sdr);
94 }
95 }
96
97 @Override
98 @SuppressWarnings("unchecked")
99 public List<ShiftDifferentialRule> getShiftDifferentialRules(String location, String hrSalGroup, String payGrade, Date fromEffdt, Date toEffdt,
100 String active, String showHistory) {
101
102 List<ShiftDifferentialRule> results = new ArrayList<ShiftDifferentialRule>();
103
104 Criteria root = new Criteria();
105
106 if (StringUtils.isNotBlank(location)) {
107 root.addLike("location", location);
108 }
109
110 if (StringUtils.isNotBlank(hrSalGroup)) {
111 root.addLike("hrSalGroup", hrSalGroup);
112 }
113
114 if (StringUtils.isNotBlank(payGrade)) {
115 root.addLike("payGrade", payGrade);
116 }
117
118 Criteria effectiveDateFilter = new Criteria();
119 if (fromEffdt != null) {
120 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
121 }
122 if (toEffdt != null) {
123 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
124 }
125 if (fromEffdt == null && toEffdt == null) {
126 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
127 }
128 root.addAndCriteria(effectiveDateFilter);
129
130 if (StringUtils.isNotBlank(active)) {
131 Criteria activeFilter = new Criteria();
132 if (StringUtils.equals(active, "Y")) {
133 activeFilter.addEqualTo("active", true);
134 } else if (StringUtils.equals(active, "N")) {
135 activeFilter.addEqualTo("active", false);
136 }
137 root.addAndCriteria(activeFilter);
138 }
139
140 if (StringUtils.equals(showHistory, "N")) {
141 Criteria effdt = new Criteria();
142 effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
143 effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
144 effdt.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
145 effdt.addAndCriteria(effectiveDateFilter);
146 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, effdt);
147 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
148
149 Criteria timestamp = new Criteria();
150 timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
151 timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
152 timestamp.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
153 timestamp.addAndCriteria(effectiveDateFilter);
154 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, timestamp);
155 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
156 }
157
158 Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
159 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
160
161 return results;
162 }
163
164 }