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