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 | 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 | OrmUtils.merge(entityManager, branch); |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | |
public RouteNode findRouteNodeById(Long nodeId) { |
85 | 0 | Query query = entityManager.createNamedQuery("RouteNode.FindByRouteNodeId"); |
86 | 0 | query.setParameter(KEWPropertyConstants.ROUTE_NODE_ID, nodeId); |
87 | 0 | return (RouteNode) query.getSingleResult(); |
88 | |
} |
89 | |
|
90 | |
public RouteNodeInstance findRouteNodeInstanceById(Long nodeInstanceId) { |
91 | 0 | Query query = entityManager.createNamedQuery("RouteNodeInstance.FindByRouteNodeInstanceId"); |
92 | 0 | query.setParameter(KEWPropertyConstants.ROUTE_NODE_INSTANCE_ID, nodeInstanceId); |
93 | |
|
94 | 0 | return (RouteNodeInstance) query.getSingleResult(); |
95 | |
} |
96 | |
|
97 | |
@SuppressWarnings("unchecked") |
98 | |
public List<RouteNodeInstance> getActiveNodeInstances(String documentId) { |
99 | 0 | Query query = entityManager.createNamedQuery("RouteNodeInstance.FindActiveNodeInstances"); |
100 | 0 | query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId); |
101 | 0 | return (List<RouteNodeInstance>)query.getResultList(); |
102 | |
} |
103 | |
|
104 | |
@SuppressWarnings("unchecked") |
105 | |
public List<RouteNodeInstance> getTerminalNodeInstances(String documentId) { |
106 | 0 | Query query = entityManager.createNamedQuery("RouteNodeInstance.FindTerminalNodeInstances"); |
107 | 0 | query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId); |
108 | |
|
109 | |
|
110 | 0 | List<RouteNodeInstance> terminalNodes = new ArrayList<RouteNodeInstance>(); |
111 | 0 | List<RouteNodeInstance> routeNodeInstances = (List<RouteNodeInstance>) query.getResultList(); |
112 | 0 | for (RouteNodeInstance routeNodeInstance : routeNodeInstances) { |
113 | 0 | if (routeNodeInstance.getNextNodeInstances().isEmpty()) { |
114 | 0 | terminalNodes.add(routeNodeInstance); |
115 | |
} |
116 | |
} |
117 | 0 | return terminalNodes; |
118 | |
} |
119 | |
|
120 | |
public List getInitialNodeInstances(String documentId) { |
121 | |
|
122 | 0 | Query query = entityManager.createNamedQuery("RouteNodeInstance.FindInitialNodeInstances"); |
123 | 0 | query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId); |
124 | 0 | return (List)query.getResultList(); |
125 | |
} |
126 | |
|
127 | |
public NodeState findNodeState(Long nodeInstanceId, String key) { |
128 | 0 | Query query = entityManager.createNamedQuery("NodeState.FindNodeState"); |
129 | 0 | query.setParameter(KEWPropertyConstants.NODE_INSTANCE_ID, nodeInstanceId); |
130 | 0 | query.setParameter(KEWPropertyConstants.KEY, key); |
131 | 0 | return (NodeState) query.getSingleResult(); |
132 | |
} |
133 | |
|
134 | |
public RouteNode findRouteNodeByName(Long documentTypeId, String name) { |
135 | 0 | Query query = entityManager.createNamedQuery("RouteNode.FindRouteNodeByName"); |
136 | 0 | query.setParameter(KEWPropertyConstants.DOCUMENT_TYPE_ID, documentTypeId); |
137 | 0 | query.setParameter(KEWPropertyConstants.ROUTE_NODE_NAME, name); |
138 | 0 | return (RouteNode)query.getSingleResult(); |
139 | |
} |
140 | |
|
141 | |
public List findFinalApprovalRouteNodes(Long documentTypeId) { |
142 | 0 | Query query = entityManager.createNamedQuery("RouteNode.FindApprovalRouteNodes"); |
143 | 0 | query.setParameter(KEWPropertyConstants.DOCUMENT_TYPE_ID, documentTypeId); |
144 | 0 | query.setParameter(KEWPropertyConstants.FINAL_APPROVAL, Boolean.TRUE); |
145 | 0 | return (List) query.getResultList(); |
146 | |
} |
147 | |
|
148 | |
public List findProcessNodeInstances(RouteNodeInstance process) { |
149 | 0 | Query query = entityManager.createNamedQuery("RouteNodeInstance.FindProcessNodeInstances"); |
150 | 0 | query.setParameter(KEWPropertyConstants.PROCESS_ID, process.getRouteNodeInstanceId()); |
151 | 0 | return (List) query.getResultList(); |
152 | |
} |
153 | |
|
154 | |
public List findRouteNodeInstances(String documentId) { |
155 | 0 | Query query = entityManager.createNamedQuery("RouteNodeInstance.FindRouteNodeInstances"); |
156 | 0 | query.setParameter(KEWPropertyConstants.DOCUMENT_ID, documentId); |
157 | 0 | return (List) query.getResultList(); |
158 | |
} |
159 | |
|
160 | |
public void deleteLinksToPreNodeInstances(RouteNodeInstance routeNodeInstance) { |
161 | 0 | List<RouteNodeInstance> preNodeInstances = routeNodeInstance.getPreviousNodeInstances(); |
162 | 0 | for (Iterator<RouteNodeInstance> preNodeInstanceIter = preNodeInstances.iterator(); preNodeInstanceIter.hasNext();) { |
163 | 0 | RouteNodeInstance preNodeInstance = (RouteNodeInstance) preNodeInstanceIter.next(); |
164 | 0 | List<RouteNodeInstance> nextInstances = preNodeInstance.getNextNodeInstances(); |
165 | 0 | nextInstances.remove(routeNodeInstance); |
166 | 0 | entityManager.merge(preNodeInstance); |
167 | 0 | } |
168 | 0 | } |
169 | |
|
170 | |
public void deleteRouteNodeInstancesHereAfter(RouteNodeInstance routeNodeInstance) { |
171 | 0 | RouteNodeInstance rnInstance = findRouteNodeInstanceById(routeNodeInstance.getRouteNodeInstanceId()); |
172 | 0 | entityManager.remove(rnInstance); |
173 | 0 | } |
174 | |
|
175 | |
public void deleteNodeStateById(Long nodeStateId) { |
176 | 0 | Query query = entityManager.createNamedQuery("RouteNode.FindNodeStateById"); |
177 | 0 | query.setParameter(KEWPropertyConstants.ROUTE_NODE_STATE_ID, nodeStateId); |
178 | 0 | NodeState nodeState = (NodeState) query.getSingleResult(); |
179 | 0 | entityManager.remove(nodeState); |
180 | 0 | } |
181 | |
|
182 | |
public void deleteNodeStates(List statesToBeDeleted) { |
183 | 0 | for (Iterator stateToBeDeletedIter = statesToBeDeleted.iterator(); stateToBeDeletedIter.hasNext();) { |
184 | 0 | Long stateId = (Long) stateToBeDeletedIter.next(); |
185 | 0 | deleteNodeStateById(stateId); |
186 | 0 | } |
187 | 0 | } |
188 | |
|
189 | |
} |