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