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