|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.kew.engine.node.dao.impl;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.util.ArrayList;  | 
  |  19 |     | 
   import java.util.Iterator;  | 
  |  20 |     | 
   import java.util.List;  | 
  |  21 |     | 
     | 
  |  22 |     | 
   import javax.persistence.EntityManager;  | 
  |  23 |     | 
   import javax.persistence.PersistenceContext;  | 
  |  24 |     | 
   import javax.persistence.Query;  | 
  |  25 |     | 
     | 
  |  26 |     | 
   import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;  | 
  |  27 |     | 
   import org.kuali.rice.kew.api.KEWPropertyConstants;  | 
  |  28 |     | 
   import org.kuali.rice.kew.engine.node.Branch;  | 
  |  29 |     | 
   import org.kuali.rice.kew.engine.node.NodeState;  | 
  |  30 |     | 
   import org.kuali.rice.kew.engine.node.RouteNode;  | 
  |  31 |     | 
   import org.kuali.rice.kew.engine.node.RouteNodeInstance;  | 
  |  32 |     | 
   import org.kuali.rice.kew.engine.node.dao.RouteNodeDAO;  | 
  |  33 |     | 
     | 
  |  34 |    0 |    public class RouteNodeDAOJpaImpl implements RouteNodeDAO { | 
  |  35 |     | 
     | 
  |  36 |     | 
           @PersistenceContext(unitName="kew-unit")  | 
  |  37 |     | 
           EntityManager entityManager;  | 
  |  38 |     | 
             | 
  |  39 |     | 
         | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
           public EntityManager getEntityManager() { | 
  |  43 |    0 |                    return this.entityManager;  | 
  |  44 |     | 
           }  | 
  |  45 |     | 
     | 
  |  46 |     | 
             | 
  |  47 |     | 
     | 
  |  48 |     | 
     | 
  |  49 |     | 
           public void setEntityManager(EntityManager entityManager) { | 
  |  50 |    0 |                    this.entityManager = entityManager;  | 
  |  51 |    0 |            }  | 
  |  52 |     | 
     | 
  |  53 |     | 
           public void save(RouteNode node) { | 
  |  54 |    0 |                if (node.getRouteNodeId() == null){ | 
  |  55 |    0 |                        entityManager.persist(node);  | 
  |  56 |     | 
               } else { | 
  |  57 |    0 |                        OrmUtils.merge(entityManager, node);  | 
  |  58 |     | 
               }  | 
  |  59 |    0 |        }  | 
  |  60 |     | 
     | 
  |  61 |     | 
       public void save(RouteNodeInstance nodeInstance) { | 
  |  62 |    0 |                if (nodeInstance.getRouteNodeInstanceId() == null){ | 
  |  63 |    0 |                        entityManager.persist(nodeInstance);  | 
  |  64 |     | 
               } else { | 
  |  65 |    0 |                        OrmUtils.merge(entityManager, nodeInstance);  | 
  |  66 |     | 
               }  | 
  |  67 |    0 |        }  | 
  |  68 |     | 
     | 
  |  69 |     | 
       public void save(NodeState nodeState) { | 
  |  70 |    0 |                if (nodeState.getNodeStateId() == null){ | 
  |  71 |    0 |                        entityManager.persist(nodeState);  | 
  |  72 |     | 
               } else { | 
  |  73 |    0 |                        OrmUtils.merge(entityManager, nodeState);  | 
  |  74 |     | 
               }  | 
  |  75 |    0 |        }  | 
  |  76 |     | 
     | 
  |  77 |     | 
       public void save(Branch branch) {            | 
  |  78 |    0 |                if (branch.getBranchId() == null){ | 
  |  79 |    0 |                        entityManager.persist(branch);  | 
  |  80 |     | 
               } else { | 
  |  81 |    0 |                        OrmUtils.merge(entityManager, branch);  | 
  |  82 |     | 
               }  | 
  |  83 |    0 |        }  | 
  |  84 |     | 
     | 
  |  85 |     | 
       public RouteNode findRouteNodeById(String nodeId) { | 
  |  86 |    0 |                Query query = entityManager.createNamedQuery("RouteNode.FindByRouteNodeId"); | 
  |  87 |    0 |                query.setParameter(KEWPropertyConstants.ROUTE_NODE_ID, nodeId);  | 
  |  88 |    0 |                return (RouteNode) query.getSingleResult();  | 
  |  89 |     | 
       }  | 
  |  90 |     | 
     | 
  |  91 |     | 
       public RouteNodeInstance findRouteNodeInstanceById(String nodeInstanceId) { | 
  |  92 |    0 |                Query query = entityManager.createNamedQuery("RouteNodeInstance.FindByRouteNodeInstanceId"); | 
  |  93 |    0 |                query.setParameter(KEWPropertyConstants.ROUTE_NODE_INSTANCE_ID, nodeInstanceId);  | 
  |  94 |     | 
     | 
  |  95 |    0 |                        return (RouteNodeInstance) query.getSingleResult();                   | 
  |  96 |     | 
       }  | 
  |  97 |     | 
     | 
  |  98 |     | 
       @SuppressWarnings("unchecked") | 
  |  99 |     | 
       public List<RouteNodeInstance> getActiveNodeInstances(String documentId) { | 
  |  100 |    0 |                Query query = entityManager.createNamedQuery("RouteNodeInstance.FindActiveNodeInstances"); | 
  |  101 |    0 |                query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId);  | 
  |  102 |    0 |                return (List<RouteNodeInstance>)query.getResultList();  | 
  |  103 |     | 
       }  | 
  |  104 |     | 
     | 
  |  105 |     | 
       @SuppressWarnings("unchecked") | 
  |  106 |     | 
       public List<RouteNodeInstance> getTerminalNodeInstances(String documentId) { | 
  |  107 |    0 |                Query query = entityManager.createNamedQuery("RouteNodeInstance.FindTerminalNodeInstances"); | 
  |  108 |    0 |                query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId);  | 
  |  109 |     | 
                     | 
  |  110 |     | 
                     | 
  |  111 |    0 |                    List<RouteNodeInstance> terminalNodes = new ArrayList<RouteNodeInstance>();  | 
  |  112 |    0 |                    List<RouteNodeInstance> routeNodeInstances = (List<RouteNodeInstance>) query.getResultList();  | 
  |  113 |    0 |                    for (RouteNodeInstance routeNodeInstance : routeNodeInstances) { | 
  |  114 |    0 |                        if (routeNodeInstance.getNextNodeInstances().isEmpty()) { | 
  |  115 |    0 |                                terminalNodes.add(routeNodeInstance);  | 
  |  116 |     | 
                       }  | 
  |  117 |     | 
                   }  | 
  |  118 |    0 |                    return terminalNodes;  | 
  |  119 |     | 
       }  | 
  |  120 |     | 
     | 
  |  121 |     | 
       public List getInitialNodeInstances(String documentId) { | 
  |  122 |     | 
                 | 
  |  123 |    0 |                Query query = entityManager.createNamedQuery("RouteNodeInstance.FindInitialNodeInstances"); | 
  |  124 |    0 |                query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId);  | 
  |  125 |    0 |                    return (List)query.getResultList();  | 
  |  126 |     | 
       }  | 
  |  127 |     | 
     | 
  |  128 |     | 
       public NodeState findNodeState(Long nodeInstanceId, String key) { | 
  |  129 |    0 |                Query query = entityManager.createNamedQuery("NodeState.FindNodeState"); | 
  |  130 |    0 |                query.setParameter(KEWPropertyConstants.NODE_INSTANCE_ID, nodeInstanceId);  | 
  |  131 |    0 |                query.setParameter(KEWPropertyConstants.KEY, key);  | 
  |  132 |    0 |                    return (NodeState) query.getSingleResult();  | 
  |  133 |     | 
       }  | 
  |  134 |     | 
     | 
  |  135 |     | 
       public RouteNode findRouteNodeByName(String documentTypeId, String name) { | 
  |  136 |    0 |                Query query = entityManager.createNamedQuery("RouteNode.FindRouteNodeByName"); | 
  |  137 |    0 |                query.setParameter(KEWPropertyConstants.DOCUMENT_TYPE_ID, documentTypeId);  | 
  |  138 |    0 |                query.setParameter(KEWPropertyConstants.ROUTE_NODE_NAME, name);  | 
  |  139 |    0 |                    return (RouteNode)query.getSingleResult();              | 
  |  140 |     | 
       }  | 
  |  141 |     | 
     | 
  |  142 |     | 
       public List<RouteNode> findFinalApprovalRouteNodes(String documentTypeId) { | 
  |  143 |    0 |                Query query = entityManager.createNamedQuery("RouteNode.FindApprovalRouteNodes"); | 
  |  144 |    0 |                query.setParameter(KEWPropertyConstants.DOCUMENT_TYPE_ID, documentTypeId);  | 
  |  145 |    0 |                query.setParameter(KEWPropertyConstants.FINAL_APPROVAL, Boolean.TRUE);  | 
  |  146 |    0 |                return new ArrayList<RouteNode>(query.getResultList());  | 
  |  147 |     | 
       }  | 
  |  148 |     | 
     | 
  |  149 |     | 
       public List findProcessNodeInstances(RouteNodeInstance process) { | 
  |  150 |    0 |                Query query = entityManager.createNamedQuery("RouteNodeInstance.FindProcessNodeInstances"); | 
  |  151 |    0 |                query.setParameter(KEWPropertyConstants.PROCESS_ID, process.getRouteNodeInstanceId());  | 
  |  152 |    0 |                return (List) query.getResultList();  | 
  |  153 |     | 
       }  | 
  |  154 |     | 
     | 
  |  155 |     | 
       public List findRouteNodeInstances(String documentId) { | 
  |  156 |    0 |                Query query = entityManager.createNamedQuery("RouteNodeInstance.FindRouteNodeInstances"); | 
  |  157 |    0 |                query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId);  | 
  |  158 |    0 |                return (List) query.getResultList();  | 
  |  159 |     | 
       }  | 
  |  160 |     | 
     | 
  |  161 |     | 
       public void deleteLinksToPreNodeInstances(RouteNodeInstance routeNodeInstance) { | 
  |  162 |    0 |                    List<RouteNodeInstance> preNodeInstances = routeNodeInstance.getPreviousNodeInstances();  | 
  |  163 |    0 |                    for (Iterator<RouteNodeInstance> preNodeInstanceIter = preNodeInstances.iterator(); preNodeInstanceIter.hasNext();) { | 
  |  164 |    0 |                        RouteNodeInstance preNodeInstance = (RouteNodeInstance) preNodeInstanceIter.next();  | 
  |  165 |    0 |                        List<RouteNodeInstance> nextInstances = preNodeInstance.getNextNodeInstances();  | 
  |  166 |    0 |                        nextInstances.remove(routeNodeInstance);  | 
  |  167 |    0 |                        entityManager.merge(preNodeInstance);  | 
  |  168 |    0 |                    }  | 
  |  169 |    0 |        }  | 
  |  170 |     | 
     | 
  |  171 |     | 
       public void deleteRouteNodeInstancesHereAfter(RouteNodeInstance routeNodeInstance) { | 
  |  172 |    0 |                RouteNodeInstance rnInstance = findRouteNodeInstanceById(routeNodeInstance.getRouteNodeInstanceId());  | 
  |  173 |    0 |                entityManager.remove(rnInstance);  | 
  |  174 |    0 |        }  | 
  |  175 |     | 
     | 
  |  176 |     | 
       public void deleteNodeStateById(Long nodeStateId) { | 
  |  177 |    0 |                Query query = entityManager.createNamedQuery("RouteNode.FindNodeStateById"); | 
  |  178 |    0 |                query.setParameter(KEWPropertyConstants.ROUTE_NODE_STATE_ID, nodeStateId);  | 
  |  179 |    0 |                NodeState nodeState = (NodeState) query.getSingleResult();  | 
  |  180 |    0 |                entityManager.remove(nodeState);  | 
  |  181 |    0 |        }  | 
  |  182 |     | 
     | 
  |  183 |     | 
       public void deleteNodeStates(List statesToBeDeleted) { | 
  |  184 |    0 |                    for (Iterator stateToBeDeletedIter = statesToBeDeleted.iterator(); stateToBeDeletedIter.hasNext();) { | 
  |  185 |    0 |                        Long stateId = (Long) stateToBeDeletedIter.next();  | 
  |  186 |    0 |                        deleteNodeStateById(stateId);  | 
  |  187 |    0 |                    }  | 
  |  188 |    0 |        }  | 
  |  189 |     | 
     | 
  |  190 |     | 
   }  |