View Javadoc

1   /**
2    * Copyright 2004-2014 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.kpme.core.location.dao;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.ojb.broker.query.Criteria;
23  import org.apache.ojb.broker.query.Query;
24  import org.apache.ojb.broker.query.QueryFactory;
25  import org.joda.time.LocalDate;
26  import org.kuali.kpme.core.location.Location;
27  import org.kuali.kpme.core.util.OjbSubQueryUtil;
28  import org.kuali.kpme.core.util.ValidationUtils;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class LocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements LocationDao {
32     
33  	@Override
34  	public Location getLocation(String location, LocalDate asOfDate) {
35  		Criteria root = new Criteria();
36  		root.addEqualTo("location", location);
37          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Location.class, asOfDate, Location.EQUAL_TO_FIELDS, false));
38          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Location.class, Location.EQUAL_TO_FIELDS, false));
39  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
40  		activeFilter.addEqualTo("active", true);
41  		root.addAndCriteria(activeFilter);
42  
43  		Query query = QueryFactory.newQuery(Location.class, root);
44  		
45  		return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
46  	}
47  
48  	@Override
49  	public Location getLocation(String hrLocationId) {
50  		Criteria crit = new Criteria();
51  		crit.addEqualTo("hrLocationId", hrLocationId);
52  		
53  		Query query = QueryFactory.newQuery(Location.class, crit);
54  		return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
55  	}
56  	
57  	@Override
58  	public int getLocationCount(String location,  LocalDate asOfDate) {
59  		Criteria crit = new Criteria();
60  		// allow wild card
61  		if(StringUtils.isNotEmpty(location) && !ValidationUtils.isWildCard(location)) {
62  			crit.addEqualTo("location", location);
63  		}
64  		crit.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Location.class, asOfDate, Location.EQUAL_TO_FIELDS, false));
65  		crit.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Location.class, Location.EQUAL_TO_FIELDS, false));
66  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
67  		activeFilter.addEqualTo("active", true);
68  		crit.addAndCriteria(activeFilter);
69  		Query query = QueryFactory.newQuery(Location.class, crit);
70  		return this.getPersistenceBrokerTemplate().getCount(query);
71  	}
72  
73  	@Override
74      @SuppressWarnings("unchecked")
75      public List<Location> searchLocations(String location, String locationDescr, String active, String showHistory) {
76          List<Location> results = new ArrayList<Location>();
77      	
78          Criteria root = new Criteria();
79          // ignore cases for location
80          if (StringUtils.isNotBlank(location)) {
81              root.addLike("UPPER(location)", location.toUpperCase());
82          }
83          
84          if (StringUtils.isNotBlank(locationDescr)) {
85              root.addLike("UPPER(description)", locationDescr.toUpperCase()); // KPME-2695
86          }
87          
88          if (StringUtils.isNotBlank(active)) {
89          	Criteria activeFilter = new Criteria();
90              if (StringUtils.equals(active, "Y")) {
91                  activeFilter.addEqualTo("active", true);
92              } else if (StringUtils.equals(active, "N")) {
93                  activeFilter.addEqualTo("active", false);
94              }
95              root.addAndCriteria(activeFilter);
96          }
97  
98          if (StringUtils.equals(showHistory, "N")) {
99              root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Location.class, Location.EQUAL_TO_FIELDS, false));
100             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Location.class, Location.EQUAL_TO_FIELDS, false));
101         }
102         
103         Query query = QueryFactory.newQuery(Location.class, root);
104         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
105         
106         return results;
107     }
108 
109     @Override
110     public List<Location> getNewerVersionLocation(String location, LocalDate asOfDate) {
111         Criteria root = new Criteria();
112         root.addEqualTo("location", location);
113         root.addGreaterThan("effectiveDate", asOfDate.toDate());
114 
115         Criteria activeFilter = new Criteria(); //Inner join for Activity
116         activeFilter.addEqualTo("active", true);
117         root.addAndCriteria(activeFilter);
118 
119         Query query = QueryFactory.newQuery(Location.class, root);
120         List<Location> locations = (List<Location>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
121         if(locations == null) {
122             locations = new ArrayList<Location>();
123         }
124 
125         return locations;
126 
127     }
128 
129     @Override
130     public List<Location> getLocations(String location) {
131         List<Location> results = new ArrayList<Location>();
132         Criteria crit = new Criteria();
133         crit.addEqualTo("location", location);
134         crit.addEqualTo("active", true);
135         Query query = QueryFactory.newQuery(Location.class, crit);
136         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
137         return results;
138     }
139 }