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