View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.wsdl;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.util.ArrayList;
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import javax.swing.ImageIcon;
23  import javax.swing.JFileChooser;
24  import javax.swing.JOptionPane;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.xmlbeans.XmlException;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.config.InterfaceConfig;
32  import com.eviware.soapui.config.ProjectConfig;
33  import com.eviware.soapui.config.SoapuiProjectDocumentConfig;
34  import com.eviware.soapui.config.TestSuiteConfig;
35  import com.eviware.soapui.impl.wsdl.actions.project.AddInterfaceActionFromFile;
36  import com.eviware.soapui.impl.wsdl.actions.project.AddInterfaceActionFromURL;
37  import com.eviware.soapui.impl.wsdl.actions.project.AddNewTestSuiteAction;
38  import com.eviware.soapui.impl.wsdl.actions.project.RemoveProjectAction;
39  import com.eviware.soapui.impl.wsdl.actions.project.RenameProjectAction;
40  import com.eviware.soapui.impl.wsdl.support.WsdlImporter;
41  import com.eviware.soapui.model.iface.Interface;
42  import com.eviware.soapui.model.project.Project;
43  import com.eviware.soapui.model.project.ProjectListener;
44  import com.eviware.soapui.model.testsuite.TestSuite;
45  import com.eviware.soapui.model.tree.SoapUITreeNode;
46  import com.eviware.soapui.model.tree.nodes.ProjectTreeNode;
47  
48  /***
49   * WSDL project implementation
50   * 
51   * @author Ole.Matzura
52   */
53  
54  public class WsdlProject extends AbstractWsdlModelItem implements Project
55  {
56     private ProjectConfig projectConfig;
57     private String path;
58     private List<WsdlInterface> interfaces = new ArrayList<WsdlInterface>();
59     private List<WsdlTestSuite> testSuites = new ArrayList<WsdlTestSuite>();
60     private Set<ProjectListener> listeners = new HashSet<ProjectListener>();
61     private SoapuiProjectDocumentConfig projectDocument;
62     
63     private final static Log log = LogFactory.getLog( WsdlProject.class );
64     private ImageIcon projectIcon;
65  
66     public WsdlProject(String path) throws XmlException, IOException
67     {
68        this.path = path;
69        File file = new File(path);
70        if( file.exists())
71        {
72           projectDocument = SoapuiProjectDocumentConfig.Factory.parse( file );
73           projectConfig = projectDocument.getSoapuiProject();
74           log.info( "Loaded project from [" + path + "]" );
75        }
76        else 
77        {
78           projectDocument = SoapuiProjectDocumentConfig.Factory.newInstance();
79           projectConfig = projectDocument.addNewSoapuiProject();
80        }
81  
82        InterfaceConfig [] interfaceConfigs = projectConfig.getInterfaceArray();
83        for (int i = 0; i < interfaceConfigs.length; i++)
84        {
85           interfaces.add( new WsdlInterface( this, interfaceConfigs[i] ));
86        }
87  
88        TestSuiteConfig [] testSuiteConfigs = projectConfig.getTestSuiteArray();
89        for (int i = 0; i < testSuiteConfigs.length; i++)
90        {
91           testSuites.add( new WsdlTestSuite( this, testSuiteConfigs[i] ));
92        }
93        
94        addAction( new AddInterfaceActionFromURL( this ) );
95        addAction( new AddInterfaceActionFromFile( this ) );
96        addAction( new AddNewTestSuiteAction( this ) );
97        addAction( new RenameProjectAction( this ) );
98        addAction( new RemoveProjectAction( this ) );
99        
100       projectIcon = SoapUI.createImageIcon("/project.gif");
101    }
102 
103    public Interface getInterfaceAt(int index)
104    {
105       return interfaces.get( index );
106    }
107 
108    public int getInterfaceCount()
109    {
110       return interfaces.size();
111    }
112 
113    public String getName()
114    {
115       return projectConfig.getName();
116    }
117    
118    public String getPath()
119 	{
120 		return path;
121 	}
122 
123 	public String save() throws IOException 
124    {
125       if( path == null )
126       {
127          JFileChooser chooser = new JFileChooser();
128          chooser.setDialogTitle( "Save project " + getName() );
129          if( chooser.showSaveDialog( SoapUI.getInstance().getFrame() ) != JFileChooser.APPROVE_OPTION ) return null;
130          
131          path = chooser.getSelectedFile().getAbsolutePath();
132       }
133       
134       File projectFile = new File( path );
135 		projectDocument.save( projectFile);
136 		log.info( "Saved project to [" + projectFile.getAbsolutePath() + "]" );
137 		return path;
138    }
139 
140    public void setName(String name)
141    {
142       String old = getName();
143       projectConfig.setName( name );
144       notifyPropertyChanged(NAME_PROPERTY, old, name);
145    }
146    
147    public Interface [] importWsdl(String url )
148    {
149       try
150       {
151          WsdlInterface[] result = WsdlImporter.getInstance().importWsdl( this, url );
152          
153          boolean createRequests = JOptionPane.showConfirmDialog( SoapUI.getInstance().getFrame(), 
154          		"Create default requests for all operations", "Import WSDL", JOptionPane.YES_NO_OPTION ) == JOptionPane.YES_OPTION;
155          
156 			if( createRequests  )
157          {
158          	for (WsdlInterface iface : result)
159 				{
160          		for( int c = 0; c < iface.getOperationCount(); c++ )
161          		{
162          			WsdlOperation operation = (WsdlOperation) iface.getOperationAt( c );
163          			WsdlRequest request = operation.addNewRequest( "Request 1");
164                   try
165                   {
166                      request.setRequest( operation.createRequest( true ));
167                   }
168                   catch (Exception e)
169                   {
170                      e.printStackTrace();
171                   }
172          		}
173 				}
174          }
175          
176 			return result;
177       }
178       catch (Exception e)
179       {
180          log.error( "Error importing wsdl: " + e.getMessage() );
181          e.printStackTrace();
182       }
183       return null;
184    }
185    
186    public ProjectConfig getProjectConfig()
187    {
188       return projectConfig;
189    }
190 
191    public WsdlInterface addNewInterface( String name )
192    {
193       WsdlInterface iface = new WsdlInterface( this, projectConfig.addNewInterface());
194       iface.setName( name );
195       interfaces.add( iface );
196       notifyInterfaceAdded( iface );
197 
198       return iface;
199    }
200    
201    public void addProjectListener(ProjectListener listener)
202    {
203       listeners.add( listener );
204    }
205    
206    public void removeProjectListener(ProjectListener listener)
207    {
208       listeners.remove( listener );
209    }
210    
211    public void notifyInterfaceAdded( WsdlInterface iface )
212    {
213       ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
214       
215       for (int c = 0; c < a.length; c++ )
216       {
217          a[c].interfaceAdded( iface );
218       }
219    }
220    
221    public void notifyInterfaceRemoved( WsdlInterface iface )
222    {
223       ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
224       
225       for (int c = 0; c < a.length; c++ )
226       {
227          a[c].interfaceRemoved( iface );
228       }
229    }
230    
231    
232    public void notifyTestSuiteAdded( WsdlTestSuite testSuite )
233    {
234       ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
235       
236       for (int c = 0; c < a.length; c++ )
237       {
238          a[c].testSuiteAdded( testSuite );
239       }
240    }
241    
242    public void notifyTestSuiteRemoved( WsdlTestSuite testSuite )
243    {
244       ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
245       
246       for (int c = 0; c < a.length; c++ )
247       {
248          a[c].testSuiteRemoved( testSuite );
249       }
250    } 
251    
252    
253    public void removeInterface(WsdlInterface iface )
254    {
255       int ix = interfaces.indexOf( iface );
256       notifyInterfaceRemoved( iface );
257       interfaces.remove( ix );
258       projectConfig.removeInterface( ix );
259    }
260    
261    public void removeTestSuite(WsdlTestSuite testSuite )
262    {
263       int ix = testSuites.indexOf( testSuite );
264       notifyTestSuiteRemoved( testSuite );
265       testSuites.remove( ix );
266       projectConfig.removeTestSuite( ix );
267    }
268 
269    public ImageIcon getIcon()
270    {
271       return projectIcon;
272    }
273 
274    public int getTestSuiteCount()
275    {
276       return testSuites.size();
277    }
278 
279    public TestSuite getTestSuiteAt(int index)
280    {
281       return testSuites.get( index );
282    }
283 
284    public WsdlTestSuite addNewTestSuite(String name)
285    {
286       WsdlTestSuite testSuite = new WsdlTestSuite( this, projectConfig.addNewTestSuite());
287       testSuite.setName( name );
288       testSuites.add( testSuite );
289       notifyTestSuiteAdded( testSuite );
290 
291       return testSuite;
292    }
293 
294    protected SoapUITreeNode createTreeNode()
295    {
296       return new ProjectTreeNode( this );
297    }
298 }