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  /*
14   *  soapUI, copyright (C) 2004-2008 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 com.eviware.soapui.impl.rest.RestService;
28  import com.eviware.soapui.impl.rest.RestServiceFactory;
29  import com.eviware.soapui.impl.rest.support.WadlImporter;
30  import com.eviware.soapui.impl.wsdl.WsdlProject;
31  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
32  import com.eviware.soapui.support.MessageSupport;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
35  import com.eviware.x.form.XFormDialog;
36  import com.eviware.x.form.XFormField;
37  import com.eviware.x.form.XFormFieldListener;
38  import com.eviware.x.form.support.ADialogBuilder;
39  import com.eviware.x.form.support.AField;
40  import com.eviware.x.form.support.AField.AFieldType;
41  import com.eviware.x.form.support.AForm;
42  
43  import java.io.File;
44  
45  /***
46   * Action for creating a new WSDL project
47   *
48   * @author Ole.Matzura
49   */
50  
51  public class AddWadlAction extends AbstractSoapUIAction<WsdlProject>
52  {
53     public static final String SOAPUI_ACTION_ID = "NewWsdlProjectAction";
54     private XFormDialog dialog;
55  
56     public static final MessageSupport messages = MessageSupport.getMessages( AddWadlAction.class );
57  
58     public AddWadlAction()
59     {
60        super( messages.get( "Title" ), messages.get( "Description" ) );
61     }
62  
63     public void perform( WsdlProject project, Object param )
64     {
65        if( dialog == null )
66        {
67           dialog = ADialogBuilder.buildDialog( Form.class );
68           dialog.getFormField( Form.INITIALWSDL ).addFormFieldListener( new XFormFieldListener()
69           {
70              public void valueChanged( XFormField sourceField, String newValue, String oldValue )
71              {
72                 String value = newValue.toLowerCase().trim();
73  
74                 dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( newValue.trim().length() > 0 );
75              }
76           } );
77        }
78        else
79        {
80           dialog.setValue( Form.INITIALWSDL, "" );
81           dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( false );
82        }
83  
84        while( dialog.show() )
85        {
86           try
87           {
88              String url = dialog.getValue( Form.INITIALWSDL ).trim();
89              if( url.length() > 0 )
90              {
91                 if( new File( url ).exists() )
92                    url = new File( url ).toURI().toURL().toString();
93  
94                 importWadl( project, url );
95                 break;
96              }
97           }
98           catch( Exception ex )
99           {
100             UISupport.showErrorMessage( ex );
101          }
102       }
103    }
104 
105    private void importWadl( WsdlProject project, String url )
106    {
107       RestService restService = (RestService) project.addNewInterface( project.getName(), RestServiceFactory.REST_TYPE );
108       UISupport.select( restService );
109       try
110       {
111          new WadlImporter( restService ).initFromWadl( url );
112       }
113       catch( Exception e )
114       {
115          UISupport.showErrorMessage( e );
116       }
117    }
118 
119    @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWPROJECT_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
120    public interface Form
121    {
122       @AField( description = "Form.InitialWadl.Description", type = AFieldType.FILE )
123       public final static String INITIALWSDL = messages.get( "Form.InitialWadl.Label" );
124 
125       @AField( description = "Form.GenerateTestSuite.Description", type = AFieldType.BOOLEAN, enabled = false )
126       public final static String GENERATETESTSUITE = messages.get( "Form.GenerateTestSuite.Label" );
127    }
128 }