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.actions.mockoperation;
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.mock.WsdlMockOperation;
20  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
21  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
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 WsdlMockOperation
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class ChangeMockOperationAction extends AbstractSoapUIAction<WsdlMockOperation>
41  {
42  	private XFormDialog dialog;
43  	private WsdlMockOperation testStep;
44  
45  	public ChangeMockOperationAction()
46  	{
47  		super( "Change Operation", "Changes the Interface Operation for this MockOperation" );
48  	}
49  
50  	public void perform( WsdlMockOperation 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.getMockService().getProject();
62  					dialog.setOptions( Form.OPERATION, 
63  								ModelSupport.getNames( project.getInterfaceByName( newValue ).getOperationList() ));
64  					WsdlOperation operation = testStep.getOperation();
65  					dialog.setValue( Form.OPERATION, operation == null ? "" : operation.getName() );
66  				}} );
67  		   
68  		   dialog.getFormField( Form.RECREATE_REQUEST ).addFormFieldListener( new XFormFieldListener() {
69  
70  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
71  				{
72  					boolean enabled = Boolean.parseBoolean( newValue );
73  					
74  					dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( enabled );
75  					dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( enabled );
76  				}} );
77  		   
78  		   dialog.getFormField( Form.CREATE_OPTIONAL ).setEnabled( false );
79  			dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( false );
80     	}
81  		
82  		WsdlOperation operation = testStep.getOperation();
83  		WsdlProject project = testStep.getMockService().getProject();
84  		String[] interfaceNames = ModelSupport.getNames( project.getInterfaceList(), 
85  				new ModelSupport.InterfaceTypeFilter( WsdlInterfaceFactory.WSDL_TYPE ));
86  		dialog.setOptions( Form.INTERFACE, interfaceNames);
87  		dialog.setValue( Form.INTERFACE, operation == null ? interfaceNames[0] : operation.getInterface().getName() );
88  		
89  		dialog.setOptions( Form.OPERATION, 
90  					ModelSupport.getNames( project.getInterfaceByName( dialog.getValue( Form.INTERFACE ) ).getOperationList() ));
91  		dialog.setValue( Form.OPERATION, operation == null ? null : operation.getName() );
92  		dialog.setValue( Form.NAME, target.getName() );
93  		
94  		if( dialog.show() )
95  		{
96  			String ifaceName = dialog.getValue( Form.INTERFACE );
97  			String operationName = dialog.getValue( Form.OPERATION );
98  			
99  			WsdlInterface iface = (WsdlInterface) project.getInterfaceByName( ifaceName );
100 			operation = iface.getOperationByName( operationName );
101 			target.setOperation( operation );
102 			
103 			String name = dialog.getValue( Form.NAME ).trim();
104 			if( name.length() > 0 && !target.getName().equals( name ) )
105 				target.setName( name );
106 			
107 			if( dialog.getBooleanValue( Form.RECREATE_REQUEST ))
108 			{
109 				String req = operation.createResponse( dialog.getBooleanValue( Form.CREATE_OPTIONAL ) );
110 		      if( req == null )
111 		      {
112 		      	UISupport.showErrorMessage( "Response creation failed" );
113 		      }
114 		      else
115 		      {
116 					for( int c = 0; c < target.getMockResponseCount(); c++ )
117 					{
118 						String msg = req;
119 						WsdlMockResponse mockResponse = target.getMockResponseAt( c );
120 						
121 			         if( dialog.getBooleanValue( Form.KEEP_EXISTING ))
122 			         {
123 							msg = XmlUtils.transferValues( mockResponse.getResponseContent(), req );
124 			         }         	
125 				      
126 				      mockResponse.setResponseContent( msg );
127 					}
128 		      }
129 			}
130 		}
131 	}
132 
133 	@AForm( description = "Specify Interface/Operation for MockOperation", name = "Change Operation",
134 				helpUrl=HelpUrls.CHANGEMOCKOPERATION_HELP_URL, icon=UISupport.TOOL_ICON_PATH )
135 	protected interface Form
136 	{
137 		@AField( name = "Name", description = "The Name of the MockOperation", type = AFieldType.STRING )
138 		public final static String NAME = "Name";
139 		
140 		@AField( name = "Interface", description = "The MockOperations Interface", type = AFieldType.ENUMERATION )
141 		public final static String INTERFACE = "Interface";
142 
143 		@AField( name = "Operation", description = "The MockOperations Operation", type = AFieldType.ENUMERATION )
144 		public final static String OPERATION = "Operation";
145 		
146 		@AField( name = "Recreate Responses", description = "Recreates all MockResponses content from the new Operations Definition", type = AFieldType.BOOLEAN )
147 		public final static String RECREATE_REQUEST = "Recreate Responses";
148 		
149 		@AField( name = "Create Optional", description = "Creates optional content when recreating the response", type = AFieldType.BOOLEAN )
150 		public final static String CREATE_OPTIONAL = "Create Optional";
151 		
152 		@AField( name = "Keep Existing", description = "Tries to keep existing values when recreating the response", type = AFieldType.BOOLEAN )
153 		public final static String KEEP_EXISTING = "Keep Existing";
154 	}
155 }