001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.process;
024:
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Set;
033: import java.util.TreeMap;
034:
035: import biz.hammurapi.sql.RowProcessor;
036: import biz.hammurapi.sql.SQLProcessor;
037: import biz.hammurapi.web.process.sql.Process;
038: import biz.hammurapi.web.process.sql.ProcessEngine;
039: import biz.hammurapi.web.process.sql.ProcessRole;
040: import biz.hammurapi.web.process.sql.ProcessRoleImpl;
041: import biz.hammurapi.web.process.sql.ProcessTaskArtifact;
042:
043: public class ProcessUtils {
044: private ProcessEngine engine;
045: private Integer processId;
046:
047: public ProcessUtils(SQLProcessor processor, Integer processId) {
048: super ();
049: this .engine = new ProcessEngine(processor);
050: this .processId = processId;
051: }
052:
053: /**
054: * Returns roles for given process with template processes.
055: * @param request - request, process ID shall be available through parameter "ID".
056: * @return
057: * @throws SQLException
058: * @throws NumberFormatException
059: */
060: public Collection getRoles() throws NumberFormatException,
061: SQLException {
062: Map ret = new TreeMap();
063: getRoles(processId, new HashSet(), ret);
064: return new ArrayList(ret.values());
065: }
066:
067: private void getRoles(Integer id, Set idSet, final Map ret)
068: throws SQLException {
069: if (id != null && idSet.add(id)) {
070: engine.processProcessRoleByProcess(id.intValue(),
071: new RowProcessor() {
072:
073: public boolean process(ResultSet rs)
074: throws SQLException {
075: ProcessRole pr = new ProcessRoleImpl(rs);
076: if (!ret.containsKey(pr.getName())) {
077: ret.put(pr.getName(), pr);
078: }
079: return true;
080: }
081:
082: });
083: Process process = engine.getProcess(id.intValue());
084: if (process != null) {
085: Integer tid = process.getInheritRoles();
086: if (tid != null) {
087: getRoles(tid, idSet, ret);
088: }
089: }
090: }
091: }
092:
093: /**
094: * Returns artifacts for given process with template processes. Artifacts associated with taskId are not included.
095: * @param request - request, process ID shall be available through parameter "ID".
096: * @return
097: * @throws SQLException
098: * @throws NumberFormatException
099: */
100: public Collection getArtifacts(int taskId)
101: throws NumberFormatException, SQLException {
102: Map map = new TreeMap();
103: getArtifacts(processId, new HashSet(), map);
104: ArrayList ret = new ArrayList(map.values());
105: Iterator ait = engine.getProcessTaskArtifactByTask(taskId)
106: .iterator();
107: while (ait.hasNext()) {
108: ProcessTaskArtifact pta = (ProcessTaskArtifact) ait.next();
109: Iterator rit = ret.iterator();
110: while (rit.hasNext()) {
111: ProcessRole pr = (ProcessRole) rit.next();
112: if (pr.getId() == pta.getArtifactId()) {
113: rit.remove();
114: }
115: }
116: }
117: return ret;
118: }
119:
120: private void getArtifacts(Integer id, Set idSet, final Map ret)
121: throws SQLException {
122: if (id != null && idSet.add(id)) {
123: engine.processProcessArtifactByProcess(id.intValue(),
124: new RowProcessor() {
125:
126: public boolean process(ResultSet rs)
127: throws SQLException {
128: ProcessRole pr = new ProcessRoleImpl(rs);
129: if (!ret.containsKey(pr.getName())) {
130: ret.put(pr.getName(), pr);
131: }
132: return true;
133: }
134:
135: });
136: Process process = engine.getProcess(id.intValue());
137: if (process != null) {
138: Integer tid = process.getInheritRoles();
139: if (tid != null) {
140: getRoles(tid, idSet, ret);
141: }
142: }
143: }
144: }
145:
146: }
|