View Javadoc

1   /**
2    * Copyright 2005-2011 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.query.Criteria;
19  import org.apache.ojb.broker.query.QueryByCriteria;
20  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
21  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
22  import org.kuali.rice.kew.actiontaken.dao.ActionTakenDAO;
23  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
24  
25  import java.sql.Timestamp;
26  import java.util.Collection;
27  import java.util.List;
28  
29  
30  /**
31   * OJB implementation of the {@link ActionTakenDAO}.
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class ActionTakenDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionTakenDAO {
36  
37      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDAOOjbImpl.class);
38  
39      public ActionTakenValue load(String id) {
40          LOG.debug("Loading Action Taken for the given id " + id);
41          Criteria crit = new Criteria();
42          crit.addEqualTo("actionTakenId", id);
43          return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
44      }
45  
46      public void deleteActionTaken(ActionTakenValue actionTaken) {
47          LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
48          this.getPersistenceBrokerTemplate().delete(actionTaken);
49      }
50  
51      public ActionTakenValue findByActionTakenId(String actionTakenId) {
52          LOG.debug("finding Action Taken by actionTakenId " + actionTakenId);
53          Criteria crit = new Criteria();
54          crit.addEqualTo("actionTakenId", actionTakenId);
55          crit.addEqualTo("currentIndicator", Boolean.TRUE);
56          return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
57      }
58  
59      public Collection<ActionTakenValue> findByDocIdAndAction(String documentId, String action) {
60          LOG.debug("finding Action Taken by documentId " + documentId + " and action " + action);
61          Criteria crit = new Criteria();
62          crit.addEqualTo("documentId", documentId);
63          crit.addEqualTo("actionTaken", action);
64          crit.addEqualTo("currentIndicator", Boolean.TRUE);
65          return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
66      }
67  
68      public Collection<ActionTakenValue> findByDocumentId(String documentId) {
69          LOG.debug("finding Action Takens by documentId " + documentId);
70          Criteria crit = new Criteria();
71          crit.addEqualTo("documentId", documentId);
72          crit.addEqualTo("currentIndicator", Boolean.TRUE);
73  
74          QueryByCriteria qByCrit = new QueryByCriteria(ActionTakenValue.class, crit);
75  
76         qByCrit.addOrderByAscending("actionDate");
77  
78          return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(qByCrit);
79      }
80  
81      public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String principalId) {
82          LOG.debug("finding Action Takens by documentId " + documentId + " and principalId" + principalId);
83          Criteria crit = new Criteria();
84          crit.addEqualTo("documentId", documentId);
85          crit.addEqualTo("principalId", principalId);
86          crit.addEqualTo("currentIndicator", Boolean.TRUE);
87          return (List<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
88      }
89  
90      public List findByDocumentIdIgnoreCurrentInd(String documentId) {
91          LOG.debug("finding ActionsTaken ignoring currentInd by documentId:" + documentId);
92          Criteria crit = new Criteria();
93          crit.addEqualTo("documentId", documentId);
94          return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
95      }
96  
97      public void saveActionTaken(ActionTakenValue actionTaken) {
98          LOG.debug("saving ActionTaken");
99          checkNull(actionTaken.getDocumentId(), "Document ID");
100         checkNull(actionTaken.getActionTaken(), "action taken code");
101         checkNull(actionTaken.getDocVersion(), "doc version");
102         checkNull(actionTaken.getPrincipal(), "user principalId");
103 
104         if (actionTaken.getActionDate() == null) {
105             actionTaken.setActionDate(new Timestamp(System.currentTimeMillis()));
106         }
107         if (actionTaken.getCurrentIndicator() == null) {
108             actionTaken.setCurrentIndicator(Boolean.TRUE);
109         }
110         LOG.debug("saving ActionTaken: routeHeader " + actionTaken.getDocumentId() +
111                 ", actionTaken " + actionTaken.getActionTaken() + ", principalId " + actionTaken.getPrincipalId());
112         this.getPersistenceBrokerTemplate().store(actionTaken);
113     }
114 
115     //TODO perhaps runtime isn't the best here, maybe a dao runtime exception
116     private void checkNull(Object value, String valueName) throws RuntimeException {
117         if (value == null) {
118             throw new RuntimeException("Null value for " + valueName);
119         }
120     }
121 
122     public void deleteByDocumentId(String documentId){
123 	    Criteria crit = new Criteria();
124 	    crit.addEqualTo("documentId", documentId);
125 	    this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
126     }
127 
128     public boolean hasUserTakenAction(String principalId, String documentId) {
129     	Criteria crit = new Criteria();
130 	    crit.addEqualTo("documentId", documentId);
131 	    crit.addEqualTo("principalId", principalId);
132 	    crit.addEqualTo("currentIndicator", Boolean.TRUE);
133         int count = getPersistenceBrokerTemplate().getCount(new QueryByCriteria(ActionTakenValue.class, crit));
134         return count > 0;
135     }
136 
137 }