View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.actiontaken.dao.impl;
18  
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.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
25  
26  import java.sql.Timestamp;
27  import java.util.Collection;
28  import java.util.List;
29  
30  
31  /**
32   * OJB implementation of the {@link ActionTakenDAO}.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class ActionTakenDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionTakenDAO {
37  
38      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDAOOjbImpl.class);
39  
40      public ActionTakenValue load(Long id) {
41          LOG.debug("Loading Action Taken for the given id " + id);
42          Criteria crit = new Criteria();
43          crit.addEqualTo("actionTakenId", id);
44          return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
45      }
46  
47      public void deleteActionTaken(ActionTakenValue actionTaken) {
48          LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
49          this.getPersistenceBrokerTemplate().delete(actionTaken);
50      }
51  
52      public ActionTakenValue findByActionTakenId(Long actionTakenId) {
53          LOG.debug("finding Action Taken by actionTakenId " + actionTakenId);
54          Criteria crit = new Criteria();
55          crit.addEqualTo("actionTakenId", actionTakenId);
56          crit.addEqualTo("currentIndicator", Boolean.TRUE);
57          return (ActionTakenValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
58      }
59  
60      public Collection<ActionTakenValue> findByDocIdAndAction(Long routeHeaderId, String action) {
61          LOG.debug("finding Action Taken by routeHeaderId " + routeHeaderId + " and action " + action);
62          Criteria crit = new Criteria();
63          crit.addEqualTo("routeHeaderId", routeHeaderId);
64          crit.addEqualTo("actionTaken", action);
65          crit.addEqualTo("currentIndicator", Boolean.TRUE);
66          return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
67      }
68  
69      public Collection<ActionTakenValue> findByRouteHeaderId(Long routeHeaderId) {
70          LOG.debug("finding Action Takens by routeHeaderId " + routeHeaderId);
71          Criteria crit = new Criteria();
72          crit.addEqualTo("routeHeaderId", routeHeaderId);
73          crit.addEqualTo("currentIndicator", Boolean.TRUE);
74          return (Collection<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
75      }
76  
77      public List<ActionTakenValue> findByRouteHeaderIdWorkflowId(Long routeHeaderId, String principalId) {
78          LOG.debug("finding Action Takens by routeHeaderId " + routeHeaderId + " and principalId" + principalId);
79          Criteria crit = new Criteria();
80          crit.addEqualTo("routeHeaderId", routeHeaderId);
81          crit.addEqualTo("principalId", principalId);
82          crit.addEqualTo("currentIndicator", Boolean.TRUE);
83          return (List<ActionTakenValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
84      }
85  
86      public List findByRouteHeaderIdIgnoreCurrentInd(Long routeHeaderId) {
87          LOG.debug("finding ActionsTaken ignoring currentInd by routeHeaderId:" + routeHeaderId);
88          Criteria crit = new Criteria();
89          crit.addEqualTo("routeHeaderId", routeHeaderId);
90          return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionTakenValue.class, crit));
91      }
92  
93      public void saveActionTaken(ActionTakenValue actionTaken) {
94          LOG.debug("saving ActionTaken");
95          checkNull(actionTaken.getRouteHeaderId(), "Document ID");
96          checkNull(actionTaken.getActionTaken(), "action taken code");
97          checkNull(actionTaken.getDocVersion(), "doc version");
98          checkNull(actionTaken.getPrincipal(), "user principalId");
99  
100         if (actionTaken.getActionDate() == null) {
101             actionTaken.setActionDate(new Timestamp(System.currentTimeMillis()));
102         }
103         if (actionTaken.getCurrentIndicator() == null) {
104             actionTaken.setCurrentIndicator(Boolean.TRUE);
105         }
106         LOG.debug("saving ActionTaken: routeHeader " + actionTaken.getRouteHeaderId() +
107                 ", actionTaken " + actionTaken.getActionTaken() + ", principalId " + actionTaken.getPrincipalId());
108         this.getPersistenceBrokerTemplate().store(actionTaken);
109     }
110 
111     //TODO perhaps runtime isn't the best here, maybe a dao runtime exception
112     private void checkNull(Object value, String valueName) throws RuntimeException {
113         if (value == null) {
114             throw new RuntimeException("Null value for " + valueName);
115         }
116     }
117 
118     public void deleteByRouteHeaderId(Long routeHeaderId){
119 	    Criteria crit = new Criteria();
120 	    crit.addEqualTo("routeHeaderId", routeHeaderId);
121 	    this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionRequestValue.class, crit));
122     }
123 
124     public boolean hasUserTakenAction(String principalId, Long routeHeaderId) {
125     	Criteria crit = new Criteria();
126 	    crit.addEqualTo("routeHeaderId", routeHeaderId);
127 	    crit.addEqualTo("principalId", principalId);
128 	    crit.addEqualTo("currentIndicator", Boolean.TRUE);
129         int count = getPersistenceBrokerTemplate().getCount(new QueryByCriteria(ActionTakenValue.class, crit));
130         return count > 0;
131     }
132 
133 }