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