001: package org.tigris.scarab.workflow;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.tigris.scarab.om.AttributeOption;
007: import org.tigris.scarab.om.Transition;
008:
009: /* ====================================================================
010: *
011: * Copyright (c) 2006 CollabNet.
012: *
013: * Licensed under the
014: *
015: * CollabNet/Tigris.org Apache-style license (the "License");
016: *
017: * you may not use this file except in compliance with the License.
018: * You may obtain a copy of the License at
019: *
020: * http://scarab.tigris.org/LICENSE
021: *
022: * Unless required by applicable law or agreed to in writing, software
023: * distributed under the License is distributed on an "AS IS" BASIS,
024: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
025: * implied. See the License for the specific language governing
026: * permissions and limitations under the License.
027: * ====================================================================
028: *
029: * This software consists of voluntary contributions made by many
030: * individuals on behalf of CollabNet.
031: *
032: *
033: */
034:
035: /**
036: * The Transitions available for Attributes span a recursive TransitionTree.
037: * A TransitionNode is one node in the TransitionTree. It contains a pointer
038: * to a source AttributeOption of a Transition and a List of TransitionNodes
039: * which contain target AttributeOptions which can be reached from
040: * this AttributeOption.
041: * @author hussayn.dabbous@saxess.de
042: */
043:
044: public class TransitionNode {
045: private AttributeOption fromOption;
046: List children;
047: TransitionNode parent;
048:
049: static final Integer BLANK = new Integer(0);
050: static final Integer SINGLE = new Integer(1);
051: static final Integer FIRST = new Integer(2);
052: static final Integer INTER = new Integer(3);
053: static final Integer LAST = new Integer(4);
054: static final Integer PASS = new Integer(5);
055:
056: TransitionNode(AttributeOption option) {
057: fromOption = option;
058: children = new ArrayList();
059: parent = null;
060: }
061:
062: public void addNode(TransitionNode node) {
063: children.add(node);
064: node.setParent(this );
065: }
066:
067: public TransitionNode addNode(Transition transition) {
068: AttributeOption toOption = transition.getTo();
069: TransitionNode child = new TransitionNode(toOption);
070: addNode(child);
071: return child;
072: }
073:
074: private void setParent(TransitionNode node) {
075: parent = node;
076: }
077:
078: public TransitionNode getParent() {
079: return parent;
080: }
081:
082: public AttributeOption getOption() {
083: return fromOption;
084: }
085:
086: public List getChildren() {
087: return children;
088: }
089:
090: public List createRows() {
091: List rows = new ArrayList();
092: List currentRow = new ArrayList();
093: return fillRows(rows, currentRow);
094: }
095:
096: private List fillRows(List rows, List currentRow) {
097: currentRow.add(getOption());
098: if (children.size() > 0) {
099: int size = children.size();
100: for (int index = 0; index < size; index++) {
101: TransitionNode child = (TransitionNode) children
102: .get(index);
103: if (size == 1) {
104: currentRow.add(SINGLE);
105: } else {
106: if (index == 0) {
107: currentRow.add(FIRST);
108: } else if (index == size - 1) {
109: currentRow.add(LAST);
110: } else {
111: currentRow.add(INTER);
112: }
113: }
114: child.fillRows(rows, currentRow);
115: if (index < (size - 1)) {
116: currentRow = createNewRow();
117: }
118: }
119: } else {
120: rows.add(currentRow);
121: }
122: return rows;
123: }
124:
125: private List createNewRow() {
126: List result;
127: TransitionNode parent = getParent();
128: if (parent != null) {
129: result = parent.createNewRow();
130: Integer type = getConnectionType();
131: result.add(type);
132: } else {
133: result = new ArrayList();
134: }
135: result.add("");
136: return result;
137: }
138:
139: private Integer getConnectionType() {
140: TransitionNode parent = getParent();
141: int parentSize = parent.getChildren().size();
142: Integer result;
143:
144: if (parentSize == 1) {
145: result = BLANK;
146: } else {
147: int index = 0;
148: for (index = 0; index < parentSize; index++) {
149: TransitionNode node = (TransitionNode) parent
150: .getChildren().get(index);
151: if (node == this ) {
152: break;
153: }
154: }
155:
156: if (index == 0) {
157: result = PASS;
158: } else if (index == (parentSize - 1)) {
159: result = BLANK;
160: } else {
161: result = PASS;
162: }
163: }
164: return result;
165: }
166:
167: public int getTreeDepth() {
168: int result = 0;
169: for (int index = 0; index < children.size(); index++) {
170: TransitionNode child = (TransitionNode) children.get(index);
171: int childDepth = child.getTreeDepth();
172: if (childDepth >= result) {
173: result = childDepth + 1;
174: }
175: }
176: return result;
177: }
178:
179: }
|