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.wsdl.actions.mockservice;
14  
15  import java.util.HashMap;
16  import java.util.HashSet;
17  import java.util.Map;
18  import java.util.Set;
19  
20  import javax.xml.namespace.QName;
21  
22  import com.eviware.soapui.impl.WorkspaceImpl;
23  import com.eviware.soapui.impl.wsdl.WsdlInterface;
24  import com.eviware.soapui.impl.wsdl.WsdlOperation;
25  import com.eviware.soapui.impl.wsdl.WsdlProject;
26  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
27  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
28  import com.eviware.soapui.model.iface.Interface;
29  import com.eviware.soapui.model.support.ModelSupport;
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.support.ADialogBuilder;
35  import com.eviware.x.form.support.AField;
36  import com.eviware.x.form.support.AForm;
37  import com.eviware.x.form.support.AField.AFieldType;
38  
39  /***
40   * Clones a WsdlMockService
41   * 
42   * @author Ole.Matzura
43   */
44  
45  public class CloneMockServiceAction extends AbstractSoapUIAction<WsdlMockService>
46  {
47  	public final static String SOAPUI_ACTION_ID = "CloneMockServiceAction";
48  	private XFormDialog dialog;
49  
50  	public CloneMockServiceAction() 
51     {
52        super( "Clone MockService", "Clones this MockService" );
53     }
54  	
55     public void perform( WsdlMockService mockService, Object param )
56  	{
57     	if( dialog == null )
58  		   dialog = ADialogBuilder.buildDialog( Form.class );
59     	
60     	dialog.setValue( Form.NAME, "Copy of " + mockService.getName() );
61     	WorkspaceImpl workspace = mockService.getProject().getWorkspace();
62  		dialog.setOptions( Form.PROJECT, 
63  					ModelSupport.getNames( workspace.getOpenProjectList(), new String[] {"<Create New>"} ) );
64  		
65  		dialog.setValue( Form.PROJECT, mockService.getProject().getName() );
66  		
67  		if( dialog.show() )
68  		{
69  			String targetProjectName = dialog.getValue( Form.PROJECT );
70  			String name = dialog.getValue( Form.NAME );
71  			
72  			WsdlProject project = (WsdlProject) mockService.getProject();
73  			WsdlMockService clonedService = null;
74  			
75  			// within same project?
76  			if( targetProjectName.equals( mockService.getProject().getName() ))
77  			{
78  				clonedService = cloneMockServiceWithinProject( mockService, name, project );
79  			}
80  			else
81  			{
82  				clonedService = cloneToAnotherProject( mockService, targetProjectName, name );
83  			}
84  			
85  			if( clonedService != null )
86  			{
87  				UISupport.select( clonedService );
88  			}
89  			
90  			if( dialog.getBooleanValue( Form.MOVE ))
91  			{
92  			   project.removeMockService( mockService );
93  			}
94  		}
95     }
96  
97  	public WsdlMockService cloneToAnotherProject( WsdlMockService mockService, String targetProjectName, String name )
98  	{
99  		WorkspaceImpl workspace = mockService.getProject().getWorkspace();
100 		WsdlProject targetProject = ( WsdlProject ) workspace.getProjectByName( targetProjectName );
101 		if( targetProject == null )
102 		{
103 			targetProjectName = UISupport.prompt( "Enter name for new Project", "Clone MockService", "" );
104 			if( targetProjectName == null )
105 				return null;
106 			
107 			try
108 			{
109 				targetProject = workspace.createProject( targetProjectName );
110 			}
111 			catch( SoapUIException e )
112 			{
113 				UISupport.showErrorMessage( e );
114 			}
115 			
116 			if( targetProject == null )
117 				return null;
118 		}
119 		
120 		Set<WsdlInterface> requiredInterfaces = getRequiredInterfaces( mockService, targetProject );
121 		
122 		if( requiredInterfaces.size() > 0 )
123 		{
124 			String msg = "Target project [" + targetProjectName  +"] is missing required interfaces;\r\n\r\n";
125 			for( WsdlInterface iface : requiredInterfaces )
126 			{
127 				msg += iface.getName() + " [" + iface.getBindingName() + "]\r\n";
128 			}
129 			msg += "\r\nThese will be cloned to the targetProject as well";
130 			
131 			if( !UISupport.confirm( msg, "Clone MockService" ))
132 				return null;
133 			
134 			for( WsdlInterface iface : requiredInterfaces )
135 			{
136 				targetProject.importInterface( iface, false, true );
137 			}
138 		}
139 		
140 		mockService = targetProject.importMockService( mockService, name, true );
141 		UISupport.select( mockService);
142 		return mockService;
143 	}
144 
145 	public WsdlMockService cloneMockServiceWithinProject( WsdlMockService mockService, String name, WsdlProject project )
146 	{
147 		WsdlMockService newMockService = project.importMockService( mockService, name, true );
148 		UISupport.select( newMockService );
149 		return newMockService;
150 	}
151 
152 	private Set<WsdlInterface> getRequiredInterfaces( WsdlMockService mockService, WsdlProject targetProject )
153 	{
154 		Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
155 		
156 		for( int i = 0; i < mockService.getMockOperationCount(); i++ )
157 		{
158 			WsdlOperation operation = mockService.getMockOperationAt( i ).getOperation();
159 			if( operation != null )
160 				requiredInterfaces.add( operation.getInterface() );
161 		}
162 		
163 		if( requiredInterfaces.size() > 0 && targetProject.getInterfaceCount() > 0 )
164 		{
165 			Map<QName,WsdlInterface> bindings = new HashMap<QName,WsdlInterface>();
166 			for( WsdlInterface iface : requiredInterfaces )
167 			{
168 				bindings.put( iface.getBindingName(), iface );
169 			}
170 			
171 			for( Interface iface : targetProject.getInterfaceList() )
172 			{
173 				bindings.remove( iface.getTechnicalId());
174 			}
175 
176 			requiredInterfaces.retainAll( bindings.values() );
177 		}
178 		
179 		return requiredInterfaces;
180 	}
181    
182    @AForm(description = "Specify target Project and name of cloned MockService", name = "Clone MockService",
183    			helpUrl=HelpUrls.CLONEMOCKSERVICE_HELP_URL, icon=UISupport.TOOL_ICON_PATH)
184 	public interface Form
185 	{
186    	@AField( name="MockService Name", description = "The name of the cloned MockService", type=AFieldType.STRING )
187 		public final static String NAME = "MockService Name";
188    	
189    	@AField( name="Target Project", description = "The target Project for the cloned MockService", type=AFieldType.ENUMERATION )
190 		public final static String PROJECT = "Target Project";
191    	
192    	@AField( name="Move instead", description = "Moves the selected MockService instead of copying", type=AFieldType.BOOLEAN )
193 		public final static String MOVE = "Move instead";
194 	}
195 }