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.actions;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.WorkspaceImpl;
17  import com.eviware.soapui.impl.WsdlInterfaceFactory;
18  import com.eviware.soapui.impl.rest.RestService;
19  import com.eviware.soapui.impl.rest.RestServiceFactory;
20  import com.eviware.soapui.impl.rest.actions.project.NewRestServiceAction;
21  import com.eviware.soapui.impl.rest.actions.service.GenerateRestTestSuiteAction;
22  import com.eviware.soapui.impl.rest.support.WadlImporter;
23  import com.eviware.soapui.impl.wsdl.WsdlInterface;
24  import com.eviware.soapui.impl.wsdl.WsdlProject;
25  import com.eviware.soapui.impl.wsdl.actions.iface.GenerateMockServiceAction;
26  import com.eviware.soapui.impl.wsdl.actions.iface.GenerateWsdlTestSuiteAction;
27  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
28  import com.eviware.soapui.support.MessageSupport;
29  import com.eviware.soapui.support.SoapUIException;
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
32  import com.eviware.x.form.XFormDialog;
33  import com.eviware.x.form.XFormField;
34  import com.eviware.x.form.XFormFieldListener;
35  import com.eviware.x.form.support.ADialogBuilder;
36  import com.eviware.x.form.support.AField;
37  import com.eviware.x.form.support.AField.AFieldType;
38  import com.eviware.x.form.support.AForm;
39  
40  import java.io.File;
41  
42  /***
43   * Action for creating a new WSDL project
44   *
45   * @author Ole.Matzura
46   */
47  
48  public class NewWsdlProjectAction extends AbstractSoapUIAction<WorkspaceImpl>
49  {
50     public static final String SOAPUI_ACTION_ID = "NewWsdlProjectAction";
51     private XFormDialog dialog;
52  
53     public static final MessageSupport messages = MessageSupport.getMessages( NewWsdlProjectAction.class );
54  
55     public NewWsdlProjectAction()
56     {
57        super( messages.get( "Title" ), messages.get( "Description" ) );
58     }
59  
60     public void perform( WorkspaceImpl workspace, Object param )
61     {
62        if( dialog == null )
63        {
64           dialog = ADialogBuilder.buildDialog( Form.class );
65           dialog.setValue( Form.CREATEREQUEST, Boolean.toString( true ) );
66           dialog.getFormField( Form.INITIALWSDL ).addFormFieldListener( new XFormFieldListener()
67           {
68              public void valueChanged( XFormField sourceField, String newValue, String oldValue )
69              {
70                 String value = newValue.toLowerCase().trim();
71  
72                 dialog.getFormField( Form.CREATEREQUEST ).setEnabled( value.length() > 0 && !newValue.endsWith( ".wadl" ));
73                 dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled( newValue.trim().length() > 0 && !newValue.endsWith( ".wadl" ));
74                 dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( newValue.trim().length() > 0 );
75                 dialog.getFormField( Form.ADDRESTSERVICE ).setEnabled( newValue.trim().length() == 0 );
76              }
77           } );
78        }
79        else
80        {
81           dialog.setValue( Form.PROJECTNAME, "" );
82           dialog.setValue( Form.INITIALWSDL, "" );
83           
84           dialog.setBooleanValue( Form.ADDRESTSERVICE, false );
85  
86           dialog.getFormField( Form.CREATEREQUEST ).setEnabled( false );
87           dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled( false );
88           dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( false );
89           dialog.getFormField( Form.ADDRESTSERVICE ).setEnabled( true );
90        }
91  
92        while( dialog.show() )
93        {
94           WsdlProject project = null;
95           try
96           {
97              String projectName = dialog.getValue( Form.PROJECTNAME ).trim();
98              if( projectName.length() == 0 )
99              {
100                UISupport.showErrorMessage( messages.get( "MissingProjectNameError" ) );
101             }
102             else
103             {
104                project = workspace.createProject( projectName, null );
105 
106                if( project != null )
107                {
108                   UISupport.select( project );
109 
110                   String url = dialog.getValue( Form.INITIALWSDL ).trim();
111                   if( url.length() > 0 )
112                   {
113                      if( new File( url ).exists() )
114                         url = new File( url ).toURI().toURL().toString();
115 
116                      if( url.toUpperCase().endsWith( "WADL" ) )
117                         importWadl( project, url );
118                      else
119                         importWsdl( project, url );
120                   }
121                   else if( dialog.getBooleanValue( Form.ADDRESTSERVICE ) )
122                   {
123                      SoapUI.getActionRegistry().getAction( NewRestServiceAction.SOAPUI_ACTION_ID ).perform( project, project );
124                   }
125 
126                   break;
127                }
128             }
129          }
130          catch( Exception ex )
131          {
132             UISupport.showErrorMessage( ex );
133             if( project != null )
134             {
135                workspace.removeProject( project );
136             }
137          }
138       }
139    }
140 
141    private void importWadl( WsdlProject project, String url )
142    {
143       RestService restService = (RestService) project.addNewInterface( project.getName(), RestServiceFactory.REST_TYPE );
144       UISupport.select( restService );
145       try
146       {
147          new WadlImporter( restService ).initFromWadl( url );
148 
149          if( dialog.getBooleanValue( Form.GENERATETESTSUITE ) )
150          {
151             GenerateRestTestSuiteAction generateTestSuiteAction = new GenerateRestTestSuiteAction();
152             generateTestSuiteAction.generateTestSuite( restService, true );
153          }
154       }
155       catch( Exception e )
156       {
157          UISupport.showErrorMessage( e );
158       }
159    }
160 
161    private void importWsdl( WsdlProject project, String url )
162            throws SoapUIException
163    {
164       WsdlInterface[] results = WsdlInterfaceFactory.importWsdl( project, url, dialog.getValue( Form.CREATEREQUEST ).equals( "true" ) );
165       for( WsdlInterface iface : results )
166       {
167          UISupport.select( iface );
168 
169          if( dialog.getBooleanValue( Form.GENERATETESTSUITE ) )
170          {
171             GenerateWsdlTestSuiteAction generateTestSuiteAction = new GenerateWsdlTestSuiteAction();
172             generateTestSuiteAction.generateTestSuite( iface, true );
173          }
174 
175          if( dialog.getBooleanValue( Form.GENERATEMOCKSERVICE ) )
176          {
177             GenerateMockServiceAction generateMockAction = new GenerateMockServiceAction();
178             generateMockAction.generateMockService( iface, false );
179          }
180       }
181    }
182 
183    @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWPROJECT_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
184    public interface Form
185    {
186       @AField( description = "Form.ProjectName.Description", type = AFieldType.STRING )
187       public final static String PROJECTNAME = messages.get( "Form.ProjectName.Label" );
188 
189       @AField( description = "Form.InitialWsdl.Description", type = AFieldType.FILE )
190       public final static String INITIALWSDL = messages.get( "Form.InitialWsdl.Label" );
191 
192       @AField( description = "Form.CreateRequests.Description", type = AFieldType.BOOLEAN, enabled = false )
193       public final static String CREATEREQUEST = messages.get( "Form.CreateRequests.Label" );
194 
195       @AField( description = "Form.GenerateTestSuite.Description", type = AFieldType.BOOLEAN, enabled = false )
196       public final static String GENERATETESTSUITE = messages.get( "Form.GenerateTestSuite.Label" );
197 
198       @AField( description = "Form.GenerateMockService.Description", type = AFieldType.BOOLEAN, enabled = false )
199       public final static String GENERATEMOCKSERVICE = messages.get( "Form.GenerateMockService.Label" );
200 
201       @AField( description = "Form.AddRestService.Description", type = AFieldType.BOOLEAN, enabled = true )
202       public final static String ADDRESTSERVICE = messages.get( "Form.AddRestService.Label" );
203 
204 //		@AField( description = "Form.CreateProjectFile.Description", type = AFieldType.BOOLEAN )
205 //		public final static String CREATEPROJECTFILE = messages.get("Form.CreateProjectFile.Label");  
206    }
207 }