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