001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.actiontaken.dao.impl;
017
018import org.apache.ojb.broker.PersistenceBroker;
019import org.apache.ojb.broker.query.Criteria;
020import org.apache.ojb.broker.query.QueryByCriteria;
021import org.kuali.rice.kew.actionrequest.ActionRequestValue;
022import org.kuali.rice.kew.actiontaken.ActionTakenValue;
023import org.kuali.rice.kew.actiontaken.dao.ActionTakenDAO;
024import org.kuali.rice.kew.api.WorkflowRuntimeException;
025import org.kuali.rice.kew.api.action.ActionType;
026import org.springmodules.orm.ojb.PersistenceBrokerCallback;
027import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
028
029import java.sql.Connection;
030import java.sql.PreparedStatement;
031import java.sql.ResultSet;
032import java.sql.SQLException;
033import java.sql.Timestamp;
034import java.util.Collection;
035import java.util.List;
036
037
038/**
039 * OJB implementation of the {@link ActionTakenDAO}.
040 *
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043public class ActionTakenDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionTakenDAO {
044
045    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDAOOjbImpl.class);
046
047    public ActionTakenValue load(String id) {
048        LOG.debug("Loading Action Taken for the given id " + id);
049        Criteria crit = new Criteria();
050        crit.addEqualTo("actionTakenId", id);
051        return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
052    }
053
054    public void deleteActionTaken(ActionTakenValue actionTaken) {
055        LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
056        this.getPersistenceBrokerTemplate().delete(actionTaken);
057    }
058
059    public ActionTakenValue findByActionTakenId(String actionTakenId) {
060        LOG.debug("finding Action Taken by actionTakenId " + actionTakenId);
061        Criteria crit = new Criteria();
062        crit.addEqualTo("actionTakenId", actionTakenId);
063        crit.addEqualTo("currentIndicator", Boolean.TRUE);
064        return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
065    }
066
067    public Collection<ActionTakenValue> findByDocIdAndAction(String documentId, String action) {
068        LOG.debug("finding Action Taken by documentId " + documentId + " and action " + action);
069        Criteria crit = new Criteria();
070        crit.addEqualTo("documentId", documentId);
071        crit.addEqualTo("actionTaken", action);
072        crit.addEqualTo("currentIndicator", Boolean.TRUE);
073        return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
074    }
075
076    public Collection<ActionTakenValue> findByDocumentId(String documentId) {
077        LOG.debug("finding Action Takens by documentId " + documentId);
078        Criteria crit = new Criteria();
079        crit.addEqualTo("documentId", documentId);
080        crit.addEqualTo("currentIndicator", Boolean.TRUE);
081
082        QueryByCriteria qByCrit = new QueryByCriteria(ActionTakenValue.class, crit);
083
084       qByCrit.addOrderByAscending("actionDate");
085
086        return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(qByCrit);
087    }
088
089    public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String principalId) {
090        LOG.debug("finding Action Takens by documentId " + documentId + " and principalId" + principalId);
091        Criteria crit = new Criteria();
092        crit.addEqualTo("documentId", documentId);
093        crit.addEqualTo("principalId", principalId);
094        crit.addEqualTo("currentIndicator", Boolean.TRUE);
095        return (List<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
096    }
097
098    public List findByDocumentIdIgnoreCurrentInd(String documentId) {
099        LOG.debug("finding ActionsTaken ignoring currentInd by documentId:" + documentId);
100        Criteria crit = new Criteria();
101        crit.addEqualTo("documentId", documentId);
102        QueryByCriteria qByCrit = new QueryByCriteria(ActionTakenValue.class, crit);
103
104       qByCrit.addOrderByAscending("actionDate");
105        return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(qByCrit);
106    }
107
108    public void saveActionTaken(ActionTakenValue actionTaken) {
109        LOG.debug("saving ActionTaken");
110        checkNull(actionTaken.getDocumentId(), "Document ID");
111        checkNull(actionTaken.getActionTaken(), "action taken code");
112        checkNull(actionTaken.getDocVersion(), "doc version");
113        checkNull(actionTaken.getPrincipal(), "user principalId");
114
115        if (actionTaken.getActionDate() == null) {
116            actionTaken.setActionDate(new Timestamp(System.currentTimeMillis()));
117        }
118        if (actionTaken.getCurrentIndicator() == null) {
119            actionTaken.setCurrentIndicator(Boolean.TRUE);
120        }
121        LOG.debug("saving ActionTaken: routeHeader " + actionTaken.getDocumentId() +
122                ", actionTaken " + actionTaken.getActionTaken() + ", principalId " + actionTaken.getPrincipalId());
123        this.getPersistenceBrokerTemplate().store(actionTaken);
124    }
125
126    //TODO perhaps runtime isn't the best here, maybe a dao runtime exception
127    private void checkNull(Object value, String valueName) throws RuntimeException {
128        if (value == null) {
129            throw new RuntimeException("Null value for " + valueName);
130        }
131    }
132
133    public void deleteByDocumentId(String documentId){
134            Criteria crit = new Criteria();
135            crit.addEqualTo("documentId", documentId);
136            this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
137    }
138
139    public boolean hasUserTakenAction(String principalId, String documentId) {
140        Criteria crit = new Criteria();
141            crit.addEqualTo("documentId", documentId);
142            crit.addEqualTo("principalId", principalId);
143            crit.addEqualTo("currentIndicator", Boolean.TRUE);
144        int count = getPersistenceBrokerTemplate().getCount(new QueryByCriteria(ActionTakenValue.class, crit));
145        return count > 0;
146    }
147
148    private static final String LAST_ACTION_TAKEN_DATE_QUERY =
149            "select max(ACTN_DT) from KREW_ACTN_TKN_T where DOC_HDR_ID=? and ACTN_CD=?";
150
151    public Timestamp getLastActionTakenDate(final String documentId, final ActionType actionType) {
152        return (Timestamp) getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
153            public Object doInPersistenceBroker(PersistenceBroker broker) {
154                PreparedStatement statement = null;
155                ResultSet resultSet = null;
156                try {
157                    Connection connection = broker.serviceConnectionManager().getConnection();
158                    statement = connection.prepareStatement(LAST_ACTION_TAKEN_DATE_QUERY);
159                    statement.setString(1, documentId);
160                    statement.setString(2, actionType.getCode());
161                    resultSet = statement.executeQuery();
162                    if (!resultSet.next()) {
163                        return null;
164                    } else {
165                        return resultSet.getTimestamp(1);
166                    }
167                } catch (Exception e) {
168                    throw new WorkflowRuntimeException("Error determining Last Action Taken Date.", e);
169                } finally {
170                    if (statement != null) {
171                        try {
172                            statement.close();
173                        } catch (SQLException e) {
174                        }
175                    }
176                    if (resultSet != null) {
177                        try {
178                            resultSet.close();
179                        } catch (SQLException e) {
180                        }
181                    }
182                }
183            }
184        });
185    }
186
187}