View Javadoc

1   /*
2    * @(#)DesignAction.java   1.5
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  package org.astrogrid.portal.cocoon.workflow.design;
12  
13  import org.apache.log4j.Logger;  
14     
15  import org.astrogrid.portal.workflow.intf.*;
16  import org.astrogrid.portal.cocoon.workflow.WorkflowHelper;
17  
18  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
19  
20  import org.astrogrid.workflow.beans.v1.AbstractActivity;
21  import org.astrogrid.workflow.beans.v1.ActivityContainer;
22  import org.astrogrid.workflow.beans.v1.Catch;
23  import org.astrogrid.workflow.beans.v1.Else;
24  import org.astrogrid.workflow.beans.v1.Flow;
25  import org.astrogrid.workflow.beans.v1.For;
26  import org.astrogrid.workflow.beans.v1.If;
27  import org.astrogrid.workflow.beans.v1.Parfor;
28  import org.astrogrid.workflow.beans.v1.Sequence;
29  import org.astrogrid.workflow.beans.v1.Set;
30  import org.astrogrid.workflow.beans.v1.Script;
31  import org.astrogrid.workflow.beans.v1.Scope;
32  import org.astrogrid.workflow.beans.v1.Step;
33  import org.astrogrid.workflow.beans.v1.Then;
34  import org.astrogrid.workflow.beans.v1.Tool;
35  import org.astrogrid.workflow.beans.v1.Try;
36  import org.astrogrid.workflow.beans.v1.Unset;
37  import org.astrogrid.workflow.beans.v1.While;
38  import org.astrogrid.workflow.beans.v1.Workflow;
39  
40  import org.astrogrid.applications.beans.v1.Interface;
41  import org.astrogrid.applications.beans.v1.InterfacesType;
42  
43  import org.astrogrid.community.beans.v1.Credentials;
44  import org.astrogrid.community.beans.v1.Account;
45  import org.astrogrid.community.beans.v1.Group;
46  
47  import org.astrogrid.config.Config;
48  import org.astrogrid.config.SimpleConfig;
49  
50  import org.astrogrid.community.client.policy.service.PolicyServiceDelegate;
51  import org.astrogrid.community.client.policy.service.PolicyServiceMockDelegate;
52  import org.astrogrid.community.client.policy.service.PolicyServiceSoapDelegate;
53  
54  import org.astrogrid.community.common.policy.data.PolicyPermission  ;
55  import org.astrogrid.community.common.policy.data.PolicyCredentials ;
56  import org.astrogrid.community.common.ivorn.CommunityIvornParser;
57  import org.astrogrid.community.common.exception.CommunityIdentifierException;
58  import org.astrogrid.community.common.exception.CommunityServiceException;
59  import org.astrogrid.community.common.exception.CommunityPolicyException;
60  
61  import org.astrogrid.community.User;
62  import org.astrogrid.store.Ivorn;
63  
64  import java.util.LinkedList;
65  import java.util.ArrayList;
66  import java.util.Map;
67  import java.util.HashMap;
68  import java.util.Enumeration;
69  import java.net.MalformedURLException;
70  
71  import org.apache.avalon.framework.parameters.Parameters;
72  import org.apache.cocoon.acting.AbstractAction;
73  import org.apache.cocoon.environment.Request;
74  import org.apache.cocoon.environment.Session;
75  import org.apache.cocoon.environment.Redirector;
76  import org.apache.cocoon.environment.SourceResolver;
77  import org.apache.cocoon.environment.ObjectModelHelper;
78  
79  import com.sun.rsasign.s;
80  
81  
82  /***
83   * A Cocoon action to handle our workflow design commands.
84   *
85   */
86  public class DesignAction extends AbstractAction {
87  
88    /*** Compile-time switch used to turn tracing on/off. 
89      * Set this to false to eliminate all trace statements within the byte code.*/
90    private static final boolean TRACE_ENABLED = true;
91  
92    /*** Compile-time switch used to turn certain debugging statements on/off. 
93      * Set this to false to eliminate these debugging statements within the byte code.*/
94    private static final boolean DEBUG_ENABLED = true;
95  
96  	private static Logger logger = Logger.getLogger(DesignAction.class);
97          
98  	private static final String ASTROGRIDERROR_WORKFLOW_PERMISSION_DENIED =
99  		"AGWKFE00050",
100 		ASTROGRIDERROR_PASSTHRU = "AGWKFE00100";  
101         
102     /***
103      * Name of JNDI property holding security delegate endpoint URL
104      */
105     public static final String ORG_ASTROGRID_PORTAL_COMMUNITY_URL = "org.astrogrid.portal.community.url";        
106         
107 	/***
108 	 * Cocoon param for the user param in the session.
109 	 *
110 	 */
111 	public static final String USER_PARAM_NAME = "user-param";
112 
113 	/***
114 	 * Cocoon param for the user param in the session.
115 	 *
116 	 */
117 	public static final String COMMUNITY_PARAM_TAG = "community_account";
118 
119 	/***
120 	 * Http request param for the action.
121 	 *
122 	 */
123 	public static final String ACTION_PARAM_TAG = "action";
124 
125 	/***
126 	 * Http request param to confirm an action.
127 	 *
128 	 */
129 	public static final String CONFIRM_PARAM_TAG = "confirm";
130 	
131     public static final String USERID_COMMUNITY_SEPARATOR = "@";
132 
133 	public static final String TEMPLATE_PARAM_TAG = "template", 
134 	    EMPTY_TEMPLATE = "none_selected";	
135     
136     public static final String
137         HTTP_WORKFLOW_TAG = "workflow-tag",
138         HTTP_TOOL_TAG = "tool-tag",
139         USER_TAG = "user",
140         COMMUNITY_ACCOUNT_TAG = "community_account",
141         COMMUNITY_NAME_TAG = "community_name",
142         CREDENTIAL_TAG = "credential",
143         COMMUNITY_TOKEN_TAG = "token";
144         
145     public static final String
146         WORKFLOW_NAME_PARAMETER = "workflow-name",
147 		WORKFLOW_NEW_NAME_PARAMETER = "workflow-new-name",  
148 	    WORKFLOW_DESCRIPTION_PARAMETER = "workflow-description",
149         WORKFLOW_IVORN_PARAMETER = "workflow-ivorn";
150         
151     public static final String
152         EDIT_CONDITION_PARAMETER = "edit_condition",
153         WORKFLOW_LIST_PARAMETER = "workflow-list",
154         QUERY_LIST_PARAMETER = "query-list",
155 	    TOOL_LIST_PARAMETER = "tool-list",
156 	    USER_TOOL_LIST_PARAMETER = "user-tool-list",
157 	    TOOL_NAME_PARAMETER = "tool_name",
158 		STEP_KEY_PARAMETER = "step-key",
159         ERROR_MESSAGE_PARAMETER = "ErrorMessage",
160         LOCATION_PARAMETER = "location",
161 	    PARAM_NAME_PARAMETER = "param-name",
162 	    PARAM_VALUE_PARAMETER = "param-value",
163         ORIG_PARAM_VALUE_PARAMETER = "original-param-value",
164         IVORN_VALUE_PARAMETER = "ivorn-value",        
165         DIRECTION_PARAMETER = "direction",
166 	    SET_VALUE_PARAMETER = "set-value",
167 	    SET_VAR_PARAMETER = "set-var",
168 	    UNSET_VAR_PARAMETER = "unset-var",
169 	    STEP_NAME_PARAMETER = "step_name",
170 	    STEP_VAR_PARAMETER = "step_var",
171 	    STEP_DESCRIPTION_PARAMETER = "step_description",
172 	    SCRIPT_DESCRIPTION_PARAMETER = "script_description",
173 	    SCRIPT_BODY_PARAMETER = "script_body",
174 	    PARAM_INDIRECT = "param_indirect",
175 	    FOR_ITEMS_PARAMETER = "for_item",
176 	    FOR_VAR_PARAMETER = "for_get",
177 	    IF_TEST_PARAMETER = "if_test",
178 	    WHILE_TEST_PARAMETER = "while_test",
179 	    COPY_ACTIVITY_KEY = "copy_activity_key";
180         
181     public static final String
182         TOOLS_CACHE = "tools-cache-",
183         WORKFLOW_SUBMIT_MESSAGE = "workflow_submit_message";
184         
185     public static final String
186         FLOW_KEY_PARAMETER = "flow-key",
187         SEQUENCE_KEY_PARAMETER = "sequence-key",
188         ACTIVITY_INDEX_PARAMETER = "activity_index_key",
189 	    ACTIVITY_KEY_PARAMETER = "activity_key",
190 	    INSERTED_ACTIVITY_KEY_PARAMETER = "inserted_activity_key",
191 	    PARENT_ACTIVITY_KEY_PARAMETER = "parent_activity_key",	    
192 	    ACTIVITY_ORDER_PARAMETER = "activity_index_order",
193 	    ACTIVITY_TYPE_PARAMETER = "activity_type",
194 	    INSERT_ACTIVITY_TYPE_PARAMETER = "insert_activity_type",
195 	    PARAMATER_SEPARATOR_PARAM = "++" ;
196         
197     public static final String
198         QUERY_NAME_PARAMETER = "query-name",
199         QUERY_DESCRIPTION_PARAMETER = "query-description";
200     
201     public static final String 
202         ACTION_CREATE_WORKFLOW = "create-workflow",
203         ACTION_SAVE_WORKFLOW = "save-workflow",
204         ACTION_READ_WORKFLOW = "read-workflow",
205         ACTION_DELETE_WORKFLOW = "delete-workflow",
206         ACTION_CREATE_WORKFLOW_FROM_TEMPLATE = "create-workflow-from-template",
207         ACTION_SUBMIT_WORKFLOW = "submit-workflow",
208 	    ACTION_EDIT_WORKFLOW = "edit-workflow",
209         ACTION_READ_WORKFLOW_LIST = "read-workflow-list",
210         ACTION_READ_TOOL_LIST = "read-tool-list",        
211 		ACTION_COPY_WORKFLOW = "copy-workflow",
212         ACTION_CHOOSE_QUERY = "choose-query",
213 		ACTION_ADD_NAME_DESCRIPTION = "update workflow details",
214         ACTION_READ_QUERY = "read-query",
215         ACTION_READ_QUERY_LIST = "read-query-list",
216 	    ACTION_CREATE_TOOL = "create-tool-for-step",
217         ACTION_INSERT_TOOL_INTO_STEP = "insert-tool-into-step",
218         ACTION_INSERT_STEP = "insert-step",
219 	    ACTION_INSERT_SCRIPT = "insert-script",
220 	    ACTION_INSERT_SCRIPT_DETAILS = "update script details",
221 	    ACTION_INSERT_ACTIVITY = "insert_activity",
222 	    ACTION_INSERT_FOR_DETAILS = "update for loop details",
223 	    ACTION_INSERT_IF_DETAILS = "update if details",
224 	    ACTION_INSERT_WHILE_DETAIL = "update while loop details",
225 	    ACTION_INSERT_PARFOR_DETAILS = "update parallel for loop details",	
226 	    ACTION_INSERT_SET_DETAILS = "update set details", 
227 	    ACTION_INSERT_UNSET_DETAILS = "update unset details",   
228 	    ACTION_INSERT_STEP_DETAILS = "update step details",	         
229 		ACTION_INSERT_PARAMETER = "insert-parameter-value",
230 	    ACTION_INSERT_INPUT_PARAMETER_INTO_TOOL = "insert-input-parameter-into-tool",
231 	    ACTION_INSERT_OUTPUT_PARAMETER_INTO_TOOL = "insert-output-parameter-into-tool",
232 	    ACTION_INSERT_INPUT_PARAMETER = "insert-input-value",
233 	    ACTION_INSERT_OUTPUT_PARAMETER = "insert-output-value",
234 	    ACTION_RESET_PARAMETER = "reset-parameter",
235 	    ACTION_REMOVE_TOOL_FROM_STEP = "remove-tool-from-step", 	    
236         ACTION_READ_LISTS = "read-lists",
237         ACTION_REMOVE_WORKFLOW_FROM_SESSION = "remove-workflow-from-session",
238         ACTION_RESET_WORKFLOW = "reset-workflow",
239         ACTION_REMOVE_ACTIVITY = "remove_activity",
240 	    SAVE_WORKFLOW_IVORN_PARAMETER = "save-workflow-ivorn",
241 	    OPEN_WORKFLOW_IVORN_PARAMETER = "open-workflow-ivorn";
242         
243     public static final String
244         AUTHORIZATION_RESOURCE_WORKFLOW = "workflow" ,
245         AUTHORIZATION_ACTION_EDIT = "edit" ;
246 
247 	public static final String        
248 	    INPUT_PARAMETER_COUNT_PARAM = "input_param_count",
249 	    OUTPUT_PARAMETER_COUNT_PARAM = "output_param_count";
250 
251     /***
252     * Our action method.
253     *
254     */
255     public Map act ( Redirector redirector
256                    , SourceResolver resolver
257                    , Map objectModel
258                    , String source
259                    , Parameters params ) {                  
260         if( TRACE_ENABLED ) trace( "DesignAction.act() entry" ) ;  
261         
262         DesignActionImpl
263             myAction = null ;
264         Map
265             retMap = null ;
266         
267         try { 
268             
269             myAction = new DesignActionImpl( redirector
270                                            , resolver
271                                            , objectModel
272                                            , source
273                                            , params ) ;
274                                            
275             retMap = myAction.act() ;    
276                                           
277         }
278         catch ( Exception ex ) {
279             ex.printStackTrace();
280         }
281         finally {
282             if( TRACE_ENABLED ) trace( "DesignAction.act() exit" ) ;  
283         }
284                                         
285         return retMap ; 
286  
287     } // end of act() 
288 
289     private class DesignActionImpl {
290         
291         private Redirector redirector;
292         private SourceResolver resolver;
293         private Map objectModel, results;
294         private String source;
295         private Parameters params;
296         private Request request;
297         private Session session;
298         private String userid, community, group, token;
299         private String action;
300         private boolean bConfirm;
301         private WorkflowManager workflowManager;
302         private Workflow workflow;
303 		private String template; 
304         private Credentials credentials;
305         private User user;         
306         
307         public DesignActionImpl( Redirector redirector
308                                , SourceResolver resolver
309                                , Map objectModel
310                                , String source
311                                , Parameters params ) {                                   
312             if( TRACE_ENABLED ) trace( "DesignActionImpl() entry" ) ; 
313             
314             try {
315           
316                 this.redirector = redirector;
317                 this.resolver = resolver;
318                 this.objectModel = objectModel;
319                 this.source = source;
320                 this.params = params; 
321             
322                 this.results = new HashMap();
323             
324                 // Get current request and session.
325                 this.request = ObjectModelHelper.getRequest( objectModel );
326                 this.session = request.getSession();
327             
328                 // Load current Workflow - if any - from our HttpSession.
329                 this.workflow = (Workflow) session.getAttribute( HTTP_WORKFLOW_TAG );
330 
331                 // Load Worflow manager in case we should need it...  
332 //                Config conf = SimpleConfig.getSingleton();
333 //                conf.setProperty(WorkflowManagerFactory.WORKFLOW_STORE_KEY,"file");
334 //                
335 //                conf.setProperty(WorkflowManagerFactory.WORKFLOW_APPLIST_KEY,"xml");
336 //                conf.setProperty(WorkflowManagerFactory.WORKFLOW_APPLIST_XML_URL_KEY,"file://localhost:8080/home/jl99/downloads/test-tool-list.xml");
337 //                
338 //                
339 //                
340 //                conf.setProperty(WorkflowManagerFactory.WORKFLOW_JES_ENDPOINT_KEY,"urn:test");
341 //                assert(conf != null);
342 //                WorkflowManagerFactory fac = new WorkflowManagerFactory(conf);
343 //                assert(fac != null);
344 //                WorkflowManager man = fac.getManager();
345 //                assert(man != null );
346 ////                assert(man.getJobExecutionService() != null );
347 //                assert(man.getToolRegistry() != null );
348 //                assert(man.getWorkflowBuilder() != null );
349 //                assert(man.getWorkflowStore() != null );                    
350                 WorkflowManagerFactory wmFactory = new WorkflowManagerFactory();
351                 this.workflowManager = wmFactory.getManager() ;
352             
353                 // Get user and community 
354                 this.retrieveUserDetails();
355             
356                 this.action = request.getParameter( ACTION_PARAM_TAG );
357                 this.bConfirm = new Boolean ( request.getParameter(CONFIRM_PARAM_TAG) ).booleanValue();
358                     
359             }
360             catch( WorkflowInterfaceException wix ) {
361                 wix.printStackTrace();
362             }
363             finally {
364                 if( TRACE_ENABLED ) trace( "DesignActionImpl() exit" ); 
365             }
366               
367         } // end of DesignActionImpl()
368         
369         
370         public Map act() {
371             if( TRACE_ENABLED ) trace( "DesignActionImpl.act() entry" );      
372         
373             try {
374             	
375 				String errorMessage = null;
376             	
377                 if( action == null ) {
378                     action = ACTION_CREATE_WORKFLOW;
379                 }
380 				debug( "action is: " + action );
381                 
382                 this.consistencyCheck();
383                     
384                 if( action == null ){
385                     debug( "action is null");  
386                 }      
387                 else if( action.equals( ACTION_CREATE_WORKFLOW ) ) {
388                     this.createWorkflow();
389                 }
390                 else if( action.equals( ACTION_SAVE_WORKFLOW ) ) { 
391                     this.saveWorkflow();                   
392                 }
393                 else if( action.equals( ACTION_READ_WORKFLOW ) ) {
394                     this.readWorkflow(); 
395                 }
396 				else if( action.equals( ACTION_EDIT_WORKFLOW ) ) {
397 					this.readWorkflow(); 
398 				}                
399                 else if( action.equals( ACTION_SUBMIT_WORKFLOW ) ) {
400                     this.submitWorkflow(); 
401                 }
402 				else if( action.equals( ACTION_ADD_NAME_DESCRIPTION ) ) {
403 					this.addWorkflowNameAndDescription(); 
404 				}			
405                 else if( action.equals( ACTION_READ_TOOL_LIST ) ) {
406                     this.readToolList(); 
407                 }
408 				else if( action.equals( ACTION_READ_LISTS ) ) {
409 					this.readLists(); 
410 				}
411 			    else if( action.equals( ACTION_INSERT_INPUT_PARAMETER ) ) {
412 					this.insertInputValue();         	
413 				}                
414 				else if( action.equals( ACTION_INSERT_OUTPUT_PARAMETER ) ) {
415 					this.insertOutputValue();
416 				}
417 				else if( action.equals( ACTION_INSERT_PARAMETER ) ) {
418 					this.insertMultipleValue();
419 				}
420 				else if( action.equals( ACTION_RESET_PARAMETER ) ) {
421 				    this.resetParameter();         	
422     			}
423     			else if( action.equals( ACTION_REMOVE_TOOL_FROM_STEP )){
424     				this.removeToolFromStep() ;
425     			}
426 				else if( action.equals( ACTION_CREATE_TOOL ) ) {
427 					this.createTool();                     								
428 				}
429 				else if( action.equals( ACTION_INSERT_STEP_DETAILS )){
430 					this.insertStepDetails();  
431 				}
432 				else if( action.equals( ACTION_REMOVE_WORKFLOW_FROM_SESSION )){
433 					this.removeWorkflow();  
434 				}
435 				else if( action.equals( ACTION_RESET_WORKFLOW )){
436 					this.removeWorkflow() ;
437 					this.createWorkflow() ;  
438 				}																
439 				else if( action.equals( ACTION_INSERT_FOR_DETAILS ) ) {
440 					this.insertForDetails();                                                    
441 				}
442 				else if( action.equals( ACTION_INSERT_IF_DETAILS ) ) {
443 					this.insertIfDetails();                                                    
444 				}
445 				else if( action.equals( ACTION_INSERT_PARFOR_DETAILS ) ) {
446 					this.insertParforDetails();                                                    
447 				}
448 				else if( action.equals( ACTION_INSERT_SET_DETAILS ) ) {
449 					this.insertSetDetails();                                                    
450 				}
451 				else if( action.equals( ACTION_INSERT_UNSET_DETAILS ) ) {
452 					this.insertUnsetDetails();                                                    
453 				}
454 				else if( action.equals( ACTION_INSERT_SCRIPT_DETAILS ) ) {
455 					this.insertScriptDetails();                                                    
456 				}
457 				else if( action.equals( ACTION_INSERT_WHILE_DETAIL ) ) {
458 					this.insertWhileDetails();                                                    
459 				}				
460 				else if( action.equals( ACTION_INSERT_ACTIVITY ) ) {
461 						this.insertActivity();                                                    
462 				}																													
463                 else if( action.equals( ACTION_REMOVE_ACTIVITY ) ) {
464                     this.removeActivity();                                                    
465                 }       			                
466                 else {
467                     results = null;
468                     debug( "unsupported action"); 
469                     throw new UnsupportedOperationException( action + " no longer supported");
470                 }
471                 				
472 				this.readToolList();
473                 
474                 if (workflow != null ){
475 	                // Save the workflow in the session object...
476 					debug( "==========================================" ) ;
477     	            debug( "about to add workflow to session object..." ) ;
478 					debug( "Using workflow: " + workflow.getName() ) ;
479         	        session.setAttribute( HTTP_WORKFLOW_TAG, workflow ) ;
480             	    debug( session.getAttribute(HTTP_WORKFLOW_TAG).toString() ); 
481                 }
482                 
483 //				this.readLists() ; // Ensure request object contains latest Workflow/Query
484 
485             }
486             catch( ConsistencyException cex ) {
487                 results = null;
488                 debug( "ConsistencyException occurred");
489             }
490             //JBL Note: these should only be here during testing...
491             catch( Exception ex) {
492                 results = null;
493                 debug( "Exception: ex" );
494                 ex.printStackTrace();
495             }
496             //JBL Note: these should only be here during testing...
497             catch( Throwable th ) {
498                 results = null;
499                 debug( "Throwable th" );
500                 th.printStackTrace();
501             }
502             finally {
503                 if( TRACE_ENABLED ) trace( "DesignActionImpl.act() exit" );  
504             }
505     
506             return results;
507             
508         } // end of DesignActionImpl.act()
509         
510         
511         private void retrieveUserDetails() {
512             if( TRACE_ENABLED ) trace( "DesignActionImpl.retrieveUserDetails() entry" );   
513                      
514             try {
515                 
516                 // 22nd April 2004.JBL note. This is Jeff's quick fix...
517                 // fullUserid contains something like...
518                 // ivo://org.atrogrid.localhost/frog
519                 String fullUserid = (String)session.getAttribute( USER_TAG );
520                 this.userid = fullUserid.substring( fullUserid.lastIndexOf('/')+1 );
521                 this.community = fullUserid.substring( fullUserid.indexOf('/')+2, fullUserid.lastIndexOf('/') );
522                 this.group = this.community;
523              
524                 debug( "userid: " + this.userid );
525 //                this.community = (String)session.getAttribute( COMMUNITY_NAME_TAG );
526                 debug( "community: " + this.community );
527 //                this.group = (String)session.getAttribute( CREDENTIAL_TAG );
528                 debug( "group: " + this.group ); 
529 //                SecurityToken secToken =
530 //                   (SecurityToken)session.getAttribute( COMMUNITY_TOKEN_TAG );
531 //                this.token = secToken.getToken();
532                 debug( "token: " + this.token ); 
533                             
534                 Account account = new Account();
535                 account.setName( userid );
536                 account.setCommunity( community );
537                 
538                 Group group = new Group();
539                 group.setName( this.userid );
540                 group.setCommunity( community );
541                         
542                 this.credentials = new Credentials();
543                 credentials.setAccount( account );
544                 credentials.setGroup( group );
545                 credentials.setSecurityToken( "dummy" );
546                 
547                 //JL Late change - 26/04/2004
548                 this.user = new User( this.userid, this.community, this.group, this.token );
549                      
550             }
551             finally {
552                 if( TRACE_ENABLED ) trace( "DesignActionImpl.retrieveUserDetails() exit" );  
553             }
554                 
555         } // end of retrieveUserDetails()
556         
557         
558         private void consistencyCheck() throws ConsistencyException {
559 			if( TRACE_ENABLED ) trace( "consistencyCheck() entry" );
560 			debug( "userid: " + this.userid );
561 			debug( "community: " + this.community ); 
562             
563             if( userid == null ) {
564                 ; // redirection required 
565                 throw new ConsistencyException() ;
566             }
567             else if( action == null ) {
568                 
569                 debug( "action is null" ) ;
570                 // throw new ConsistencyException() ;
571      
572             }
573 			if( TRACE_ENABLED ) trace( "consistencyCheck()) exit" ) ;
574         }
575         
576         
577         private void createWorkflow() throws ConsistencyException {
578             if( TRACE_ENABLED ) trace( "DesignActionImpl.createWorkflow() entry" ) ;
579               
580             try {
581                 
582                 WorkflowBuilder workflowBuilder = workflowManager.getWorkflowBuilder();
583                 String
584                     name = request.getParameter( WORKFLOW_NAME_PARAMETER ),
585 					description = request.getParameter( WORKFLOW_DESCRIPTION_PARAMETER ) ;                    
586                     
587                 if( name == null ) {
588                     name = "new workflow";
589                 }
590                 
591 				if( description == null ) {
592 					description = "no description entered" ;
593 				}                
594                 
595                 if( workflow == null ) {
596                     workflow = workflowBuilder.createWorkflow( credentials, name, description ) ;
597                 }
598                 else if( bConfirm == true ) {
599                     workflow = workflowBuilder.createWorkflow( credentials, name, description ) ;
600                 }
601                 else {
602                     debug( "Use workflow that already exists in session" ) ;
603 					// workflow = workflowBuilder.createWorkflow( credentials, name, description ) ;
604                 }
605         
606             }
607             catch( WorkflowInterfaceException wix ) {
608                 wix.printStackTrace();
609             }
610             finally {
611                 if( TRACE_ENABLED ) trace( "DesignActionImpl.createWorkflow() exit" ) ;
612             }
613             
614         } // end of createWorkflow()
615         
616         
617         private void saveWorkflow() throws ConsistencyException {
618             if( TRACE_ENABLED ) trace( "DesignActionImpl.saveWorkflow() entry" ) ;
619             
620             String ivornName = request.getParameter( SAVE_WORKFLOW_IVORN_PARAMETER ) ;
621 			debug( "ivornName: " + ivornName );
622             Ivorn ivorn = null;
623                                        
624             try {
625 
626                 if( (workflow == null) || (ivornName == null) ) {
627                     ; // some logging here
628                     throw new ConsistencyException() ; 
629                 }
630                 else {
631                     ivorn = (new CommunityIvornParser(ivornName)).getIvorn();
632                     debug( "user: " + user );
633                     debug( "ivorn: " + ivorn );
634                     debug( "workflow: " + workflow );
635                     WorkflowStore wfStore = this.workflowManager.getWorkflowStore();
636                     debug( "wfStore: " + wfStore );
637                     wfStore.saveWorkflow( user, ivorn, workflow ) ;
638                 }            
639             }
640             catch( CommunityIdentifierException cix ) {
641                 cix.printStackTrace();
642             }         
643             catch( WorkflowInterfaceException wix ) {            	
644 				session.setAttribute( WORKFLOW_SUBMIT_MESSAGE, "An error has occured whilst trying to save your workflow (possibly because your workflow failed to validate - e.g. do all steps contain tools)" ) ;
645                 wix.printStackTrace();
646                }
647             finally {
648                 if( TRACE_ENABLED ) trace( "DesignActionImpl.saveWorkflow() exit" ) ;
649             }
650          
651         } // end of saveWorkflow()
652         
653    
654         private void removeWorkflow() throws ConsistencyException {
655             if( TRACE_ENABLED ) trace( "DesignActionImpl.removeWorkflow() entry" ) ;
656               
657             try {
658                 session.removeAttribute( HTTP_WORKFLOW_TAG );
659                 workflow = null ;
660             }
661             finally {
662                if( TRACE_ENABLED ) trace( "DesignActionImpl.removeWorkflow() exit" ) ;
663             }
664         } // end of removeWorkflow()  
665        
666          
667               
668         private void readWorkflow() throws ConsistencyException {
669             if( TRACE_ENABLED ) trace( "DesignActionImpl.readWorkflow() entry" ) ;
670               
671             String
672                  name = request.getParameter( WORKFLOW_NAME_PARAMETER ),
673                  ivornName = request.getParameter( OPEN_WORKFLOW_IVORN_PARAMETER ) ;
674                  debug("workflow name: " + name) ;
675                  debug("ivorn name: " + ivornName) ;
676             Ivorn ivorn = null;
677               
678             try {
679                                  
680                 if( (name == null) || (ivornName == null) ) {
681                     ; // some logging here
682                     throw new ConsistencyException() ; 
683                 }
684                 
685                 ivorn = (new CommunityIvornParser(ivornName)).getIvorn();
686                 
687 //                if( workflow == null  || bConfirm == true ) {
688 				if( true ) {
689                     WorkflowStore wfStore = this.workflowManager.getWorkflowStore();
690                     workflow = wfStore.readWorkflow( user, ivorn ) ;
691  
692                 } 
693                 
694                 if( workflow != null ) {   
695                     debug( "account: " + workflow.getCredentials().getAccount() ) ;
696                     debug( "name: " + workflow.getName() ) ; 
697                     debug( "description: " + workflow.getDescription() ) ;         
698                 }
699                     
700             }
701             catch( CommunityIdentifierException cix ) {
702                 cix.printStackTrace();
703             }
704             catch( WorkflowInterfaceException wix ) {
705                 wix.printStackTrace(); //JBL note
706             }
707             finally {
708                 if( TRACE_ENABLED ) trace( "DesignActionImpl.readWorkflow() exit" ) ;
709             }
710          
711         }
712 
713         private void submitWorkflow() throws ConsistencyException {
714             if( TRACE_ENABLED ) trace( "DesignActionImpl.submitWorkflow() entry" ) ;
715             
716             String ivornName = request.getParameter( WORKFLOW_IVORN_PARAMETER ) ;
717             String submitWorkflowMessage = "Workflow successfully submitted to JES;";
718             Ivorn ivorn = null;
719                 
720             try {
721 
722                if( workflow == null ) {
723                   ; // some logging here
724                   throw new ConsistencyException() ; 
725                }
726                else {
727                   JobExecutionService jes = this.workflowManager.getJobExecutionService();
728                   jes.submitWorkflow( workflow ) ;
729                }
730                           
731             }
732 
733             catch( WorkflowInterfaceException wix ) {
734                 wix.printStackTrace(); // JBL note
735 				submitWorkflowMessage =  "Unable to submit workflow: " + wix.getMessage() ;
736             }                        
737             finally {
738 				this.session.setAttribute( WORKFLOW_SUBMIT_MESSAGE, submitWorkflowMessage ) ;
739                 if( TRACE_ENABLED ) trace( "DesignActionImpl.submitWorkflow() exit" ) ;
740             }
741          
742         } // end of submitQuery()
743         
744         
745         private void createTool() throws ConsistencyException {
746            if( TRACE_ENABLED ) trace( "DesignActionImpl.createTool() entry" ) ;
747            try {
748             
749               this.createTool( request.getParameter( TOOL_NAME_PARAMETER ) );
750                     
751            }
752            finally {
753               if( TRACE_ENABLED ) trace( "DesignActionImpl.createTool() exit" ) ;
754            }                             
755         } // end of createTool()
756     
757 
758         private Tool createTool( String toolName ) throws ConsistencyException {
759 		   if( TRACE_ENABLED ) trace( "DesignActionImpl.createTool(toolName) entry" ) ;
760            
761            Tool tool = null;
762            
763 		   try
764 		   {
765                if( toolName == null ) {
766 			   throw new ConsistencyException() ;		   	
767 		   }
768 			
769 			trace("Toolname: " + toolName) ;  
770 			// createToolFromDefaultInterface() if no interface present
771 			if (toolName.indexOf("#") == -1)
772 			{
773 				tool = this.locateDescription(toolName).createToolFromDefaultInterface();
774 			}
775 			else
776 			{
777 				boolean intFound = false ;
778 				String interfaceName = toolName.substring(toolName.indexOf("#")+1).trim() ;
779 				String toolNewName = toolName.substring(0 , toolName.indexOf("#")).trim() ;
780 				
781 				ApplicationDescription appDesc = this.locateDescription( toolNewName ) ;
782 				InterfacesType intTypes = appDesc.getInterfaces() ;
783 				Interface intf = null ;
784 				for (int i=0 ; i < intTypes.get_interfaceCount() ; i++) 
785 				{
786 					if ( intTypes.get_interface(i).getName().equalsIgnoreCase(interfaceName)) 
787 					{
788 					    intf = intTypes.get_interface(i) ;
789 						intFound = true ;
790 					    break ;
791 					}
792 				}
793 				if (intFound)
794 				{
795 					tool = appDesc.createToolFromInterface( intf );
796 				}
797 				else
798 				{
799 					// an interface should always be found - in case not use default interface
800 					tool = appDesc.createToolFromDefaultInterface();
801 				}
802 			}              
803 				                           
804 			  this.request.setAttribute( HTTP_TOOL_TAG, tool ) ;
805 		   }
806 
807            catch( WorkflowInterfaceException wix ) {
808                wix.printStackTrace();
809            }
810 	       finally {
811 		      if( TRACE_ENABLED ) trace( "DesignActionImpl.createTool(toolName) exit" ) ;
812 		   }		
813            
814            return tool;
815            			         
816         } // end of createTool(toolName)
817            
818            
819         private void readToolList() {
820            if( TRACE_ENABLED ) trace( "DesignActionImpl.readToolList() entry" ) ;
821            
822 		   LinkedList tools = new LinkedList();
823 		   if (session.getAttribute(TOOL_LIST_PARAMETER) != null) {
824 			   tools = (LinkedList) session.getAttribute(TOOL_LIST_PARAMETER) ;				
825 		   }
826               
827            try {
828                          
829               if( tools.size() <= 0 ) 
830               {
831                   ApplicationRegistry toolRegistry = workflowManager.getToolRegistry();
832 				  ApplicationDescriptionSummary[] appDescSum = toolRegistry.listUIApplications();
833 				  				
834 				  for (int i = 0 ; i < appDescSum.length ; i++)													
835 				  {
836 				      String UIName = appDescSum[i].getUIName() ;					
837 					  String Name = appDescSum[i].getName() ;
838 					  String[] intNames = appDescSum[i].getInterfaceNames() ;
839 				  }
840 				  for (int i=0;i < appDescSum.length ;i++)
841 				  {
842 				      tools.add(appDescSum[i]) ;							
843 					  if (i > 14)
844 					      break;
845 				  }
846  
847                   this.session.setAttribute( TOOL_LIST_PARAMETER, tools ) ;                     
848               }
849               this.session.setAttribute( TOOL_LIST_PARAMETER, tools ) ; 
850        
851            }
852            catch( WorkflowInterfaceException wix ) {
853                 debug( "wix exception: " + wix.toString() );
854                 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, wix.toString() ) ;
855            }           
856            finally {
857               if( TRACE_ENABLED ) trace( "DesignActionImpl.readToolList() exit" ) ;
858            }
859            
860         } // end of readQueryList()  
861   
862 			private void updateUserToolList(String toolName) {
863 				if( TRACE_ENABLED ) trace( "DesignActionImpl.updateUserToolList() entry" ) ;
864            
865 				LinkedList tools = new LinkedList();
866 				boolean toolPresent = false;
867 				
868 				try 
869 				{				    
870 				    if (session.getAttribute(TOOL_LIST_PARAMETER) != null) 
871 				    {
872 					    tools = (LinkedList) session.getAttribute(TOOL_LIST_PARAMETER) ;				
873 				    }
874 				    else 
875 				    {
876 						ApplicationRegistry toolRegistry = workflowManager.getToolRegistry();
877 						ApplicationDescriptionSummary[] appDescSum = toolRegistry.listUIApplications();
878 					    for (int i=0;i < appDescSum.length ;i++)
879 					    {
880 						    tools.add(appDescSum[i]) ;							
881 						    if (i > 14)
882 						        break;
883 					    }
884 				    }
885                           
886 					for (int i=0; i < tools.size(); i++)
887 					{
888 						if (toolName.indexOf("#") != -1)
889 						{
890 							toolName = toolName.substring(0 , toolName.indexOf("#")).trim() ;					
891 						}
892 						if ( ((ApplicationDescriptionSummary)tools.get(i)).getName().equalsIgnoreCase(toolName) )
893 						{
894 							toolPresent = true;
895 							ApplicationDescriptionSummary a = (ApplicationDescriptionSummary)tools.get(i) ;
896 							tools.remove(i);
897 							tools.addFirst(a);
898 							break;      
899 						}
900 					}
901 					if (!toolPresent)
902 					{
903 						ApplicationRegistry toolRegistry = workflowManager.getToolRegistry();
904 						ApplicationDescriptionSummary[] appDescSum = toolRegistry.listUIApplications();
905 
906 						for (int i=0 ; i < appDescSum.length ; i++ )
907 						{
908 							if(appDescSum[i].getName().equalsIgnoreCase(toolName))
909 							{
910 								tools.addFirst(appDescSum[i]) ;
911 								break ;
912 							}
913 						}
914 											
915 						if (tools.size() > 15)
916 							tools.removeLast() ;
917 					}			    					
918 
919 					this.session.setAttribute( TOOL_LIST_PARAMETER, tools ) ;   												        									
920 				}
921 				catch( WorkflowInterfaceException wix ) {
922 						 debug( "wix exception: " + wix.toString() );
923 						 this.request.setAttribute( ERROR_MESSAGE_PARAMETER, wix.toString() ) ;
924 				}          
925 				finally {
926 					if( TRACE_ENABLED ) trace( "DesignActionImpl.updateUserToolList() exit" ) ;
927 				}
928            
929 			} // end of updateUserToolList()          
930         
931 
932 		private void readLists() {
933 			if( TRACE_ENABLED ) trace( "DesignActionImpl.readLists() entry" ) ;
934               
935 			try {
936                     
937 					// For the moment this is where we have placed the door.
938 					// If users cannot see a list, then they cannot do anything...
939 //					this.checkPermissions( AUTHORIZATION_RESOURCE_WORKFLOW
940 //									     , AUTHORIZATION_ACTION_EDIT ) ;
941 //                     
942 //                    this.readWorkflowList();          
943 //                    this.readQueryList();
944                     this.readToolList() ;
945 
946 				}
947 				catch( Exception ex ) {              
948 					this.request.setAttribute( ERROR_MESSAGE_PARAMETER, "permission denied" ) ;
949 				}
950 				finally {
951 					if( TRACE_ENABLED ) trace( "DesignActionImpl.readLists() exit" ) ;
952 				}
953                     
954 			} // end of readLists()
955 
956            
957         private void removeToolFromStep() throws ConsistencyException {
958            if( TRACE_ENABLED ) trace( "DesignActionImpl.removeToolFromStep() entry" ) ;
959 			
960            Step step = null;
961               
962            try {		
963                  step = locateStep( workflow, request.getParameter( ACTIVITY_KEY_PARAMETER ) );       
964                  step.setTool( null );   	
965            }
966            finally {
967               if( TRACE_ENABLED ) trace( "DesignActionImpl.removeToolFromStep() exit" ) ;
968             }
969                     
970 		} // end of removeToolFromStep() 
971 
972 		private void insertMultipleValue() throws ConsistencyException {
973 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertMultipleValue() entry" ) ;
974 			try {		
975 				int input_param_count = new Integer(request.getParameter( INPUT_PARAMETER_COUNT_PARAM )).intValue();
976 				int output_param_count = new Integer(request.getParameter( OUTPUT_PARAMETER_COUNT_PARAM )).intValue();
977 				int total_param_count = input_param_count + output_param_count ;
978                 
979 				for (int i = 0 ; i < input_param_count; i++)
980 				{
981 					insertInputValue(i) ;				
982 				}
983 				for (int i = input_param_count ; i < total_param_count; i++)
984 				{
985 					insertOutputValue(i) ;				
986 				}				 
987 			}
988 			finally {
989 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertMultipleValue() exit" ) ;
990 			}
991 				}
992 
993 		private void insertValue() throws ConsistencyException {
994 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertValue() entry" ) ;
995 			try {		
996 				String direction = request.getParameter( DIRECTION_PARAMETER ) ;
997 				
998 				if (direction.equalsIgnoreCase("input")) 
999 				{
1000 					insertInputValue() ;
1001 				}
1002 				else if (direction.equalsIgnoreCase("output"))
1003 				{
1004 					insertOutputValue() ;
1005 				}
1006 				else
1007 				{
1008 					debug("Direction of parameter not available");
1009 				}
1010 			}
1011 			finally {
1012 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertValue() exit" ) ;
1013 			}
1014         }  	  	
1015 
1016 
1017       private void insertInputValue() throws ConsistencyException {
1018          if( TRACE_ENABLED ) trace( "DesignActionImpl.insertInputValue() entry" ) ;
1019 			
1020          Step step = null;
1021          Tool tool = null ;
1022          ParameterValue p = null ;
1023 		 boolean parameterIndirect = false;
1024               
1025          try {
1026             // Tool should already have been inserted into step
1027 			
1028             String oldParameterValue = request.getParameter( ORIG_PARAM_VALUE_PARAMETER );						
1029 			String parameterName = request.getParameter( PARAM_NAME_PARAMETER ) ;				    					
1030 			String parameterValue = request.getParameter( PARAM_VALUE_PARAMETER ) ;
1031 			String activityKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1032 			String ivornValue = request.getParameter( IVORN_VALUE_PARAMETER ) ;
1033 //		Bug #560: PARAM_INDIRECT is now a radio button			
1034 			if (request.getParameter( PARAM_INDIRECT ).equalsIgnoreCase("on") )
1035 			  parameterIndirect = true;
1036 //			boolean parameterIndirect = new Boolean(request.getParameter( PARAM_INDIRECT ) ).booleanValue() ;
1037 			debug( "ivornValue: " + ivornValue );
1038 			debug( "parameterName:" + parameterName ) ;
1039 			debug( "parameterValue: " + parameterValue ) ;
1040 			debug( "oldParameterValue: " + oldParameterValue ) ;
1041 			debug( "activityKey: " + activityKey ) ;
1042 			debug( "parameterIndirect: " + parameterIndirect  ) ;   
1043                             
1044 			if ( parameterName == null) {
1045 				debug( "parameterName is null" ) ;
1046 			}
1047 			else if ( parameterValue == null) {
1048 				debug( "parameterValue is null" ) ;
1049 			}
1050 			else if ( activityKey == null) {
1051 				debug( "activityKey is null" ) ;
1052 			}
1053 			else if (ivornValue != null && ivornValue.length() > 0) {
1054 				debug( "setting parameterValue to equal ivornValue" ) ;
1055 				parameterValue = ivornValue ;          
1056 				debug( "parameterValue now: " + parameterValue ) ;
1057 			}
1058 
1059             step = locateStep( workflow, request.getParameter( ACTIVITY_KEY_PARAMETER ) );
1060             tool = step.getTool() ;
1061             ApplicationRegistry applRegistry = workflowManager.getToolRegistry();
1062             ApplicationDescription applDescription = applRegistry.getDescriptionFor( tool.getName() );
1063 				
1064             WorkflowHelper.insertInputParameterValue( applDescription
1065                                                     , tool
1066                                                     , parameterName
1067                                                     , oldParameterValue
1068                                                     , parameterValue
1069                                                     , parameterIndirect ) ;
1070 				
1071 		  }
1072             catch( WorkflowInterfaceException wix ) {
1073                 wix.printStackTrace();
1074             }
1075 		  finally {
1076 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertInputValue() exit" ) ;
1077 		  }
1078                     
1079 	  } // end of insertInputValue()       
1080 
1081 	  private void insertInputValue(int i) throws ConsistencyException {
1082 	      if( TRACE_ENABLED ) trace( "DesignActionImpl.insertInputValue() entry" ) ;
1083 			
1084 		  Step step = null;
1085 		  Tool tool = null ;
1086 		  ParameterValue p = null ;
1087 	      boolean parameterIndirect = false;
1088 	      String paramCount = "" + i ;
1089               
1090 	      try {
1091 			   // Tool should already have been inserted into step
1092 			
1093 			   String oldParameterValue = request.getParameter( ORIG_PARAM_VALUE_PARAMETER+"#input#"+paramCount );						
1094 		       String parameterName = request.getParameter( PARAM_NAME_PARAMETER+"#input#"+paramCount ) ;				    					
1095 		       String parameterValue = request.getParameter( PARAM_VALUE_PARAMETER+"#input#"+paramCount ) ;
1096 		       String activityKey = request.getParameter( ACTIVITY_KEY_PARAMETER+"#input#"+paramCount ) ;
1097 		       String ivornValue = request.getParameter( IVORN_VALUE_PARAMETER+"#input#"+paramCount ) ;
1098 			
1099 		       if (request.getParameter( PARAM_INDIRECT+"#input#"+paramCount ).equalsIgnoreCase("on") )
1100 			     parameterIndirect = true;
1101 
1102 		debug( "multi: ivornValue: " + i + ": " + ivornValue );
1103 		debug( "multi: parameterName:" + i + ": " + parameterName ) ;
1104 		debug( "multi: parameterValue: " + i + ": " + parameterValue ) ;
1105 		debug( "multi: oldParameterValue: " + i + ": " + oldParameterValue ) ;
1106 		debug( "multi: activityKey: " + i + ": " + activityKey ) ;
1107 		debug( "multi: parameterIndirect: " + i + ": " + parameterIndirect  ) ;   
1108                             
1109 		if ( parameterName == null) {
1110 			debug( "parameterName is null" ) ;
1111 		}
1112 		else if ( parameterValue == null) {
1113 			debug( "parameterValue is null" ) ;
1114 		}
1115 		else if ( activityKey == null) {
1116 			debug( "activityKey is null" ) ;
1117 		}
1118 		else if (ivornValue != null && ivornValue.length() > 0) {
1119 			debug( "setting parameterValue to equal ivornValue" ) ;
1120 			parameterValue = ivornValue ;          
1121 			debug( "parameterValue now: " + parameterValue ) ;
1122 		}
1123 		
1124 		if ( (parameterValue != null && parameterValue.length() > 0 ) || (oldParameterValue.length() > 0) )
1125 		{			
1126 		    step = locateStep( workflow, activityKey );
1127 		    tool = step.getTool() ;
1128 		    ApplicationRegistry applRegistry = workflowManager.getToolRegistry();
1129 		    ApplicationDescription applDescription = applRegistry.getDescriptionFor( tool.getName() );
1130 
1131             if (parameterValue.indexOf(PARAMATER_SEPARATOR_PARAM) == -1 ) // single parameter value
1132                 {
1133 		            WorkflowHelper.insertInputParameterValue( applDescription
1134 					        							    , tool
1135 							        					    , parameterName
1136 									        			    , oldParameterValue
1137 											        	    , parameterValue
1138 												            , parameterIndirect ) ;
1139                 }
1140             else // multiple parameter values
1141             {
1142             	while (parameterValue.indexOf(PARAMATER_SEPARATOR_PARAM) != -1)
1143             	{
1144                     String singleParamValue = parameterValue.substring( 0, parameterValue.indexOf(PARAMATER_SEPARATOR_PARAM ) );
1145                     parameterValue = parameterValue.substring(singleParamValue.length() + 2 , parameterValue.length() ) ;
1146 
1147 					WorkflowHelper.insertInputParameterValue( applDescription
1148 															, tool
1149 															, parameterName
1150 															, ""
1151 															, singleParamValue.trim()
1152 															, parameterIndirect ) ;
1153         		
1154             	}
1155             	// and the final value
1156 				WorkflowHelper.insertInputParameterValue( applDescription
1157 														, tool
1158 														, parameterName
1159 														, ""
1160 														, parameterValue.trim()
1161 														, parameterIndirect ) ;            	
1162             }
1163 		}				
1164 		}
1165 		catch( WorkflowInterfaceException wix ) {
1166 			wix.printStackTrace();
1167 		}
1168 		finally {
1169 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertInputValue() exit" ) ;
1170 		}
1171                     
1172 	} // end of insertInputValue()       
1173 
1174 
1175 
1176         private void insertOutputValue() throws ConsistencyException {
1177            if( TRACE_ENABLED ) trace( "DesignActionImpl.insertOutputValue() entry" ) ;
1178 			
1179 		   Step step = null;
1180 		   Tool tool = null ;
1181 		   ParameterValue p = null ;
1182 		   boolean parameterIndirect = false ;
1183               
1184 	       try {
1185 				 // Tool should already have been inserted into step
1186 									
1187 			     String
1188                     oldParameterValue = request.getParameter( ORIG_PARAM_VALUE_PARAMETER ),
1189 					parameterName = request.getParameter( PARAM_NAME_PARAMETER ),			  
1190 					parameterValue = request.getParameter( PARAM_VALUE_PARAMETER ) ;
1191 					String ivornValue = request.getParameter( IVORN_VALUE_PARAMETER ) ;
1192 // Bug #560: PARAM_INDIRECT is now a radio button
1193 					if (request.getParameter( PARAM_INDIRECT ).equalsIgnoreCase("on") )
1194 						parameterIndirect = true;					
1195 //					boolean parameterIndirect = new Boolean( request.getParameter( PARAM_INDIRECT ) ).booleanValue() ;
1196 					debug( "ivornValue: " + ivornValue );					
1197                             
1198 			     if ( parameterName == null) {
1199 					debug( "parameterName is null" ) ;
1200 			     }
1201 			     else if ( parameterValue == null) {
1202 					debug( "parameterValue is null" ) ;
1203 			     }
1204 			    
1205 				else if (ivornValue != null && ivornValue.length() > 0) {
1206 					debug( "setting parameterValue to equal ivornValue" ) ;
1207 					parameterValue = ivornValue ;          
1208 					debug( "parameterValue now: " + parameterValue ) ;
1209 				}			     
1210 				
1211                step = locateStep( workflow, request.getParameter( ACTIVITY_KEY_PARAMETER ) );
1212                tool = step.getTool() ;
1213                ApplicationRegistry applRegistry = workflowManager.getToolRegistry();
1214                ApplicationDescription applDescription = applRegistry.getDescriptionFor( tool.getName() );
1215                 
1216                WorkflowHelper.insertOutputParameterValue( applDescription
1217                                                         , tool
1218                                                         , parameterName
1219                                                         , oldParameterValue
1220                                                         , parameterValue
1221 					                                    , parameterIndirect ) ;
1222 				
1223 		   }
1224            catch( WorkflowInterfaceException wix ) {
1225                wix.printStackTrace();
1226            }
1227 		   finally {
1228 			  if( TRACE_ENABLED ) trace( "DesignActionImpl.insertOutputValue() exit" ) ;
1229 		   }
1230                     
1231 	} // end of insertOutputValue()
1232 
1233 		private void insertOutputValue(int i) throws ConsistencyException {
1234 		    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertOutputValue() entry" ) ;
1235 	
1236 		    Step step = null;
1237 		    Tool tool = null ;
1238 		    ParameterValue p = null ;
1239 		    boolean parameterIndirect = false ;
1240 			String paramCount = "" + i ;
1241 			try {
1242 			// Tool should already have been inserted into step
1243 									
1244 			String oldParameterValue = request.getParameter( ORIG_PARAM_VALUE_PARAMETER+"#output#"+paramCount ) ;
1245 			String parameterName = request.getParameter( PARAM_NAME_PARAMETER+"#output#"+paramCount ) ;
1246 			String parameterValue = request.getParameter( PARAM_VALUE_PARAMETER+"#output#"+paramCount ) ;
1247 			String activityKey = request.getParameter( ACTIVITY_KEY_PARAMETER+"#output#"+paramCount ) ;
1248 			String ivornValue = request.getParameter( IVORN_VALUE_PARAMETER+"#output#"+paramCount ) ;
1249 			if (request.getParameter( PARAM_INDIRECT+"#output#"+paramCount ).equalsIgnoreCase("on") )
1250 				parameterIndirect = true;
1251 
1252 				debug( "multi: ivornValue: " + i + ": " + ivornValue );
1253 				debug( "multi: parameterName:" + i + ": " + parameterName ) ;
1254 				debug( "multi: parameterValue: " + i + ": " + parameterValue ) ;
1255 				debug( "multi: oldParameterValue: " + i + ": " + oldParameterValue ) ;
1256 				debug( "multi: activityKey: " + i + ": " + activityKey ) ;
1257 				debug( "multi: parameterIndirect: " + i + ": " + parameterIndirect  ) ;									
1258 
1259 			if ( parameterName == null) {
1260 				debug( "parameterName is null" ) ;
1261 			}
1262 			else if ( parameterValue == null) {
1263 				debug( "parameterValue is null" ) ;
1264 			}
1265 			    
1266 			else if (ivornValue != null && ivornValue.length() > 0) {
1267 				debug( "setting parameterValue to equal ivornValue" ) ;
1268 				parameterValue = ivornValue ;          
1269 				debug( "parameterValue now: " + parameterValue ) ;
1270 			}			     
1271 
1272 			step = locateStep( workflow, activityKey );
1273 			tool = step.getTool() ;
1274 			ApplicationRegistry applRegistry = workflowManager.getToolRegistry();
1275 			ApplicationDescription applDescription = applRegistry.getDescriptionFor( tool.getName() );
1276                 
1277 			WorkflowHelper.insertOutputParameterValue( applDescription
1278 													, tool
1279 													, parameterName
1280 													, oldParameterValue
1281 													, parameterValue
1282 													, parameterIndirect ) ;
1283 				
1284 		    }
1285 			catch( WorkflowInterfaceException wix ) {
1286 			   wix.printStackTrace();
1287 		    }
1288 		    finally {
1289 			  if( TRACE_ENABLED ) trace( "DesignActionImpl.insertOutputValue() exit" ) ;
1290 		   }
1291                     
1292     } // end of insertOutputValue()
1293 
1294 
1295     private void resetParameter() throws ConsistencyException {
1296        if( TRACE_ENABLED ) trace( "DesignActionImpl.resetParameter() entry" ) ;
1297 			
1298        Step step = null;
1299        Tool tool = null ;
1300        ParameterValue p = null ;
1301               
1302         try {		
1303 									
1304            String
1305               parameterName = request.getParameter( PARAM_NAME_PARAMETER ),
1306               parameterValue = request.getParameter( PARAM_VALUE_PARAMETER ),
1307               direction = request.getParameter( DIRECTION_PARAMETER );
1308            boolean bInput = direction.equalsIgnoreCase( "input" );
1309                             
1310            if ( parameterName == null) {
1311               debug( "parameterName is null" ) ;
1312            }
1313           else if ( parameterValue == null) {
1314               debug( "parameterValue is null" ) ;
1315           }      
1316           else if ( direction == null) {
1317              debug( "direction is null" ) ;
1318           }
1319 
1320           step = locateStep( workflow, request.getParameter( ACTIVITY_KEY_PARAMETER ) );
1321           tool = step.getTool() ;
1322 				
1323           ApplicationRegistry applRegistry = workflowManager.getToolRegistry();
1324           ApplicationDescription applDescription = applRegistry.getDescriptionFor( tool.getName() );
1325     
1326           if( bInput ) {                           
1327               WorkflowHelper.insertInputParameterValue( applDescription, tool, parameterName, parameterValue, "", false ) ;   
1328           } else {
1329               WorkflowHelper.insertOutputParameterValue( applDescription, tool, parameterName, parameterValue, "", false ) ;   
1330           }
1331                                            
1332 				
1333        }
1334        catch( WorkflowInterfaceException wix ) {
1335            wix.printStackTrace();
1336        }
1337 	   finally {
1338           if( TRACE_ENABLED ) trace( "DesignActionImpl.resetParameter() exit" ) ;
1339 	   }
1340                     
1341     } // end of deleteParameter() 
1342 	
1343 	
1344 	private void pasteActivity() throws ConsistencyException {
1345 		if( TRACE_ENABLED ) trace( "DesignActionImpl.pasteActivity() entry" ) ;
1346         
1347 		AbstractActivity abstractActivity = null ;      
1348 		try {
1349 			String xpathKey = (String)session.getAttribute( COPY_ACTIVITY_KEY ) ;			   
1350 			if ( xpathKey == null) {
1351 				debug( "xpathKey is null" ) ;
1352 				throw new ConsistencyException();
1353 			}
1354 			else
1355 			{				
1356                  Step copyStep = new Step() ;
1357 				 Step origionalStep = new Step() ;
1358 				 origionalStep = locateStep( workflow, xpathKey ) ;
1359                  copyStep.setName( origionalStep.getName()+"_COPY" ) ;
1360                  copyStep.setDescription( origionalStep.getDescription() ) ;
1361                  copyStep.setTool( origionalStep.getTool() ) ;                 
1362                  this.insertActivity( copyStep ) ; 
1363 			}          
1364 		}
1365 		finally {
1366 			if( TRACE_ENABLED ) trace( "DesignActionImpl.pasteActivity() exit" ) ;
1367 		}                    
1368 	} // end of pasteActivity()	
1369 
1370 
1371         private void insertStep() throws ConsistencyException {
1372            if( TRACE_ENABLED ) trace( "DesignActionImpl.insertStep() entry" ) ;
1373               
1374            try {
1375            	  Step step = new Step() ;
1376            	  step.setDescription("...") ;
1377               this.insertActivity( step ) ;
1378             }
1379             finally {
1380                   if( TRACE_ENABLED ) trace( "DesignActionImpl.insertStep() exit" ) ;
1381             }
1382                     
1383         } // end of insertStep()       
1384         
1385         
1386         private void insertFlow() throws ConsistencyException {
1387            if( TRACE_ENABLED ) trace( "DesignActionImpl.insertFlow() entry" ) ;
1388               
1389            try {
1390               this.insertActivity( new Flow() ) ;          
1391             }
1392             finally {
1393                   if( TRACE_ENABLED ) trace( "DesignActionImpl.insertFlow() exit" ) ;
1394             }
1395                     
1396         } // end of insertFlow()
1397 
1398 		private void insertElse() throws ConsistencyException {
1399 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertFor() entry" ) ;
1400               
1401 			try {
1402 				If ifActivity = new If() ;
1403 				Else elseActivity = new Else() ;			
1404 				elseActivity.setActivity(new Sequence() );					
1405 				
1406 				String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;    
1407 				if ( xpathKey == null) {
1408 				    debug( "xpathKey is null" ) ;
1409 				    throw new ConsistencyException();
1410 				}
1411 				else
1412 				{				
1413 				    ifActivity = locateIf( workflow, xpathKey ) ;				  				    
1414 				    ifActivity.setElse( elseActivity ) ;
1415 				}          
1416 			}
1417 			finally {
1418 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertElse() exit" ) ;
1419 			}                    
1420 		} // end of insertElse()        
1421 
1422 		private void insertFor() throws ConsistencyException {
1423 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertFor() entry" ) ;
1424               
1425 		    try {
1426 		    	For forActivity = new For();
1427 		    	forActivity.setActivity(new Sequence() );		    	
1428 			    this.insertActivity( forActivity ) ;          
1429 		    }
1430 		    finally {
1431 		        if( TRACE_ENABLED ) trace( "DesignActionImpl.insertFor() exit" ) ;
1432 		    }                    
1433 		} // end of insertFor()
1434 		
1435 		
1436 		private void insertParfor() throws ConsistencyException {
1437 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertParfor() entry" ) ;
1438               
1439 			try {
1440 				Parfor parForActivity = new Parfor() ;
1441 				parForActivity.setActivity( new Sequence() ) ;
1442 				this.insertActivity( parForActivity ) ; 
1443 			}
1444 			finally {
1445 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertParfor() exit" ) ;
1446 			}                    
1447 		} // end of insertParfor()
1448 		
1449 		
1450 		private void insertParforDetails() throws ConsistencyException {
1451 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertParforDetails() entry" ) ;
1452               
1453 			Parfor parForObject = null ;
1454             
1455 			try {
1456 				String defaultVar = "(the name of the loop variable...)" ;
1457 				String defaultItems = "(A sequence or iterator of items - the loop variable will be assigned to each in turn, and then loop body executed...)" ;					
1458 			    String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1459 			    String items = request.getParameter( FOR_ITEMS_PARAMETER ) ;
1460 				String var = request.getParameter( FOR_VAR_PARAMETER ) ;
1461                                 
1462 				if ( xpathKey == null) {
1463 					debug( "xpathKey is null" ) ;
1464 				}
1465 
1466 				parForObject = locateParfor( workflow, xpathKey ); 
1467 				if (!(items.equalsIgnoreCase(defaultItems))) {				 
1468 				    parForObject.setItems(items);
1469 				}
1470 				if (!(var.equalsIgnoreCase(defaultVar))) {				
1471 				    parForObject.setVar(var);
1472 				}
1473 			}
1474 			finally {
1475 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertParforDetails() exit" ) ;
1476 			}                    
1477 		} // end of insertParforDetails()						
1478 		
1479 		
1480 		private void insertForDetails() throws ConsistencyException {
1481 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertForDetails() entry" ) ;
1482               
1483 			 For forObject = null ;
1484             
1485              try {
1486              	String defaultVar = "(the name of the loop variable...)" ;
1487              	String defaultItems = "(A sequence or iterator of items - the loop variable will be assigned to each in turn, and then loop body executed...)" ;	
1488 		        String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1489 	            String items = request.getParameter( FOR_ITEMS_PARAMETER ).trim() ;
1490 				String var = request.getParameter( FOR_VAR_PARAMETER ).trim() ;	
1491 				// Bug #799 - temporary fix ...' is used to prevent text area collapsing in xsl
1492 				if (var.equalsIgnoreCase("..."))
1493 						var = "" ;
1494                                 
1495 	            if ( xpathKey == null) {
1496 		           debug( "xpathKey is null" ) ;
1497 	            }
1498 
1499 				forObject = locateFor( workflow, xpathKey );
1500 				if (!(items.equalsIgnoreCase(defaultItems))) {
1501 					forObject.setItems(items);				  
1502 				}				    
1503 
1504 				if (!(var.equalsIgnoreCase(defaultVar))) {
1505 				    forObject.setVar(var);
1506 				}
1507 				
1508             }
1509 			finally {
1510 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertForDetails() exit" ) ;
1511 			}                    
1512 		} // end of insertForDetails()	
1513 		
1514 		
1515 		private void insertThen() throws ConsistencyException {
1516 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertThen() entry" ) ;
1517               
1518 			try {
1519 				If ifActivity = new If() ;
1520 				Then thenActivity = new Then() ;			
1521 				thenActivity.setActivity( new Sequence() );					
1522 				
1523 				String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;    
1524 				if ( xpathKey == null) {
1525 						debug( "xpathKey is null" ) ;
1526 				}
1527 				else
1528 				{				
1529 						ifActivity = locateIf( workflow, xpathKey ) ;				  				    
1530 						ifActivity.setThen( thenActivity ) ;
1531 				}          
1532 			}
1533 			finally {
1534 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertThen() exit" ) ;
1535 			}                    
1536 		} // end of insertThen()
1537 		
1538 		
1539 		private void insertWhile() throws ConsistencyException {
1540 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertWhile() entry" ) ;
1541               
1542 				try {
1543 					While whileActivity = new While();
1544 					whileActivity.setActivity(new Sequence() );
1545 					this.insertActivity( whileActivity ) ;
1546 				}
1547 				finally {
1548 						if( TRACE_ENABLED ) trace( "DesignActionImpl.insertWhile() exit" ) ;
1549 				}                    
1550 		} // end of insertWhile()
1551 
1552 		
1553 		private void insertWhileDetails() throws ConsistencyException {
1554 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertWhileDetails() entry" ) ;
1555               
1556 			 While whileObject = null ;
1557             
1558 			try {	
1559 			    String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1560 				String test = request.getParameter( WHILE_TEST_PARAMETER ) ;				
1561                                 
1562 				if ( xpathKey == null) {
1563 			        debug( "xpathKey is null" ) ;
1564 				}
1565 
1566 				whileObject = locateWhile( workflow, xpathKey );  
1567 				whileObject.setTest(test);				
1568 				
1569 			}
1570 			finally {
1571 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertWhileDetails() exit" ) ;
1572 			}                    
1573 		} // end of insertWhileDetails()		
1574 		
1575 				
1576 		private void insertIf() throws ConsistencyException {
1577 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertIf() entry" ) ;
1578               
1579 			try {
1580 				If ifActivity = new If() ;
1581 				Then thenActivity = new Then() ;
1582 				thenActivity.setActivity(new Sequence() ) ;
1583 				ifActivity.setThen(thenActivity) ;
1584 				this.insertActivity( ifActivity ) ;          
1585 			}
1586 			finally {
1587 			    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertIf() exit" ) ;
1588 			}                    
1589 		} // end of insertIf()	
1590 		
1591 		
1592 		private void insertIfDetails() throws ConsistencyException {
1593 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertIfDetails() entry" ) ;
1594               
1595 			If ifObject = null ;						
1596             
1597 			try {	
1598 			    String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1599 				String ifTest = request.getParameter( IF_TEST_PARAMETER ) ;
1600                                 
1601 				if ( xpathKey == null) {
1602 				    debug( "xpathKey is null" ) ;
1603 				}
1604 
1605 				ifObject = locateIf( workflow, xpathKey );  
1606 				ifObject.setTest(ifTest);				
1607 			}
1608 			finally {
1609 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertIfDetails() exit" ) ;
1610 			}                    
1611 		} // end of insertIfDetails()
1612 
1613 
1614 		private void insertScope() throws ConsistencyException {
1615 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertScope() entry" ) ;
1616               
1617 			try {
1618 				Scope scopeActivity = new Scope();
1619 				scopeActivity.setActivity(new Sequence() );
1620 				this.insertActivity( scopeActivity ) ;         
1621 			}
1622 			finally {
1623 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertScope() exit" ) ;
1624 			}                    
1625 		} // end of insertScope()
1626 
1627 
1628 		private void insertSet() throws ConsistencyException {
1629 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertSet() entry" ) ;
1630               
1631 			try {
1632 					this.insertActivity( new Set() ) ;          
1633 			}
1634 			finally {
1635 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertSet() exit" ) ;
1636 			}                    
1637 		} // end of insertSet()
1638 
1639 
1640 		private void insertSetDetails() throws ConsistencyException {
1641 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertSetDetails() entry" ) ;
1642               
1643 			Set setObject = null ;
1644             
1645 			try {	
1646 				String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1647 				String val = request.getParameter( SET_VALUE_PARAMETER ) ;
1648 				String var = request.getParameter( SET_VAR_PARAMETER ).trim() ;
1649 				// Bug #799 - temporary fix ...' is used to prevent text area collapsing in xsl
1650 				if (var.equalsIgnoreCase("..."))
1651 				    var = "" ;
1652                                 
1653 				if ( xpathKey == null) {
1654 					debug( "xpathKey is null" ) ;
1655 				}
1656 
1657 				setObject = locateSet( workflow, xpathKey );  
1658 				setObject.setValue(val);
1659 				setObject.setVar(var);
1660 				
1661 						}
1662 			finally {
1663 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertSetDetails() exit" ) ;
1664 			}                    
1665 		} // end of insertSetDetails()
1666 		
1667 		
1668 		private void insertUnset() throws ConsistencyException {
1669 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertUnset() entry" ) ;
1670               
1671 			try {
1672 					this.insertActivity( new Unset() ) ;          
1673 			}
1674 			finally {
1675 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertUnset() exit" ) ;
1676 			}                    
1677 		} // end of insertUnset()
1678 		
1679 		
1680 		private void insertUnsetDetails() throws ConsistencyException {
1681 			if( TRACE_ENABLED ) trace( "DesignActionImpl.insertUnsetDetails() entry" ) ;
1682               
1683 			Unset unsetObject = null ;
1684             
1685 			try {	
1686 				String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1687 				String var = request.getParameter( UNSET_VAR_PARAMETER ) ;
1688                                 
1689 				if ( xpathKey == null) {
1690 					debug( "xpathKey is null" ) ;
1691 				}
1692 
1693 				unsetObject = locateUnset( workflow, xpathKey );  
1694 				unsetObject.setVar(var);				
1695 			}
1696 			finally {
1697 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertUnsetDetails() exit" ) ;
1698 			}                    
1699 		} // end of insertUnsetDetails()
1700 
1701         
1702 		private void insertScript() throws ConsistencyException {
1703 		    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertScript() entry" ) ;
1704               
1705 			try {
1706 			    this.insertActivity( new Script() ) ;          
1707 			}
1708 			finally {
1709 			    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertScript() exit" ) ;
1710 			}                    
1711 		} // end of insertScript()
1712 		
1713 		
1714 		private void insertScriptDetails() throws ConsistencyException {
1715 		    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertScriptBody() entry" ) ;
1716               
1717 			Script script = null ;              
1718               
1719 			try {
1720 				
1721 				String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1722 				String body = request.getParameter( SCRIPT_BODY_PARAMETER ) ;
1723 				String desc = request.getParameter( SCRIPT_DESCRIPTION_PARAMETER ) ;
1724                                 
1725 				if ( xpathKey == null) {
1726 					debug( "xpathKey is null" ) ;
1727 				}
1728 
1729 				script = locateScript( workflow, xpathKey );  
1730 				script.setBody( body );
1731 				script.setDescription( desc );        
1732 			}
1733 			finally {
1734 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertScriptBody() exit" ) ;
1735 			}                    
1736 		} // end of insertScriptBody()
1737 			
1738 			
1739         private void insertSequence() throws ConsistencyException {
1740            if( TRACE_ENABLED ) trace( "DesignActionImpl.insertSequence() entry" ) ;
1741               
1742            try {
1743               this.insertActivity( new Sequence() ) ;          
1744             }
1745             finally {
1746                   if( TRACE_ENABLED ) trace( "DesignActionImpl.insertSequence() exit" ) ;
1747             }
1748                     
1749         } // end of insertSequence()       
1750 
1751 
1752 		private void addWorkflowNameAndDescription() throws ConsistencyException 
1753 		{
1754 			if( TRACE_ENABLED ) trace( "DesignActionImpl.addWorkflowNameAndDescription() entry" ) ;
1755               
1756 			try {
1757 				
1758 				if( workflow == null ) {
1759 						throw new ConsistencyException() ; 
1760 				}
1761 								
1762 				String
1763 					name = request.getParameter( WORKFLOW_NAME_PARAMETER ),
1764 					description = request.getParameter( WORKFLOW_DESCRIPTION_PARAMETER ) ;                    
1765                     
1766 				if( name == null ) {
1767 					name = "new workflow";
1768 				}
1769                 
1770 				if( description == null ) {
1771 					description = "no description entered" ;
1772 				}
1773 				workflow.setName(name) ;
1774 				workflow.setDescription(description) ;		          
1775 			}
1776 			finally {
1777 				if( TRACE_ENABLED ) trace( "DesignActionImpl.addWorkflowNameAndDescription() exit" ) ;
1778 			}                    
1779 		} // end of addWorkflowNameAndDescription()         
1780 
1781 
1782         private void insertActivity( AbstractActivity activity ) throws ConsistencyException {
1783            if( TRACE_ENABLED ) trace( "DesignActionImpl.insertActivity() entry" ) ;
1784             
1785            Step step = null;
1786            ActivityContainer activityContainer = null;
1787            int index = 0;
1788            int highWaterMark = 0;
1789               
1790            try {
1791 
1792               String activityTargetKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1793               debug("activityTargetKey: " + activityTargetKey ) ;
1794 			  String parentKey = request.getParameter( PARENT_ACTIVITY_KEY_PARAMETER ) ; 
1795 			  debug("parentKey: " + parentKey ) ;                                                   
1796               String activityIndex = request.getParameter( ACTIVITY_INDEX_PARAMETER ) ;
1797               debug("activityIndex: " + activityIndex ) ;
1798 			  String activityOrder = request.getParameter( ACTIVITY_ORDER_PARAMETER ) ;
1799 			  debug("activityOrder: " + activityOrder ) ; 
1800 			  String activityType = request.getParameter( ACTIVITY_TYPE_PARAMETER ) ;
1801 			  debug("activityType: " + activityType ) ;			               
1802                             
1803               if ( activityTargetKey == null) {
1804                   debug( "activityTargetKey is null" ) ;
1805               }
1806               else if ( activityIndex == null) {
1807                   debug( "activityIndex is null" ) ;
1808               }
1809                             
1810               try { 
1811                   index = new Integer( activityIndex ).intValue() ;
1812               }
1813               catch ( NumberFormatException nfx ) {
1814                   index = -1;
1815               }
1816               
1817 			  if (activityOrder.equalsIgnoreCase("HERE"))
1818 			  {
1819 			      activityContainer = locateActivityContainer( workflow, activityTargetKey );
1820 			      index = 0;
1821 			  }
1822 			  else 
1823 			  {
1824 					activityContainer = locateActivityContainer( workflow, parentKey );
1825 			  }
1826 			  	              
1827 			  
1828 			  highWaterMark = activityContainer.getActivityCount() - 1;
1829 			  
1830 			  if( highWaterMark < 0 ) {
1831 			      highWaterMark = 0;
1832 			  }			  
1833                                 
1834 			  if( index < 0 || index > highWaterMark ){
1835 			      index = highWaterMark;
1836 			  }
1837 			  
1838 			  if (activityOrder.equalsIgnoreCase("AFTER"))
1839 			  {                            
1840 			      index ++;
1841 			  }
1842          	                                                            
1843               activityContainer.addActivity( index, activity ) ;
1844 			  request.setAttribute( INSERTED_ACTIVITY_KEY_PARAMETER, workflow.getXPathFor( activity ) ) ;
1845 			  debug("request inserted_activity_key: " + request.getAttribute(INSERTED_ACTIVITY_KEY_PARAMETER));              
1846                         
1847             }
1848             finally {
1849                   if( TRACE_ENABLED ) trace( "DesignActionImpl.insertActivity() exit" ) ;
1850             }
1851                     
1852         } // end of insertActivity()                     
1853 
1854 
1855         private void insertActivity() throws ConsistencyException {
1856 		    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertActivity() entry" ) ;           
1857               
1858 			try {
1859 			    String insertActivityType = request.getParameter( INSERT_ACTIVITY_TYPE_PARAMETER ) ;
1860 				debug("insertActivityType: " + insertActivityType ) ;
1861 				if (insertActivityType == null){
1862 					debug( "insertActivityType is null" ) ;
1863 				}
1864 				else if (insertActivityType.equals("ELSE")){this.insertElse();}
1865 				else if (insertActivityType.equals("FLOW")){this.insertFlow();}
1866 				else if (insertActivityType.equals("FORLOOP")){this.insertFor();}
1867 				else if (insertActivityType.equals("PARLOOP")){this.insertParfor();}
1868 				else if (insertActivityType.equals("IF")){this.insertIf();}
1869 				else if (insertActivityType.equals("SCOPE")){this.insertScope();}											
1870 				else if (insertActivityType.equals("SCRIPT")){this.insertScript();}
1871 				else if (insertActivityType.equals("SEQUENCE")){this.insertSequence();}
1872 				else if (insertActivityType.equals("SET")){this.insertSet();}
1873 				else if (insertActivityType.equals("STEP")){this.insertStep();}
1874 				else if (insertActivityType.equals("THEN")){this.insertThen();}
1875 				else if (insertActivityType.equals("UNSET")){this.insertUnset();}
1876 				else if (insertActivityType.equals("WHILELOOP")){this.insertWhile();}
1877 				else if (insertActivityType.equals("PASTE")){this.pasteActivity();}
1878 				else if (insertActivityType.equals("COPY"))
1879 				    {
1880 				    	session.setAttribute( COPY_ACTIVITY_KEY, request.getParameter( ACTIVITY_KEY_PARAMETER ) );
1881 				    }								
1882 				else {
1883 					debug("Unknown activity: " + insertActivityType);
1884 				}					
1885 			}                                   			
1886 					
1887 			finally {
1888 			    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertActivity() exit" ) ;
1889 			}                    
1890 		} // end of insertActivity() 
1891 
1892 
1893         private void removeActivity() throws ConsistencyException {
1894            if( TRACE_ENABLED ) trace( "DesignActionImpl.removeActivity() entry" ) ;
1895             
1896            ActivityContainer activityContainer = null;
1897            AbstractActivity activity = null;
1898 		   int index = 0;
1899               
1900            try {
1901 
1902               String activityTargetKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1903               debug("activityTargetKey: " + activityTargetKey ) ;
1904 			  String parentKey = request.getParameter( PARENT_ACTIVITY_KEY_PARAMETER ) ; 
1905 			  debug("parentKey: " + parentKey ) ;
1906 			  String activityIndex = request.getParameter( ACTIVITY_INDEX_PARAMETER ) ; 
1907 		      debug("activityIndex: " + activityIndex ) ;
1908 			  index = new Integer( activityIndex ).intValue() ;                                   
1909                             
1910               if ( activityTargetKey == null) {
1911                   debug( "activityTargetKey is null" ) ;
1912               }
1913 			  else if ( parentKey == null) {
1914 			      debug( "parentKey is null" ) ;
1915 			  }
1916   			  else if( activityTargetKey.equals( "/sequence" ) && index == 0) {						
1917 			      ; // ignore the top sequence
1918 			  }
1919 			                
1920               else {
1921 					activity = (AbstractActivity)workflow.findXPathValue( activityTargetKey );
1922 					activityContainer = locateActivityContainer( workflow, parentKey );
1923 				if( activityContainer != null ) {
1924 						activityContainer.removeActivity( activity ) ;
1925 				}					
1926               	
1927               }
1928 
1929                                     
1930            }
1931            catch (Exception e) {
1932 					e.printStackTrace() ;
1933 					}
1934 					
1935            finally {
1936               if( TRACE_ENABLED ) trace( "DesignActionImpl.removeActivity() exit" ) ;
1937            }
1938                     
1939         } // end of removeActivity()       
1940 
1941 	    private void insertStepDetails() throws ConsistencyException {
1942 		    if( TRACE_ENABLED ) trace( "DesignActionImpl.insertStepDetails() entry" ) ;
1943 			
1944 			Step step = null ;
1945 			Tool tool = null;
1946             
1947 			try {
1948 		        String xpathKey = request.getParameter( ACTIVITY_KEY_PARAMETER ) ;
1949 				String toolName = request.getParameter( TOOL_NAME_PARAMETER ) ;
1950 				String stepName = request.getParameter( STEP_NAME_PARAMETER ) ;
1951 				String stepVar = request.getParameter( STEP_VAR_PARAMETER ) ;				
1952 				String stepDescription = request.getParameter( STEP_DESCRIPTION_PARAMETER ) ;
1953                                 
1954 				if ( xpathKey == null) {
1955 					debug( "xpathKey is null" ) ;
1956 				}
1957 
1958 				step = locateStep( workflow, xpathKey ); 
1959 				step.setName( stepName );				
1960 				if (!(stepVar.length() <= 0 || stepVar == null)) // the resultVar must contain a valid string, it cannot be empty so do not set if no value present.
1961 				  step.setResultVar( stepVar ) ;
1962 				step.setDescription( stepDescription );
1963 				
1964 				tool = this.createTool( toolName ) ;
1965 				step.setTool( tool );
1966 				updateUserToolList( toolName );				
1967 				
1968 			}
1969 			finally {
1970 				if( TRACE_ENABLED ) trace( "DesignActionImpl.insertStepDescription() exit" ) ;
1971 			}                   
1972 		} // end of insertStepDetails()
1973 
1974         
1975         private void checkPermissions ( String someResource, String anAction ) 
1976                                  throws CommunityServiceException, 
1977                                         CommunityPolicyException, 
1978                                         CommunityIdentifierException {
1979             if( TRACE_ENABLED ) trace( "DesignActionImpl.checkPermission() entry" ) ;
1980                        
1981             PolicyServiceDelegate ps = null;
1982             PolicyCredentials pCredentials = null;
1983             PolicyPermission pPermission = null;
1984             this.credentials = new Credentials();
1985 
1986             try {               
1987                 ps = getPolicyDelegate () ;
1988                 pCredentials = new PolicyCredentials();
1989                 pCredentials.setAccount( this.credentials.getAccount().getName() );
1990                 pCredentials.setGroup( this.credentials.getGroup().getName() );
1991                 pPermission = ps.checkPermissions( pCredentials, someResource, anAction ) ;             
1992                
1993             }
1994             catch( MalformedURLException muex ) {
1995                 muex.printStackTrace();
1996             }
1997             finally {
1998                 if( TRACE_ENABLED ) trace( "DesignActionImpl.checkPermission() exit" ) ;  
1999             }
2000                         
2001         } // end of checkPermission()
2002         
2003                
2004         /***
2005          * Get the policy delegate.  
2006          * Looks for the property org.astrogrid.portal.community.url
2007          * if this property is not found (or is set to "dummy")
2008          * then a mock delegate is returned.
2009          *   
2010          * @return either a genuine or mock delegate
2011          * @throws MalformedURLException if the url is malformed
2012          */
2013         private PolicyServiceDelegate getPolicyDelegate() throws MalformedURLException {
2014             final Config config = SimpleConfig.getSingleton();
2015             final String endpoint = config.getString(ORG_ASTROGRID_PORTAL_COMMUNITY_URL, "dummy");
2016             if ("dummy".equals(endpoint)) {
2017                 debug("Using dummy delegate");
2018                 return new PolicyServiceMockDelegate();
2019             } else {
2020                 debug("Using delegate at "+endpoint);
2021                 return new PolicyServiceSoapDelegate(endpoint);
2022             }
2023         }
2024 
2025 		private Set locateSet( Workflow workflow, String xpathKey ) throws ConsistencyException {
2026         
2027 			Object obj = workflow.findXPathValue( xpathKey );
2028                     
2029 			if( obj instanceof Set ) {
2030 				return (Set)obj ;
2031 			}
2032 			else {
2033 				throw new ConsistencyException() ;
2034 			}   
2035 		}
2036         
2037         private Step locateStep( Workflow workflow, String xpathKey ) throws ConsistencyException {
2038         
2039             Object obj = workflow.findXPathValue( xpathKey );
2040                     
2041             if( obj instanceof Step ) {
2042                 return (Step)obj ;
2043             }
2044             else {
2045                 throw new ConsistencyException() ;
2046             }   
2047         }
2048         
2049 		private Unset locateUnset( Workflow workflow, String xpathKey ) throws ConsistencyException {
2050         
2051 			Object obj = workflow.findXPathValue( xpathKey );
2052                     
2053 			if( obj instanceof Unset ) {
2054 				return (Unset)obj ;
2055 			}
2056 			else {
2057 				throw new ConsistencyException() ;
2058 			}   
2059 		}        
2060         
2061 		private While locateWhile( Workflow workflow, String xpathKey ) throws ConsistencyException {
2062         
2063 			Object obj = workflow.findXPathValue( xpathKey );
2064                     
2065 			if( obj instanceof While ) {
2066 					return (While)obj ;
2067 			}
2068 			else {
2069 				throw new ConsistencyException() ;
2070 			}   
2071 		}        
2072         
2073 		private Script locateScript( Workflow workflow, String xpathKey ) throws ConsistencyException {
2074         
2075 			Object obj = workflow.findXPathValue( xpathKey );
2076                     
2077 			if( obj instanceof Script ) {
2078 			    return (Script)obj ;
2079 			}
2080 		    else {
2081 			    throw new ConsistencyException() ;
2082 			}   
2083 		}        
2084 
2085 		private If locateIf( Workflow workflow, String xpathKey ) throws ConsistencyException {
2086         
2087 			Object obj = workflow.findXPathValue( xpathKey );
2088                     
2089 			if( obj instanceof If ) {
2090 				return (If)obj ;
2091 			}
2092 			else {
2093 				throw new ConsistencyException() ;
2094 			}   
2095 		}
2096 
2097 		private For locateFor( Workflow workflow, String xpathKey ) throws ConsistencyException {
2098         
2099 			Object obj = workflow.findXPathValue( xpathKey );
2100                     
2101 			if( obj instanceof For ) {
2102 					return (For)obj ;
2103 			}
2104 			else {
2105 				throw new ConsistencyException() ;
2106 			}   
2107 		}
2108 		
2109 		private Parfor locateParfor( Workflow workflow, String xpathKey ) throws ConsistencyException {
2110         
2111 			Object obj = workflow.findXPathValue( xpathKey );
2112                     
2113 			if( obj instanceof Parfor ) {
2114 					return (Parfor)obj ;
2115 			}
2116 			else {
2117 				throw new ConsistencyException() ;
2118 			}   
2119 		}		
2120         
2121         private ActivityContainer locateActivityContainer( Workflow workflow, String xpathKey ) throws ConsistencyException {
2122             
2123             Object obj = workflow.findXPathValue( xpathKey );
2124             ActivityContainer activityContainer = null;
2125                 
2126             if( obj instanceof Flow || obj instanceof Sequence ) {
2127                activityContainer = (ActivityContainer)obj ;
2128             }
2129             else {
2130                throw new ConsistencyException() ;
2131             } 
2132                               
2133             return activityContainer;  
2134             
2135         } // end of locateActivityContainer()
2136         
2137         
2138 		private AbstractActivity locateAbstractActivity( Workflow workflow, String xpathKey ) throws ConsistencyException {
2139             
2140 			Object obj = workflow.findXPathValue( xpathKey );
2141 		    AbstractActivity abstractActivity = null;
2142                 
2143 			if( obj instanceof Flow || obj instanceof Sequence ) {
2144 				 // do something
2145 			}
2146 			else if( obj instanceof Step || obj instanceof Script ) {
2147 			    abstractActivity = (AbstractActivity)obj ;
2148 			    // success
2149 			}
2150 			else {
2151 				 throw new ConsistencyException() ;
2152 			} 
2153                               
2154 			return abstractActivity;  
2155             
2156 		} // end of locateActivity()        
2157         
2158         
2159         private ApplicationDescription locateDescription( String toolName ) throws WorkflowInterfaceException {
2160            if( TRACE_ENABLED ) trace( "DesignActionImpl.locateDescription(toolName) entry" ) ;
2161            
2162            ApplicationDescription description = null;
2163            String toolCacheKey = TOOLS_CACHE + toolName;
2164            String WorkflowMessage = null ;
2165            
2166            try {
2167                
2168               description = (ApplicationDescription)session.getAttribute( toolCacheKey );
2169               
2170               if( description == null ){
2171                   ApplicationRegistry applRegistry = workflowManager.getToolRegistry();
2172                   description = applRegistry.getDescriptionFor( toolName );
2173                   session.setAttribute( toolCacheKey, description );
2174               }
2175                                                         
2176            }
2177            catch(WorkflowInterfaceException wiex) {
2178 				WorkflowMessage =  "Unable to locate tool named: "+toolName ;
2179            }
2180            finally {
2181 			  this.session.setAttribute( WORKFLOW_SUBMIT_MESSAGE, WorkflowMessage ) ;
2182               if( TRACE_ENABLED ) trace( "DesignActionImpl.locateDescription(toolName) exit" ) ;
2183            }        
2184            
2185            return description;
2186                              
2187         } // end of locateDescription(toolName)
2188            
2189            
2190                           
2191     } // end of inner class DesignActionImpl
2192     
2193     
2194     private class ConsistencyException extends Exception {
2195     }
2196     
2197     
2198     private void trace( String traceString ) {
2199         // logger.debug( traceString ) ;
2200         System.out.println( traceString ) ;
2201     }
2202     
2203     private void debug( String logString ){
2204         // logger.debug( logString ) ;
2205         System.out.println( logString ) ;
2206     }
2207           
2208 } // end of class DesignAction