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 com.google.common.collect.ImmutableList;
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.hr.core.util.OjbSubQueryUtil;
29  import org.kuali.hr.location.Location;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  
32  public class LocationDaoSpringObjImpl extends PlatformAwareDaoBaseOjb implements LocationDao {
33      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
34              .add("location")
35              .build();
36  	@Override
37  	public Location getLocation(String location, Date asOfDate) {
38  		Criteria root = new Criteria();
39  
40  		root.addEqualTo("location", location);
41          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Location.class, new java.sql.Date(asOfDate.getTime()), EQUAL_TO_FIELDS, false));
42          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Location.class, EQUAL_TO_FIELDS, false));
43  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
44  		activeFilter.addEqualTo("active", true);
45  		root.addAndCriteria(activeFilter);
46  
47  		
48  		Query query = QueryFactory.newQuery(Location.class, root);
49  		
50  		return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
51  	}
52  
53  	@Override
54  	public Location getLocation(String hrLocationId) {
55  		Criteria crit = new Criteria();
56  		crit.addEqualTo("hrLocationId", hrLocationId);
57  		
58  		Query query = QueryFactory.newQuery(Location.class, crit);
59  		return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
60  	}
61  	
62  	@Override
63  	public int getLocationCount(String location) {
64  		Criteria crit = new Criteria();
65  		crit.addEqualTo("location", location);
66  		Query query = QueryFactory.newQuery(Location.class, crit);
67  		return this.getPersistenceBrokerTemplate().getCount(query);
68  	}
69  
70  	@Override
71      @SuppressWarnings("unchecked")
72      public List<Location> searchLocations(String location, String locationDescr, String active, String showHistory) {
73          List<Location> results = new ArrayList<Location>();
74      	
75          Criteria root = new Criteria();
76  
77          if (StringUtils.isNotBlank(location)) {
78              root.addLike("location", location);
79          }
80          
81          if (StringUtils.isNotBlank(locationDescr)) {
82              root.addLike("description", locationDescr);
83          }
84          
85          if (StringUtils.isNotBlank(active)) {
86          	Criteria activeFilter = new Criteria();
87              if (StringUtils.equals(active, "Y")) {
88                  activeFilter.addEqualTo("active", true);
89              } else if (StringUtils.equals(active, "N")) {
90                  activeFilter.addEqualTo("active", false);
91              }
92              root.addAndCriteria(activeFilter);
93          }
94  
95          if (StringUtils.equals(showHistory, "N")) {
96              root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Location.class, EQUAL_TO_FIELDS, false));
97              root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Location.class, EQUAL_TO_FIELDS, false));
98          }
99          
100         Query query = QueryFactory.newQuery(Location.class, root);
101         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
102         
103         return results;
104     }
105 
106 }