View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
6    * compliance with the License. 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 distributed under the License is distributed on an "AS
11   * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
12   * language governing permissions and limitations under the License.
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      	// this is because the branch table relates to the node instance table - both through their keys - and
55      	// ojb can't automatically do this bi-directional relationship
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  //	criteria.addIsNull("nextNodeInstances.routeNodeInstanceId");
97  //	QueryByCriteria query = new QueryByCriteria(RouteNodeInstance.class, criteria);
98  //	// we need to outer join here because we are looking for nodes with no nextNodeInstances
99  //	query.setPathOuterJoin("nextNodeInstances");
100 //	return (List) getPersistenceBrokerTemplate().getCollectionByQuery(query);
101 	
102 	//forced to do this programmatically, for some reason the above code stopped working 
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 }