View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.iface;
14  
15  import java.io.File;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.WsdlInterface;
21  import com.eviware.soapui.impl.wsdl.WsdlOperation;
22  import com.eviware.soapui.impl.wsdl.WsdlRequest;
23  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
26  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
27  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
28  import com.eviware.soapui.model.ModelItem;
29  import com.eviware.soapui.model.iface.Request;
30  import com.eviware.soapui.model.testsuite.TestCase;
31  import com.eviware.soapui.model.testsuite.TestSuite;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
34  import com.eviware.soapui.support.components.ModelItemListDesktopPanel;
35  import com.eviware.soapui.support.xml.XmlUtils;
36  import com.eviware.x.form.XFormDialog;
37  import com.eviware.x.form.XFormField;
38  import com.eviware.x.form.XFormFieldListener;
39  import com.eviware.x.form.support.ADialogBuilder;
40  import com.eviware.x.form.support.AField;
41  import com.eviware.x.form.support.AForm;
42  import com.eviware.x.form.support.AField.AFieldType;
43  
44  /***
45   * Updates the definition of a WsdlInterface.
46   * 
47   * @author Ole.Matzura
48   */
49  
50  public class UpdateInterfaceAction extends AbstractSoapUIAction<WsdlInterface>
51  {
52     public static final String SOAPUI_ACTION_ID = "UpdateInterfaceAction";
53     private XFormDialog dialog = null;
54  
55  	public UpdateInterfaceAction()
56     {
57        this("Update Definition", "Reloads the definition for this interface and its operations");
58     }
59  
60  	protected UpdateInterfaceAction(String name, String description)
61     {
62        super(name, description);
63     }
64  	
65     public void perform( WsdlInterface iface, Object param )
66  	{
67     	if( RemoveInterfaceAction.hasRunningDependingTests( iface ))
68     	{
69     		UISupport.showErrorMessage( "Cannot update Interface due to running depending tests" );
70     		return;
71     	}
72     	
73     	if( dialog == null )
74     	{
75     		dialog = ADialogBuilder.buildDialog( Form.class );
76     		dialog.setBooleanValue( Form.CREATE_REQUESTS, true );
77     		dialog.getFormField( Form.CREATE_BACKUPS ).setEnabled( false );
78     		dialog.getFormField( Form.RECREATE_OPTIONAL ).setEnabled( false );
79     		dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( false );
80     		dialog.getFormField( Form.RECREATE_REQUESTS ).addFormFieldListener( new XFormFieldListener() {
81  
82  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
83  				{
84  					boolean enabled = dialog.getBooleanValue( Form.RECREATE_REQUESTS );
85  					
86  					dialog.getFormField( Form.CREATE_BACKUPS ).setEnabled( enabled );
87  					dialog.getFormField( Form.RECREATE_OPTIONAL ).setEnabled( enabled );
88  					dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( enabled );
89  				}} );
90     	}
91     	
92     	dialog.setValue( Form.DEFINITION_URL, iface.getDefinition() );
93     	if( !dialog.show() )
94     		return;
95     	
96        String url = dialog.getValue( Form.DEFINITION_URL );
97        if (url == null || url.trim().length() == 0 )
98           return;
99  
100       try
101 		{
102 			File file = new File( url );
103 			if( file.exists() ) 
104 				url = file.toURI().toURL().toString();
105 		}
106 		catch( Exception e )
107 		{
108 			SoapUI.logError( e );
109 		}
110       	
111       boolean createRequests = dialog.getBooleanValue( Form.CREATE_REQUESTS );
112       
113       try
114 		{
115       	if( iface.updateDefinition( url, createRequests ))
116       		afterUpdate(iface);
117       	else
118       		UISupport.showInfoMessage(	"Update of interface failed",  "Update Definition" );
119 		}
120 		catch (Exception e1)
121 		{
122 			UISupport.showInfoMessage(	"Failed to update interface: [" + e1 + "]", "Update Definition" );
123 			SoapUI.logError( e1 );
124 		}
125    }
126 
127    protected void afterUpdate(WsdlInterface iface) throws Exception
128    {
129       if( !dialog.getBooleanValue( Form.RECREATE_REQUESTS ))
130       {
131       	boolean buildOptional = dialog.getBooleanValue( Form.RECREATE_OPTIONAL );
132       	boolean createBackups = dialog.getBooleanValue( Form.CREATE_BACKUPS );
133       	boolean keepExisting = dialog.getBooleanValue( Form.KEEP_EXISTING );
134       	
135       	List<ModelItem> updated = new ArrayList<ModelItem>();
136       	
137       	updated.addAll( recreateRequests( iface, buildOptional, createBackups, keepExisting));
138       	
139       	if( dialog.getBooleanValue( Form.UPDATE_TESTREQUESTS ))
140       		updated.addAll( recreateTestRequests( iface, buildOptional, createBackups, keepExisting ));
141       	
142       	UISupport.showInfoMessage(	"Update of interface successfull, [" + updated.size() + "] Requests/TestRequests have" +
143       			" been updated.",  "Update Definition" );
144       	
145       	if( dialog.getBooleanValue( Form.OPEN_LIST ))
146       	{
147       		UISupport.showDesktopPanel( new ModelItemListDesktopPanel( "Updated Requests/TestRequests", 
148       					"The following Request/TestRequests where updated", updated.toArray( new ModelItem[updated.size()] )));
149       	}
150       }
151       else
152       {
153       	UISupport.showInfoMessage(	"Update of interface successful",  "Update Definition" );
154       }
155    }
156 
157 	protected List<Request> recreateRequests( WsdlInterface iface, boolean buildOptional, boolean createBackups, boolean keepExisting )
158 	{
159 		int count = 0;
160 		
161 		List<Request> result = new ArrayList<Request>();
162 		
163 		// first check operations
164 		for( int c = 0; c < iface.getOperationCount(); c++ )
165 		{
166 			WsdlOperation operation = iface.getOperationAt( c );
167 			String newRequest = operation.createRequest( buildOptional );
168 			List<Request> requests = operation.getRequests();
169 			
170 			for( Request request : requests )
171 			{
172 				String requestContent = request.getRequestContent();
173 				String req = XmlUtils.transferValues( requestContent, newRequest );
174 				
175 				// changed?
176 				if( !req.equals( requestContent ) )
177 				{
178 					if( !XmlUtils.prettyPrintXml( req ).equals( XmlUtils.prettyPrintXml( requestContent )))
179 					{
180 						if( createBackups )
181 						{
182 							WsdlRequest backupRequest = operation.addNewRequest( 
183 										"Backup of [" + request.getName() + "]" );
184 							((WsdlRequest)request).copyTo( backupRequest, false, false );
185 						}
186 						
187 						((WsdlRequest)request).setRequestContent( req );
188 						count++;
189 						
190 						result.add( request );
191 					}
192 				}
193 			}
194 		}
195 		
196 		return result;
197 	}
198 
199 	private List<WsdlTestRequestStep> recreateTestRequests( WsdlInterface iface, boolean buildOptional, boolean createBackups, boolean keepExisting )
200 	{
201 		int count = 0;
202 		
203 		List<WsdlTestRequestStep> result = new ArrayList<WsdlTestRequestStep>();
204 		
205 		// now check testsuites..
206 		for( TestSuite testSuite : iface.getProject().getTestSuiteList() )
207 		{
208 			for( TestCase testCase : testSuite.getTestCaseList() )
209 			{
210 				int testStepCount = testCase.getTestStepCount();
211 				for( int c = 0; c < testStepCount; c++ )
212 				{
213 					WsdlTestStep testStep = ( WsdlTestStep ) testCase.getTestStepAt( c );
214 					if( testStep instanceof WsdlTestRequestStep )
215 					{
216 						WsdlTestRequest testRequest = ((WsdlTestRequestStep)testStep).getTestRequest();
217 						if( testRequest.getOperation().getInterface() == iface )
218 						{
219 							String newRequest = testRequest.getOperation().createRequest( buildOptional );
220 							
221 							if( keepExisting )
222 								newRequest = XmlUtils.transferValues( testRequest.getRequestContent(), newRequest );
223 							
224 							// changed?
225 							if( !newRequest.equals( testRequest.getRequestContent() ) )
226 							{
227 								if( createBackups )
228 								{
229 									((WsdlTestCase)testCase).importTestStep( testStep,
230 												"Backup of [" + testStep.getName() + "]", -1, true ).setDisabled( true );
231 								}
232 								
233 								((WsdlRequest)testRequest).setRequestContent( newRequest );
234 								count++;
235 								
236 								result.add( ( WsdlTestRequestStep ) testStep );
237 							}
238 						}
239 					}
240 				}
241 			}
242 		}
243 		
244 		return result;
245 	} 
246    
247    @AForm(description = "Specify Update Definition options", name = "Update Definition", 
248    			helpUrl=HelpUrls.UPDATE_INTERFACE_HELP_URL,  icon=UISupport.TOOL_ICON_PATH  )
249 	protected interface Form
250 	{
251    	@AField( name="Definition URL", description = "The URL or file for the updated definition", type=AFieldType.FILE )
252 		public final static String DEFINITION_URL = "Definition URL";
253    	
254    	@AField( name="Create New Requests", description = "Create default requests for new methods", type=AFieldType.BOOLEAN )
255 		public final static String CREATE_REQUESTS = "Create New Requests";
256    	
257    	@AField( name="Recreate Requests", description = "Recreate existing request with the new schema", type=AFieldType.BOOLEAN )
258 		public final static String RECREATE_REQUESTS = "Recreate Requests";
259    	
260    	@AField( name="Recreate Optional", description = "Recreate optional content when updating requests", type=AFieldType.BOOLEAN )
261 		public final static String RECREATE_OPTIONAL = "Recreate Optional";
262    	
263    	@AField( name="Keep Existing", description = "Keeps existing values when recreating requests", type=AFieldType.BOOLEAN )
264 		public final static String KEEP_EXISTING = "Keep Existing";
265    	
266    	@AField( name="Create Backups", description = "Create backup copies of changed requests", type=AFieldType.BOOLEAN )
267 		public final static String CREATE_BACKUPS = "Create Backups";
268    	
269    	@AField( name="Update TestRequests", description = "Updates all TestRequests for operations in this Interface also", type=AFieldType.BOOLEAN )
270 		public final static String UPDATE_TESTREQUESTS = "Update TestRequests";
271    	
272    	@AField( name="Open Request List", description = "Opens a list of all requests that have been updated", type=AFieldType.BOOLEAN )
273 		public final static String OPEN_LIST = "Open Request List";
274 	}
275 }