001    /**
002     * Copyright 2005-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.rice.kew.actiontaken.dao.impl;
017    
018    import org.apache.ojb.broker.query.Criteria;
019    import org.apache.ojb.broker.query.QueryByCriteria;
020    import org.kuali.rice.kew.actionrequest.ActionRequestValue;
021    import org.kuali.rice.kew.actiontaken.ActionTakenValue;
022    import org.kuali.rice.kew.actiontaken.dao.ActionTakenDAO;
023    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
024    
025    import java.sql.Timestamp;
026    import java.util.Collection;
027    import java.util.List;
028    
029    
030    /**
031     * OJB implementation of the {@link ActionTakenDAO}.
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class ActionTakenDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionTakenDAO {
036    
037        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDAOOjbImpl.class);
038    
039        public ActionTakenValue load(String id) {
040            LOG.debug("Loading Action Taken for the given id " + id);
041            Criteria crit = new Criteria();
042            crit.addEqualTo("actionTakenId", id);
043            return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
044        }
045    
046        public void deleteActionTaken(ActionTakenValue actionTaken) {
047            LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
048            this.getPersistenceBrokerTemplate().delete(actionTaken);
049        }
050    
051        public ActionTakenValue findByActionTakenId(String actionTakenId) {
052            LOG.debug("finding Action Taken by actionTakenId " + actionTakenId);
053            Criteria crit = new Criteria();
054            crit.addEqualTo("actionTakenId", actionTakenId);
055            crit.addEqualTo("currentIndicator", Boolean.TRUE);
056            return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
057        }
058    
059        public Collection<ActionTakenValue> findByDocIdAndAction(String documentId, String action) {
060            LOG.debug("finding Action Taken by documentId " + documentId + " and action " + action);
061            Criteria crit = new Criteria();
062            crit.addEqualTo("documentId", documentId);
063            crit.addEqualTo("actionTaken", action);
064            crit.addEqualTo("currentIndicator", Boolean.TRUE);
065            return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
066        }
067    
068        public Collection<ActionTakenValue> findByDocumentId(String documentId) {
069            LOG.debug("finding Action Takens by documentId " + documentId);
070            Criteria crit = new Criteria();
071            crit.addEqualTo("documentId", documentId);
072            crit.addEqualTo("currentIndicator", Boolean.TRUE);
073    
074            QueryByCriteria qByCrit = new QueryByCriteria(ActionTakenValue.class, crit);
075    
076           qByCrit.addOrderByAscending("actionDate");
077    
078            return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(qByCrit);
079        }
080    
081        public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String principalId) {
082            LOG.debug("finding Action Takens by documentId " + documentId + " and principalId" + principalId);
083            Criteria crit = new Criteria();
084            crit.addEqualTo("documentId", documentId);
085            crit.addEqualTo("principalId", principalId);
086            crit.addEqualTo("currentIndicator", Boolean.TRUE);
087            return (List<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
088        }
089    
090        public List findByDocumentIdIgnoreCurrentInd(String documentId) {
091            LOG.debug("finding ActionsTaken ignoring currentInd by documentId:" + documentId);
092            Criteria crit = new Criteria();
093            crit.addEqualTo("documentId", documentId);
094            QueryByCriteria qByCrit = new QueryByCriteria(ActionTakenValue.class, crit);
095    
096           qByCrit.addOrderByAscending("actionDate");
097            return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(qByCrit);
098        }
099    
100        public void saveActionTaken(ActionTakenValue actionTaken) {
101            LOG.debug("saving ActionTaken");
102            checkNull(actionTaken.getDocumentId(), "Document ID");
103            checkNull(actionTaken.getActionTaken(), "action taken code");
104            checkNull(actionTaken.getDocVersion(), "doc version");
105            checkNull(actionTaken.getPrincipal(), "user principalId");
106    
107            if (actionTaken.getActionDate() == null) {
108                actionTaken.setActionDate(new Timestamp(System.currentTimeMillis()));
109            }
110            if (actionTaken.getCurrentIndicator() == null) {
111                actionTaken.setCurrentIndicator(Boolean.TRUE);
112            }
113            LOG.debug("saving ActionTaken: routeHeader " + actionTaken.getDocumentId() +
114                    ", actionTaken " + actionTaken.getActionTaken() + ", principalId " + actionTaken.getPrincipalId());
115            this.getPersistenceBrokerTemplate().store(actionTaken);
116        }
117    
118        //TODO perhaps runtime isn't the best here, maybe a dao runtime exception
119        private void checkNull(Object value, String valueName) throws RuntimeException {
120            if (value == null) {
121                throw new RuntimeException("Null value for " + valueName);
122            }
123        }
124    
125        public void deleteByDocumentId(String documentId){
126                Criteria crit = new Criteria();
127                crit.addEqualTo("documentId", documentId);
128                this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
129        }
130    
131        public boolean hasUserTakenAction(String principalId, String documentId) {
132            Criteria crit = new Criteria();
133                crit.addEqualTo("documentId", documentId);
134                crit.addEqualTo("principalId", principalId);
135                crit.addEqualTo("currentIndicator", Boolean.TRUE);
136            int count = getPersistenceBrokerTemplate().getCount(new QueryByCriteria(ActionTakenValue.class, crit));
137            return count > 0;
138        }
139    
140    }