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.kuali.rice.core.framework.persistence.jpa.OrmUtils;
019import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
020import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
021import org.kuali.rice.kew.actionrequest.ActionRequestValue;
022import org.kuali.rice.kew.actiontaken.ActionTakenValue;
023import org.kuali.rice.kew.actiontaken.dao.ActionTakenDAO;
024import org.kuali.rice.kew.api.action.ActionType;
025
026import javax.persistence.EntityManager;
027import javax.persistence.PersistenceContext;
028import java.io.Serializable;
029import java.sql.Timestamp;
030import java.util.Collection;
031import java.util.List;
032
033
034/**
035 * OJB implementation of the {@link ActionTakenDAO}.
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public class ActionTakenDAOJpaImpl implements ActionTakenDAO {
040
041        @PersistenceContext(unitName="kew-unit")
042        private EntityManager entityManager;
043
044    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDAOJpaImpl.class);
045
046    public ActionTakenValue load(String id) {
047        LOG.debug("Loading Action Taken for the given id " + id);
048        return entityManager.find(ActionTakenValue.class, id);
049    }
050
051    public void deleteActionTaken(ActionTakenValue actionTaken) {
052        LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
053        entityManager.remove(entityManager.find(ActionTakenValue.class, actionTaken.getActionTakenId()));
054    }
055
056    public ActionTakenValue findByActionTakenId(String actionTakenId) {
057        LOG.debug("finding Action Taken by actionTakenId " + actionTakenId);
058        Criteria crit = new Criteria(ActionTakenValue.class.getName());
059        crit.eq("actionTakenId", actionTakenId);
060        crit.eq("currentIndicator", Boolean.TRUE);
061        return (ActionTakenValue) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
062    }
063
064    public Collection<ActionTakenValue> findByDocIdAndAction(String documentId, String action) {
065        LOG.debug("finding Action Taken by documentId " + documentId + " and action " + action);
066        Criteria crit = new Criteria(ActionTakenValue.class.getName());
067        crit.eq("documentId", documentId);
068        crit.eq("actionTaken", action);
069        crit.eq("currentIndicator", Boolean.TRUE);
070        return (Collection<ActionTakenValue>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
071    }
072
073    public Collection<ActionTakenValue> findByDocumentId(String documentId) {
074        LOG.debug("finding Action Takens by documentId " + documentId);
075        Criteria crit = new Criteria(ActionTakenValue.class.getName());
076        crit.eq("documentId", documentId);
077        crit.eq("currentIndicator", Boolean.TRUE);
078        crit.orderBy("actionDate", true);
079        return (Collection<ActionTakenValue>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
080    }
081
082    public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String workflowId) {
083        LOG.debug("finding Action Takens by documentId " + documentId + " and workflowId" + workflowId);
084        Criteria crit = new Criteria(ActionTakenValue.class.getName());
085        crit.eq("documentId", documentId);
086        crit.eq("principalId", workflowId);
087        crit.eq("currentIndicator", Boolean.TRUE);
088        return (List<ActionTakenValue>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
089    }
090
091    public List findByDocumentIdIgnoreCurrentInd(String documentId) {
092        LOG.debug("finding ActionsTaken ignoring currentInd by documentId:" + documentId);
093        Criteria crit = new Criteria(ActionTakenValue.class.getName());
094        crit.eq("documentId", documentId);
095        return (List) new QueryByCriteria(entityManager, crit);
096    }
097
098    public void saveActionTaken(ActionTakenValue actionTaken) {
099        LOG.debug("saving ActionTaken");
100        checkNull(actionTaken.getDocumentId(), "Document ID");
101        checkNull(actionTaken.getActionTaken(), "action taken code");
102        checkNull(actionTaken.getDocVersion(), "doc version");
103        checkNull(actionTaken.getPrincipalId(), "principal ID");
104
105        if (actionTaken.getActionDate() == null) {
106            actionTaken.setActionDate(new Timestamp(System.currentTimeMillis()));
107        }
108        if (actionTaken.getCurrentIndicator() == null) {
109            actionTaken.setCurrentIndicator(Boolean.TRUE);
110        }
111        LOG.debug("saving ActionTaken: routeHeader " + actionTaken.getDocumentId() +
112                ", actionTaken " + actionTaken.getActionTaken() + ", principalId " + actionTaken.getPrincipalId());
113
114        if(actionTaken.getActionTakenId()==null){
115                entityManager.persist(actionTaken);
116        }else{
117                OrmUtils.merge(entityManager, actionTaken);
118        }
119    }
120
121    //TODO perhaps runtime isn't the best here, maybe a dao runtime exception
122    private void checkNull(Serializable value, String valueName) throws RuntimeException {
123        if (value == null) {
124            throw new RuntimeException("Null value for " + valueName);
125        }
126    }
127
128    public void deleteByDocumentId(String documentId){
129            Criteria crit = new Criteria(ActionRequestValue.class.getName());
130            crit.eq("documentId", documentId);
131            ActionRequestValue actionRequestValue = (ActionRequestValue) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
132            entityManager.remove(actionRequestValue);
133    }
134
135    public boolean hasUserTakenAction(String workflowId, String documentId) {
136        Criteria crit = new Criteria(ActionTakenValue.class.getName());
137            crit.eq("documentId", documentId);
138            crit.eq("principalId", workflowId);
139            crit.eq("currentIndicator", Boolean.TRUE);
140            long count = (Long) new QueryByCriteria(entityManager, crit).toCountQuery().getSingleResult();
141        return count > 0;
142    }
143
144    @Override
145    public Timestamp getLastActionTakenDate(String documentId, ActionType actionType) {
146        // TODO - fix this!
147
148        throw new UnsupportedOperationException("The JPA version of this method still needs to be implemented!");
149
150    }
151
152    public EntityManager getEntityManager() {
153        return this.entityManager;
154    }
155
156    public void setEntityManager(EntityManager entityManager) {
157        this.entityManager = entityManager;
158    }
159
160}