001    /**
002     * Copyright 2004-2012 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.time.clock.location.dao;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.ojb.broker.query.Criteria;
023    import org.apache.ojb.broker.query.Query;
024    import org.apache.ojb.broker.query.QueryFactory;
025    import org.apache.ojb.broker.query.ReportQueryByCriteria;
026    import org.kuali.hr.time.clock.location.ClockLocationRule;
027    import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
028    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
029    
030    public class ClockLocationDaoOjbImpl extends PlatformAwareDaoBaseOjb implements ClockLocationDao{       
031            @SuppressWarnings("unchecked")
032            public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea, String principalId, Long jobNumber, Date asOfDate){               
033                    Criteria root = new Criteria();
034                    Criteria effdt = new Criteria();
035                    Criteria timestamp = new Criteria();
036    
037                    effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
038                    effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
039                    effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
040                    effdt.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
041                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
042                    //effdt.addEqualTo("active", true);
043                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, effdt);
044                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
045    
046                    timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
047                    timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
048                    timestamp.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
049                    timestamp.addEqualToField("jobNumber", Criteria.PARENT_QUERY_PREFIX + "jobNumber");
050                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
051                    //timestamp.addEqualTo("active", true);
052                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ClockLocationRule.class, timestamp);
053                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
054    
055                    root.addEqualTo("dept", dept);
056                    root.addEqualTo("workArea", workArea);
057                    root.addEqualTo("principalId", principalId);
058                    root.addEqualTo("jobNumber", jobNumber);
059                    root.addEqualTo("effectiveDate", effdtSubQuery);
060                    root.addEqualTo("timestamp", timestampSubQuery);
061                    //root.addEqualTo("active", true);
062                    
063                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
064                    activeFilter.addEqualTo("active", true);
065                    root.addAndCriteria(activeFilter);
066                    
067                    Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
068                    List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
069                    if(clockLocationRules==null){
070                            clockLocationRules = new ArrayList<ClockLocationRule>();
071                    }
072                    for(ClockLocationRule clr : clockLocationRules ) {
073                            this.populateIPAddressesForCLR(clr);
074                    }
075                    return clockLocationRules;
076            }
077    
078            @SuppressWarnings("unchecked")
079            @Override
080            public List<ClockLocationRule> getNewerVersionClockLocationRule(
081                            String dept, Long workArea, String principalId, Long jobNumber,
082                            Date asOfDate) {
083                    Criteria root = new Criteria();
084                    root.addEqualTo("dept", dept);
085                    root.addEqualTo("workArea", workArea);
086                    root.addEqualTo("principalId", principalId);
087                    root.addEqualTo("jobNumber", jobNumber);
088                    root.addGreaterThan("effectiveDate", asOfDate);
089                    
090                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
091                    activeFilter.addEqualTo("active", true);
092                    root.addAndCriteria(activeFilter);
093                    
094                    Query query = QueryFactory.newQuery(ClockLocationRule.class, root);
095                    List<ClockLocationRule> clockLocationRules = (List<ClockLocationRule>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
096                    if(clockLocationRules==null){
097                            clockLocationRules = new ArrayList<ClockLocationRule>();
098                    }
099                    for(ClockLocationRule clr : clockLocationRules ) {
100                            this.populateIPAddressesForCLR(clr);
101                    }
102                    return clockLocationRules;
103            }
104            
105            public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId){
106                    Criteria criteria = new Criteria();
107                    criteria.addEqualTo("tkClockLocationRuleId", tkClockLocationRuleId);
108                    ClockLocationRule clr = (ClockLocationRule) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(
109                                                                            ClockLocationRule.class, criteria));
110                    if(clr != null) {
111                            this.populateIPAddressesForCLR(clr);
112                    }
113                    return clr;
114            }
115            
116            // get ip address from tk_ip_addresses table for this ClockLocationRule
117            @SuppressWarnings("unchecked")
118            public void populateIPAddressesForCLR(ClockLocationRule clr) {
119                    if(clr.getTkClockLocationRuleId() == null) {
120                            return;
121                    }
122                    Criteria root = new Criteria();
123                    root.addEqualTo("tkClockLocationRuleId", clr.getTkClockLocationRuleId().toString());
124                    Query query = QueryFactory.newQuery(ClockLocationRuleIpAddress.class, root);
125                    List<ClockLocationRuleIpAddress> ipAddresses = (List<ClockLocationRuleIpAddress>) this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
126                    clr.setIpAddresses(ipAddresses);
127            }
128    
129    }