1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.syslunch.dao;
17
18 import java.sql.Date;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.ojb.broker.query.Criteria;
24 import org.apache.ojb.broker.query.Query;
25 import org.apache.ojb.broker.query.QueryFactory;
26 import org.apache.ojb.broker.query.ReportQueryByCriteria;
27 import org.kuali.hr.time.syslunch.rule.SystemLunchRule;
28 import org.kuali.hr.time.util.TKUtils;
29 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30
31 public class SystemLunchRuleDaoImpl extends PlatformAwareDaoBaseOjb implements SystemLunchRuleDao {
32
33 @Override
34 public SystemLunchRule getSystemLunchRule(Date asOfDate) {
35 Criteria root = new Criteria();
36 Criteria effdt = new Criteria();
37 Criteria timestamp = new Criteria();
38
39
40 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
41 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, effdt);
42 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
43
44
45 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
46 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, timestamp);
47 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
48
49 root.addEqualTo("effectiveDate", effdtSubQuery);
50 root.addEqualTo("timestamp", timestampSubQuery);
51
52 Criteria activeFilter = new Criteria();
53 activeFilter.addEqualTo("active", true);
54 root.addAndCriteria(activeFilter);
55
56 Query query = QueryFactory.newQuery(SystemLunchRule.class, root);
57 return (SystemLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
58 }
59
60 @Override
61 public SystemLunchRule getSystemLunchRule(String tkSystemLunchRuleId) {
62 Criteria crit = new Criteria();
63 crit.addEqualTo("tkSystemLunchRuleId", tkSystemLunchRuleId);
64
65 Query query = QueryFactory.newQuery(SystemLunchRule.class, crit);
66
67 return (SystemLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
68 }
69
70 @Override
71 @SuppressWarnings("unchecked")
72 public List<SystemLunchRule> getSystemLunchRules(Date fromEffdt, Date toEffdt, String active, String showHistory) {
73 List<SystemLunchRule> results = new ArrayList<SystemLunchRule>();
74
75 Criteria root = new Criteria();
76
77 Criteria effectiveDateFilter = new Criteria();
78 if (fromEffdt != null) {
79 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
80 }
81 if (toEffdt != null) {
82 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
83 }
84 if (fromEffdt == null && toEffdt == null) {
85 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
86 }
87 root.addAndCriteria(effectiveDateFilter);
88
89 if (StringUtils.isNotBlank(active)) {
90 Criteria activeFilter = new Criteria();
91 if (StringUtils.equals(active, "Y")) {
92 activeFilter.addEqualTo("active", true);
93 } else if (StringUtils.equals(active, "N")) {
94 activeFilter.addEqualTo("active", false);
95 }
96 root.addAndCriteria(activeFilter);
97 }
98
99 if (StringUtils.equals(showHistory, "N")) {
100 Criteria effdt = new Criteria();
101 effdt.addAndCriteria(effectiveDateFilter);
102 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, effdt);
103 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
104 root.addEqualTo("effectiveDate", effdtSubQuery);
105
106 Criteria timestamp = new Criteria();
107 timestamp.addAndCriteria(effectiveDateFilter);
108 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, timestamp);
109 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
110 root.addEqualTo("timestamp", timestampSubQuery);
111 }
112
113 Query query = QueryFactory.newQuery(SystemLunchRule.class, root);
114 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
115
116 return results;
117 }
118
119 }