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