View Javadoc

1   /*$Id: ActivityKey.java,v 1.3 2004/03/11 13:53:36 nw Exp $
2    * Created on 01-Mar-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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      // machine-generated guff
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 $Log: ActivityKey.java,v $
102 Revision 1.3  2004/03/11 13:53:36  nw
103 merged in branch bz#236 - implementation of interfaces
104 
105 Revision 1.2.4.1  2004/03/11 13:36:46  nw
106 tidied up interfaces, documented
107 
108 Revision 1.2  2004/03/03 01:36:38  nw
109 merged interfaces in from branch nww-int05-bz#146
110 
111 Revision 1.1.2.1  2004/03/01 19:02:57  nw
112 refined interfaces. almost ready to publish
113  
114 */