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.model.support;
14  
15  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
16  import com.eviware.soapui.impl.wsdl.WsdlProject;
17  import com.eviware.soapui.model.ModelItem;
18  import com.eviware.soapui.model.iface.Interface;
19  import com.eviware.soapui.model.project.Project;
20  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
21  import com.eviware.soapui.support.UISupport;
22  import com.eviware.soapui.support.types.StringList;
23  
24  import java.util.List;
25  import java.util.UUID;
26  
27  /***
28   * Utility methods for soapui model-related interfaces
29   *  
30   * @author Ole.Matzura
31   */
32  
33  public class ModelSupport
34  {
35  	public static <T extends ModelItem> String[] getNames(List<T> list  )
36  	{
37  		String [] names = new String[list.size()];
38  		for( int c = 0; c < names.length; c++ )
39  		{
40  			names[c] = list.get( c ).getName();
41  		}
42  		
43  		return names;
44  	}
45  
46  	public static <T extends ModelItem> String[] getNames(List<T> list, ModelItemFilter<T> filter )
47  	{
48  		String [] names = new String[list.size()];
49  		for( int c = 0; c < names.length; c++ )
50  		{
51  			if( filter == null || filter.accept(list.get( c )))
52  				names[c] = list.get( c ).getName();
53  		}
54  		
55  		return names;
56  	}
57  	
58  	public static String[] getNames( String [] firstItems, List<? extends ModelItem> list)
59  	{
60  		String [] names = new String[list.size()+firstItems.length];
61  		for( int c = 0; c < firstItems.length; c++ )
62  		{
63  			names[c] = firstItems[c];
64  		}
65  		
66  		for( int c = 0; c < list.size(); c++ )
67  		{
68  			names[c+firstItems.length] = list.get( c ).getName();
69  		}
70  		
71  		return names;
72  	}
73  
74  	public static String[] getNames( List<? extends ModelItem> list, String [] lastItems)
75  	{
76  		String [] names = new String[list.size()+lastItems.length];
77  		for( int c = 0; c < lastItems.length; c++ )
78  		{
79  			names[c+list.size()] = lastItems[c];
80  		}
81  		
82  		for( int c = 0; c < list.size(); c++ )
83  		{
84  			names[c] = list.get( c ).getName();
85  		}
86  		
87  		return names;
88  	}
89  	
90  	public static String generateModelItemID()
91  	{
92  		return UUID.randomUUID().toString();
93  	}
94  	
95  	@SuppressWarnings("unchecked")
96  	public static <T extends ModelItem> T findModelItemById( String id, ModelItem root )
97  	{
98        if( root == null || id == null )
99           return null;
100 
101 		for( ModelItem child : root.getChildren() )
102 		{
103 			if( child.getId().equals( id ))
104 				return ( T ) child;
105 			
106 			ModelItem result = findModelItemById( id, child );
107 			if( result != null )
108 				return ( T ) result;
109 		}
110 		
111 		return null;
112 	}
113 
114 	public static String promptForUniqueName( String typeName, ModelItem parent, String def )
115 	{
116 		String name = UISupport.prompt( "Specify name for new " + typeName, "New " + typeName, def );
117 		StringList names = new StringList();
118 		for( ModelItem item : parent.getChildren() )
119 			names.add( item.getName() );
120 	
121 		while( name != null && names.contains( name ) )
122 		{
123 			name = UISupport.prompt( "Specify unique name for new " + typeName, "New " + typeName, def );
124 		}
125 	
126 		return name;
127 	}
128 
129 	public static Project getModelItemProject( ModelItem modelItem )
130 	{
131 		if( modelItem == null )
132 			return null;
133 		
134 		while( !(modelItem instanceof Project) && modelItem != null )
135 		{
136 			modelItem = modelItem.getParent();
137 		}
138 		
139 		return ( Project ) modelItem;
140 	}
141 
142 	public static String getResourceRoot(AbstractWsdlModelItem<?> testStep)
143 	{
144 		WsdlProject project = (WsdlProject) getModelItemProject(testStep);
145 		if( project == null )
146 			return null;
147 		
148 		return PropertyExpansionUtils.expandProperties(project, project.getResourceRoot());
149 	}
150 	
151 	public interface ModelItemFilter<T extends ModelItem>
152 	{
153 		public boolean accept( T modelItem );
154 	}
155 	
156 	public static class InterfaceTypeFilter implements ModelItemFilter<Interface>
157 	{
158 		private String type;
159 
160 		public InterfaceTypeFilter(String type)
161 		{
162 			this.type = type;
163 		}
164 		
165 		public boolean accept(Interface modelItem)
166 		{
167 			return modelItem.getInterfaceType().equals(type);
168 		}
169 		
170 	}
171 }