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.WsdlInterfaceFactory;
28  import com.eviware.soapui.impl.wsdl.WsdlInterface;
29  import com.eviware.soapui.impl.wsdl.WsdlProject;
30  import com.eviware.soapui.impl.wsdl.actions.iface.GenerateMockServiceAction;
31  import com.eviware.soapui.impl.wsdl.actions.iface.GenerateWsdlTestSuiteAction;
32  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
33  import com.eviware.soapui.support.MessageSupport;
34  import com.eviware.soapui.support.SoapUIException;
35  import com.eviware.soapui.support.UISupport;
36  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
37  import com.eviware.x.form.XFormDialog;
38  import com.eviware.x.form.XFormField;
39  import com.eviware.x.form.XFormFieldListener;
40  import com.eviware.x.form.support.ADialogBuilder;
41  import com.eviware.x.form.support.AField;
42  import com.eviware.x.form.support.AField.AFieldType;
43  import com.eviware.x.form.support.AForm;
44  
45  import java.io.File;
46  
47  /***
48   * Action for creating a new WSDL project
49   *
50   * @author Ole.Matzura
51   */
52  
53  public class AddWsdlAction extends AbstractSoapUIAction<WsdlProject>
54  {
55     public static final String SOAPUI_ACTION_ID = "NewWsdlProjectAction";
56     private XFormDialog dialog;
57  
58     public static final MessageSupport messages = MessageSupport.getMessages( AddWsdlAction.class );
59  
60     public AddWsdlAction()
61     {
62        super( messages.get( "Title" ), messages.get( "Description" ) );
63     }
64  
65     public void perform( WsdlProject project, Object param )
66     {
67        if( dialog == null )
68        {
69           dialog = ADialogBuilder.buildDialog( Form.class );
70           dialog.setValue( Form.CREATEREQUEST, Boolean.toString( true ) );
71           dialog.getFormField( Form.INITIALWSDL ).addFormFieldListener( new XFormFieldListener()
72           {
73              public void valueChanged( XFormField sourceField, String newValue, String oldValue )
74              {
75                 String value = newValue.toLowerCase().trim();
76  
77                 dialog.getFormField( Form.CREATEREQUEST ).setEnabled( value.length() > 0 );
78                 dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled( newValue.trim().length() > 0  );
79                 dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( newValue.trim().length() > 0  );
80              }
81           } );
82        }
83        else
84        {
85           dialog.setValue( Form.INITIALWSDL, "" );
86  
87           dialog.getFormField( Form.CREATEREQUEST ).setEnabled( false );
88           dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled( false );
89           dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( false );
90        }
91  
92        while( dialog.show() )
93        {
94           try
95           {
96              String url = dialog.getValue( Form.INITIALWSDL ).trim();
97              if( url.length() > 0 )
98              {
99                 if( new File( url ).exists() )
100                   url = new File( url ).toURI().toURL().toString();
101 
102                importWsdl( project, url );
103                break;
104             }
105          }
106          catch( Exception ex )
107          {
108             UISupport.showErrorMessage( ex );
109          }
110       }
111    }
112 
113    private void importWsdl( WsdlProject project, String url )
114            throws SoapUIException
115    {
116       WsdlInterface[] results = WsdlInterfaceFactory.importWsdl( project, url, dialog.getValue( Form.CREATEREQUEST ).equals( "true" ) );
117       for( WsdlInterface iface : results )
118       {
119          UISupport.select( iface );
120 
121          if( dialog.getValue( Form.GENERATETESTSUITE ).equals( "true" ) )
122          {
123             GenerateWsdlTestSuiteAction generateTestSuiteAction = new GenerateWsdlTestSuiteAction();
124             generateTestSuiteAction.generateTestSuite( iface, true );
125          }
126 
127          if( dialog.getValue( Form.GENERATEMOCKSERVICE ).equals( "true" ) )
128          {
129             GenerateMockServiceAction generateMockAction = new GenerateMockServiceAction();
130             generateMockAction.generateMockService( iface, false );
131          }
132       }
133    }
134 
135    @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWPROJECT_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
136    public interface Form
137    {
138       @AField( description = "Form.InitialWsdl.Description", type = AFieldType.FILE )
139       public final static String INITIALWSDL = messages.get( "Form.InitialWsdl.Label" );
140 
141       @AField( description = "Form.CreateRequests.Description", type = AFieldType.BOOLEAN, enabled = false )
142       public final static String CREATEREQUEST = messages.get( "Form.CreateRequests.Label" );
143 
144       @AField( description = "Form.GenerateTestSuite.Description", type = AFieldType.BOOLEAN, enabled = false )
145       public final static String GENERATETESTSUITE = messages.get( "Form.GenerateTestSuite.Label" );
146 
147       @AField( description = "Form.GenerateMockService.Description", type = AFieldType.BOOLEAN, enabled = false )
148       public final static String GENERATEMOCKSERVICE = messages.get( "Form.GenerateMockService.Label" );
149    }
150 }