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.commons.lang.StringUtils;
019import org.kuali.rice.kew.actiontaken.dao.ActionTakenDao;
020import org.kuali.rice.kew.api.action.ActionType;
021
022import javax.persistence.EntityManager;
023import javax.persistence.NoResultException;
024import javax.persistence.TypedQuery;
025import java.sql.Timestamp;
026
027
028/**
029 * JPA implementation of the {@link org.kuali.rice.kew.actiontaken.dao.ActionTakenDao}.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class ActionTakenDaoJpa implements ActionTakenDao {
034
035        private EntityManager entityManager;
036
037    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDaoJpa.class);
038
039    public static final String GET_LAST_ACTION_TAKEN_DATE_NAME = "ActionTakenValue.getLastActionTakenDate";
040    public static final String GET_LAST_ACTION_TAKEN_DATE_QUERY =
041            "SELECT max(a.actionDate) from ActionTakenValue a where a.documentId = :documentId and a.actionTaken=:actionTaken";
042
043    public Timestamp getLastActionTakenDate(String documentId, ActionType actionType) {
044        if (StringUtils.isBlank(documentId) || actionType == null) {
045            throw new IllegalArgumentException("Both documentId and actionType must be non-null");
046        }
047        TypedQuery<Timestamp> query =
048                entityManager.createNamedQuery(GET_LAST_ACTION_TAKEN_DATE_NAME, Timestamp.class);
049        query.setParameter("documentId", documentId);
050        query.setParameter("actionTaken", actionType.getCode());
051        try {
052            return query.getSingleResult();
053        } catch (NoResultException e) {
054            return null;
055        }
056    }
057
058    public EntityManager getEntityManager() {
059        return this.entityManager;
060    }
061
062    public void setEntityManager(EntityManager entityManager) {
063        this.entityManager = entityManager;
064    }
065
066}