View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  //		effdt.addEqualToField("org", Criteria.PARENT_QUERY_PREFIX + "org");
40  //		effdt.addEqualToField("chart", Criteria.PARENT_QUERY_PREFIX + "chart");
41  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
42  //		effdt.addEqualTo("active", true);
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  //		timestamp.addEqualToField("org", Criteria.PARENT_QUERY_PREFIX + "org");
48  //		timestamp.addEqualToField("chart", Criteria.PARENT_QUERY_PREFIX + "chart");
49  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
50  //		timestamp.addEqualTo("active", true);
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  //		root.addEqualTo("active", true);
58  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
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 }