001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.location.dao;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import com.google.common.collect.ImmutableList;
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.ojb.broker.query.Criteria;
025    import org.apache.ojb.broker.query.Query;
026    import org.apache.ojb.broker.query.QueryFactory;
027    import org.apache.ojb.broker.query.ReportQueryByCriteria;
028    import org.kuali.hr.core.util.OjbSubQueryUtil;
029    import org.kuali.hr.location.Location;
030    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
031    
032    public class LocationDaoSpringObjImpl extends PlatformAwareDaoBaseOjb implements LocationDao {
033        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
034                .add("location")
035                .build();
036            @Override
037            public Location getLocation(String location, Date asOfDate) {
038                    Criteria root = new Criteria();
039    
040                    root.addEqualTo("location", location);
041            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Location.class, new java.sql.Date(asOfDate.getTime()), EQUAL_TO_FIELDS, false));
042            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Location.class, EQUAL_TO_FIELDS, false));
043                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
044                    activeFilter.addEqualTo("active", true);
045                    root.addAndCriteria(activeFilter);
046    
047                    
048                    Query query = QueryFactory.newQuery(Location.class, root);
049                    
050                    return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
051            }
052    
053            @Override
054            public Location getLocation(String hrLocationId) {
055                    Criteria crit = new Criteria();
056                    crit.addEqualTo("hrLocationId", hrLocationId);
057                    
058                    Query query = QueryFactory.newQuery(Location.class, crit);
059                    return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
060            }
061            
062            @Override
063            public int getLocationCount(String location) {
064                    Criteria crit = new Criteria();
065                    crit.addEqualTo("location", location);
066                    Query query = QueryFactory.newQuery(Location.class, crit);
067                    return this.getPersistenceBrokerTemplate().getCount(query);
068            }
069    
070            @Override
071        @SuppressWarnings("unchecked")
072        public List<Location> searchLocations(String location, String locationDescr, String active, String showHistory) {
073            List<Location> results = new ArrayList<Location>();
074            
075            Criteria root = new Criteria();
076    
077            if (StringUtils.isNotBlank(location)) {
078                root.addLike("location", location);
079            }
080            
081            if (StringUtils.isNotBlank(locationDescr)) {
082                root.addLike("description", locationDescr);
083            }
084            
085            if (StringUtils.isNotBlank(active)) {
086                    Criteria activeFilter = new Criteria();
087                if (StringUtils.equals(active, "Y")) {
088                    activeFilter.addEqualTo("active", true);
089                } else if (StringUtils.equals(active, "N")) {
090                    activeFilter.addEqualTo("active", false);
091                }
092                root.addAndCriteria(activeFilter);
093            }
094    
095            if (StringUtils.equals(showHistory, "N")) {
096                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Location.class, EQUAL_TO_FIELDS, false));
097                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Location.class, EQUAL_TO_FIELDS, false));
098            }
099            
100            Query query = QueryFactory.newQuery(Location.class, root);
101            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
102            
103            return results;
104        }
105    
106    }