View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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 java.util.ArrayList;
16  import java.util.List;
17  import java.util.UUID;
18  
19  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
20  import com.eviware.soapui.impl.wsdl.WsdlProject;
21  import com.eviware.soapui.model.ModelItem;
22  import com.eviware.soapui.model.iface.Interface;
23  import com.eviware.soapui.model.project.Project;
24  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
25  import com.eviware.soapui.support.UISupport;
26  import com.eviware.soapui.support.types.StringList;
27  
28  /***
29   * Utility methods for soapui model-related interfaces
30   * 
31   * @author Ole.Matzura
32   */
33  
34  public class ModelSupport
35  {
36  	public static <T extends ModelItem> String[] getNames( List<T> list )
37  	{
38  		String[] names = new String[list.size()];
39  		for( int c = 0; c < names.length; c++ )
40  		{
41  			names[c] = list.get( c ).getName();
42  		}
43  
44  		return names;
45  	}
46  
47  	public static <T extends ModelItem> List<T> getChildren( ModelItem modelItem, Class<T> childType )
48  	{
49  		List<T> result = new ArrayList<T>();
50  		for( ModelItem child : modelItem.getChildren() )
51  		{
52  			if( child.getClass().equals( childType ) )
53  			{
54  				result.add( ( T )child );
55  			}
56  		}
57  
58  		return result;
59  	}
60  
61  	public static <T extends ModelItem> String[] getNames( List<T> list, ModelItemFilter<T> filter )
62  	{
63  		String[] names = new String[list.size()];
64  		for( int c = 0; c < names.length; c++ )
65  		{
66  			if( filter == null || filter.accept( list.get( c ) ) )
67  				names[c] = list.get( c ).getName();
68  		}
69  
70  		return names;
71  	}
72  
73  	public static String[] getNames( String[] firstItems, List<? extends ModelItem> list )
74  	{
75  		String[] names = new String[list.size() + firstItems.length];
76  		for( int c = 0; c < firstItems.length; c++ )
77  		{
78  			names[c] = firstItems[c];
79  		}
80  
81  		for( int c = 0; c < list.size(); c++ )
82  		{
83  			names[c + firstItems.length] = list.get( c ).getName();
84  		}
85  
86  		return names;
87  	}
88  
89  	public static String[] getNames( List<? extends ModelItem> list, String[] lastItems )
90  	{
91  		String[] names = new String[list.size() + lastItems.length];
92  		for( int c = 0; c < lastItems.length; c++ )
93  		{
94  			names[c + list.size()] = lastItems[c];
95  		}
96  
97  		for( int c = 0; c < list.size(); c++ )
98  		{
99  			names[c] = list.get( c ).getName();
100 		}
101 
102 		return names;
103 	}
104 
105 	public static String generateModelItemID()
106 	{
107 		return UUID.randomUUID().toString();
108 	}
109 
110 	@SuppressWarnings( "unchecked" )
111 	public static <T extends ModelItem> T findModelItemById( String id, ModelItem root )
112 	{
113 		if( root == null || id == null )
114 			return null;
115 
116 		for( ModelItem child : root.getChildren() )
117 		{
118 			if( child.getId().equals( id ) )
119 				return ( T )child;
120 
121 			ModelItem result = findModelItemById( id, child );
122 			if( result != null )
123 				return ( T )result;
124 		}
125 
126 		return null;
127 	}
128 
129 	public static String promptForUniqueName( String typeName, ModelItem parent, String def )
130 	{
131 		String name = UISupport.prompt( "Specify name for new " + typeName, "New " + typeName, def );
132 		StringList names = new StringList();
133 		for( ModelItem item : parent.getChildren() )
134 			names.add( item.getName() );
135 
136 		while( name != null && names.contains( name ) )
137 		{
138 			name = UISupport.prompt( "Specify unique name for new " + typeName, "New " + typeName, def );
139 		}
140 
141 		return name;
142 	}
143 
144 	public static Project getModelItemProject( ModelItem modelItem )
145 	{
146 		if( modelItem == null )
147 			return null;
148 
149 		while( !( modelItem instanceof Project ) && modelItem != null )
150 		{
151 			modelItem = modelItem.getParent();
152 		}
153 
154 		return ( Project )modelItem;
155 	}
156 
157 	public static String getResourceRoot( AbstractWsdlModelItem<?> testStep )
158 	{
159 		WsdlProject project = ( WsdlProject )getModelItemProject( testStep );
160 		if( project == null )
161 			return null;
162 
163 		return PropertyExpander.expandProperties( project, project.getResourceRoot() );
164 	}
165 
166 	public static void unsetIds( AbstractWsdlModelItem<?> modelItem )
167 	{
168 		if( modelItem.getConfig().isSetId() )
169 			modelItem.getConfig().unsetId();
170 
171 		for( ModelItem child : modelItem.getChildren() )
172 		{
173 			if( child instanceof AbstractWsdlModelItem )
174 			{
175 				unsetIds( ( AbstractWsdlModelItem<?> )child );
176 			}
177 		}
178 	}
179 
180 	public static void unsetIds( AbstractWsdlModelItem<?>[] modelItems )
181 	{
182 		for( AbstractWsdlModelItem<?> modelItem : modelItems )
183 			unsetIds( modelItem );
184 	}
185 
186 	public interface ModelItemFilter<T extends ModelItem>
187 	{
188 		public boolean accept( T modelItem );
189 	}
190 
191 	public static boolean dependsOn( ModelItem source, ModelItem target )
192 	{
193 		if( source == target )
194 			return true;
195 
196 		ModelItem p = source.getParent();
197 		while( p != null )
198 		{
199 			if( p == target )
200 				return true;
201 
202 			p = p.getParent();
203 		}
204 
205 		return false;
206 	}
207 
208 	public static class InterfaceTypeFilter implements ModelItemFilter<Interface>
209 	{
210 		private String type;
211 
212 		public InterfaceTypeFilter( String type )
213 		{
214 			this.type = type;
215 		}
216 
217 		public boolean accept( Interface modelItem )
218 		{
219 			return modelItem.getInterfaceType().equals( type );
220 		}
221 
222 	}
223 }