View Javadoc

1   /*
2    * @(#)WorkflowHelper.java   1.0
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.3, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10   */
11  
12  package org.astrogrid.portal.cocoon.workflow;
13  
14  import org.apache.log4j.Logger ;
15  
16  import org.astrogrid.portal.workflow.intf.*;
17  
18  import org.astrogrid.applications.beans.v1.parameters.BaseParameterDefinition;
19  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
20  import org.astrogrid.applications.beans.v1.ParameterRef;
21  import org.astrogrid.applications.beans.v1.Interface;
22  
23  import org.astrogrid.workflow.beans.v1.Workflow;
24  import org.astrogrid.workflow.beans.v1.Tool;
25  
26  import java.util.Enumeration;
27  
28  /***
29   * The <code>WorkflowHelper</code> class represents... 
30   * <p>
31   *
32   * <p>
33   * The class... 
34   * 
35   *
36   * @author  Jeff Lusted, Phil Nicolson
37   * @version 1.0 12-Mar-2004
38   * @see     
39   * @see     
40   * @since   AstroGrid 1.4
41   */
42  public class WorkflowHelper {
43      
44      /*** Compile-time switch used to turn tracing on/off. 
45        * Set this to false to eliminate all trace statements within the byte code.*/         
46      private static final boolean 
47          TRACE_ENABLED = true ;
48          
49      private static Logger 
50          logger = Logger.getLogger( Workflow.class ) ; 
51          
52      /*** 
53        * A helper method that inserts contents into a parameter for
54        * a given tool. The old contents are used to identify the 
55        * suitable parameter as target. This can also be used to establish
56        * new contents in a virgin set of new, empty parameters by setting 
57        * the old contents to null or the empty string.
58        * <p>
59        * Note. If the contents to be inserted is a parameter with no upper cardinality  
60        * and it is not a replacement for old contents (ie: it is a new insert), then
61        * as a convenience to gui development, we examine how many empty ones are still
62        * left, and insert a new empty parameter if none are found.
63        *  
64        * @param applDescription the application description
65        * @param tool       the owning tool
66        * @param paramName  the name of the parameter
67        * @param oldContents   the contents used to identify the target parameter.
68        *                   Can be null or the empty string, in which case the first empty 
69        *                   candidate will be used.
70        * @param newContents  the contents to be inserted. 
71        * @param paramIndirect true for URN/IVORN/AGSL reference to value, false for actual value 
72        * @param bInParam  true for input, false for output.
73        * @return  boolean indicating success or failure.
74        * 
75        * 
76        **/      
77      private static boolean insertParameterValue( ApplicationDescription applDescription
78                                                 , Tool tool
79                                                 , String paramName
80                                                 , String oldContents
81                                                 , String newContents
82                                                 , boolean paramIndirect
83                                                 , boolean bInParam ) {
84      
85          if( TRACE_ENABLED ) trace( "entry: WorkflowHelper.insertParameterValue(applDescription,tool,paramName,oldValue,newValue,indirect,direction)") ; 
86          
87              boolean retValue = false ;
88              Enumeration iterator = null ;
89              ParameterValue 
90                  p = null,
91                  savedNewInsertTarget = null ;
92              int countOfEmptyParams = 0 ;
93       
94          try {
95              
96              if( bInParam ){
97                  iterator = tool.getInput().enumerateParameter() ;
98              }
99              else {
100                 iterator = tool.getOutput().enumerateParameter() ;
101             }
102             
103             while( iterator.hasMoreElements() ) {
104                 p = (ParameterValue)iterator.nextElement() ;
105                 if( p.getName().equals( paramName ) ) {
106                     // OK, we've identified the parameter...
107                     
108                     if( (oldContents == null || oldContents.equals("") )
109                         &&
110                         (p.getValue() == null || p.getValue().equals("") ) ) {
111                           
112                         // OK, we've identified that we need to insert
113                         // into an empty parameter...    
114                         p.setValue( newContents ) ;
115                         p.setIndirect( paramIndirect );
116                         savedNewInsertTarget = p ;
117                         retValue = true ;
118                         break ;
119                     }
120                     else if( (oldContents != null)
121                              &&
122                              (oldContents.equals( p.getValue() )) ) {
123                         
124                         // OK, we've identified that we wish to replace
125                         // existing contents with new contents...         
126                         p.setValue( newContents ) ;
127 						p.setIndirect( paramIndirect );
128                         retValue = true ;
129                         break ;
130                     }
131                 } // end if
132             } // end while
133             
134             if( retValue == false ) {
135                 ParameterValue pValue = new ParameterValue(); 
136                 pValue.setName( paramName ) ;
137                 pValue.setIndirect( paramIndirect ) ;
138                 ParameterRef pRef = WorkflowHelper.getParameterRef(applDescription,tool,pValue);                 
139                 BaseParameterDefinition paramDef = applDescription.getDefinitionForReference(pRef);
140                 ParameterValue paramVal = applDescription.createValueFromDefinition(paramDef);
141                            
142                 if( bInParam ){
143                    tool.getInput().addParameter( paramVal );
144                 }
145                 else {
146                    tool.getOutput().addParameter( paramVal );
147                 }
148                 
149             }
150             
151             // If we have inserted a brand new value and the cardinality is unbounded
152             if( savedNewInsertTarget != null ) {
153                 
154                 ParameterRef pRef = WorkflowHelper.getParameterRef(applDescription,tool,savedNewInsertTarget);                 
155                 
156                 if( pRef.getMaxoccurs() <= 0) { // Bug # 417: max cardinality of 0 now indicates unbounded parameter
157                   
158                     // First, count up the empty parameters of this type still left...
159                      while( iterator.hasMoreElements() ) {
160                          p = (ParameterValue)iterator.nextElement() ;
161                          if( (p.getName().equals( paramName )) 
162                              &&
163                              (p.getValue() == null || p.getValue().equals("") ) ) {
164                             
165                              countOfEmptyParams++ ;
166                          }
167                     
168                      } // endwhile
169                  
170                      // If the count of empty parameters (of this type )is now zero
171                      // then for the convenience of gui gui development we create a further empty 
172                      // parameter... 
173                      if( countOfEmptyParams == 0 ) { 
174                          BaseParameterDefinition paramDef = applDescription.getDefinitionForReference(pRef);
175                          ParameterValue paramVal = applDescription.createValueFromDefinition(paramDef);
176                          if( bInParam ){
177                              tool.getInput().addParameter( paramVal );
178                          }
179                          else {
180                              tool.getOutput().addParameter( paramVal );
181                          }
182                          
183                      }
184                      
185                 } // endif
186                            
187                                  
188             } // endif
189              
190         }
191         finally {
192             if( TRACE_ENABLED ) trace( "exit: WorkflowHelper.insertParameterValue(applDescription,tool,paramName,oldValue,newValue,direction)") ; 
193         }
194         
195         return retValue ;
196                                                           
197     } // end of WorkflowHelper.insertParameterValue(applDescription,tool,paramName,oldValue,newValue,direction)
198 
199 
200 	/*** 
201 	  * A helper method that inserts contents into a parameter for
202 	  * a given tool. The old contents are used to identify the 
203 	  * suitable parameter as target. This can also be used to establish
204 	  * new contents in a virgin set of new, empty parameters by setting 
205 	  * the old contents to null or the empty string.
206 	  * <p>
207 	  * Note. If the contents to be inserted is a parameter with no upper cardinality  
208 	  * and it is not a replacement for old contents (ie: it is a new insert), then
209 	  * as a convenience to gui development, we examine how many empty ones are still
210 	  * left, and insert a new empty parameter if none are found.
211 	  *  
212 	  * @param applDescription the application description
213 	  * @param tool       the owning tool
214 	  * @param paramName  the name of the parameter
215 	  * @param oldContents   the contents used to identify the target parameter.
216 	  *                   Can be null or the empty string, in which case the first empty 
217 	  *                   candidate will be used.
218 	  * @param newContents  the contents to be inserted. 
219 	  * @param paramIndirect whether parameter is a reference or an actual value.
220 	  *                      If 'true' then the value of the ParameterValue is expected to be a 
221       *                      URN/AGSL/IVORN that gives a reference to the actual value for this 
222       *                      parameter (hence, its an 'indirect' value). 
223       *                      If 'false' then the value of the ParameterValue is expected to be the 
224       *                      actual value for this parameter.	                        
225 	  * @return  boolean indicating success or failure.
226 	  * 
227 	  * 
228 	  **/      
229 	public static boolean insertInputParameterValue( ApplicationDescription applDescription
230 	                                               , Tool tool
231 	                                               , String paramName
232 	                                               , String oldContents
233 	                                               , String newContents
234 	                                               , boolean paramIndirect ) {
235 	    
236 	    return insertParameterValue(applDescription,tool,paramName,oldContents,newContents,paramIndirect,true) ;
237 	                                                      
238 	}
239 
240 	/*** 
241 	  * A helper method that inserts contents into a parameter for
242 	  * a given tool. The old contents are used to identify the 
243 	  * suitable parameter as target. This can also be used to establish
244 	  * new contents in a virgin set of new, empty parameters by setting 
245 	  * the old contents to null or the empty string.
246 	  * <p>
247 	  * Note. If the contents to be inserted is a parameter with no upper cardinality  
248 	  * and it is not a replacement for old contents (ie: it is a new insert), then
249 	  * as a convenience to gui development, we examine how many empty ones are still
250 	  * left, and insert a new empty parameter if none are found.
251 	  *  
252 	  * @param applDescription the application description
253 	  * @param tool       the owning tool
254 	  * @param paramName  the name of the parameter
255 	  * @param oldContents   the contents used to identify the target parameter.
256 	  *                   Can be null or the empty string, in which case the first empty 
257 	  *                   candidate will be used.
258 	  * @param paramIndirect whether parameter is a reference or an actual value.
259 	  *                      If 'true' then the value of the ParameterValue is expected to be a 
260       *                      URN/AGSL/IVORN that gives a reference to the actual value for this 
261       *                      parameter (hence, its an 'indirect' value). 
262       *                      If 'false' then the value of the ParameterValue is expected to be the 
263       *                      actual value for this parameter.	  
264 	  * @param newContents  the contents to be inserted. 
265 	  * @return  boolean indicating success or failure.
266 	  * 
267 	  * 
268 	  **/      
269 	public static boolean insertOutputParameterValue( ApplicationDescription applDescription
270 	                                                , Tool tool
271 	                                                , String paramName
272 	                                                , String oldContents
273 	                                                , String newContents 
274 	                                                , boolean paramIndirect) {
275 	    
276 	    return insertParameterValue(applDescription,tool,paramName,oldContents,newContents,paramIndirect,false) ;
277 	                                                      
278 	}  
279     
280     
281     
282     
283     
284     
285     
286     
287     
288     
289     /***> 
290       * A helper method that deletes a parameter from the given tool.
291       * The parameter value is used to identify the parameter as target. 
292       * Can be used to delete redundant empty parameters by setting the value
293       * to null or the empty string.
294       * <p>
295       * This is by far the easiest way of deleting parameters! We simply locate
296       * the parameter with the given name and given value, and delete it.
297       *  
298       * @param tool       the tool that owns the relevant parameter
299       * @param paramName  the name of the parameter
300       * @param value      the value used to identify the target parameter.
301       *                   Can be null or the empty string, in which case the first empty 
302       *                   candidate will be used. 
303       * @param bInParameter  true for input, false for output.
304       * @return boolean indicating success or failure.
305       * 
306       **/      
307     private static boolean deleteParameter( Tool tool
308                                           , String paramName
309                                           , String content
310                                           , boolean bInParameter ) {
311     
312         if( TRACE_ENABLED ) trace( "entry: WorkflowHelper.deleteParameter(tool,paramName,value,direction)") ; 
313         
314             boolean retValue = false ;
315             Enumeration iterator = null ;
316             ParameterValue p = null;
317      
318         try {
319             
320             if( bInParameter ){
321                 iterator = tool.getInput().enumerateParameter() ;
322             }
323             else {
324                 iterator = tool.getOutput().enumerateParameter() ;
325             }
326             
327             while( iterator.hasMoreElements() ) {
328                 p = (ParameterValue)iterator.nextElement() ;
329                 if( p.getName().equals( paramName ) ) {
330                     if( (content == null || content.equals("") )
331                         &&
332                         (p.getValue() == null || p.getValue().equals("") ) ) {
333                         retValue = true ;
334                         break ;
335                     }
336                     else if( (content != null)
337                              &&
338                              (content.equals( p.getValue() )) ) {
339                         retValue = true ;
340                         break ;
341                     }
342                 } // end if
343             } // end while 
344             
345             if( retValue == true ) {
346                 
347                 if( bInParameter ){
348                     tool.getInput().removeParameter(p) ;
349                 }
350                 else  {
351                     tool.getOutput().removeParameter(p) ;
352                 }
353                 
354             }
355             
356         }
357         finally {
358             if( TRACE_ENABLED ) trace( "exit: WorkflowHelper.deleteParameter(tool,paramName,value,direction)") ; 
359         }
360         
361         return retValue ;
362                                                           
363     } // end of WorkflowHelper.deleteParameter(tool,paramName,value,direction)
364 
365 
366 	/***> 
367 	  * A helper method that deletes a parameter from the given tool.
368 	  * The parameter value is used to identify the parameter as target. 
369 	  * Can be used to delete redundant empty parameters by setting the value
370 	  * to null or the empty string.
371 	  * <p>
372 	  * This is by far the easiest way of deleting parameters! We simply locate
373 	  * the parameter with the given name and given value, and delete it.
374 	  *  
375 	  * @param tool       the tool that owns the relevant parameter
376 	  * @param paramName  the name of the parameter
377 	  * @param value      the value used to identify the target parameter.
378 	  *                   Can be null or the empty string, in which case the first empty 
379 	  *                   candidate will be used. 
380 	  * @return boolean indicating success or failure.
381 	  * 
382 	  **/      
383 	public static boolean deleteInputParameter( Tool tool
384 	                                          , String paramName
385 	                                          , String content ) {
386         return WorkflowHelper.deleteParameter( tool, paramName, content, true );            
387 	}
388 
389 	/***> 
390 	  * A helper method that deletes a parameter from the given tool.
391 	  * The parameter value is used to identify the parameter as target. 
392 	  * Can be used to delete redundant empty parameters by setting the value
393 	  * to null or the empty string.
394 	  * <p>
395 	  * This is by far the easiest way of deleting parameters! We simply locate
396 	  * the parameter with the given name and given value, and delete it.
397 	  *  
398 	  * @param tool       the tool that owns the relevant parameter
399 	  * @param paramName  the name of the parameter
400 	  * @param value      the value used to identify the target parameter.
401 	  *                   Can be null or the empty string, in which case the first empty 
402 	  *                   candidate will be used. 
403 	  * @return boolean indicating success or failure.
404 	  * 
405 	  **/      
406 	public static boolean deleteOutputParameter( Tool tool
407 	                                           , String paramName
408 	                                           , String content ) {
409 	    return WorkflowHelper.deleteParameter( tool, paramName, content, false );            
410 	}    
411     
412 	public static ParameterRef getParameterRef( ApplicationDescription applDescription, Tool tool, ParameterValue pv ) {
413 	    
414 	    int[] cardinality = new int[2] ; 
415 	    
416 	    Interface[] intfs = applDescription.getInterfaces().get_interface();
417 	    Interface intf = null;
418 	    for (int i = 0; i < intfs.length; i++) {
419 	        if (intfs[i].getName().equals(tool.getInterface())) {
420 	            intf = intfs[i];
421 	            break;
422 	        }            
423 	    }
424 	            
425 	    ParameterRef pRef = applDescription.getReferenceForValue( pv, intf );
426 	    
427 	    return pRef;
428         
429 	}
430 
431 	/***> 
432 		* A helper method that returns a parameter definition for a parameter value.
433 		* The parameter value is used to identify the parameter as target.
434 		*  
435 		* @param applDescription the application description
436 		* @param tool       the tool that owns the relevant parameter
437 		* @param paramName  the name of the parameter
438 		* @return pDef      the BaseParameterDefinition
439 		* 
440 		**/	
441 	public static BaseParameterDefinition getParameterDef( ApplicationDescription applDescription, Tool tool, ParameterValue pv ) {
442 		if( TRACE_ENABLED ) trace( "entry: WorkflowHelper.getParameterDef()") ;      
443 	    
444 			Interface[] intfs = applDescription.getInterfaces().get_interface();
445 			Interface intf = null;
446 			for (int i = 0; i < intfs.length; i++) {
447 					if (intfs[i].getName().equals(tool.getInterface())) {
448 							intf = intfs[i];
449 							break;
450 					}            
451 			}
452 	            
453 		    BaseParameterDefinition pDef = applDescription.getDefinitionForValue( pv, intf );
454 
455 		    
456 		    if( TRACE_ENABLED ) trace( "exit: WorkflowHelper.getParameterDef()") ;
457 			return pDef;
458 		
459 	}		
460 
461                                 
462     private static void trace( String traceString ) {
463         System.out.println( traceString ) ;
464         // logger.debug( traceString ) ;
465     }
466     
467     private static void debug( String logString ){
468         System.out.println( logString ) ;
469         // logger.debug( logString ) ;
470     }
471 
472 }// end of class WorkflowHelper