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.time.position.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.time.position.Position;
030    import org.kuali.hr.time.util.TKUtils;
031    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032    
033    public class PositionDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements PositionDao {
034        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
035                .add("positionNumber")
036                .build();
037    
038        @Override
039        public Position getPosition(String hrPositionId) {
040            Criteria crit = new Criteria();
041            crit.addEqualTo("hrPositionId", hrPositionId);
042    
043            Query query = QueryFactory.newQuery(Position.class, crit);
044            return (Position) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
045        }
046    
047    
048        @Override
049        public Position getPosition(String positionNumber, Date effectiveDate) {
050            Criteria root = new Criteria();
051    
052            root.addEqualTo("positionNumber", positionNumber);
053            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Position.class, effectiveDate, EQUAL_TO_FIELDS, false));
054            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Position.class, EQUAL_TO_FIELDS, false));
055    
056            Criteria activeFilter = new Criteria(); // Inner Join For Activity
057            activeFilter.addEqualTo("active", true);
058            root.addAndCriteria(activeFilter);
059    
060            Query query = QueryFactory.newQuery(Position.class, root);
061            return (Position) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
062        }
063    
064            @Override
065        @SuppressWarnings("unchecked")
066        public List<Position> getPositions(String positionNum, String workArea, String description, Date fromEffdt, Date toEffdt, 
067                                                                       String active, String showHistory) {
068            
069            List<Position> results = new ArrayList<Position>();
070            
071            Criteria root = new Criteria();
072    
073            if (StringUtils.isNotBlank(positionNum)) {
074                root.addLike("positionNumber", positionNum);
075            }
076            
077            if (StringUtils.isNotBlank(workArea)) {
078                OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea);
079            }
080    
081            if (StringUtils.isNotBlank(description)) {
082                root.addLike("description", description);
083            }
084            
085            Criteria effectiveDateFilter = new Criteria();
086            if (fromEffdt != null) {
087                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
088            }
089            if (toEffdt != null) {
090                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
091            }
092            if (fromEffdt == null && toEffdt == null) {
093                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
094            }
095            root.addAndCriteria(effectiveDateFilter);
096            
097            if (StringUtils.isNotBlank(active)) {
098                    Criteria activeFilter = new Criteria();
099                if (StringUtils.equals(active, "Y")) {
100                    activeFilter.addEqualTo("active", true);
101                } else if (StringUtils.equals(active, "N")) {
102                    activeFilter.addEqualTo("active", false);
103                }
104                root.addAndCriteria(activeFilter);
105            }
106    
107            if (StringUtils.equals(showHistory, "N")) {
108                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Position.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
109                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Position.class, EQUAL_TO_FIELDS, false));
110            }
111            
112            Query query = QueryFactory.newQuery(Position.class, root);
113            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
114    
115            return results;
116        }
117    
118        /*@Override
119        public PositionNumber getNextUniquePositionNumber() {
120            Criteria crit = new Criteria();
121            ReportQueryByCriteria query = QueryFactory.newReportQuery(PositionNumber.class, crit);
122            query.setAttributes(new String[]{"max(id)"});
123            return (PositionNumber) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
124        }
125    
126        @Override
127        public void saveOrUpdate(PositionNumber positionNumber) {
128            this.getPersistenceBrokerTemplate().store(positionNumber);
129        }*/
130    
131    }