1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.rules.clocklocation.dao;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.commons.collections.CollectionUtils;
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.joda.time.LocalDate;
27 import org.kuali.kpme.core.service.HrServiceLocator;
28 import org.kuali.kpme.core.util.OjbSubQueryUtil;
29 import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRule;
30 import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRuleIpAddress;
31 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32
33 public class ClockLocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLocationDao{
34 public List<ClockLocationRule> getClockLocationRule(String groupKeyCode, String dept, Long workArea, String principalId, Long jobNumber, LocalDate asOfDate){
35 Criteria root = new Criteria();
36
37 root.addEqualTo("groupKeyCode", groupKeyCode);
38 root.addEqualTo("dept", dept);
39 root.addEqualTo("workArea", workArea);
40 root.addEqualTo("principalId", principalId);
41 root.addEqualTo("jobNumber", jobNumber);
42 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(ClockLocationRule.class, asOfDate, ClockLocationRule.BUSINESS_KEYS, false));
43 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ClockLocationRule.class, ClockLocationRule.BUSINESS_KEYS, false));
44
45
46 Criteria activeFilter = new Criteria();
47 activeFilter.addEqualTo("active", true);
48 root.addAndCriteria(activeFilter);
49
50 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
51 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
52 if(clockLocationRules==null){
53 clockLocationRules = new ArrayList<ClockLocationRule>();
54 }
55 for(ClockLocationRule clr : clockLocationRules ) {
56 this.populateIPAddressesForCLR(clr);
57 }
58 return clockLocationRules;
59 }
60
61 @SuppressWarnings("unchecked")
62 @Override
63 public List<ClockLocationRule> getNewerVersionClockLocationRule(String groupKeyCode,
64 String dept, Long workArea, String principalId, Long jobNumber,
65 LocalDate asOfDate) {
66 Criteria root = new Criteria();
67
68 root.addEqualTo("groupKeyCode", groupKeyCode);
69 root.addEqualTo("dept", dept);
70 root.addEqualTo("workArea", workArea);
71 root.addEqualTo("principalId", principalId);
72 root.addEqualTo("jobNumber", jobNumber);
73 root.addGreaterThan("effectiveDate", asOfDate.toDate());
74
75 Criteria activeFilter = new Criteria();
76 activeFilter.addEqualTo("active", true);
77 root.addAndCriteria(activeFilter);
78
79 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
80 List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
81 if(clockLocationRules==null){
82 clockLocationRules = new ArrayList<ClockLocationRule>();
83 }
84 for(ClockLocationRule clr : clockLocationRules ) {
85 this.populateIPAddressesForCLR(clr);
86 }
87 return clockLocationRules;
88 }
89
90 public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId){
91 Criteria criteria = new Criteria();
92 criteria.addEqualTo("tkClockLocationRuleId", tkClockLocationRuleId);
93 ClockLocationRule clr = (ClockLocationRule) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(
94 ClockLocationRule.class, criteria));
95 if(clr != null) {
96 this.populateIPAddressesForCLR(clr);
97 }
98 return clr;
99 }
100
101
102 @SuppressWarnings("unchecked")
103 public void populateIPAddressesForCLR(ClockLocationRule clr) {
104 if(clr.getTkClockLocationRuleId() == null) {
105 return;
106 }
107 Criteria root = new Criteria();
108 root.addEqualTo("tkClockLocationRuleId", clr.getTkClockLocationRuleId().toString());
109 Query query = QueryFactory.newQuery(ClockLocationRuleIpAddress.class, root);
110 List<ClockLocationRuleIpAddress> ipAddresses = (List<ClockLocationRuleIpAddress>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
111 clr.setIpAddresses(ipAddresses);
112 }
113
114 @Override
115 @SuppressWarnings("unchecked")
116 public List<ClockLocationRule> getClockLocationRules(String groupKeyCode, LocalDate fromEffdt, LocalDate toEffdt, String principalId, String jobNumber, String dept, String workArea,
117 String active, String showHistory) {
118
119 List<ClockLocationRule> results = new ArrayList<ClockLocationRule>();
120
121 Criteria root = new Criteria();
122
123 Criteria effectiveDateFilter = new Criteria();
124 if (fromEffdt != null) {
125 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
126 }
127 if (toEffdt != null) {
128 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
129 }
130 if (fromEffdt == null && toEffdt == null) {
131 effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
132 }
133 root.addAndCriteria(effectiveDateFilter);
134
135 if (StringUtils.isNotBlank(principalId)) {
136 root.addLike("UPPER(principalId)", principalId.toUpperCase());
137 }
138
139 if (StringUtils.isNotBlank(dept)) {
140 root.addLike("UPPER(dept)", dept.toUpperCase());
141 }
142
143 if (StringUtils.isNotBlank(groupKeyCode)) {
144 root.addLike("groupKeyCode", groupKeyCode);
145 }
146
147 if (StringUtils.isNotBlank(jobNumber)) {
148 OjbSubQueryUtil.addNumericCriteria(root, "jobNumber", jobNumber);
149 }
150
151 if (StringUtils.isNotBlank(dept)
152 && StringUtils.isBlank(workArea)) {
153 Criteria workAreaCriteria = new Criteria();
154 LocalDate asOfDate = toEffdt != null ? toEffdt : LocalDate.now();
155 List<Long> workAreasForDept = HrServiceLocator.getWorkAreaService().getWorkAreasForDepartment(dept, asOfDate);
156 workAreasForDept.add(-1L);
157 if (CollectionUtils.isNotEmpty(workAreasForDept)) {
158 workAreaCriteria.addIn("workArea", workAreasForDept);
159 }
160 root.addAndCriteria(workAreaCriteria);
161 }
162
163 if (StringUtils.isNotBlank(workArea)) {
164 OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
165 }
166
167 if (StringUtils.isNotBlank(active)) {
168 Criteria activeFilter = new Criteria();
169 if (StringUtils.equals(active, "Y")) {
170 activeFilter.addEqualTo("active", true);
171 } else if (StringUtils.equals(active, "N")) {
172 activeFilter.addEqualTo("active", false);
173 }
174 root.addAndCriteria(activeFilter);
175 }
176
177 if (StringUtils.equals(showHistory, "N")) {
178 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(ClockLocationRule.class, effectiveDateFilter, ClockLocationRule.BUSINESS_KEYS, false));
179 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ClockLocationRule.class, ClockLocationRule.BUSINESS_KEYS, false));
180 }
181
182 Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
183 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
184
185 return results;
186 }
187
188 }