View Javadoc

1   /*
2    * $Id: Castor2Axis.java,v 1.12 2004/12/03 14:47:41 jdt Exp $
3    * 
4    * Created on 11-Mar-2004 by Paul Harrison (pah@jb.man.ac.uk)
5    *
6    * Copyright 2004 AstroGrid. All rights reserved.
7    *
8    * This software is published under the terms of the AstroGrid 
9    * Software License version 1.2, a copy of which has been included 
10   * with this distribution in the LICENSE.txt file.  
11   *
12   */
13  
14  package org.astrogrid.common.bean;
15  
16  import java.util.Calendar;
17  
18  import org.apache.axis.types.NMToken;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.astrogrid.applications.beans.v1.axis.ceaparameters.ParameterValue;
22  import org.astrogrid.applications.beans.v1.cea.castor.MessageType;
23  import org.astrogrid.jes.beans.v1.axis.executionrecord.JobURN;
24  import org.astrogrid.jes.beans.v1.axis.executionrecord._extension;
25  import org.astrogrid.jes.types.v1.cea.axis.ExecutionPhase;
26  import org.astrogrid.jes.types.v1.cea.axis.ExecutionSummaryType;
27  import org.astrogrid.jes.types.v1.cea.axis.InputListType;
28  import org.astrogrid.jes.types.v1.cea.axis.ResultListType;
29  import org.astrogrid.workflow.beans.v1.Input;
30  import org.astrogrid.workflow.beans.v1.Output;
31  import org.astrogrid.workflow.beans.v1.Tool;
32  import org.astrogrid.workflow.beans.v1.axis._input;
33  import org.astrogrid.workflow.beans.v1.axis._output;
34  import org.astrogrid.workflow.beans.v1.axis._tool;
35  import org.astrogrid.workflow.beans.v1.execution.Extension;
36  
37  /***
38   * Static methods to convert Castor to Axis objects.
39   * @author Paul Harrison (pah@jb.man.ac.uk) 11-Mar-2004
40   * @version $Name:  $
41   * @since iteration5
42   * @TODO - this is rather fragile to changes in the schema - would be better perhaps to use castor serialization in axis directly...or get the axis object marshalling - anything please!!!
43   */
44  public class Castor2Axis {
45     static public org.apache.commons.logging.Log logger =
46        org.apache.commons.logging.LogFactory.getLog(Castor2Axis.class);
47  
48     /***
49      * 
50      */
51     public Castor2Axis() {
52     }
53  
54     public static _tool convert(Tool ctool) {
55        _tool result = new _tool();
56        result.set_interface(ctool.getInterface());
57        result.setName(ctool.getName());
58        result.setInput(convert(ctool.getInput()));
59        result.setOutput(convert(ctool.getOutput()));
60  
61        return result;
62     }
63     /*** convert between castor and axis representations of the same schema object */
64     public static JobURN convert(org.astrogrid.workflow.beans.v1.execution.JobURN jobURN) {
65         if (jobURN == null) {
66             return null;
67         }
68         return new JobURN(jobURN.getContent());
69     }
70     /***
71      * @param output
72      * @return
73      */
74     public static _output convert(Output output) {
75        
76        org.astrogrid.applications.beans.v1.parameters.ParameterValue[] inpars = output.getParameter();
77        _output result = new _output();
78        result.setParameter(transformParameters(inpars));
79        return result;
80     }
81  
82     /***
83      * @param input
84      * @return
85      */
86     public static _input convert(Input input) {
87        _input result = new _input();
88        org.astrogrid.applications.beans.v1.parameters.ParameterValue[] inpars = input.getParameter();
89        result.setParameter(transformParameters(inpars));
90        return result;
91     }
92     
93     public static ParameterValue[] transformParameters(org.astrogrid.applications.beans.v1.parameters.ParameterValue[] inpars)
94     {
95        ParameterValue[] result = new ParameterValue[inpars.length];
96        
97        for (int i = 0; i < inpars.length; i++) {
98           // this is a bit inefficient as we are thowing away the objects created in the new above..
99           result[i] = convert(inpars[i]);
100       }
101       
102       return result;
103    }
104    
105    public static ParameterValue convert(org.astrogrid.applications.beans.v1.parameters.ParameterValue parameterValue)
106    {
107       ParameterValue result = new ParameterValue();
108       result.setName(parameterValue.getName());
109       if (parameterValue.getEncoding() != null) {
110         result.setEncoding(new NMToken(parameterValue.getEncoding()));
111       } else {
112           result.setEncoding(new NMToken());
113       }
114       result.setIndirect(parameterValue.getIndirect());
115       result.setValue(parameterValue.getValue());
116      
117       return result;
118    }
119 
120    /***
121     * @param types
122     * @return
123     */
124    /* not used
125    public static ParameterTypes convert(org.astrogrid.applications.beans.v1.parameters.types.ParameterTypes type) {
126       ParameterTypes result = null;
127       if (type != null) {
128          result = ParameterTypes.fromValue(new QName(type.toString()));
129       }
130       return result;
131    }
132    */
133 
134 
135 
136    /***
137     * @param definitions
138     * @return
139     */
140    /* not used 
141    public static org.astrogrid.applications.beans.v1.axis.ceaparameters.BaseParameterDefinition[] convert(BaseParameterDefinition[] definitions) {
142       org.astrogrid.applications.beans.v1.axis.ceaparameters.BaseParameterDefinition[] result = new org.astrogrid.applications.beans.v1.axis.ceaparameters.BaseParameterDefinition[definitions.length];
143       for (int i = 0; i < result.length; i++) {
144          result[i] = convert(definitions[i]);
145       }
146       return result;
147    }
148    */
149 
150    /***
151     * @param definition
152     * @return
153     */
154    /* not used
155    public static org.astrogrid.applications.beans.v1.axis.ceaparameters.BaseParameterDefinition convert(BaseParameterDefinition definition) {
156       org.astrogrid.applications.beans.v1.axis.ceaparameters.BaseParameterDefinition result = new org.astrogrid.applications.beans.v1.axis.ceaparameters.BaseParameterDefinition();
157       String nothing="";
158       String val;
159       result.setName(definition.getName());
160       
161       result.setDefaultValue(definition.getDefaultValue());
162       result.setType(convert(definition.getType()));
163       result.setSubType(definition.getSubType());
164       String[] encodings = definition.getAcceptEncodings();
165       StringBuffer buff = new StringBuffer();
166       for (int i = 0; i < encodings.length; i++) {
167           buff.append(encodings[i]);
168           buff.append(" ");
169       }
170       result.setAcceptEncodings(new NMTokens(buff.toString()));
171       result.setUCD(definition.getUCD());
172       result.setUI_Description(convert(definition.getUI_Description()));
173       result.setUI_Name(definition.getUI_Name());
174       result.setUnits(definition.getUnits());
175       
176       return result;
177    }
178 */
179    /***
180     * convert between the documentation elements. This is very messy just to get between two "any" elements!
181     * @param documentation
182     * @return
183     * @TODO FIXME this really needs a better implementation - it does not work - do not really understand how castor/axis deal with any - the schema has been converted to a type derived from string for now to make things easier...
184     * 
185     */
186    /* not used
187    public static org.astrogrid.applications.beans.v1.axis.ceaparameters.XhtmlDocumentation convert(XhtmlDocumentation documentation) {
188 //      MessageElement[] _an = new MessageElement[1];
189 //      StringWriter out = new StringWriter();
190 //      MessageElement el;
191 //      try {
192 //         documentation.marshal(out);
193 //         StringReader in = new StringReader(out.toString());
194 //         Document doc = XMLUtils.newDocument(new InputSource(in));
195 //         
196 //          el = new MessageElement(doc.getDocumentElement());
197 //        
198 //      }
199 //      catch (Exception e) {
200 //         
201 //         logger.error("problem with the Xdocumentation element conversion", e);
202 //         el = new MessageElement();
203 //       }
204 //       _an[0] = el;
205       org.astrogrid.applications.beans.v1.axis.ceaparameters.XhtmlDocumentation result = new org.astrogrid.applications.beans.v1.axis.ceaparameters.XhtmlDocumentation();
206       if (documentation != null) {
207          result.setValue(documentation.getContent());
208       }
209       return result;
210    }
211 
212 */
213 
214 
215    /***
216     * @param mess
217     * @return
218     */
219    public static org.astrogrid.jes.types.v1.cea.axis.MessageType convert(MessageType mess) {
220       org.astrogrid.jes.types.v1.cea.axis.MessageType result = new org.astrogrid.jes.types.v1.cea.axis.MessageType();
221       result.setContent(mess.getContent());
222       result.setLevel(convert(mess.getLevel()));
223       result.setPhase(convert(mess.getPhase()));
224       result.setSource(mess.getSource());
225       Calendar cal = Calendar.getInstance();
226       cal.setTime(mess.getTimestamp());
227       result.setTimestamp(cal);
228       return result;
229    }
230    
231    public static org.astrogrid.jes.types.v1.cea.axis.MessageType[] convert(MessageType[] mess) {
232        org.astrogrid.jes.types.v1.cea.axis.MessageType[] axis = new org.astrogrid.jes.types.v1.cea.axis.MessageType[mess.length];
233        for (int i = 0; i < mess.length; i++) {
234            axis[i] = convert(mess[i]);
235        }
236        return axis;
237    }
238    
239    public static _extension convert(Extension ext) {
240        _extension axis = new _extension();
241        axis.setKey(ext.getKey());
242        axis.setValue(ext.getContent());
243        return axis;
244    }
245    
246    public static _extension[] convert(Extension[] ext) {
247        _extension[] axis = new _extension[ext.length];
248        for ( int i = 0; i < ext.length; i++) {
249            axis[i] = convert(ext[i]);
250        }
251        return axis;
252    }
253    
254   public static ExecutionPhase convert(org.astrogrid.applications.beans.v1.cea.castor.types.ExecutionPhase ep)
255   {
256      ExecutionPhase result = ExecutionPhase.fromValue(ep.toString());
257      return result;
258   }
259 
260 
261   public static org.astrogrid.jes.types.v1.cea.axis.LogLevel convert(org.astrogrid.applications.beans.v1.cea.castor.types.LogLevel ll)
262   {
263      return org.astrogrid.jes.types.v1.cea.axis.LogLevel.fromString(ll.toString());
264   }
265   
266   public static ExecutionSummaryType convert(org.astrogrid.applications.beans.v1.cea.castor.ExecutionSummaryType castor) {
267       ExecutionSummaryType axis = new ExecutionSummaryType();
268       axis.setApplicationName(castor.getApplicationName());
269       axis.setExecutionId(castor.getExecutionId());
270       axis.setResultList(convert(castor.getResultList()));
271       axis.setInputList(new InputListType());
272       axis.getInputList().setInput(convert(castor.getInputList().getInput()));
273       axis.setStatus(convert(castor.getStatus()));
274       return axis;
275   }
276   
277   public static ResultListType convert(org.astrogrid.applications.beans.v1.cea.castor.ResultListType castor) {
278       ResultListType axis = new ResultListType();
279       axis.setResult(convert(castor.getResult()));
280       return axis;
281   }
282   
283   public static ParameterValue[] convert(org.astrogrid.applications.beans.v1.parameters.ParameterValue[] castor) {
284       ParameterValue[] axis = new ParameterValue[castor.length];
285       for (int i = 0; i < axis.length; i++) {
286           axis[i] = convert(castor[i]);
287       }
288       return axis;
289   }
290   
291  
292 }