View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.pm.positionflag.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.query.Criteria;
24  import org.apache.ojb.broker.query.Query;
25  import org.apache.ojb.broker.query.QueryFactory;
26  import org.joda.time.LocalDate;
27  import org.kuali.kpme.core.util.OjbSubQueryUtil;
28  import org.kuali.kpme.core.util.ValidationUtils;
29  import org.kuali.kpme.pm.positionflag.PositionFlagBo;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  
32  public class PositionFlagDaoObjImpl extends PlatformAwareDaoBaseOjb implements PositionFlagDao {
33  
34  	@Override
35  	public PositionFlagBo getPositionFlagById(String pmPositionFlagId) {
36  		Criteria crit = new Criteria();
37          crit.addEqualTo("pmPositionFlagId", pmPositionFlagId);
38  
39          Query query = QueryFactory.newQuery(PositionFlagBo.class, crit);
40          return (PositionFlagBo) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
41  	}
42  	
43  	@Override
44  	public List<PositionFlagBo> getAllActivePositionFlags(String category, String name, LocalDate effDate) {
45  		List<PositionFlagBo> pfList = new ArrayList<PositionFlagBo>();
46  		Criteria root = new Criteria();
47  
48  		if(StringUtils.isNotEmpty(category) 
49  				&& !ValidationUtils.isWildCard(category)) {
50  			root.addEqualTo("category", category);  
51  		}
52  		if(StringUtils.isNotEmpty(name) 
53  				&& !ValidationUtils.isWildCard(name)) {
54  			root.addEqualTo("positionFlagName", name); 
55  		}
56          
57          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PositionFlagBo.class, effDate, PositionFlagBo.BUSINESS_KEYS, false));
58          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PositionFlagBo.class, PositionFlagBo.BUSINESS_KEYS, false));
59          
60          Criteria activeFilter = new Criteria();
61          activeFilter.addEqualTo("active", true);
62          root.addAndCriteria(activeFilter);
63  
64          Query query = QueryFactory.newQuery(PositionFlagBo.class, root);
65          
66          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
67  		if(!c.isEmpty())
68  			pfList.addAll(c);
69  		
70  		return pfList;
71  	}
72  	
73  	@Override
74  	public List<String> getAllActiveFlagCategories() {
75  		List<String> categoryList = new ArrayList<String>();
76  		Criteria root = new Criteria();
77  		root.addEqualTo("active", true); 
78  		Query query = QueryFactory.newQuery(PositionFlagBo.class, root);
79          
80          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
81          List<PositionFlagBo> pfList = new ArrayList<PositionFlagBo>();
82          if(!c.isEmpty())
83  			pfList.addAll(c);
84         
85          for(PositionFlagBo aFlag : pfList) {
86          	if(aFlag != null && StringUtils.isNotEmpty(aFlag.getCategory())
87          			&& !categoryList.contains(aFlag.getCategory())) {
88          		categoryList.add(aFlag.getCategory());
89          	}
90          }
91          return categoryList;
92  	}
93  	
94  	@Override
95  	public List<PositionFlagBo> getAllActivePositionFlagsWithCategory(String category, LocalDate effDate) {
96  		List<PositionFlagBo> pfList = new ArrayList<PositionFlagBo>();
97  		Criteria root = new Criteria();
98  		if(StringUtils.isEmpty(category)) {
99  			return pfList;
100 		}
101 		root.addEqualTo("category", category);
102 		
103 		if(effDate != null) {
104 			root.addLessOrEqualThan("effectiveDate", effDate.toDate());
105 			root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PositionFlagBo.class, effDate, PositionFlagBo.BUSINESS_KEYS, false));
106 		}
107 		Criteria activeFilter = new Criteria();
108         activeFilter.addEqualTo("active", true);
109         root.addAndCriteria(activeFilter);
110 
111         Query query = QueryFactory.newQuery(PositionFlagBo.class, root);
112         
113         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
114 		if(!c.isEmpty())
115 			pfList.addAll(c);
116 		
117 		return pfList;
118 	}
119 
120 }