1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.location.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.location.Location;
28 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29
30 public class LocationDaoSpringObjImpl extends PlatformAwareDaoBaseOjb implements LocationDao {
31
32 @Override
33 public Location getLocation(String location, Date asOfDate) {
34 Criteria root = new Criteria();
35 Criteria effdt = new Criteria();
36 Criteria timestamp = new Criteria();
37
38 effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
39
40
41 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
42
43 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Location.class, effdt);
44 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
45
46 timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
47
48
49 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
50
51 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Location.class, timestamp);
52 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
53
54 root.addEqualTo("location", location);
55 root.addEqualTo("effectiveDate", effdtSubQuery);
56 root.addEqualTo("timestamp", timestampSubQuery);
57
58 Criteria activeFilter = new Criteria();
59 activeFilter.addEqualTo("active", true);
60 root.addAndCriteria(activeFilter);
61
62
63 Query query = QueryFactory.newQuery(Location.class, root);
64
65 Location l = (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
66
67 return l;
68 }
69
70 @Override
71 public Location getLocation(String hrLocationId) {
72 Criteria crit = new Criteria();
73 crit.addEqualTo("hrLocationId", hrLocationId);
74
75 Query query = QueryFactory.newQuery(Location.class, crit);
76 return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
77 }
78
79 @Override
80 public int getLocationCount(String location) {
81 Criteria crit = new Criteria();
82 crit.addEqualTo("location", location);
83 Query query = QueryFactory.newQuery(Location.class, crit);
84 return this.getPersistenceBrokerTemplate().getCount(query);
85 }
86
87 @Override
88 @SuppressWarnings("unchecked")
89 public List<Location> searchLocations(String location, String locationDescr, String active, String showHistory) {
90 List<Location> results = new ArrayList<Location>();
91
92 Criteria root = new Criteria();
93
94 if (StringUtils.isNotBlank(location)) {
95 root.addLike("location", location);
96 }
97
98 if (StringUtils.isNotBlank(locationDescr)) {
99 root.addLike("description", locationDescr);
100 }
101
102 if (StringUtils.isNotBlank(active)) {
103 Criteria activeFilter = new Criteria();
104 if (StringUtils.equals(active, "Y")) {
105 activeFilter.addEqualTo("active", true);
106 } else if (StringUtils.equals(active, "N")) {
107 activeFilter.addEqualTo("active", false);
108 }
109 root.addAndCriteria(activeFilter);
110 }
111
112 Query query = QueryFactory.newQuery(Location.class, root);
113 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
114
115 return results;
116 }
117
118 }