001    /**
002     * Copyright 2004-2012 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    
020    import org.apache.ojb.broker.query.Criteria;
021    import org.apache.ojb.broker.query.Query;
022    import org.apache.ojb.broker.query.QueryFactory;
023    import org.apache.ojb.broker.query.ReportQueryByCriteria;
024    import org.kuali.hr.location.Location;
025    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
026    
027    public class LocationDaoSpringObjImpl extends PlatformAwareDaoBaseOjb implements LocationDao {
028    
029            @Override
030            public Location getLocation(String location, Date asOfDate) {
031                    Criteria root = new Criteria();
032                    Criteria effdt = new Criteria();
033                    Criteria timestamp = new Criteria();
034    
035                    effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
036    //              effdt.addEqualToField("org", Criteria.PARENT_QUERY_PREFIX + "org");
037    //              effdt.addEqualToField("chart", Criteria.PARENT_QUERY_PREFIX + "chart");
038                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
039    //              effdt.addEqualTo("active", true);
040                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Location.class, effdt);
041                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
042    
043                    timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
044    //              timestamp.addEqualToField("org", Criteria.PARENT_QUERY_PREFIX + "org");
045    //              timestamp.addEqualToField("chart", Criteria.PARENT_QUERY_PREFIX + "chart");
046                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
047    //              timestamp.addEqualTo("active", true);
048                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Location.class, timestamp);
049                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
050    
051                    root.addEqualTo("location", location);
052                    root.addEqualTo("effectiveDate", effdtSubQuery);
053                    root.addEqualTo("timestamp", timestampSubQuery);
054    //              root.addEqualTo("active", true);
055                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
056                    activeFilter.addEqualTo("active", true);
057                    root.addAndCriteria(activeFilter);
058    
059                    
060                    Query query = QueryFactory.newQuery(Location.class, root);
061                    
062                    Location l = (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
063                    
064                    return l;
065            }
066    
067            @Override
068            public Location getLocation(String hrLocationId) {
069                    Criteria crit = new Criteria();
070                    crit.addEqualTo("hrLocationId", hrLocationId);
071                    
072                    Query query = QueryFactory.newQuery(Location.class, crit);
073                    return (Location)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
074            }
075            
076            @Override
077            public int getLocationCount(String location) {
078                    Criteria crit = new Criteria();
079                    crit.addEqualTo("location", location);
080                    Query query = QueryFactory.newQuery(Location.class, crit);
081                    return this.getPersistenceBrokerTemplate().getCount(query);
082            }
083    
084    }