001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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 org.bpmscript.db;
018:
019: import java.sql.Timestamp;
020: import java.util.List;
021:
022: import org.bpmscript.BpmScriptException;
023: import org.bpmscript.IProcess;
024: import org.bpmscript.IProcessManager;
025: import org.bpmscript.IProcessSource;
026: import org.bpmscript.Process;
027: import org.bpmscript.ProcessSource;
028: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
029:
030: public class HibernateProcessManager extends HibernateDaoSupport
031: implements IProcessManager {
032:
033: public DbProcessInstanceState getProcessInstanceState(String name)
034: throws BpmScriptException {
035: List list = getHibernateTemplate()
036: .findByNamedParam(
037: "from org.bpmscript.db.DbProcessInstanceState processState where processState.name = :name",
038: "name", name);
039: if (list.size() == 1) {
040: return (DbProcessInstanceState) list.get(0);
041: } else {
042: throw new BpmScriptException(
043: "wrong number of process states returned "
044: + list.size());
045: }
046: }
047:
048: public IProcessSource getMainSource(String processId)
049: throws BpmScriptException {
050: List list = getHibernateTemplate()
051: .findByNamedParam(
052: "from org.bpmscript.db.DbProcessSource source where source.processId = :processId and source.main = true",
053: "processId", processId);
054: if (list.size() == 0) {
055: return null;
056: } else if (list.size() == 1) {
057: return (IProcessSource) list.get(0);
058: } else {
059: throw new BpmScriptException(
060: "wrong number of main sources returned "
061: + list.size());
062: }
063: }
064:
065: @SuppressWarnings("unchecked")
066: public List<IProcess> getPrimaryProcesses() {
067: return getHibernateTemplate()
068: .find(
069: "from org.bpmscript.db.DbProcess process where process.primaryProcess = true");
070: }
071:
072: public IProcess getProcess(String processId) {
073: IProcess process = (IProcess) getHibernateTemplate().load(
074: DbProcess.class, processId);
075: return new Process(process.getId(), process.getLastModified(),
076: process.getCreationDate(), process.getSource(), process
077: .getName(), process.isPrimaryProcess(), process
078: .isPinned());
079: }
080:
081: @SuppressWarnings("unchecked")
082: public List<IProcessSource> getProcessSources(String processId) {
083: return getHibernateTemplate()
084: .findByNamedParam(
085: "from org.bpmscript.db.DbProcessSource source where source.processId = :processId",
086: "processId", processId);
087: }
088:
089: public void setProcessAsPrimary(String processId)
090: throws BpmScriptException {
091: DbProcess process = (DbProcess) getHibernateTemplate().load(
092: DbProcess.class, processId);
093: List list = getHibernateTemplate()
094: .findByNamedParam(
095: "from org.bpmscript.db.DbProcess process where process.name = :processName and process.primaryProcess = true",
096: "processName", process.getName());
097: process.setPrimaryProcess(true);
098: if (list.size() == 0) {
099: getHibernateTemplate().saveOrUpdate(process);
100: } else if (list.size() == 1) {
101: DbProcess oldPrimaryProcess = (DbProcess) list.get(0);
102: oldPrimaryProcess.setPrimaryProcess(false);
103: getHibernateTemplate().saveOrUpdate(oldPrimaryProcess);
104: getHibernateTemplate().saveOrUpdate(process);
105: } else {
106: throw new BpmScriptException(
107: "wrong number of processes returned " + list.size());
108: }
109: }
110:
111: public String createProcess(String source, String name,
112: boolean pinned) {
113: DbProcess dbProcess = new DbProcess();
114: dbProcess.setSource(source);
115: dbProcess.setName(name);
116: dbProcess.setPrimaryProcess(false);
117: dbProcess.setPinned(pinned);
118: dbProcess.setCreationDate(new Timestamp(System
119: .currentTimeMillis()));
120: getHibernateTemplate().save(dbProcess);
121: return dbProcess.getId();
122: }
123:
124: public void addSourceToProcess(String processId, String name,
125: String source, boolean main) {
126: DbProcessSource processSource = new DbProcessSource();
127: processSource.setCreationDate(new Timestamp(System
128: .currentTimeMillis()));
129: processSource.setMain(main);
130: processSource.setProcessId(processId);
131: processSource.setSource(source);
132: processSource.setName(name);
133: processSource.setCreationDate(new Timestamp(System
134: .currentTimeMillis()));
135: getHibernateTemplate().save(processSource);
136: }
137:
138: public IProcessSource getProcessSource(String processSourceId)
139: throws BpmScriptException {
140: IProcessSource processSource = (IProcessSource) getHibernateTemplate()
141: .load(DbProcessSource.class, processSourceId);
142: return new ProcessSource(processSource);
143: }
144:
145: public IProcess getPrimaryProcess(String name)
146: throws BpmScriptException {
147: List list = getHibernateTemplate()
148: .findByNamedParam(
149: "from org.bpmscript.db.DbProcess process where process.primaryProcess = true and process.name = :name",
150: "name", name);
151: if (list == null || list.size() == 0) {
152: return null;
153: } else if (list.size() == 1) {
154: return (IProcess) list.get(0);
155: } else {
156: throw new BpmScriptException(
157: "wrong number of processes returned " + list.size());
158: }
159: }
160:
161: @SuppressWarnings("unchecked")
162: public List<IProcess> getProcessVersions(String name)
163: throws BpmScriptException {
164: return getHibernateTemplate()
165: .findByNamedParam(
166: "from org.bpmscript.db.DbProcess process where process.name = :name",
167: "name", name);
168: }
169:
170: }
|