View Javadoc

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