View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 org.apache.ojb.broker.query.Criteria;
23  import org.apache.ojb.broker.query.QueryByCriteria;
24  import org.apache.ojb.broker.query.QueryFactory;
25  import org.apache.ojb.broker.query.ReportQueryByCriteria;
26  import org.kuali.rice.kew.engine.node.Branch;
27  import org.kuali.rice.kew.engine.node.NodeState;
28  import org.kuali.rice.kew.engine.node.RouteNode;
29  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
30  import org.kuali.rice.kew.engine.node.dao.RouteNodeDAO;
31  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
32  import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
33  
34  
35  public class RouteNodeDAOOjbImpl extends PersistenceBrokerDaoSupport implements RouteNodeDAO {
36  
37      private static final String ROUTE_NODE_ID = "routeNodeId";
38      private static final String ROUTE_NODE_INSTANCE_ID = "routeNodeInstanceId";
39      private static final String NODE_INSTANCE_ID = "nodeInstanceId";
40      private static final String DOCUMENT_ID = "documentId";
41      private static final String ROUTE_NODE_NAME = "routeNodeName";
42      private static final String DOCUMENT_TYPE_ID = "documentTypeId";
43      private static final String PROCESS_ID = "processId";
44      private static final String ACTIVE = "active";
45      private static final String COMPLETE = "complete";
46      private static final String FINAL_APPROVAL = "finalApprovalInd";
47      private static final String KEY = "key";
48      private static final String Route_Node_State_ID = "nodeStateId";
49  
50      public void save(RouteNode node) {
51  	getPersistenceBrokerTemplate().store(node);
52      }
53  
54      public void save(RouteNodeInstance nodeInstance) {
55      	// this is because the branch table relates to the node instance table - both through their keys - and
56      	// ojb can't automatically do this bi-directional relationship
57      	getPersistenceBrokerTemplate().store(nodeInstance.getBranch());
58      	getPersistenceBrokerTemplate().store(nodeInstance);
59      }
60  
61      public void save(NodeState nodeState) {
62  	getPersistenceBrokerTemplate().store(nodeState);
63      }
64  
65      public void save(Branch branch) {
66  	getPersistenceBrokerTemplate().store(branch);
67      }
68  
69      public RouteNode findRouteNodeById(String nodeId) {
70  	Criteria criteria = new Criteria();
71  	criteria.addEqualTo(ROUTE_NODE_ID, nodeId);
72  	return (RouteNode) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RouteNode.class, criteria));
73      }
74  
75      public RouteNodeInstance findRouteNodeInstanceById(String nodeInstanceId) {
76  	Criteria criteria = new Criteria();
77  	criteria.addEqualTo(ROUTE_NODE_INSTANCE_ID, nodeInstanceId);
78  	return (RouteNodeInstance) getPersistenceBrokerTemplate().getObjectByQuery(
79  		new QueryByCriteria(RouteNodeInstance.class, criteria));
80      }
81  
82      @SuppressWarnings(value = "unchecked")
83      public List<RouteNodeInstance> getActiveNodeInstances(String documentId) {
84  	Criteria criteria = new Criteria();
85  	criteria.addEqualTo(DOCUMENT_ID, documentId);
86  	criteria.addEqualTo(ACTIVE, Boolean.TRUE);
87  	return (List<RouteNodeInstance>) getPersistenceBrokerTemplate().getCollectionByQuery(
88  		new QueryByCriteria(RouteNodeInstance.class, criteria));
89      }
90  
91      @SuppressWarnings("unchecked")
92      public List<RouteNodeInstance> getTerminalNodeInstances(String documentId) {
93  	Criteria criteria = new Criteria();
94  	criteria.addEqualTo(DOCUMENT_ID, documentId);
95  	criteria.addEqualTo(ACTIVE, Boolean.FALSE);
96  	criteria.addEqualTo(COMPLETE, Boolean.TRUE);
97  //	criteria.addIsNull("nextNodeInstances.routeNodeInstanceId");
98  //	QueryByCriteria query = new QueryByCriteria(RouteNodeInstance.class, criteria);
99  //	// we need to outer join here because we are looking for nodes with no nextNodeInstances
100 //	query.setPathOuterJoin("nextNodeInstances");
101 //	return (List) getPersistenceBrokerTemplate().getCollectionByQuery(query);
102 	
103 	//forced to do this programmatically, for some reason the above code stopped working 
104 	List<RouteNodeInstance> terminalNodes = new ArrayList<RouteNodeInstance>();
105 	List<RouteNodeInstance> routeNodeInstances = (List<RouteNodeInstance>) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RouteNodeInstance.class, criteria));
106 	for (RouteNodeInstance routeNodeInstance : routeNodeInstances) {
107 	    if (routeNodeInstance.getNextNodeInstances().isEmpty()) {
108 		terminalNodes.add(routeNodeInstance);
109 	    }
110 	}
111 	return terminalNodes;
112     }
113 
114     public List getInitialNodeInstances(String documentId) {
115 	Criteria subCriteria = new Criteria();
116 	subCriteria.addEqualTo(DOCUMENT_ID, documentId);
117 	ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, subCriteria);
118 	subQuery.setAttributes(new String[]{"initialRouteNodeInstances.routeNodeInstanceId"});
119 	Criteria criteria = new Criteria();
120 	criteria.addIn(ROUTE_NODE_INSTANCE_ID, subQuery);
121 	return (List) getPersistenceBrokerTemplate().getCollectionByQuery(
122 		new QueryByCriteria(RouteNodeInstance.class, criteria));
123     }
124 
125     public NodeState findNodeState(Long nodeInstanceId, String key) {
126 	Criteria criteria = new Criteria();
127 	criteria.addEqualTo(NODE_INSTANCE_ID, nodeInstanceId);
128 	criteria.addEqualTo(KEY, key);
129 	return (NodeState) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(NodeState.class, criteria));
130     }
131 
132     public RouteNode findRouteNodeByName(String documentTypeId, String name) {
133 	Criteria criteria = new Criteria();
134 	criteria.addEqualTo(ROUTE_NODE_NAME, name);
135 	criteria.addEqualTo(DOCUMENT_TYPE_ID, documentTypeId);
136 	return (RouteNode) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(RouteNode.class, criteria));
137     }
138 
139     public List<RouteNode> findFinalApprovalRouteNodes(String documentTypeId) {
140 	Criteria criteria = new Criteria();
141 	criteria.addEqualTo(DOCUMENT_TYPE_ID, documentTypeId);
142 	criteria.addEqualTo(FINAL_APPROVAL, Boolean.TRUE);
143 	return new ArrayList<RouteNode>(getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(RouteNode.class, criteria)));
144     }
145 
146     public List findProcessNodeInstances(RouteNodeInstance process) {
147 	Criteria crit = new Criteria();
148 	crit.addEqualTo(PROCESS_ID, process.getRouteNodeInstanceId());
149 	return (List) getPersistenceBrokerTemplate()
150 		.getCollectionByQuery(new QueryByCriteria(RouteNodeInstance.class, crit));
151     }
152 
153     public List findRouteNodeInstances(String documentId) {
154 	Criteria criteria = new Criteria();
155 	criteria.addEqualTo(DOCUMENT_ID, documentId);
156 	return (List) getPersistenceBrokerTemplate().getCollectionByQuery(
157 		new QueryByCriteria(RouteNodeInstance.class, criteria));
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 	    save(preNodeInstance);
167 	}
168     }
169 
170     public void deleteRouteNodeInstancesHereAfter(RouteNodeInstance routeNodeInstance) {
171 	this.getPersistenceBrokerTemplate().delete(routeNodeInstance);
172     }
173 
174     public void deleteNodeStateById(Long nodeStateId) {
175 	Criteria criteria = new Criteria();
176 	criteria.addEqualTo(Route_Node_State_ID, nodeStateId);
177 	NodeState nodeState = (NodeState) getPersistenceBrokerTemplate().getObjectByQuery(
178 		new QueryByCriteria(NodeState.class, criteria));
179 	getPersistenceBrokerTemplate().delete(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 }