Coverage Report - org.kuali.rice.kew.actiontaken.dao.impl.ActionTakenDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionTakenDAOJpaImpl
0%
0/65
0%
0/10
1.385
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.actiontaken.dao.impl;
 18  
 
 19  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 20  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
 21  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
 22  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 23  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 24  
 import org.kuali.rice.kew.actiontaken.dao.ActionTakenDAO;
 25  
 
 26  
 import javax.persistence.EntityManager;
 27  
 import javax.persistence.PersistenceContext;
 28  
 import java.io.Serializable;
 29  
 import java.sql.Timestamp;
 30  
 import java.util.Collection;
 31  
 import java.util.List;
 32  
 
 33  
 
 34  
 /**
 35  
  * OJB implementation of the {@link ActionTakenDAO}.
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  0
 public class ActionTakenDAOJpaImpl implements ActionTakenDAO {
 40  
 
 41  
         @PersistenceContext(unitName="kew-unit")
 42  
         private EntityManager entityManager;
 43  
 
 44  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ActionTakenDAOJpaImpl.class);
 45  
 
 46  
     public ActionTakenValue load(String id) {
 47  0
         LOG.debug("Loading Action Taken for the given id " + id);
 48  0
         return entityManager.find(ActionTakenValue.class, id);
 49  
     }
 50  
 
 51  
     public void deleteActionTaken(ActionTakenValue actionTaken) {
 52  0
         LOG.debug("deleting ActionTaken " + actionTaken.getActionTakenId());
 53  0
         entityManager.remove(entityManager.find(ActionTakenValue.class, actionTaken.getActionTakenId()));
 54  0
     }
 55  
 
 56  
     public ActionTakenValue findByActionTakenId(String actionTakenId) {
 57  0
         LOG.debug("finding Action Taken by actionTakenId " + actionTakenId);
 58  0
         Criteria crit = new Criteria(ActionTakenValue.class.getName());
 59  0
         crit.eq("actionTakenId", actionTakenId);
 60  0
         crit.eq("currentIndicator", Boolean.TRUE);
 61  0
         return (ActionTakenValue) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 62  
     }
 63  
 
 64  
     public Collection<ActionTakenValue> findByDocIdAndAction(String documentId, String action) {
 65  0
         LOG.debug("finding Action Taken by documentId " + documentId + " and action " + action);
 66  0
         Criteria crit = new Criteria(ActionTakenValue.class.getName());
 67  0
         crit.eq("documentId", documentId);
 68  0
         crit.eq("actionTaken", action);
 69  0
         crit.eq("currentIndicator", Boolean.TRUE);
 70  0
         return (Collection<ActionTakenValue>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 71  
     }
 72  
 
 73  
     public Collection<ActionTakenValue> findByDocumentId(String documentId) {
 74  0
         LOG.debug("finding Action Takens by documentId " + documentId);
 75  0
         Criteria crit = new Criteria(ActionTakenValue.class.getName());
 76  0
         crit.eq("documentId", documentId);
 77  0
         crit.eq("currentIndicator", Boolean.TRUE);
 78  0
         crit.orderBy("actionDate", true);
 79  0
         return (Collection<ActionTakenValue>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 80  
     }
 81  
 
 82  
     public List<ActionTakenValue> findByDocumentIdWorkflowId(String documentId, String workflowId) {
 83  0
         LOG.debug("finding Action Takens by documentId " + documentId + " and workflowId" + workflowId);
 84  0
         Criteria crit = new Criteria(ActionTakenValue.class.getName());
 85  0
         crit.eq("documentId", documentId);
 86  0
         crit.eq("principalId", workflowId);
 87  0
         crit.eq("currentIndicator", Boolean.TRUE);
 88  0
         return (List<ActionTakenValue>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 89  
     }
 90  
 
 91  
     public List findByDocumentIdIgnoreCurrentInd(String documentId) {
 92  0
         LOG.debug("finding ActionsTaken ignoring currentInd by documentId:" + documentId);
 93  0
         Criteria crit = new Criteria(ActionTakenValue.class.getName());
 94  0
         crit.eq("documentId", documentId);
 95  0
         return (List) new QueryByCriteria(entityManager, crit);
 96  
     }
 97  
 
 98  
     public void saveActionTaken(ActionTakenValue actionTaken) {
 99  0
         LOG.debug("saving ActionTaken");
 100  0
         checkNull(actionTaken.getDocumentId(), "Document ID");
 101  0
         checkNull(actionTaken.getActionTaken(), "action taken code");
 102  0
         checkNull(actionTaken.getDocVersion(), "doc version");
 103  0
         checkNull(actionTaken.getPrincipalId(), "principal ID");
 104  
 
 105  0
         if (actionTaken.getActionDate() == null) {
 106  0
             actionTaken.setActionDate(new Timestamp(System.currentTimeMillis()));
 107  
         }
 108  0
         if (actionTaken.getCurrentIndicator() == null) {
 109  0
             actionTaken.setCurrentIndicator(Boolean.TRUE);
 110  
         }
 111  0
         LOG.debug("saving ActionTaken: routeHeader " + actionTaken.getDocumentId() +
 112  
                 ", actionTaken " + actionTaken.getActionTaken() + ", principalId " + actionTaken.getPrincipalId());
 113  
 
 114  0
         if(actionTaken.getActionTakenId()==null){
 115  0
                 entityManager.persist(actionTaken);
 116  
         }else{
 117  0
                 OrmUtils.merge(entityManager, actionTaken);
 118  
         }
 119  0
     }
 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  0
         if (value == null) {
 124  0
             throw new RuntimeException("Null value for " + valueName);
 125  
         }
 126  0
     }
 127  
 
 128  
     public void deleteByDocumentId(String documentId){
 129  0
             Criteria crit = new Criteria(ActionRequestValue.class.getName());
 130  0
             crit.eq("documentId", documentId);
 131  0
             ActionRequestValue actionRequestValue = (ActionRequestValue) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 132  0
             entityManager.remove(actionRequestValue);
 133  0
     }
 134  
 
 135  
     public boolean hasUserTakenAction(String workflowId, String documentId) {
 136  0
             Criteria crit = new Criteria(ActionTakenValue.class.getName());
 137  0
             crit.eq("documentId", documentId);
 138  0
             crit.eq("principalId", workflowId);
 139  0
             crit.eq("currentIndicator", Boolean.TRUE);
 140  0
             long count = (Long) new QueryByCriteria(entityManager, crit).toCountQuery().getSingleResult();
 141  0
         return count > 0;
 142  
     }
 143  
 
 144  
     public EntityManager getEntityManager() {
 145  0
         return this.entityManager;
 146  
     }
 147  
 
 148  
     public void setEntityManager(EntityManager entityManager) {
 149  0
         this.entityManager = entityManager;
 150  0
     }
 151  
 
 152  
 }