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 com.google.common.collect.ImmutableList;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.ojb.broker.query.Criteria;
26  import org.apache.ojb.broker.query.Query;
27  import org.apache.ojb.broker.query.QueryFactory;
28  import org.apache.ojb.broker.query.ReportQueryByCriteria;
29  import org.kuali.hr.core.util.OjbSubQueryUtil;
30  import org.kuali.hr.time.util.TKUtils;
31  import org.kuali.hr.time.workarea.WorkArea;
32  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  
35  public class WorkAreaDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements WorkAreaDao {
36      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
37              .add("workArea")
38              .build();
39  
40      @Override
41      public WorkArea getWorkArea(Long workArea, Date asOfDate) {
42  		Criteria root = new Criteria();
43  
44  		root.addEqualTo("workArea", workArea);
45          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(WorkArea.class, asOfDate, EQUAL_TO_FIELDS, false));
46          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(WorkArea.class, EQUAL_TO_FIELDS, false));
47  
48  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
49  		activeFilter.addEqualTo("active", true);
50  		root.addAndCriteria(activeFilter);
51  
52  		Query query = QueryFactory.newQuery(WorkArea.class, root);
53  		return (WorkArea) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
54      }
55  
56      @Override
57      public List<WorkArea> getWorkArea(String department, Date asOfDate) {
58          Criteria root = new Criteria();
59  
60          root.addEqualTo("dept", department);
61          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(WorkArea.class, asOfDate, EQUAL_TO_FIELDS, false));
62          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(WorkArea.class, EQUAL_TO_FIELDS, false));
63  
64          Criteria activeFilter = new Criteria(); // Inner Join For Activity
65          activeFilter.addEqualTo("active", true);
66          root.addAndCriteria(activeFilter);
67  
68          Query query = QueryFactory.newQuery(WorkArea.class, root);
69          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
70          List<WorkArea> wal = new ArrayList<WorkArea>(c.size());
71          wal.addAll(c);
72          return wal;
73      }
74  
75      @Override
76      public void saveOrUpdate(WorkArea workArea) {
77      	this.getPersistenceBrokerTemplate().store(workArea);
78      }
79  
80  	@Override
81  	public WorkArea getWorkArea(String tkWorkAreaId) {
82  		Criteria crit = new Criteria();
83  		crit.addEqualTo("tkWorkAreaId", tkWorkAreaId);
84  		
85  		Query query = QueryFactory.newQuery(WorkArea.class, crit);
86  		return (WorkArea)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
87  	}
88  	
89  	public Long getNextWorkAreaKey(){
90  		 return KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("tk_work_area_key_s");
91  	}
92  
93  	@Override
94  	@SuppressWarnings("unchecked")
95  	public List<WorkArea> getWorkAreas(String dept, String workArea, String description, Date fromEffdt, Date toEffdt, String active, String showHistory) {
96  		List<WorkArea> results = new ArrayList<WorkArea>();
97  		
98  		Criteria root = new Criteria();
99  
100 		if (StringUtils.isNotBlank(dept)) {
101 			root.addLike("dept", dept);
102 		}
103 		
104 		if (StringUtils.isNotBlank(workArea)) {
105 			root.addLike("workArea", workArea);
106 		}
107 		
108 		if (StringUtils.isNotBlank(description)) {
109 			root.addLike("description", description);
110 		}
111 		
112 		Criteria effectiveDateFilter = new Criteria();
113 		if (fromEffdt != null) {
114 		    effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
115 		}
116 		if (toEffdt != null) {
117 		    effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
118 		}
119 		if (fromEffdt == null && toEffdt == null) {
120 		    effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
121 		}
122 		root.addAndCriteria(effectiveDateFilter);
123 		
124         if (StringUtils.isNotBlank(active)) {
125         	Criteria activeFilter = new Criteria();
126             if (StringUtils.equals(active, "Y")) {
127                 activeFilter.addEqualTo("active", true);
128             } else if (StringUtils.equals(active, "N")) {
129                 activeFilter.addEqualTo("active", false);
130             }
131             root.addAndCriteria(activeFilter);
132         }
133 		
134 		if (StringUtils.equals(showHistory, "N")) {
135             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(WorkArea.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
136             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(WorkArea.class, EQUAL_TO_FIELDS, false));
137 		}
138 		
139         Query query = QueryFactory.newQuery(WorkArea.class, root);
140         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
141 		
142 		return results;
143 	}
144 
145 	@Override
146 	public int getWorkAreaCount(String dept, Long workArea) {
147 		Criteria crit = new Criteria();
148 		if(dept != null) {
149 			crit.addEqualTo("dept", dept);
150 		}
151 		crit.addEqualTo("workArea", workArea);
152 		Query query = QueryFactory.newQuery(WorkArea.class, crit);
153 		return this.getPersistenceBrokerTemplate().getCount(query); 
154 	}
155 }