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.awt.geom.Point2D;
026: import java.awt.geom.Rectangle2D;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.Enumeration;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.StringTokenizer;
035:
036: import org.jgraph.JGraph;
037: import org.jgraph.graph.CellView;
038: import org.jgraph.graph.EdgeView;
039: import org.jgraph.graph.GraphConstants;
040: import org.jgraph.graph.Port;
041:
042: import biz.hammurapi.sql.IDatabaseObject;
043: import biz.hammurapi.util.Attributable;
044: import biz.hammurapi.web.process.sql.ProcessEngine;
045: import biz.hammurapi.web.process.sql.ProcessImpl;
046: import biz.hammurapi.web.process.sql.ProcessTask;
047: import biz.hammurapi.web.process.sql.ProcessTaskImpl;
048: import biz.hammurapi.web.process.sql.ProcessTransition;
049: import biz.hammurapi.web.process.sql.ProcessTransitionImpl;
050: import biz.hammurapi.web.util.ComparablePoint;
051:
052: public class Process {
053: private static final int TOLERANCE = 5;
054: private Collection tasks = new ArrayList();
055: private Collection transitions = new ArrayList();
056: private biz.hammurapi.web.process.sql.Process data;
057:
058: /**
059: * Creates new empty process
060: */
061: public Process() {
062: data = new ProcessImpl(true);
063: }
064:
065: /**
066: * Creates new empty process
067: */
068: public Process(biz.hammurapi.web.process.sql.Process data) {
069: this .data = data;
070: }
071:
072: /**
073: * Loads data from database.
074: * @param engine
075: * @param id
076: * @throws SQLException
077: */
078: public Process(ProcessEngine engine, int id) throws SQLException {
079: // TODO
080: }
081:
082: /**
083: * @return Task names
084: */
085: public boolean checkDuplicateTaskName(String name) {
086: Iterator it = tasks.iterator();
087: while (it.hasNext()) {
088: if (((Task) it.next()).getName().equals(name)) {
089: return true;
090: }
091: }
092: return false;
093: }
094:
095: /**
096: * @return Task names
097: */
098: public Task getTask(String name) {
099: Iterator it = tasks.iterator();
100: while (it.hasNext()) {
101: Task task = (Task) it.next();
102: if (task.getName().equals(name)) {
103: return task;
104: }
105: }
106: return null;
107: }
108:
109: private transient ProcessApplet applet;
110:
111: public ProcessApplet getApplet() {
112: return applet;
113: }
114:
115: public void setApplet(ProcessApplet applet) {
116: this .applet = applet;
117: if (data != null) {
118: Collection tasksData = (Collection) ((Attributable) data)
119: .getAttribute("tasks");
120: if (tasksData != null) {
121: Iterator it = tasksData.iterator();
122: while (it.hasNext()) {
123: ProcessTask task = (ProcessTask) it.next();
124: if (task.getGeometry() != null) {
125: int idx = task.getGeometry().indexOf(',');
126: double x = Double.parseDouble(task
127: .getGeometry().substring(0, idx));
128: double y = Double.parseDouble(task
129: .getGeometry().substring(idx + 1));
130: Point2D point = new Point2D.Double(x, y);
131: Task theTask = new Task(this , task);
132: tasks.add(theTask);
133: applet.insert(point, theTask);
134: }
135: }
136: }
137:
138: Collection transitionsData = (Collection) ((Attributable) data)
139: .getAttribute("transitions");
140: if (transitionsData != null) {
141: Iterator it = transitionsData.iterator();
142: while (it.hasNext()) {
143: ProcessTransition transition = (ProcessTransition) it
144: .next();
145: if (transition.getGeometry() != null) {
146: StringTokenizer st = new StringTokenizer(
147: transition.getGeometry(), ";");
148: List points = new ArrayList();
149: while (st.hasMoreTokens()) {
150: String token = st.nextToken();
151: int idx = token.indexOf(',');
152: double x = Double.parseDouble(token
153: .substring(0, idx));
154: double y = Double.parseDouble(token
155: .substring(idx + 1));
156: Point2D point = new Point2D.Double(x, y);
157: points.add(point);
158: }
159: Transition theTransition = new Transition(this ,
160: transition);
161: transitions.add(theTransition);
162: Port sourcePort = null;
163: ProcessTask transitionSource = (ProcessTask) ((IDatabaseObject) transition)
164: .getColumnAttribute("FROM_TASK", "task");
165: Iterator sit = tasks.iterator();
166: while (sit.hasNext()) {
167: Task candidate = (Task) sit.next();
168: if (candidate.getData() == transitionSource) {
169: Enumeration children = candidate
170: .children();
171: children.nextElement();
172: sourcePort = (Port) children
173: .nextElement();
174: break;
175: }
176: }
177:
178: Port targetPort = null;
179: ProcessTask transitionTarget = (ProcessTask) ((IDatabaseObject) transition)
180: .getColumnAttribute("TO_TASK", "task");
181: sit = tasks.iterator();
182: while (sit.hasNext()) {
183: Task candidate = (Task) sit.next();
184: if (candidate.getData() == transitionTarget) {
185: Enumeration children = candidate
186: .children();
187: targetPort = (Port) children
188: .nextElement();
189: break;
190: }
191: }
192:
193: if (sourcePort == null || targetPort == null) {
194: throw new IllegalStateException(
195: "Transition source or target not found");
196: }
197:
198: applet.connect(theTransition, sourcePort,
199: targetPort, points);
200: }
201: }
202: }
203: }
204: }
205:
206: public Task createTask() {
207: String taskName = "Task 1";
208: for (int i = 1; checkDuplicateTaskName(taskName); ++i) {
209: taskName = "Task " + i;
210: }
211:
212: ProcessTask data = new ProcessTaskImpl(true);
213: data.setStartType("None");
214: data.setName(taskName);
215: Task ret = new Task(this , data);
216: tasks.add(ret);
217: return ret;
218: }
219:
220: public Transition createTransition() {
221: Transition ret = new Transition(this ,
222: new ProcessTransitionImpl(true));
223: transitions.add(ret);
224: return ret;
225: }
226:
227: public void removeTransition(Transition transition) {
228: transition.invalidate();
229: }
230:
231: public void removeTask(Task task) {
232: task.invalidate();
233: }
234:
235: /**
236: * Stores geometry in process elements
237: * @param graph
238: */
239: public void storeGeometry() {
240: JGraph graph = getApplet().getGraph();
241: List comparablePoints = new ArrayList();
242: Iterator it = tasks.iterator();
243: while (it.hasNext()) {
244: Task task = (Task) it.next();
245: if (task.isValid()) {
246: CellView taskView = graph.getGraphLayoutCache()
247: .getMapping(task, false);
248: Rectangle2D bounds = taskView.getBounds();
249: double x = bounds.getX();
250: double y = bounds.getY();
251: task.getData().setGeometry(x + "," + y);
252: comparablePoints.add(new ComparablePoint(x, y, task,
253: TOLERANCE, data.getIsVertical()));
254: } else {
255: it.remove();
256: }
257: }
258:
259: Collections.sort(comparablePoints);
260: it = comparablePoints.iterator();
261: for (int i = 0; it.hasNext(); ++i) {
262: ((Task) ((ComparablePoint) it.next()).getUserObject())
263: .getData().setTaskOrder(i);
264: }
265:
266: comparablePoints.clear();
267: it = transitions.iterator();
268: while (it.hasNext()) {
269: Transition transition = (Transition) it.next();
270: if (transition.isValid()) {
271: EdgeView taskView = (EdgeView) graph
272: .getGraphLayoutCache().getMapping(transition,
273: false);
274: List points = taskView.getPoints();
275: if (points == null) {
276: transition.getData().setGeometry("");
277: } else {
278: StringBuffer geometryBuffer = new StringBuffer();
279: Iterator pit = points.iterator();
280: for (int i = 0; pit.hasNext(); ++i) {
281: Object point = pit.next();
282: if ((point instanceof Point2D)) {
283: Point2D thePoint = (Point2D) point;
284: geometryBuffer.append(thePoint.getX() + ","
285: + thePoint.getY() + ";");
286: }
287:
288: if (i == 1) {
289: if ((point instanceof Point2D)) {
290: Point2D thePoint = (Point2D) point;
291: ComparablePoint cp = new ComparablePoint(
292: thePoint, transition,
293: TOLERANCE, data.getIsVertical());
294: comparablePoints.add(cp);
295: } else if (point instanceof Port) {
296: Rectangle2D bounds = GraphConstants
297: .getBounds(((Port) point)
298: .getAttributes());
299: ComparablePoint cp = new ComparablePoint(
300: bounds.getCenterX(), bounds
301: .getCenterY(),
302: transition, TOLERANCE, data
303: .getIsVertical());
304: comparablePoints.add(cp);
305: }
306: }
307: }
308: transition.getData().setGeometry(
309: geometryBuffer.toString());
310: }
311: } else {
312: it.remove();
313: }
314: }
315:
316: Collections.sort(comparablePoints);
317: it = comparablePoints.iterator();
318: for (int i = 0; it.hasNext(); ++i) {
319: ((Transition) ((ComparablePoint) it.next()).getUserObject())
320: .getData().setTransitionOrder(i);
321: }
322: }
323:
324: public String getAfterCode() {
325: return data.getAfterCode();
326: }
327:
328: public String getBeforeCode() {
329: return data.getBeforeCode();
330: }
331:
332: public String getSummary() {
333: return data.getSummary();
334: }
335:
336: public String getName() {
337: return data.getName();
338: }
339:
340: public void setAfterCode(String code) {
341: data.setAfterCode(code);
342: }
343:
344: public void setBeforeCode(String code) {
345: data.setBeforeCode(code);
346: }
347:
348: public void setSummary(String summary) {
349: data.setSummary(summary);
350: }
351:
352: public void setName(String name) {
353: data.setName(name);
354: }
355:
356: public biz.hammurapi.web.process.sql.Process getData() {
357: ArrayList tasksData = new ArrayList();
358: Iterator it = tasks.iterator();
359: while (it.hasNext()) {
360: Task task = (Task) it.next();
361: if (task.isValid()) {
362: tasksData.add(task.getData());
363: }
364: }
365: ((Attributable) data).setAttribute("tasks", tasksData);
366:
367: ArrayList transitionsData = new ArrayList();
368: it = transitions.iterator();
369: while (it.hasNext()) {
370: Transition transition = (Transition) it.next();
371: if (transition.isValid()) {
372: Object sourceData = transition.getSourceTask()
373: .getData();
374: ((IDatabaseObject) transition.getData())
375: .setColumnAttribute("FROM_TASK", "task",
376: sourceData);
377:
378: Object targetData = transition.getTargetTask()
379: .getData();
380: ((IDatabaseObject) transition.getData())
381: .setColumnAttribute("TO_TASK", "task",
382: targetData);
383:
384: transitionsData.add(transition.getData());
385: }
386: }
387: ((Attributable) data).setAttribute("transitions",
388: transitionsData);
389:
390: return data;
391: }
392:
393: public boolean getIsVertical() {
394: return data.getIsVertical();
395: }
396:
397: public void setIsVertical(boolean isVertical) {
398: data.setIsVertical(isVertical);
399: }
400: }
|