001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.engine.node.dao;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.ojb.broker.query.Criteria;
023: import org.apache.ojb.broker.query.QueryByCriteria;
024: import org.apache.ojb.broker.query.QueryFactory;
025: import org.apache.ojb.broker.query.ReportQueryByCriteria;
026: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
027:
028: import edu.iu.uis.eden.engine.node.Branch;
029: import edu.iu.uis.eden.engine.node.NodeState;
030: import edu.iu.uis.eden.engine.node.RouteNode;
031: import edu.iu.uis.eden.engine.node.RouteNodeInstance;
032: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
033:
034: public class RouteNodeDAOOjbImpl extends PersistenceBrokerDaoSupport
035: implements RouteNodeDAO {
036:
037: private static final String ROUTE_NODE_ID = "routeNodeId";
038: private static final String ROUTE_NODE_INSTANCE_ID = "routeNodeInstanceId";
039: private static final String NODE_INSTANCE_ID = "nodeInstanceId";
040: private static final String DOCUMENT_ID = "documentId";
041: private static final String ROUTE_HEADER_ID = "routeHeaderId";
042: private static final String ROUTE_NODE_NAME = "routeNodeName";
043: private static final String DOCUMENT_TYPE_ID = "documentTypeId";
044: private static final String PROCESS_ID = "processId";
045: private static final String ACTIVE = "active";
046: private static final String COMPLETE = "complete";
047: private static final String FINAL_APPROVAL = "finalApprovalInd";
048: private static final String KEY = "key";
049: private static final String Route_Node_State_ID = "nodeStateId";
050:
051: public void save(RouteNode node) {
052: getPersistenceBrokerTemplate().store(node);
053: }
054:
055: public void save(RouteNodeInstance nodeInstance) {
056: //this is because the branch table relates to the node instance table - both through their keys - and
057: //ojb can't automatically do this bi-directional relationship
058: getPersistenceBrokerTemplate().store(nodeInstance.getBranch());
059: getPersistenceBrokerTemplate().store(nodeInstance);
060: }
061:
062: public void save(NodeState nodeState) {
063: getPersistenceBrokerTemplate().store(nodeState);
064: }
065:
066: public void save(Branch branch) {
067: getPersistenceBrokerTemplate().store(branch);
068: }
069:
070: public RouteNode findRouteNodeById(Long nodeId) {
071: Criteria criteria = new Criteria();
072: criteria.addEqualTo(ROUTE_NODE_ID, nodeId);
073: return (RouteNode) getPersistenceBrokerTemplate()
074: .getObjectByQuery(
075: new QueryByCriteria(RouteNode.class, criteria));
076: }
077:
078: public RouteNodeInstance findRouteNodeInstanceById(
079: Long nodeInstanceId) {
080: Criteria criteria = new Criteria();
081: criteria.addEqualTo(ROUTE_NODE_INSTANCE_ID, nodeInstanceId);
082: return (RouteNodeInstance) getPersistenceBrokerTemplate()
083: .getObjectByQuery(
084: new QueryByCriteria(RouteNodeInstance.class,
085: criteria));
086: }
087:
088: public List getActiveNodeInstances(Long documentId) {
089: Criteria criteria = new Criteria();
090: criteria.addEqualTo(DOCUMENT_ID, documentId);
091: criteria.addEqualTo(ACTIVE, Boolean.TRUE);
092: return (List) getPersistenceBrokerTemplate()
093: .getCollectionByQuery(
094: new QueryByCriteria(RouteNodeInstance.class,
095: criteria));
096: }
097:
098: public List getTerminalNodeInstances(Long documentId) {
099: Criteria criteria = new Criteria();
100: criteria.addEqualTo(DOCUMENT_ID, documentId);
101: criteria.addEqualTo(ACTIVE, Boolean.FALSE);
102: criteria.addEqualTo(COMPLETE, Boolean.TRUE);
103: criteria.addIsNull("nextNodeInstances.routeNodeInstanceId");
104: QueryByCriteria query = new QueryByCriteria(
105: RouteNodeInstance.class, criteria);
106: // we need to outer join here because we are looking for nodes with no nextNodeInstances
107: query.setPathOuterJoin("nextNodeInstances");
108: return (List) getPersistenceBrokerTemplate()
109: .getCollectionByQuery(query);
110: }
111:
112: public List getInitialNodeInstances(Long documentId) {
113: Criteria subCriteria = new Criteria();
114: subCriteria.addEqualTo(ROUTE_HEADER_ID, documentId);
115: ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(
116: DocumentRouteHeaderValue.class, subCriteria);
117: subQuery
118: .setAttributes(new String[] { "initialRouteNodeInstances.routeNodeInstanceId" });
119: Criteria criteria = new Criteria();
120: criteria.addIn(ROUTE_NODE_INSTANCE_ID, subQuery);
121: return (List) getPersistenceBrokerTemplate()
122: .getCollectionByQuery(
123: new QueryByCriteria(RouteNodeInstance.class,
124: criteria));
125: }
126:
127: public NodeState findNodeState(Long nodeInstanceId, String key) {
128: Criteria criteria = new Criteria();
129: criteria.addEqualTo(NODE_INSTANCE_ID, nodeInstanceId);
130: criteria.addEqualTo(KEY, key);
131: return (NodeState) getPersistenceBrokerTemplate()
132: .getObjectByQuery(
133: new QueryByCriteria(NodeState.class, criteria));
134: }
135:
136: public RouteNode findRouteNodeByName(Long documentTypeId,
137: String name) {
138: Criteria criteria = new Criteria();
139: criteria.addEqualTo(ROUTE_NODE_NAME, name);
140: criteria.addEqualTo(DOCUMENT_TYPE_ID, documentTypeId);
141: return (RouteNode) getPersistenceBrokerTemplate()
142: .getObjectByQuery(
143: new QueryByCriteria(RouteNode.class, criteria));
144: }
145:
146: public List findFinalApprovalRouteNodes(Long documentTypeId) {
147: Criteria criteria = new Criteria();
148: criteria.addEqualTo(DOCUMENT_TYPE_ID, documentTypeId);
149: criteria.addEqualTo(FINAL_APPROVAL, Boolean.TRUE);
150: return (List) getPersistenceBrokerTemplate()
151: .getCollectionByQuery(
152: new QueryByCriteria(RouteNode.class, criteria));
153: }
154:
155: public List findProcessNodeInstances(RouteNodeInstance process) {
156: Criteria crit = new Criteria();
157: crit.addEqualTo(PROCESS_ID, process.getRouteNodeInstanceId());
158: return (List) getPersistenceBrokerTemplate()
159: .getCollectionByQuery(
160: new QueryByCriteria(RouteNodeInstance.class,
161: crit));
162: }
163:
164: public List findRouteNodeInstances(Long documentId) {
165: Criteria criteria = new Criteria();
166: criteria.addEqualTo(DOCUMENT_ID, documentId);
167: return (List) getPersistenceBrokerTemplate()
168: .getCollectionByQuery(
169: new QueryByCriteria(RouteNodeInstance.class,
170: criteria));
171: }
172:
173: public void deleteLinksToPreNodeInstances(
174: RouteNodeInstance routeNodeInstance) {
175: List preNodeInstances = routeNodeInstance
176: .getPreviousNodeInstances();
177: for (Iterator preNodeInstanceIter = preNodeInstances.iterator(); preNodeInstanceIter
178: .hasNext();) {
179: RouteNodeInstance preNodeInstance = (RouteNodeInstance) preNodeInstanceIter
180: .next();
181: List nextInstances = preNodeInstance.getNextNodeInstances();
182: nextInstances.remove(routeNodeInstance);
183: save(preNodeInstance);
184: }
185: }
186:
187: public void deleteRouteNodeInstancesHereAfter(
188: RouteNodeInstance routeNodeInstance) {
189: this .getPersistenceBrokerTemplate().delete(routeNodeInstance);
190: }
191:
192: public void deleteNodeStateById(Long nodeStateId) {
193: Criteria criteria = new Criteria();
194: criteria.addEqualTo(Route_Node_State_ID, nodeStateId);
195: NodeState nodeState = (NodeState) getPersistenceBrokerTemplate()
196: .getObjectByQuery(
197: new QueryByCriteria(NodeState.class, criteria));
198: getPersistenceBrokerTemplate().delete(nodeState);
199: }
200:
201: public void deleteNodeStates(List statesToBeDeleted) {
202: for (Iterator stateToBeDeletedIter = statesToBeDeleted
203: .iterator(); stateToBeDeletedIter.hasNext();) {
204: Long stateId = (Long) stateToBeDeletedIter.next();
205: deleteNodeStateById(stateId);
206: }
207: }
208:
209: }
|