View Javadoc
1   /**
2    * Copyright 2005-2016 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.rice.kew.actiontaken.dao.impl;
17  
18  import org.apache.ojb.broker.PersistenceBroker;
19  import org.apache.ojb.broker.query.Criteria;
20  import org.apache.ojb.broker.query.QueryByCriteria;
21  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
22  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
23  import org.kuali.rice.kew.actiontaken.dao.ActionTakenDAO;
24  import org.kuali.rice.kew.api.WorkflowRuntimeException;
25  import org.kuali.rice.kew.api.action.ActionType;
26  import org.springmodules.orm.ojb.PersistenceBrokerCallback;
27  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
28  
29  import java.sql.Connection;
30  import java.sql.PreparedStatement;
31  import java.sql.ResultSet;
32  import java.sql.SQLException;
33  import java.sql.Timestamp;
34  import java.util.Collection;
35  import java.util.List;
36  
37  
38  /**
39   * OJB implementation of the {@link ActionTakenDAO}.
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class ActionTakenDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionTakenDAO {
44  
45      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDAOOjbImpl.class);
46  
47      public ActionTakenValue load(String id) {
48          LOG.debug("Loading Action Taken for the given id " + id);
49          Criteria crit = new Criteria();
50          crit.addEqualTo("actionTakenId", id);
51          return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
52      }
53  
54      public void deleteActionTaken(ActionTakenValue actionTaken) {
55          LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
56          this.getPersistenceBrokerTemplate().delete(actionTaken);
57      }
58  
59      public ActionTakenValue findByActionTakenId(String actionTakenId) {
60          LOG.debug("finding Action Taken by actionTakenId " + actionTakenId);
61          Criteria crit = new Criteria();
62          crit.addEqualTo("actionTakenId", actionTakenId);
63          crit.addEqualTo("currentIndicator", Boolean.TRUE);
64          return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
65      }
66  
67      public Collection<ActionTakenValue> findByDocIdAndAction(String documentId, String action) {
68          LOG.debug("finding Action Taken by documentId " + documentId + " and action " + action);
69          Criteria crit = new Criteria();
70          crit.addEqualTo("documentId", documentId);
71          crit.addEqualTo("actionTaken", action);
72          crit.addEqualTo("currentIndicator", Boolean.TRUE);
73          return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
74      }
75  
76      public Collection<ActionTakenValue> findByDocumentId(String documentId) {
77          LOG.debug("finding Action Takens by documentId " + documentId);
78          Criteria crit = new Criteria();
79          crit.addEqualTo("documentId", documentId);
80          crit.addEqualTo("currentIndicator", Boolean.TRUE);
81  
82          QueryByCriteria qByCrit = new QueryByCriteria(ActionTakenValue.class, crit);
83  
84         qByCrit.addOrderByAscending("actionDate");
85  
86          return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(qByCrit);
87      }
88  
89      public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String principalId) {
90          LOG.debug("finding Action Takens by documentId " + documentId + " and principalId" + principalId);
91          Criteria crit = new Criteria();
92          crit.addEqualTo("documentId", documentId);
93          crit.addEqualTo("principalId", principalId);
94          crit.addEqualTo("currentIndicator", Boolean.TRUE);
95          return (List<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
96      }
97  
98      public List findByDocumentIdIgnoreCurrentInd(String documentId) {
99          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 }