View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.teststeps.actions;
14  
15  import com.eviware.soapui.impl.WsdlInterfaceFactory;
16  import com.eviware.soapui.impl.wsdl.WsdlInterface;
17  import com.eviware.soapui.impl.wsdl.WsdlOperation;
18  import com.eviware.soapui.impl.wsdl.WsdlProject;
19  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
20  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
21  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
22  import com.eviware.soapui.model.support.ModelSupport;
23  import com.eviware.soapui.support.UISupport;
24  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
25  import com.eviware.soapui.support.xml.XmlUtils;
26  import com.eviware.x.form.XFormDialog;
27  import com.eviware.x.form.XFormField;
28  import com.eviware.x.form.XFormFieldListener;
29  import com.eviware.x.form.support.ADialogBuilder;
30  import com.eviware.x.form.support.AField;
31  import com.eviware.x.form.support.AForm;
32  import com.eviware.x.form.support.AField.AFieldType;
33  
34  /***
35   * Prompts to change the WsdlOperation of a WsdlTestRequestStep
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class ChangeOperationAction extends AbstractSoapUIAction<WsdlTestRequestStep>
41  {
42  	private XFormDialog dialog;
43  	private WsdlTestRequestStep testStep;
44  
45  	public ChangeOperationAction()
46  	{
47  		super( "Change Operation", "Changes the Interface Operation for this Test Request" );
48  	}
49  
50  	public void perform( WsdlTestRequestStep target, Object param )
51  	{
52  		this.testStep = target;
53  		
54  		if( dialog == null )
55     	{
56  		   dialog = ADialogBuilder.buildDialog( Form.class );
57  		   dialog.getFormField( Form.INTERFACE ).addFormFieldListener( new XFormFieldListener() {
58  
59  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
60  				{
61  					WsdlProject project = testStep.getTestCase().getTestSuite().getProject();
62  					dialog.setOptions( Form.OPERATION, 
63  								ModelSupport.getNames( project.getInterfaceByName( newValue ).getOperationList() ));
64  					dialog.setValue( Form.OPERATION, testStep.getOperationName() );
65  				}} );
66  		   
67  		   dialog.getFormField( Form.RECREATE_REQUEST ).addFormFieldListener( new XFormFieldListener() {
68  
69  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
70  				{
71  					boolean enabled = Boolean.parseBoolean( newValue );
72  					
73  					dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( enabled );
74  					dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( enabled );
75  				}} );
76  		   
77  		   dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( false );
78  			dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( false );
79     	}
80  		
81  		WsdlProject project = target.getTestCase().getTestSuite().getProject();
82  		dialog.setOptions( Form.INTERFACE, ModelSupport.getNames( project.getInterfaceList(),
83  				new ModelSupport.InterfaceTypeFilter( WsdlInterfaceFactory.WSDL_TYPE )));
84  		dialog.setValue( Form.INTERFACE, target.getInterfaceName() );
85  		
86  		dialog.setOptions( Form.OPERATION, 
87  					ModelSupport.getNames( project.getInterfaceByName( target.getInterfaceName() ).getOperationList() ));
88  		dialog.setValue( Form.OPERATION, target.getOperationName() );
89  		dialog.setValue( Form.NAME, target.getName() );
90  		
91  		if( dialog.show() )
92  		{
93  			String ifaceName = dialog.getValue( Form.INTERFACE );
94  			String operationName = dialog.getValue( Form.OPERATION );
95  			
96  			WsdlInterface iface = (WsdlInterface) project.getInterfaceByName( ifaceName );
97  			WsdlOperation operation = iface.getOperationByName( operationName );
98  			target.setOperation( operation );
99  			
100 			String name = dialog.getValue( Form.NAME ).trim();
101 			if( name.length() > 0 && !target.getName().equals( name ) )
102 				target.setName( name );
103 			
104 			if( dialog.getBooleanValue( Form.RECREATE_REQUEST ))
105 			{
106 				String req = operation.createRequest( dialog.getBooleanValue( Form.CREATE_OPTIONAL ) );
107 		      if( req == null )
108 		      {
109 		      	UISupport.showErrorMessage( "Request creation failed" );
110 		      	return;
111 		      }
112 		      
113 		      WsdlTestRequest request = target.getTestRequest();
114 	         if( dialog.getBooleanValue( Form.KEEP_EXISTING ))
115 	         {
116 	        		req = XmlUtils.transferValues( request.getRequestContent(), req );
117 	         }         	
118 		      
119 		      request.setRequestContent( req );
120 			}
121 		}
122 	}
123 
124 	@AForm( description = "Specify Interface/Operation for TestRequest", name = "Change Operation",
125 				helpUrl=HelpUrls.CHANGEOPERATION_HELP_URL, icon=UISupport.TOOL_ICON_PATH)
126 	protected interface Form
127 	{
128 		@AField( name = "Name", description = "The Name of the TestRequests", type = AFieldType.STRING )
129 		public final static String NAME = "Name";
130 		
131 		@AField( name = "Interface", description = "The TestRequests' Interface", type = AFieldType.ENUMERATION )
132 		public final static String INTERFACE = "Interface";
133 
134 		@AField( name = "Operation", description = "The TestRequests' Operation", type = AFieldType.ENUMERATION )
135 		public final static String OPERATION = "Operation";
136 		
137 		@AField( name = "Recreate Request", description = "Recreates the request content from the new Operations Definition", type = AFieldType.BOOLEAN )
138 		public final static String RECREATE_REQUEST = "Recreate Request";
139 		
140 		@AField( name = "Create Optional", description = "Creates optional content when recreating the request", type = AFieldType.BOOLEAN )
141 		public final static String CREATE_OPTIONAL = "Create Optional";
142 		
143 		@AField( name = "Keep Existing", description = "Tries to keep existing values when recreating the request", type = AFieldType.BOOLEAN )
144 		public final static String KEEP_EXISTING = "Keep Existing";
145 	}
146 }