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                    Location l = (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
051                    
052                    return l;
053            }
054    
055            @Override
056            public Location getLocation(String hrLocationId) {
057                    Criteria crit = new Criteria();
058                    crit.addEqualTo("hrLocationId", hrLocationId);
059                    
060                    Query query = QueryFactory.newQuery(Location.class, crit);
061                    return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
062            }
063            
064            @Override
065            public int getLocationCount(String location) {
066                    Criteria crit = new Criteria();
067                    crit.addEqualTo("location", location);
068                    Query query = QueryFactory.newQuery(Location.class, crit);
069                    return this.getPersistenceBrokerTemplate().getCount(query);
070            }
071    
072            @Override
073        @SuppressWarnings("unchecked")
074        public List<Location> searchLocations(String location, String locationDescr, String active, String showHistory) {
075            List<Location> results = new ArrayList<Location>();
076            
077            Criteria root = new Criteria();
078    
079            if (StringUtils.isNotBlank(location)) {
080                root.addLike("location", location);
081            }
082            
083            if (StringUtils.isNotBlank(locationDescr)) {
084                root.addLike("description", locationDescr);
085            }
086            
087            if (StringUtils.isNotBlank(active)) {
088                    Criteria activeFilter = new Criteria();
089                if (StringUtils.equals(active, "Y")) {
090                    activeFilter.addEqualTo("active", true);
091                } else if (StringUtils.equals(active, "N")) {
092                    activeFilter.addEqualTo("active", false);
093                }
094                root.addAndCriteria(activeFilter);
095            }
096            
097            Query query = QueryFactory.newQuery(Location.class, root);
098            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
099            
100            return results;
101        }
102    
103    }