1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.portal.workflow.intf;
12
13 import org.astrogrid.common.bean.BaseBean;
14 import org.astrogrid.workflow.beans.v1.Workflow;
15
16 /*** Implementation of activity keys for the new workflow object model
17 * @author Noel Winstanley nw@jb.man.ac.uk 01-Mar-2004
18 */
19 public class ActivityKey {
20 /*** no public constructor
21 *
22 */
23 private ActivityKey(String xpath) {
24 this.xpath = xpath;
25 }
26 private final String xpath;
27
28 /*** create a key for the current position in a workflow
29 *
30 * @param root document root object
31 * @param current object to create activity key for
32 * @return activity key that points to the current object.
33 * @throws IllegalArgumentException - if <tt>root</tt> or <tt>current</tt> is null, or <tt>current</tt> is not within the workflow object tree
34 * @todo - can we strengthen the type of 'current' - ie. is it always going to be a Step, etc?
35 */
36 public static ActivityKey createKey(Workflow root, BaseBean current) throws IllegalArgumentException {
37 if (root == null) {
38 throw new IllegalArgumentException("workflow root is null");
39 }
40 if (current == null) {
41 throw new IllegalArgumentException("current node is null");
42 }
43 String xpath = root.getXPathFor(current);
44 if (xpath == null) {
45 throw new IllegalArgumentException("current node not found in workflow");
46 }
47 return new ActivityKey(xpath);
48 }
49
50 /*** apply the activity key to a document, to access the object it refers to
51 *
52 * @param root workflow document
53 * @return the object in the tree that the activity key pointed to, or null if no object was found
54 */
55 public BaseBean extractFrom(Workflow root) {
56 return (BaseBean)root.findXPathValue(this.xpath);
57 }
58
59
60 public String toString() {
61 StringBuffer buffer = new StringBuffer();
62 buffer.append("[ActivityKey:");
63 buffer.append(" xpath: ");
64 buffer.append(xpath);
65 buffer.append("]");
66 return buffer.toString();
67 }
68 /***
69 * Returns <code>true</code> if this <code>ActivityKey</code> is the same as the o argument.
70 *
71 * @return <code>true</code> if this <code>ActivityKey</code> is the same as the o argument.
72 */
73 public boolean equals(Object o) {
74 if (this == o) {
75 return true;
76 }
77 if (o == null) {
78 return false;
79 }
80 if (o.getClass() != getClass()) {
81 return false;
82 }
83 ActivityKey castedObj = (ActivityKey) o;
84 return ((this.xpath == null ? castedObj.xpath == null : this.xpath.equals(castedObj.xpath)));
85 }
86 /***
87 * Override hashCode.
88 *
89 * @return the Objects hashcode.
90 */
91 public int hashCode() {
92 int hashCode = 1;
93 hashCode = 31 * hashCode + (xpath == null ? 0 : xpath.hashCode());
94 return hashCode;
95 }
96
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114