001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.actions.mockservice;
014:
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import javax.xml.namespace.QName;
021:
022: import com.eviware.soapui.impl.WorkspaceImpl;
023: import com.eviware.soapui.impl.wsdl.WsdlInterface;
024: import com.eviware.soapui.impl.wsdl.WsdlProject;
025: import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
026: import com.eviware.soapui.model.iface.Interface;
027: import com.eviware.soapui.model.support.ModelSupport;
028: import com.eviware.soapui.support.SoapUIException;
029: import com.eviware.soapui.support.UISupport;
030: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
031: import com.eviware.x.form.XFormDialog;
032: import com.eviware.x.form.support.ADialogBuilder;
033: import com.eviware.x.form.support.AField;
034: import com.eviware.x.form.support.AForm;
035: import com.eviware.x.form.support.AField.AFieldType;
036:
037: /**
038: * Clones a WsdlMockService
039: *
040: * @author Ole.Matzura
041: */
042:
043: public class CloneMockServiceAction extends
044: AbstractSoapUIAction<WsdlMockService> {
045: public final static String SOAPUI_ACTION_ID = "CloneMockServiceAction";
046: private XFormDialog dialog;
047:
048: public CloneMockServiceAction() {
049: super ("Clone MockService", "Clones this MockService");
050: }
051:
052: public void perform(WsdlMockService mockService, Object param) {
053: if (dialog == null)
054: dialog = ADialogBuilder.buildDialog(Form.class);
055:
056: dialog.setValue(Form.NAME, "Copy of " + mockService.getName());
057: WorkspaceImpl workspace = mockService.getProject()
058: .getWorkspace();
059: dialog.setOptions(Form.PROJECT, ModelSupport.getNames(workspace
060: .getProjectList(), new String[] { "<Create New>" }));
061:
062: dialog.setValue(Form.PROJECT, mockService.getProject()
063: .getName());
064:
065: if (dialog.show()) {
066: String targetProjectName = dialog.getValue(Form.PROJECT);
067: String name = dialog.getValue(Form.NAME);
068:
069: WsdlProject project = (WsdlProject) mockService
070: .getProject();
071: WsdlMockService clonedService = null;
072:
073: // within same project?
074: if (targetProjectName.equals(mockService.getProject()
075: .getName())) {
076: clonedService = cloneMockServiceWithinProject(
077: mockService, name, project);
078: } else {
079: clonedService = cloneToAnotherProject(mockService,
080: targetProjectName, name);
081: }
082:
083: if (clonedService != null) {
084: UISupport.select(clonedService);
085: }
086:
087: if (dialog.getBooleanValue(Form.MOVE)) {
088: project.removeMockService(mockService);
089: }
090: }
091: }
092:
093: public WsdlMockService cloneToAnotherProject(
094: WsdlMockService mockService, String targetProjectName,
095: String name) {
096: WorkspaceImpl workspace = mockService.getProject()
097: .getWorkspace();
098: WsdlProject targetProject = (WsdlProject) workspace
099: .getProjectByName(targetProjectName);
100: if (targetProject == null) {
101: targetProjectName = UISupport.prompt(
102: "Enter name for new Project", "Clone MockService",
103: "");
104: if (targetProjectName == null)
105: return null;
106:
107: try {
108: targetProject = workspace
109: .createProject(targetProjectName);
110: } catch (SoapUIException e) {
111: UISupport.showErrorMessage(e);
112: }
113:
114: if (targetProject == null)
115: return null;
116: }
117:
118: Set<WsdlInterface> requiredInterfaces = getRequiredInterfaces(
119: mockService, targetProject);
120:
121: if (requiredInterfaces.size() > 0) {
122: String msg = "Target project [" + targetProjectName
123: + "] is missing required interfaces;\r\n\r\n";
124: for (WsdlInterface iface : requiredInterfaces) {
125: msg += iface.getName() + " [" + iface.getBindingName()
126: + "]\r\n";
127: }
128: msg += "\r\nThese will be cloned to the targetProject as well";
129:
130: if (!UISupport.confirm(msg, "Clone MockService"))
131: return null;
132:
133: for (WsdlInterface iface : requiredInterfaces) {
134: targetProject.importInterface(iface);
135: }
136: }
137:
138: mockService = targetProject
139: .importMockService(mockService, name);
140: UISupport.select(mockService);
141: return mockService;
142: }
143:
144: public WsdlMockService cloneMockServiceWithinProject(
145: WsdlMockService mockService, String name,
146: WsdlProject project) {
147: WsdlMockService newMockService = project.cloneMockService(
148: mockService, name);
149: UISupport.select(newMockService);
150: return newMockService;
151: }
152:
153: private Set<WsdlInterface> getRequiredInterfaces(
154: WsdlMockService mockService, WsdlProject targetProject) {
155: Set<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
156:
157: for (int i = 0; i < mockService.getMockOperationCount(); i++) {
158: requiredInterfaces.add(mockService.getMockOperationAt(i)
159: .getOperation().getInterface());
160: }
161:
162: if (requiredInterfaces.size() > 0
163: && targetProject.getInterfaceCount() > 0) {
164: Map<QName, WsdlInterface> bindings = new HashMap<QName, WsdlInterface>();
165: for (WsdlInterface iface : requiredInterfaces) {
166: bindings.put(iface.getBindingName(), iface);
167: }
168:
169: for (Interface iface : targetProject.getInterfaces()) {
170: bindings.remove(iface.getBindingName());
171: }
172:
173: requiredInterfaces.retainAll(bindings.values());
174: }
175:
176: return requiredInterfaces;
177: }
178:
179: @AForm(description="Specify target Project and name of cloned MockService",name="Clone MockService")
180: public interface Form {
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: }
|