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