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 org.apache.commons.lang.StringUtils;
023    import org.apache.ojb.broker.query.Criteria;
024    import org.apache.ojb.broker.query.Query;
025    import org.apache.ojb.broker.query.QueryFactory;
026    import org.apache.ojb.broker.query.ReportQueryByCriteria;
027    import org.kuali.hr.location.Location;
028    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
029    
030    public class LocationDaoSpringObjImpl extends PlatformAwareDaoBaseOjb implements LocationDao {
031    
032            @Override
033            public Location getLocation(String location, Date asOfDate) {
034                    Criteria root = new Criteria();
035                    Criteria effdt = new Criteria();
036                    Criteria timestamp = new Criteria();
037    
038                    effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
039    //              effdt.addEqualToField("org", Criteria.PARENT_QUERY_PREFIX + "org");
040    //              effdt.addEqualToField("chart", Criteria.PARENT_QUERY_PREFIX + "chart");
041                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
042    //              effdt.addEqualTo("active", true);
043                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Location.class, effdt);
044                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
045    
046                    timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
047    //              timestamp.addEqualToField("org", Criteria.PARENT_QUERY_PREFIX + "org");
048    //              timestamp.addEqualToField("chart", Criteria.PARENT_QUERY_PREFIX + "chart");
049                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
050    //              timestamp.addEqualTo("active", true);
051                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Location.class, timestamp);
052                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
053    
054                    root.addEqualTo("location", location);
055                    root.addEqualTo("effectiveDate", effdtSubQuery);
056                    root.addEqualTo("timestamp", timestampSubQuery);
057    //              root.addEqualTo("active", true);
058                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
059                    activeFilter.addEqualTo("active", true);
060                    root.addAndCriteria(activeFilter);
061    
062                    
063                    Query query = QueryFactory.newQuery(Location.class, root);
064                    
065                    Location l = (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
066                    
067                    return l;
068            }
069    
070            @Override
071            public Location getLocation(String hrLocationId) {
072                    Criteria crit = new Criteria();
073                    crit.addEqualTo("hrLocationId", hrLocationId);
074                    
075                    Query query = QueryFactory.newQuery(Location.class, crit);
076                    return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
077            }
078            
079            @Override
080            public int getLocationCount(String location) {
081                    Criteria crit = new Criteria();
082                    crit.addEqualTo("location", location);
083                    Query query = QueryFactory.newQuery(Location.class, crit);
084                    return this.getPersistenceBrokerTemplate().getCount(query);
085            }
086    
087            @Override
088        @SuppressWarnings("unchecked")
089        public List<Location> searchLocations(String location, String locationDescr, String active, String showHistory) {
090            List<Location> results = new ArrayList<Location>();
091            
092            Criteria root = new Criteria();
093    
094            if (StringUtils.isNotBlank(location)) {
095                root.addLike("location", location);
096            }
097            
098            if (StringUtils.isNotBlank(locationDescr)) {
099                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    }