View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.support.dnd.handlers;
14  
15  import java.util.HashMap;
16  import java.util.HashSet;
17  import java.util.Map;
18  import java.util.Set;
19  
20  import com.eviware.soapui.impl.support.AbstractInterface;
21  import com.eviware.soapui.impl.wsdl.WsdlProject;
22  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
24  import com.eviware.soapui.model.iface.Interface;
25  import com.eviware.soapui.support.UISupport;
26  
27  public class DragAndDropSupport
28  {
29  
30  	public static boolean copyTestStep( WsdlTestStep source, WsdlTestCase target, int defaultPosition )
31  	{
32  		String name = UISupport.prompt( "Enter name for copied TestStep", "Copy TestStep",
33  				target == source.getTestCase() ? "Copy of " + source.getName() : source.getName() );
34  		if( name == null )
35  			return false;
36  
37  		WsdlProject sourceProject = source.getTestCase().getTestSuite().getProject();
38  		WsdlProject targetProject = target.getTestSuite().getProject();
39  
40  		if( sourceProject != targetProject )
41  		{
42  			if( !importRequiredInterfaces( targetProject, new HashSet<Interface>( source.getRequiredInterfaces() ),
43  					"Copy Test Step" ) )
44  				return false;
45  		}
46  
47  		target.importTestStep( source, name, defaultPosition, true );
48  
49  		return true;
50  	}
51  
52  	public static boolean importRequiredInterfaces( WsdlProject project, Set<Interface> requiredInterfaces, String title )
53  	{
54  		if( requiredInterfaces.size() > 0 && project.getInterfaceCount() > 0 )
55  		{
56  			Map<String, Interface> bindings = new HashMap<String, Interface>();
57  			for( Interface iface : requiredInterfaces )
58  			{
59  				bindings.put( iface.getTechnicalId(), iface );
60  			}
61  
62  			for( Interface iface : project.getInterfaceList() )
63  			{
64  				bindings.remove( iface.getTechnicalId() );
65  			}
66  
67  			requiredInterfaces.retainAll( bindings.values() );
68  		}
69  
70  		if( requiredInterfaces.size() > 0 )
71  		{
72  			String msg = "Target project [" + project.getName() + "] is missing required Interfaces;\r\n\r\n";
73  			for( Interface iface : requiredInterfaces )
74  			{
75  				msg += iface.getName() + " [" + iface.getTechnicalId() + "]\r\n";
76  			}
77  			msg += "\r\nThese will be cloned to the target project as well";
78  
79  			if( !UISupport.confirm( msg, title ) )
80  				return false;
81  
82  			for( Interface iface : requiredInterfaces )
83  			{
84  				project.importInterface( ( AbstractInterface<?> )iface, true, true );
85  			}
86  		}
87  
88  		return true;
89  	}
90  
91  	public static boolean moveTestStep( WsdlTestStep source, WsdlTestCase target, int defaultPosition )
92  	{
93  		if( source.getTestCase() == target )
94  		{
95  			int ix = target.getIndexOfTestStep( source );
96  
97  			if( defaultPosition == -1 )
98  			{
99  				target.moveTestStep( ix, target.getTestStepCount() - ix );
100 			}
101 			else if( ix >= 0 && defaultPosition != ix )
102 			{
103 				int offset = defaultPosition - ix;
104 				if( offset > 0 )
105 					offset-- ;
106 				target.moveTestStep( ix, offset );
107 			}
108 		}
109 		else
110 		{
111 			String name = UISupport.prompt( "Enter name for moved TestStep", "Move TestStep", source.getName() );
112 			if( name == null )
113 				return false;
114 
115 			WsdlProject sourceProject = source.getTestCase().getTestSuite().getProject();
116 			WsdlProject targetProject = target.getTestSuite().getProject();
117 
118 			if( sourceProject != targetProject )
119 			{
120 				if( !importRequiredInterfaces( targetProject, new HashSet<Interface>( source.getRequiredInterfaces() ),
121 						"Move Test Step" ) )
122 					return false;
123 			}
124 
125 			target.importTestStep( source, name, defaultPosition, false );
126 			source.getTestCase().removeTestStep( source );
127 		}
128 
129 		return true;
130 	}
131 
132 }