001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.lm.leavecode.dao;
017
018
019 import java.sql.Date;
020 import java.util.ArrayList;
021 import java.util.Collection;
022 import java.util.List;
023
024 import com.google.common.collect.ImmutableList;
025 import org.apache.log4j.Logger;
026 import org.apache.ojb.broker.query.Criteria;
027 import org.apache.ojb.broker.query.Query;
028 import org.apache.ojb.broker.query.QueryFactory;
029 import org.apache.ojb.broker.query.ReportQueryByCriteria;
030 import org.kuali.hr.core.util.OjbSubQueryUtil;
031 import org.kuali.hr.lm.leavecode.LeaveCode;
032 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
033
034 public class LeaveCodeDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements LeaveCodeDao {
035 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
036 .add("leaveCode")
037 .add("leavePlan")
038 .add("principalId")
039 .build();
040 private static final Logger LOG = Logger.getLogger(LeaveCodeDaoSpringOjbImpl.class);
041
042 @Override
043 public LeaveCode getLeaveCode(String lmLeaveCodeId) {
044 Criteria crit = new Criteria();
045 crit.addEqualTo("lmLeaveCodeId", lmLeaveCodeId);
046 Query query = QueryFactory.newQuery(LeaveCode.class, crit);
047 return (LeaveCode) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
048 }
049
050 public List<LeaveCode> getLeaveCodes(String leavePlan, Date asOfDate){
051 List<LeaveCode> leaveCodes = new ArrayList<LeaveCode>();
052 Criteria root = new Criteria();
053
054 java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
055 root.addEqualTo("leavePlan", leavePlan);
056 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(LeaveCode.class, effDate, EQUAL_TO_FIELDS, false));
057 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(LeaveCode.class, EQUAL_TO_FIELDS, false));
058
059 Criteria activeFilter = new Criteria(); // Inner Join For Activity
060 activeFilter.addEqualTo("active", true);
061 root.addAndCriteria(activeFilter);
062
063
064 Query query = QueryFactory.newQuery(LeaveCode.class, root);
065 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
066
067 if (c != null) {
068 leaveCodes.addAll(c);
069 }
070 return leaveCodes;
071 }
072
073 @Override
074 public LeaveCode getLeaveCode(String leaveCode, Date asOfDate) {
075 LeaveCode myleaveCode = null;
076 Criteria root = new Criteria();
077 root.addEqualTo("leaveCode", leaveCode);
078 root.addLessOrEqualThan("effectiveDate", asOfDate);
079 root.addEqualTo("active",true);
080
081 Query query = QueryFactory.newQuery(LeaveCode.class, root);
082 Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
083 if (obj != null) {
084 myleaveCode = (LeaveCode) obj;
085 }
086
087 return myleaveCode;
088 }
089 }