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  package com.eviware.soapui.impl.rest.support;
13  
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.HashMap;
17  import java.util.HashSet;
18  import java.util.List;
19  import java.util.Map;
20  import java.util.Properties;
21  import java.util.Set;
22  
23  import com.eviware.soapui.model.ModelItem;
24  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
25  import com.eviware.soapui.model.testsuite.TestProperty;
26  import com.eviware.soapui.model.testsuite.TestPropertyListener;
27  
28  public class OverlayRestParamsPropertyHolder implements RestParamsPropertyHolder
29  {
30  	private RestParamsPropertyHolder parent;
31  	private RestParamsPropertyHolder overlay;
32  	private Set<TestPropertyListener> listeners = new HashSet<TestPropertyListener>();
33  
34  	public OverlayRestParamsPropertyHolder( RestParamsPropertyHolder parent, RestParamsPropertyHolder overlay )
35  	{
36  		this.parent = parent;
37  		this.overlay = overlay;
38  		parent.addTestPropertyListener( new ParentListener() );
39  		overlay.addTestPropertyListener( new OverlayListener() );
40  	}
41  
42  	public void addParameter( RestParamProperty prop )
43  	{
44  		overlay.addParameter( prop );
45  	}
46  
47  	public RestParamProperty addProperty( String name )
48  	{
49  		return overlay.addProperty( name );
50  	}
51  
52  	public void clear()
53  	{
54  		overlay.clear();
55  	}
56  
57  	public boolean containsKey( Object key )
58  	{
59  		return overlay.containsKey( key ) || parent.containsKey( key );
60  	}
61  
62  	public boolean containsValue( Object value )
63  	{
64  		return overlay.containsValue( value ) || parent.containsValue( value );
65  	}
66  
67  	public Set<java.util.Map.Entry<String, TestProperty>> entrySet()
68  	{
69  		return getProperties().entrySet();
70  	}
71  
72  	public RestParamProperty get( Object key )
73  	{
74  		return overlay.containsKey( key ) ? overlay.get( key ) : parent.get( key );
75  	}
76  
77  	public ModelItem getModelItem()
78  	{
79  		return overlay.getModelItem();
80  	}
81  
82  	public Map<String, TestProperty> getProperties()
83  	{
84  		HashMap<String, TestProperty> result = new HashMap<String, TestProperty>();
85  
86  		for( TestProperty p : values() )
87  		{
88  			result.put( p.getName(), p );
89  		}
90  
91  		return result;
92  	}
93  
94  	public String getPropertiesLabel()
95  	{
96  		return overlay.getPropertiesLabel();
97  	}
98  
99  	public RestParamProperty getProperty( String name )
100 	{
101 		return get( name );
102 	}
103 
104 	public RestParamProperty getPropertyAt( int index )
105 	{
106 		return values().toArray( new RestParamProperty[] {} )[index];
107 	}
108 
109 	public int getPropertyCount()
110 	{
111 		return values().size();
112 	}
113 
114 	public PropertyExpansion[] getPropertyExpansions()
115 	{
116 		return overlay.getPropertyExpansions();
117 	}
118 
119 	public int getPropertyIndex( String name )
120 	{
121 		int index = 0;
122 		for( TestProperty prop : values() )
123 		{
124 			if( prop.getName().equals( name ) )
125 				return index;
126 			index++ ;
127 		}
128 		return -1;
129 	}
130 
131 	public String[] getPropertyNames()
132 	{
133 		return keySet().toArray( new String[] {} );
134 	}
135 
136 	public String getPropertyValue( String name )
137 	{
138 		return overlay.hasProperty( name ) ? overlay.getPropertyValue( name ) : parent.getPropertyValue( name );
139 	}
140 
141 	public boolean hasProperty( String name )
142 	{
143 		return containsKey( name );
144 	}
145 
146 	public boolean isEmpty()
147 	{
148 		return overlay.isEmpty() && parent.isEmpty();
149 	}
150 
151 	public Set<String> keySet()
152 	{
153 		Set<String> names = new HashSet<String>();
154 		for( TestProperty prop : values() )
155 		{
156 			names.add( prop.getName() );
157 		}
158 		return names;
159 	}
160 
161 	public void moveProperty( String propertyName, int targetIndex )
162 	{
163 		overlay.moveProperty( propertyName, targetIndex );
164 	}
165 
166 	public TestProperty put( String key, TestProperty value )
167 	{
168 		return overlay.put( key, value );
169 	}
170 
171 	public void putAll( Map<? extends String, ? extends TestProperty> m )
172 	{
173 		overlay.putAll( m );
174 	}
175 
176 	public TestProperty remove( Object key )
177 	{
178 		return overlay.remove( key );
179 	}
180 
181 	public RestParamProperty removeProperty( String propertyName )
182 	{
183 		return overlay.removeProperty( propertyName );
184 	}
185 
186 	public boolean renameProperty( String name, String newName )
187 	{
188 		return overlay.renameProperty( name, newName );
189 	}
190 
191 	public void resetValues()
192 	{
193 		overlay.resetValues();
194 	}
195 
196 	public void saveTo( Properties props )
197 	{
198 		for( TestProperty prop : values() )
199 		{
200 			props.setProperty( prop.getName(), prop.getValue() != null ? prop.getValue() : "" );
201 		}
202 	}
203 
204 	public void setPropertiesLabel( String propertiesLabel )
205 	{
206 		overlay.setPropertiesLabel( propertiesLabel );
207 	}
208 
209 	public void setPropertyValue( String name, String value )
210 	{
211 		overlay.setPropertyValue( name, value );
212 	}
213 
214 	public int size()
215 	{
216 		return getPropertyCount();
217 	}
218 
219 	public Collection<TestProperty> values()
220 	{
221 		// List<TestProperty> values = new
222 		// ArrayList<TestProperty>(overlay.values());
223 		List<TestProperty> values = new ArrayList<TestProperty>();
224 		for( TestProperty prop : parent.values() )
225 		{
226 			if( overlay.hasProperty( prop.getName() ) )
227 				values.add( overlay.getProperty( prop.getName() ) );
228 			else
229 				values.add( prop );
230 		}
231 		for( TestProperty prop : overlay.values() )
232 		{
233 			if( !parent.hasProperty( prop.getName() ) )
234 				values.add( prop );
235 		}
236 		return values;
237 	}
238 
239 	private void firePropertyAdded( String name )
240 	{
241 		TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
242 		for( TestPropertyListener listener : listenersArray )
243 		{
244 			listener.propertyAdded( name );
245 		}
246 	}
247 
248 	private void firePropertyRemoved( String name )
249 	{
250 		TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
251 		for( TestPropertyListener listener : listenersArray )
252 		{
253 			listener.propertyRemoved( name );
254 		}
255 	}
256 
257 	private void firePropertyMoved( String name, int oldIndex, int newIndex )
258 	{
259 		TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
260 		for( TestPropertyListener listener : listenersArray )
261 		{
262 			listener.propertyMoved( name, oldIndex, newIndex );
263 		}
264 	}
265 
266 	private void firePropertyRenamed( String oldName, String newName )
267 	{
268 		TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
269 		for( TestPropertyListener listener : listenersArray )
270 		{
271 			listener.propertyRenamed( oldName, newName );
272 		}
273 	}
274 
275 	private void firePropertyValueChanged( String name, String oldValue, String newValue )
276 	{
277 		TestPropertyListener[] listenersArray = listeners.toArray( new TestPropertyListener[listeners.size()] );
278 		for( TestPropertyListener listener : listenersArray )
279 		{
280 			listener.propertyValueChanged( name, oldValue, newValue );
281 		}
282 	}
283 
284 	public void addTestPropertyListener( TestPropertyListener listener )
285 	{
286 		listeners.add( listener );
287 	}
288 
289 	public void removeTestPropertyListener( TestPropertyListener listener )
290 	{
291 		listeners.remove( listener );
292 	}
293 
294 	private class ParentListener implements TestPropertyListener
295 	{
296 
297 		public void propertyAdded( String name )
298 		{
299 			if( !overlay.hasProperty( name ) )
300 				firePropertyAdded( name );
301 		}
302 
303 		public void propertyMoved( String name, int oldIndex, int newIndex )
304 		{
305 		}
306 
307 		public void propertyRemoved( String name )
308 		{
309 			if( !overlay.hasProperty( name ) )
310 				firePropertyRemoved( name );
311 		}
312 
313 		public void propertyRenamed( String oldName, String newName )
314 		{
315 			if( overlay.hasProperty( oldName ) )
316 			{
317 				if( !overlay.hasProperty( newName ) )
318 					firePropertyAdded( newName );
319 			}
320 			else if( overlay.hasProperty( newName ) )
321 			{
322 				firePropertyRemoved( oldName );
323 			}
324 		}
325 
326 		public void propertyValueChanged( String name, String oldValue, String newValue )
327 		{
328 			if( !overlay.hasProperty( name ) )
329 				firePropertyValueChanged( name, oldValue, newValue );
330 		}
331 	}
332 
333 	private class OverlayListener implements TestPropertyListener
334 	{
335 
336 		public void propertyAdded( String name )
337 		{
338 			if( parent.hasProperty( name ) )
339 			{
340 				if( !parent.getPropertyValue( name ).equals( overlay.getPropertyValue( name ) ) )
341 					firePropertyValueChanged( name, parent.getPropertyValue( name ), overlay.getPropertyValue( name ) );
342 			}
343 			else
344 				firePropertyAdded( name );
345 		}
346 
347 		public void propertyMoved( String name, int oldIndex, int newIndex )
348 		{
349 		}
350 
351 		public void propertyRemoved( String name )
352 		{
353 			if( parent.hasProperty( name ) )
354 				firePropertyValueChanged( name, null, parent.getPropertyValue( name ) );
355 			else
356 				firePropertyRemoved( name );
357 		}
358 
359 		public void propertyRenamed( String oldName, String newName )
360 		{
361 			if( !parent.hasProperty( oldName ) && !parent.hasProperty( newName ) )
362 				firePropertyRenamed( oldName, newName );
363 			else if( parent.hasProperty( oldName ) && parent.hasProperty( newName ) )
364 			{
365 				firePropertyValueChanged( oldName, overlay.getPropertyValue( newName ), parent.getPropertyValue( oldName ) );
366 				firePropertyValueChanged( newName, parent.getPropertyValue( newName ), overlay.getPropertyValue( newName ) );
367 			}
368 			else if( parent.hasProperty( oldName ) )
369 				firePropertyAdded( newName );
370 			else
371 				firePropertyRemoved( oldName );
372 		}
373 
374 		public void propertyValueChanged( String name, String oldValue, String newValue )
375 		{
376 			firePropertyValueChanged( name, oldValue, newValue );
377 		}
378 	}
379 
380 	public List<TestProperty> getPropertyList()
381 	{
382 		return overlay.getPropertyList();
383 	}
384 
385 }