View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.position.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import com.google.common.collect.ImmutableList;
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.hr.core.util.OjbSubQueryUtil;
29  import org.kuali.hr.time.position.Position;
30  import org.kuali.hr.time.util.TKUtils;
31  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32  
33  public class PositionDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements PositionDao {
34      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
35              .add("positionNumber")
36              .build();
37  
38      @Override
39      public Position getPosition(String hrPositionId) {
40          Criteria crit = new Criteria();
41          crit.addEqualTo("hrPositionId", hrPositionId);
42  
43          Query query = QueryFactory.newQuery(Position.class, crit);
44          return (Position) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
45      }
46  
47  
48      @Override
49      public Position getPosition(String positionNumber, Date effectiveDate) {
50          Criteria root = new Criteria();
51  
52          root.addEqualTo("positionNumber", positionNumber);
53          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Position.class, effectiveDate, EQUAL_TO_FIELDS, false));
54          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Position.class, EQUAL_TO_FIELDS, false));
55  
56          Criteria activeFilter = new Criteria(); // Inner Join For Activity
57          activeFilter.addEqualTo("active", true);
58          root.addAndCriteria(activeFilter);
59  
60          Query query = QueryFactory.newQuery(Position.class, root);
61          return (Position) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
62      }
63  
64  	@Override
65      @SuppressWarnings("unchecked")
66      public List<Position> getPositions(String positionNum, String workArea, String description, Date fromEffdt, Date toEffdt, 
67      								   String active, String showHistory) {
68          
69          List<Position> results = new ArrayList<Position>();
70          
71      	Criteria root = new Criteria();
72  
73          if (StringUtils.isNotBlank(positionNum)) {
74              root.addLike("positionNumber", positionNum);
75          }
76          
77          if (StringUtils.isNotBlank(workArea)) {
78              root.addLike("workArea", workArea);
79          }
80  
81          if (StringUtils.isNotBlank(description)) {
82              root.addLike("description", description);
83          }
84          
85          Criteria effectiveDateFilter = new Criteria();
86          if (fromEffdt != null) {
87              effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
88          }
89          if (toEffdt != null) {
90              effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
91          }
92          if (fromEffdt == null && toEffdt == null) {
93              effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
94          }
95          root.addAndCriteria(effectiveDateFilter);
96          
97          if (StringUtils.isNotBlank(active)) {
98          	Criteria activeFilter = new Criteria();
99              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 }