View Javadoc
1   /**
2    * Copyright 2005-2015 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.commons.lang.StringUtils;
19  import org.kuali.rice.kew.actiontaken.dao.ActionTakenDao;
20  import org.kuali.rice.kew.api.action.ActionType;
21  
22  import javax.persistence.EntityManager;
23  import javax.persistence.NoResultException;
24  import javax.persistence.TypedQuery;
25  import java.sql.Timestamp;
26  
27  
28  /**
29   * JPA implementation of the {@link org.kuali.rice.kew.actiontaken.dao.ActionTakenDao}.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class ActionTakenDaoJpa implements ActionTakenDao {
34  
35  	private EntityManager entityManager;
36  
37      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDaoJpa.class);
38  
39      public static final String GET_LAST_ACTION_TAKEN_DATE_NAME = "ActionTakenValue.getLastActionTakenDate";
40      public static final String GET_LAST_ACTION_TAKEN_DATE_QUERY =
41              "SELECT max(a.actionDate) from ActionTakenValue a where a.documentId = :documentId and a.actionTaken=:actionTaken";
42  
43      public Timestamp getLastActionTakenDate(String documentId, ActionType actionType) {
44          if (StringUtils.isBlank(documentId) || actionType == null) {
45              throw new IllegalArgumentException("Both documentId and actionType must be non-null");
46          }
47          TypedQuery<Timestamp> query =
48                  entityManager.createNamedQuery(GET_LAST_ACTION_TAKEN_DATE_NAME, Timestamp.class);
49          query.setParameter("documentId", documentId);
50          query.setParameter("actionTaken", actionType.getCode());
51          try {
52              return query.getSingleResult();
53          } catch (NoResultException e) {
54              return null;
55          }
56      }
57  
58      public EntityManager getEntityManager() {
59          return this.entityManager;
60      }
61  
62      public void setEntityManager(EntityManager entityManager) {
63          this.entityManager = entityManager;
64      }
65  
66  }