View Javadoc

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