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.apache.ojb.broker.query.Criteria;
17 import org.apache.ojb.broker.query.QueryByCriteria;
18 import org.apache.ojb.broker.query.QueryFactory;
19 import org.apache.ojb.broker.query.ReportQueryByCriteria;
20 import org.kuali.rice.kew.engine.node.Branch;
21 import org.kuali.rice.kew.engine.node.NodeState;
22 import org.kuali.rice.kew.engine.node.RouteNode;
23 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
24 import org.kuali.rice.kew.engine.node.dao.RouteNodeDAO;
25 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
26 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
27
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32
33 public class RouteNodeDAOOjbImpl extends PersistenceBrokerDaoSupport implements RouteNodeDAO {
34
35 private static final String ROUTE_NODE_ID = "routeNodeId";
36 private static final String ROUTE_NODE_INSTANCE_ID = "routeNodeInstanceId";
37 private static final String NODE_INSTANCE_ID = "nodeInstanceId";
38 private static final String DOCUMENT_ID = "documentId";
39 private static final String ROUTE_HEADER_ID = "routeHeaderId";
40 private static final String ROUTE_NODE_NAME = "routeNodeName";
41 private static final String DOCUMENT_TYPE_ID = "documentTypeId";
42 private static final String PROCESS_ID = "processId";
43 private static final String ACTIVE = "active";
44 private static final String COMPLETE = "complete";
45 private static final String FINAL_APPROVAL = "finalApprovalInd";
46 private static final String KEY = "key";
47 private static final String Route_Node_State_ID = "nodeStateId";
48
49 public void save(RouteNode node) {
50 getPersistenceBrokerTemplate().store(node);
51 }
52
53 public void save(RouteNodeInstance nodeInstance) {
54
55
56 getPersistenceBrokerTemplate().store(nodeInstance.getBranch());
57 getPersistenceBrokerTemplate().store(nodeInstance);
58 }
59
60 public void save(NodeState nodeState) {
61 getPersistenceBrokerTemplate().store(nodeState);
62 }
63
64 public void save(Branch branch) {
65 getPersistenceBrokerTemplate().store(branch);
66 }
67
68 public RouteNode findRouteNodeById(Long nodeId) {
69 Criteria criteria = new Criteria();
70 criteria.addEqualTo(ROUTE_NODE_ID, nodeId);
71 return (RouteNode) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RouteNode.class, criteria));
72 }
73
74 public RouteNodeInstance findRouteNodeInstanceById(Long nodeInstanceId) {
75 Criteria criteria = new Criteria();
76 criteria.addEqualTo(ROUTE_NODE_INSTANCE_ID, nodeInstanceId);
77 return (RouteNodeInstance) getPersistenceBrokerTemplate().getObjectByQuery(
78 new QueryByCriteria(RouteNodeInstance.class, criteria));
79 }
80
81 @SuppressWarnings(value = "unchecked")
82 public List<RouteNodeInstance> getActiveNodeInstances(Long documentId) {
83 Criteria criteria = new Criteria();
84 criteria.addEqualTo(DOCUMENT_ID, documentId);
85 criteria.addEqualTo(ACTIVE, Boolean.TRUE);
86 return (List<RouteNodeInstance>) getPersistenceBrokerTemplate().getCollectionByQuery(
87 new QueryByCriteria(RouteNodeInstance.class, criteria));
88 }
89
90 @SuppressWarnings("unchecked")
91 public List<RouteNodeInstance> getTerminalNodeInstances(Long documentId) {
92 Criteria criteria = new Criteria();
93 criteria.addEqualTo(DOCUMENT_ID, documentId);
94 criteria.addEqualTo(ACTIVE, Boolean.FALSE);
95 criteria.addEqualTo(COMPLETE, Boolean.TRUE);
96
97
98
99
100
101
102
103 List<RouteNodeInstance> terminalNodes = new ArrayList<RouteNodeInstance>();
104 List<RouteNodeInstance> routeNodeInstances = (List<RouteNodeInstance>) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RouteNodeInstance.class, criteria));
105 for (RouteNodeInstance routeNodeInstance : routeNodeInstances) {
106 if (routeNodeInstance.getNextNodeInstances().isEmpty()) {
107 terminalNodes.add(routeNodeInstance);
108 }
109 }
110 return terminalNodes;
111 }
112
113 public List getInitialNodeInstances(Long documentId) {
114 Criteria subCriteria = new Criteria();
115 subCriteria.addEqualTo(ROUTE_HEADER_ID, documentId);
116 ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, subCriteria);
117 subQuery.setAttributes(new String[]{"initialRouteNodeInstances.routeNodeInstanceId"});
118 Criteria criteria = new Criteria();
119 criteria.addIn(ROUTE_NODE_INSTANCE_ID, subQuery);
120 return (List) getPersistenceBrokerTemplate().getCollectionByQuery(
121 new QueryByCriteria(RouteNodeInstance.class, criteria));
122 }
123
124 public NodeState findNodeState(Long nodeInstanceId, String key) {
125 Criteria criteria = new Criteria();
126 criteria.addEqualTo(NODE_INSTANCE_ID, nodeInstanceId);
127 criteria.addEqualTo(KEY, key);
128 return (NodeState) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(NodeState.class, criteria));
129 }
130
131 public RouteNode findRouteNodeByName(Long documentTypeId, String name) {
132 Criteria criteria = new Criteria();
133 criteria.addEqualTo(ROUTE_NODE_NAME, name);
134 criteria.addEqualTo(DOCUMENT_TYPE_ID, documentTypeId);
135 return (RouteNode) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RouteNode.class, criteria));
136 }
137
138 public List findFinalApprovalRouteNodes(Long documentTypeId) {
139 Criteria criteria = new Criteria();
140 criteria.addEqualTo(DOCUMENT_TYPE_ID, documentTypeId);
141 criteria.addEqualTo(FINAL_APPROVAL, Boolean.TRUE);
142 return (List) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RouteNode.class, criteria));
143 }
144
145 public List findProcessNodeInstances(RouteNodeInstance process) {
146 Criteria crit = new Criteria();
147 crit.addEqualTo(PROCESS_ID, process.getRouteNodeInstanceId());
148 return (List) getPersistenceBrokerTemplate()
149 .getCollectionByQuery(new QueryByCriteria(RouteNodeInstance.class, crit));
150 }
151
152 public List findRouteNodeInstances(Long documentId) {
153 Criteria criteria = new Criteria();
154 criteria.addEqualTo(DOCUMENT_ID, documentId);
155 return (List) getPersistenceBrokerTemplate().getCollectionByQuery(
156 new QueryByCriteria(RouteNodeInstance.class, criteria));
157 }
158
159 public void deleteLinksToPreNodeInstances(RouteNodeInstance routeNodeInstance) {
160 List<RouteNodeInstance> preNodeInstances = routeNodeInstance.getPreviousNodeInstances();
161 for (Iterator<RouteNodeInstance> preNodeInstanceIter = preNodeInstances.iterator(); preNodeInstanceIter.hasNext();) {
162 RouteNodeInstance preNodeInstance = (RouteNodeInstance) preNodeInstanceIter.next();
163 List<RouteNodeInstance> nextInstances = preNodeInstance.getNextNodeInstances();
164 nextInstances.remove(routeNodeInstance);
165 save(preNodeInstance);
166 }
167 }
168
169 public void deleteRouteNodeInstancesHereAfter(RouteNodeInstance routeNodeInstance) {
170 this.getPersistenceBrokerTemplate().delete(routeNodeInstance);
171 }
172
173 public void deleteNodeStateById(Long nodeStateId) {
174 Criteria criteria = new Criteria();
175 criteria.addEqualTo(Route_Node_State_ID, nodeStateId);
176 NodeState nodeState = (NodeState) getPersistenceBrokerTemplate().getObjectByQuery(
177 new QueryByCriteria(NodeState.class, criteria));
178 getPersistenceBrokerTemplate().delete(nodeState);
179 }
180
181 public void deleteNodeStates(List statesToBeDeleted) {
182 for (Iterator stateToBeDeletedIter = statesToBeDeleted.iterator(); stateToBeDeletedIter.hasNext();) {
183 Long stateId = (Long) stateToBeDeletedIter.next();
184 deleteNodeStateById(stateId);
185 }
186 }
187
188 }