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