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.workarea.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
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.time.util.TKUtils;
29  import org.kuali.hr.time.workarea.WorkArea;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  
33  public class WorkAreaDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements WorkAreaDao {
34  
35      @Override
36      public WorkArea getWorkArea(Long workArea, Date asOfDate) {
37  		Criteria root = new Criteria();
38  		Criteria effdt = new Criteria();
39  		Criteria timestamp = new Criteria();
40  
41  		effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
42  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
43  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
44  		effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
45  
46  		timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
47  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
48  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
49  		timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
50  
51  		root.addEqualTo("workArea", workArea);
52  		root.addEqualTo("effectiveDate", effdtSubQuery);
53  		root.addEqualTo("timestamp", timestampSubQuery);
54  
55  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
56  		activeFilter.addEqualTo("active", true);
57  		root.addAndCriteria(activeFilter);
58  
59  		Query query = QueryFactory.newQuery(WorkArea.class, root);
60  		return (WorkArea) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
61      }
62  
63      @Override
64      public List<WorkArea> getWorkArea(String department, Date asOfDate) {
65          Criteria root = new Criteria();
66          Criteria effdt = new Criteria();
67          Criteria timestamp = new Criteria();
68  
69          effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
70          effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
71          effdt.addLessOrEqualThan("effectiveDate", asOfDate);
72          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
73          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
74  
75          timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
76          timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
77          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
78          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
79          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
80  
81          root.addEqualTo("dept", department);
82          root.addEqualTo("effectiveDate", effdtSubQuery);
83          root.addEqualTo("timestamp", timestampSubQuery);
84  
85          Criteria activeFilter = new Criteria(); // Inner Join For Activity
86          activeFilter.addEqualTo("active", true);
87          root.addAndCriteria(activeFilter);
88  
89          Query query = QueryFactory.newQuery(WorkArea.class, root);
90          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
91          List<WorkArea> wal = new ArrayList<WorkArea>(c.size());
92          wal.addAll(c);
93          return wal;
94      }
95  
96      @Override
97      public void saveOrUpdate(WorkArea workArea) {
98      	this.getPersistenceBrokerTemplate().store(workArea);
99      }
100 
101 	@Override
102 	public WorkArea getWorkArea(String tkWorkAreaId) {
103 		Criteria crit = new Criteria();
104 		crit.addEqualTo("tkWorkAreaId", tkWorkAreaId);
105 		
106 		Query query = QueryFactory.newQuery(WorkArea.class, crit);
107 		return (WorkArea)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
108 	}
109 	
110 	public Long getNextWorkAreaKey(){
111 		 return KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("tk_work_area_key_s");
112 	}
113 
114 	@Override
115 	@SuppressWarnings("unchecked")
116 	public List<WorkArea> getWorkAreas(String dept, String workArea, String description, Date fromEffdt, Date toEffdt, String active, String showHistory) {
117 		List<WorkArea> results = new ArrayList<WorkArea>();
118 		
119 		Criteria root = new Criteria();
120 
121 		if (StringUtils.isNotBlank(dept)) {
122 			root.addLike("dept", dept);
123 		}
124 		
125 		if (StringUtils.isNotBlank(workArea)) {
126 			root.addLike("workArea", workArea);
127 		}
128 		
129 		if (StringUtils.isNotBlank(description)) {
130 			root.addLike("description", description);
131 		}
132 		
133 		Criteria effectiveDateFilter = new Criteria();
134 		if (fromEffdt != null) {
135 		    effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
136 		}
137 		if (toEffdt != null) {
138 		    effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
139 		}
140 		if (fromEffdt == null && toEffdt == null) {
141 		    effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
142 		}
143 		root.addAndCriteria(effectiveDateFilter);
144 		
145         if (StringUtils.isNotBlank(active)) {
146         	Criteria activeFilter = new Criteria();
147             if (StringUtils.equals(active, "Y")) {
148                 activeFilter.addEqualTo("active", true);
149             } else if (StringUtils.equals(active, "N")) {
150                 activeFilter.addEqualTo("active", false);
151             }
152             root.addAndCriteria(activeFilter);
153         }
154 		
155 		if (StringUtils.equals(showHistory, "N")) {
156 			Criteria effdt = new Criteria();
157 			effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
158 			effdt.addAndCriteria(effectiveDateFilter);
159 			ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(WorkArea.class, effdt);
160 			effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"});
161 			root.addEqualTo("effectiveDate", effdtSubQuery);
162 			
163 			Criteria timestamp = new Criteria();
164 			timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
165 			timestamp.addAndCriteria(effectiveDateFilter);
166 			ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(WorkArea.class, timestamp);
167 			timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
168 			root.addEqualTo("timestamp", timestampSubQuery);
169 		}
170 		
171         Query query = QueryFactory.newQuery(WorkArea.class, root);
172         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
173 		
174 		return results;
175 	}
176 
177 	@Override
178 	public int getWorkAreaCount(String dept, Long workArea) {
179 		Criteria crit = new Criteria();
180 		if(dept != null) {
181 			crit.addEqualTo("dept", dept);
182 		}
183 		crit.addEqualTo("workArea", workArea);
184 		Query query = QueryFactory.newQuery(WorkArea.class, crit);
185 		return this.getPersistenceBrokerTemplate().getCount(query); 
186 	}
187 }