1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.clock.location.dao;
17
18 import java.sql.Date;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.ojb.broker.query.Criteria;
23 import org.apache.ojb.broker.query.Query;
24 import org.apache.ojb.broker.query.QueryFactory;
25 import org.apache.ojb.broker.query.ReportQueryByCriteria;
26 import org.kuali.hr.time.clock.location.ClockLocationRule;
27 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
28 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29
30 public class ClockLocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLocationDao{
31 @SuppressWarnings("unchecked")
32 public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea, String principalId, Long jobNumber, Date asOfDate){
33 Criteria root = new Criteria();
34 Criteria effdt = new Criteria();
35 Criteria timestamp = new Criteria();
36
37 effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
38 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
39 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
40 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
41 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
42
43 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, effdt);
44 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
45
46 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
47 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
48 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
49 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
50 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
51
52 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, timestamp);
53 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
54
55 root.addEqualTo("dept", dept);
56 root.addEqualTo("workArea", workArea);
57 root.addEqualTo("principalId", principalId);
58 root.addEqualTo("jobNumber", jobNumber);
59 root.addEqualTo("effectiveDate", effdtSubQuery);
60 root.addEqualTo("timestamp", timestampSubQuery);
61
62
63 Criteria activeFilter = new Criteria();
64 activeFilter.addEqualTo("active", true);
65 root.addAndCriteria(activeFilter);
66
67 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
68 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
69 if(clockLocationRules==null){
70 clockLocationRules = new ArrayList<ClockLocationRule>();
71 }
72 for(ClockLocationRule clr : clockLocationRules ) {
73 this.populateIPAddressesForCLR(clr);
74 }
75 return clockLocationRules;
76 }
77
78 @SuppressWarnings("unchecked")
79 @Override
80 public List<ClockLocationRule> getNewerVersionClockLocationRule(
81 String dept, Long workArea, String principalId, Long jobNumber,
82 Date asOfDate) {
83 Criteria root = new Criteria();
84 root.addEqualTo("dept", dept);
85 root.addEqualTo("workArea", workArea);
86 root.addEqualTo("principalId", principalId);
87 root.addEqualTo("jobNumber", jobNumber);
88 root.addGreaterThan("effectiveDate", asOfDate);
89
90 Criteria activeFilter = new Criteria();
91 activeFilter.addEqualTo("active", true);
92 root.addAndCriteria(activeFilter);
93
94 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
95 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
96 if(clockLocationRules==null){
97 clockLocationRules = new ArrayList<ClockLocationRule>();
98 }
99 for(ClockLocationRule clr : clockLocationRules ) {
100 this.populateIPAddressesForCLR(clr);
101 }
102 return clockLocationRules;
103 }
104
105 public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId){
106 Criteria criteria = new Criteria();
107 criteria.addEqualTo("tkClockLocationRuleId", tkClockLocationRuleId);
108 ClockLocationRule clr = (ClockLocationRule) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(
109 ClockLocationRule.class, criteria));
110 if(clr != null) {
111 this.populateIPAddressesForCLR(clr);
112 }
113 return clr;
114 }
115
116
117 @SuppressWarnings("unchecked")
118 public void populateIPAddressesForCLR(ClockLocationRule clr) {
119 if(clr.getTkClockLocationRuleId() == null) {
120 return;
121 }
122 Criteria root = new Criteria();
123 root.addEqualTo("tkClockLocationRuleId", clr.getTkClockLocationRuleId().toString());
124 Query query = QueryFactory.newQuery(ClockLocationRuleIpAddress.class, root);
125 List<ClockLocationRuleIpAddress> ipAddresses = (List<ClockLocationRuleIpAddress>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
126 clr.setIpAddresses(ipAddresses);
127 }
128
129 }