View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2010 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  /*
14   *  soapUI, copyright (C) 2004-2010 eviware.com 
15   *
16   *  soapUI is free software; you can redistribute it and/or modify it under the 
17   *  terms of version 2.1 of the GNU Lesser General Public License as published by 
18   *  the Free Software Foundation.
19   *
20   *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
21   *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
22   *  See the GNU Lesser General Public License for more details at gnu.org.
23   */
24  
25  package com.eviware.soapui.impl.wsdl.actions.project;
26  
27  import java.io.File;
28  
29  import com.eviware.soapui.impl.WsdlInterfaceFactory;
30  import com.eviware.soapui.impl.wsdl.WsdlInterface;
31  import com.eviware.soapui.impl.wsdl.WsdlProject;
32  import com.eviware.soapui.impl.wsdl.actions.iface.GenerateMockServiceAction;
33  import com.eviware.soapui.impl.wsdl.actions.iface.GenerateWsdlTestSuiteAction;
34  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
35  import com.eviware.soapui.impl.wsdl.support.PathUtils;
36  import com.eviware.soapui.support.MessageSupport;
37  import com.eviware.soapui.support.SoapUIException;
38  import com.eviware.soapui.support.StringUtils;
39  import com.eviware.soapui.support.UISupport;
40  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
41  import com.eviware.x.form.XFormDialog;
42  import com.eviware.x.form.XFormField;
43  import com.eviware.x.form.XFormFieldListener;
44  import com.eviware.x.form.support.ADialogBuilder;
45  import com.eviware.x.form.support.AField;
46  import com.eviware.x.form.support.AForm;
47  import com.eviware.x.form.support.AField.AFieldType;
48  
49  /***
50   * Action for creating a new WSDL project
51   * 
52   * @author Ole.Matzura
53   */
54  
55  public class AddWsdlAction extends AbstractSoapUIAction<WsdlProject>
56  {
57  	public static final String SOAPUI_ACTION_ID = "NewWsdlProjectAction";
58  	private XFormDialog dialog;
59  
60  	public static final MessageSupport messages = MessageSupport.getMessages( AddWsdlAction.class );
61  
62  	public AddWsdlAction()
63  	{
64  		super( messages.get( "Title" ), messages.get( "Description" ) );
65  	}
66  
67  	public void perform( WsdlProject project, Object param )
68  	{
69  		if( dialog == null )
70  		{
71  			dialog = ADialogBuilder.buildDialog( Form.class );
72  			dialog.setValue( Form.CREATEREQUEST, Boolean.toString( true ) );
73  			dialog.getFormField( Form.INITIALWSDL ).addFormFieldListener( new XFormFieldListener()
74  			{
75  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
76  				{
77  					String value = newValue.toLowerCase().trim();
78  
79  					dialog.getFormField( Form.CREATEREQUEST ).setEnabled( value.length() > 0 );
80  					dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled( newValue.trim().length() > 0 );
81  					dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( newValue.trim().length() > 0 );
82  				}
83  			} );
84  		}
85  		else
86  		{
87  			dialog.setValue( Form.INITIALWSDL, "" );
88  
89  			dialog.getFormField( Form.CREATEREQUEST ).setEnabled( false );
90  			dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled( false );
91  			dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( false );
92  		}
93  
94  		while( dialog.show() )
95  		{
96  			try
97  			{
98  				String url = dialog.getValue( Form.INITIALWSDL ).trim();
99  				if( StringUtils.hasContent( url ) )
100 				{
101 					String expUrl = PathUtils.expandPath( url, project );
102 					if( new File( expUrl ).exists() )
103 						url = new File( expUrl ).toURI().toURL().toString();
104 
105 					WsdlInterface[] results = importWsdl( project, expUrl );
106 
107 					if( !url.equals( expUrl ) )
108 					{
109 						for( WsdlInterface iface : results )
110 				{
111 							iface.setDefinition( url, false );
112 						}
113 					}
114 
115 					break;
116 				}
117 			}
118 			catch( Exception ex )
119 			{
120 				UISupport.showErrorMessage( ex );
121 			}
122 		}
123 	}
124 
125 	private WsdlInterface[] importWsdl( WsdlProject project, String url ) throws SoapUIException
126 	{
127 		WsdlInterface[] results = WsdlInterfaceFactory.importWsdl( project, url, dialog.getValue( Form.CREATEREQUEST )
128 				.equals( "true" ) );
129 		for( WsdlInterface iface : results )
130 		{
131 			UISupport.select( iface );
132 
133 			if( dialog.getValue( Form.GENERATETESTSUITE ).equals( "true" ) )
134 			{
135 				GenerateWsdlTestSuiteAction generateTestSuiteAction = new GenerateWsdlTestSuiteAction();
136 				generateTestSuiteAction.generateTestSuite( iface, true );
137 			}
138 
139 			if( dialog.getValue( Form.GENERATEMOCKSERVICE ).equals( "true" ) )
140 			{
141 				GenerateMockServiceAction generateMockAction = new GenerateMockServiceAction();
142 				generateMockAction.generateMockService( iface, false );
143 			}
144 		}
145 
146 		return results;
147 	}
148 
149 	@AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWPROJECT_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
150 	public interface Form
151 	{
152 		@AField( description = "Form.InitialWsdl.Description", type = AFieldType.FILE )
153 		public final static String INITIALWSDL = messages.get( "Form.InitialWsdl.Label" );
154 
155 		@AField( description = "Form.CreateRequests.Description", type = AFieldType.BOOLEAN, enabled = false )
156 		public final static String CREATEREQUEST = messages.get( "Form.CreateRequests.Label" );
157 
158 		@AField( description = "Form.GenerateTestSuite.Description", type = AFieldType.BOOLEAN, enabled = false )
159 		public final static String GENERATETESTSUITE = messages.get( "Form.GenerateTestSuite.Label" );
160 
161 		@AField( description = "Form.GenerateMockService.Description", type = AFieldType.BOOLEAN, enabled = false )
162 		public final static String GENERATEMOCKSERVICE = messages.get( "Form.GenerateMockService.Label" );
163 	}
164 }