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  		Location l = (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
51  		
52  		return l;
53  	}
54  
55  	@Override
56  	public Location getLocation(String hrLocationId) {
57  		Criteria crit = new Criteria();
58  		crit.addEqualTo("hrLocationId", hrLocationId);
59  		
60  		Query query = QueryFactory.newQuery(Location.class, crit);
61  		return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
62  	}
63  	
64  	@Override
65  	public int getLocationCount(String location) {
66  		Criteria crit = new Criteria();
67  		crit.addEqualTo("location", location);
68  		Query query = QueryFactory.newQuery(Location.class, crit);
69  		return this.getPersistenceBrokerTemplate().getCount(query);
70  	}
71  
72  	@Override
73      @SuppressWarnings("unchecked")
74      public List<Location> searchLocations(String location, String locationDescr, String active, String showHistory) {
75          List<Location> results = new ArrayList<Location>();
76      	
77          Criteria root = new Criteria();
78  
79          if (StringUtils.isNotBlank(location)) {
80              root.addLike("location", location);
81          }
82          
83          if (StringUtils.isNotBlank(locationDescr)) {
84              root.addLike("description", locationDescr);
85          }
86          
87          if (StringUtils.isNotBlank(active)) {
88          	Criteria activeFilter = new Criteria();
89              if (StringUtils.equals(active, "Y")) {
90                  activeFilter.addEqualTo("active", true);
91              } else if (StringUtils.equals(active, "N")) {
92                  activeFilter.addEqualTo("active", false);
93              }
94              root.addAndCriteria(activeFilter);
95          }
96          
97          Query query = QueryFactory.newQuery(Location.class, root);
98          results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
99          
100         return results;
101     }
102 
103 }