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.core.institution.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.institution.Institution;
28  import org.kuali.kpme.core.util.OjbSubQueryUtil;
29  import org.kuali.kpme.core.util.ValidationUtils;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  
32  public class InstitutionDaoOjbImpl extends PlatformAwareDaoBaseOjb implements InstitutionDao {
33  	
34      
35      @Override
36      public Institution getInstitution(String institution, LocalDate asOfDate) {
37  		Institution inst = null;
38  
39  		Criteria root = new Criteria();
40  		// allow wild card
41  		if(StringUtils.isNotEmpty(institution) && !ValidationUtils.isWildCard(institution)) {
42  			root.addEqualTo("institutionCode", institution);
43  		}
44          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Institution.class, asOfDate, Institution.EQUAL_TO_FIELDS, false));
45          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Institution.class, Institution.EQUAL_TO_FIELDS, false));
46  		
47  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
48  		activeFilter.addEqualTo("active", true);
49  		root.addAndCriteria(activeFilter);
50  		
51  		
52  		Query query = QueryFactory.newQuery(Institution.class, root);
53  		Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
54  
55  		if (obj != null) {
56  			inst = (Institution) obj;
57  		}
58  
59  		return inst;
60      }
61      
62  	@Override
63  	public List<Institution> getActiveInstitutions(LocalDate asOfDate) {
64  		List<Institution> institutions = new ArrayList<Institution>();
65  		
66  		Criteria root = new Criteria();
67  
68  		root.addEqualTo("effectiveDate",  OjbSubQueryUtil.getEffectiveDateSubQuery(Institution.class, asOfDate, Institution.EQUAL_TO_FIELDS, false));
69  
70  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
71  		activeFilter.addEqualTo("active", true);
72  		root.addAndCriteria(activeFilter);
73  
74  		Query query = QueryFactory.newQuery(Institution.class, root);
75  		
76  		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
77  		
78  		if(!c.isEmpty())
79  			institutions.addAll(c);
80  		
81  		return institutions;
82  	}
83  
84  	@Override
85  	public List<Institution> getInstitutionByCode(String code) {
86  		List<Institution> institutions = new ArrayList<Institution>();
87  		
88  		Criteria root = new Criteria();
89  
90  		root.addEqualTo("institutionCode", code);
91  
92  		Query query = QueryFactory.newQuery(Institution.class, root);
93  		
94  		Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
95  		
96  		if(!c.isEmpty())
97  			institutions.addAll(c);
98  		
99  		return institutions;
100 
101 	}
102 
103 	@Override
104 	public Institution getInstitutionById(String institutionId) {
105 		
106 		Criteria root = new Criteria();
107 
108 		root.addEqualTo("pmInstitutionId", institutionId);
109 
110 		Query query = QueryFactory.newQuery(Institution.class, root);
111 		
112 		return (Institution) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
113 
114 	}
115 
116 	@Override
117 	public int getInstitutionCount(String institutionCode, LocalDate asOfDate) {
118 		Criteria crit = new Criteria();
119 		// allow wild card
120 		if(StringUtils.isNotEmpty(institutionCode) && !ValidationUtils.isWildCard(institutionCode)) {
121 			crit.addEqualTo("institutionCode", institutionCode);
122 		}
123 		crit.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Institution.class, asOfDate, Institution.EQUAL_TO_FIELDS, false));
124 		crit.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Institution.class, Institution.EQUAL_TO_FIELDS, false));
125 		Criteria activeFilter = new Criteria(); // Inner Join For Activity
126 		activeFilter.addEqualTo("active", true);
127 		crit.addAndCriteria(activeFilter);
128 		Query query = QueryFactory.newQuery(Institution.class, crit);
129 		return this.getPersistenceBrokerTemplate().getCount(query);
130 	}
131 
132 	@Override
133 	public List<Institution> getInstitutions(LocalDate fromEffdt, LocalDate toEffdt, String institutionCode, String active, String showHistory) {
134         Criteria root = new Criteria();
135 
136         List<Institution> results = new ArrayList<Institution>();
137 
138         if (StringUtils.isNotBlank(institutionCode)) {
139         	root.addLike("institutionCode", institutionCode);
140         }
141         
142         Criteria effectiveDateFilter = new Criteria();
143         if (fromEffdt != null) {
144             effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
145         }
146         if (toEffdt != null) {
147             effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
148         }
149         if (fromEffdt == null && toEffdt == null) {
150             effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
151         }
152         root.addAndCriteria(effectiveDateFilter);
153 
154         if (StringUtils.isNotBlank(active)) {
155             Criteria activeFilter = new Criteria();
156             if (StringUtils.equals(active, "Y")) {
157                 activeFilter.addEqualTo("active", true);
158             } else if (StringUtils.equals(active, "N")) {
159                 activeFilter.addEqualTo("active", false);
160             }
161             root.addAndCriteria(activeFilter);
162         }
163 
164         if (StringUtils.equals(showHistory, "N")) {
165         	root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Institution.class, effectiveDateFilter, Institution.EQUAL_TO_FIELDS, false));
166         	root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Institution.class, Institution.EQUAL_TO_FIELDS, false));
167         }
168 
169         Query query = QueryFactory.newQuery(Institution.class, root);
170         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
171 
172         return results;
173 	}
174 
175 }