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.Collection;
21 import java.util.List;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.ojb.broker.query.Criteria;
26 import org.apache.ojb.broker.query.Query;
27 import org.apache.ojb.broker.query.QueryFactory;
28 import org.apache.ojb.broker.query.ReportQueryByCriteria;
29 import org.kuali.hr.time.clock.location.ClockLocationRule;
30 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
31 import org.kuali.hr.time.service.base.TkServiceLocator;
32 import org.kuali.hr.time.util.TKUtils;
33 import org.kuali.hr.time.workarea.WorkArea;
34 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
35
36 public class ClockLocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLocationDao{
37 @SuppressWarnings("unchecked")
38 public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea, String principalId, Long jobNumber, Date asOfDate){
39 Criteria root = new Criteria();
40 Criteria effdt = new Criteria();
41 Criteria timestamp = new Criteria();
42
43 effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
44 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
45 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
46 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
47 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
48
49 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, effdt);
50 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
51
52 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
53 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
54 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
55 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
56 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
57
58 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, timestamp);
59 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
60
61 root.addEqualTo("dept", dept);
62 root.addEqualTo("workArea", workArea);
63 root.addEqualTo("principalId", principalId);
64 root.addEqualTo("jobNumber", jobNumber);
65 root.addEqualTo("effectiveDate", effdtSubQuery);
66 root.addEqualTo("timestamp", timestampSubQuery);
67
68
69 Criteria activeFilter = new Criteria();
70 activeFilter.addEqualTo("active", true);
71 root.addAndCriteria(activeFilter);
72
73 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
74 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
75 if(clockLocationRules==null){
76 clockLocationRules = new ArrayList<ClockLocationRule>();
77 }
78 for(ClockLocationRule clr : clockLocationRules ) {
79 this.populateIPAddressesForCLR(clr);
80 }
81 return clockLocationRules;
82 }
83
84 @SuppressWarnings("unchecked")
85 @Override
86 public List<ClockLocationRule> getNewerVersionClockLocationRule(
87 String dept, Long workArea, String principalId, Long jobNumber,
88 Date asOfDate) {
89 Criteria root = new Criteria();
90 root.addEqualTo("dept", dept);
91 root.addEqualTo("workArea", workArea);
92 root.addEqualTo("principalId", principalId);
93 root.addEqualTo("jobNumber", jobNumber);
94 root.addGreaterThan("effectiveDate", asOfDate);
95
96 Criteria activeFilter = new Criteria();
97 activeFilter.addEqualTo("active", true);
98 root.addAndCriteria(activeFilter);
99
100 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
101 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
102 if(clockLocationRules==null){
103 clockLocationRules = new ArrayList<ClockLocationRule>();
104 }
105 for(ClockLocationRule clr : clockLocationRules ) {
106 this.populateIPAddressesForCLR(clr);
107 }
108 return clockLocationRules;
109 }
110
111 public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId){
112 Criteria criteria = new Criteria();
113 criteria.addEqualTo("tkClockLocationRuleId", tkClockLocationRuleId);
114 ClockLocationRule clr = (ClockLocationRule) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(
115 ClockLocationRule.class, criteria));
116 if(clr != null) {
117 this.populateIPAddressesForCLR(clr);
118 }
119 return clr;
120 }
121
122
123 @SuppressWarnings("unchecked")
124 public void populateIPAddressesForCLR(ClockLocationRule clr) {
125 if(clr.getTkClockLocationRuleId() == null) {
126 return;
127 }
128 Criteria root = new Criteria();
129 root.addEqualTo("tkClockLocationRuleId", clr.getTkClockLocationRuleId().toString());
130 Query query = QueryFactory.newQuery(ClockLocationRuleIpAddress.class, root);
131 List<ClockLocationRuleIpAddress> ipAddresses = (List<ClockLocationRuleIpAddress>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
132 clr.setIpAddresses(ipAddresses);
133 }
134
135 @Override
136 @SuppressWarnings("unchecked")
137 public List<ClockLocationRule> getClockLocationRules(Date fromEffdt, Date toEffdt, String principalId, String jobNumber, String dept, String workArea,
138 String active, String showHistory) {
139
140 List<ClockLocationRule> results = new ArrayList<ClockLocationRule>();
141
142 Criteria root = new Criteria();
143
144 Criteria effectiveDateFilter = new Criteria();
145 if (fromEffdt != null) {
146 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
147 }
148 if (toEffdt != null) {
149 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
150 }
151 if (fromEffdt == null && toEffdt == null) {
152 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
153 }
154 root.addAndCriteria(effectiveDateFilter);
155
156 if (StringUtils.isNotBlank(principalId)) {
157 root.addLike("principalId", principalId);
158 }
159
160 if (StringUtils.isNotBlank(dept)) {
161 root.addLike("dept", dept);
162 }
163
164 if (StringUtils.isNotBlank(jobNumber)) {
165 root.addLike("jobNumber", jobNumber);
166 }
167
168 if (StringUtils.isNotBlank(dept)) {
169 Criteria workAreaCriteria = new Criteria();
170 Date asOfDate = toEffdt != null ? toEffdt : TKUtils.getCurrentDate();
171 Collection<WorkArea> workAreasForDept = TkServiceLocator.getWorkAreaService().getWorkAreas(dept,asOfDate);
172 if (CollectionUtils.isNotEmpty(workAreasForDept)) {
173 List<Long> longWorkAreas = new ArrayList<Long>();
174 for(WorkArea cwa : workAreasForDept){
175 longWorkAreas.add(cwa.getWorkArea());
176 }
177 workAreaCriteria.addIn("workArea", longWorkAreas);
178 }
179 root.addAndCriteria(workAreaCriteria);
180 }
181
182 if (StringUtils.isNotBlank(workArea)) {
183 root.addLike("workArea", workArea);
184 }
185
186 if (StringUtils.isNotBlank(active)) {
187 Criteria activeFilter = new Criteria();
188 if (StringUtils.equals(active, "Y")) {
189 activeFilter.addEqualTo("active", true);
190 } else if (StringUtils.equals(active, "N")) {
191 activeFilter.addEqualTo("active", false);
192 }
193 root.addAndCriteria(activeFilter);
194 }
195
196 if (StringUtils.equals(showHistory, "N")) {
197 Criteria effdt = new Criteria();
198 effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
199 effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
200 effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
201 effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
202 effdt.addAndCriteria(effectiveDateFilter);
203 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, effdt);
204 effdtSubQuery.setAttributes(new String[] { "max(effectiveDate)" });
205 root.addEqualTo("effectiveDate", effdtSubQuery);
206
207 Criteria timestamp = new Criteria();
208 timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
209 timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
210 timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
211 timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
212 timestamp.addAndCriteria(effectiveDateFilter);
213 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, timestamp);
214 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
215 root.addEqualTo("timestamp", timestampSubQuery);
216 }
217
218 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
219 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
220
221 return results;
222 }
223
224 }