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.support.resolver;
14  
15  import java.io.File;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
20  import com.eviware.soapui.support.StringUtils;
21  import com.eviware.soapui.support.UISupport;
22  
23  public class ResolveContext<T extends AbstractWsdlModelItem<?>>
24  {
25  	private List<PathToResolve> pathsToResolve = new ArrayList<PathToResolve>();
26  	private final T modelItem;
27  
28  	public ResolveContext( T modelItem )
29  	{
30  		this.modelItem = modelItem;
31  	}
32  
33  	public T getModelItem()
34  	{
35  		return modelItem;
36  	}
37  
38  	public PathToResolve addPathToResolve( AbstractWsdlModelItem<?> owner, String description, String path )
39  	{
40  		PathToResolve pathToResolve = new PathToResolve( owner, description, path );
41  		pathsToResolve.add( pathToResolve );
42  		return pathToResolve;
43  	}
44  
45  	public PathToResolve addPathToResolve( AbstractWsdlModelItem<?> owner, String description, String path,
46  			Resolver resolver )
47  	{
48  		PathToResolve pathToResolve = new PathToResolve( owner, description, path );
49  		pathToResolve.addResolvers( resolver );
50  		pathsToResolve.add( pathToResolve );
51  		return pathToResolve;
52  	}
53  
54  	public class PathToResolve
55  	{
56  		private final AbstractWsdlModelItem<?> owner;
57  		private final String description;
58  		private List<Resolver> resolvers = new ArrayList<Resolver>();
59  		private final String path;
60  		private Resolver resolver;
61  		private boolean resolved;
62  
63  		public PathToResolve( AbstractWsdlModelItem<?> owner, String description, String path )
64  		{
65  			this.owner = owner;
66  			this.description = description;
67  			this.path = path;
68  		}
69  
70  		public void addResolvers( Resolver... resolvers )
71  		{
72  			for( Resolver res : resolvers )
73  			{
74  				this.resolvers.add( res );
75  			}
76  		}
77  
78  		public AbstractWsdlModelItem<?> getOwner()
79  		{
80  			return owner;
81  		}
82  
83  		public String getDescription()
84  		{
85  			return description;
86  		}
87  
88  		public Resolver getResolver()
89  		{
90  			return resolver;
91  		}
92  
93  		public String getPath()
94  		{
95  			return path;
96  		}
97  
98  		public boolean resolve()
99  		{
100 			if( resolver != null )
101 			{
102 				resolved = resolver.resolve();
103 				return resolved;
104 			}
105 
106 			return false;
107 		}
108 
109 		public void setResolver( Object resolveOrDefaultAction )
110 		{
111 			this.resolver = ( Resolver )resolveOrDefaultAction;
112 		}
113 
114 		public ArrayList<Resolver> getResolvers()
115 		{
116 			return ( ArrayList<Resolver> )resolvers;
117 		}
118 
119 		public boolean isResolved()
120 		{
121 			return resolved;
122 		}
123 
124 		@Override
125 		public int hashCode()
126 		{
127 			final int prime = 31;
128 			int result = 1;
129 			result = prime * result + getOuterType().hashCode();
130 			result = prime * result + ( ( description == null ) ? 0 : description.hashCode() );
131 			result = prime * result + ( ( owner == null ) ? 0 : owner.hashCode() );
132 			result = prime * result + ( ( path == null ) ? 0 : path.hashCode() );
133 			return result;
134 		}
135 
136 		@Override
137 		public boolean equals( Object obj )
138 		{
139 			if( this == obj )
140 				return true;
141 			if( obj == null )
142 				return false;
143 			if( getClass() != obj.getClass() )
144 				return false;
145 			PathToResolve other = ( PathToResolve )obj;
146 			if( !getOuterType().equals( other.getOuterType() ) )
147 				return false;
148 			if( description == null )
149 			{
150 				if( other.description != null )
151 					return false;
152 			}
153 			else if( !description.equals( other.description ) )
154 				return false;
155 			if( owner == null )
156 			{
157 				if( other.owner != null )
158 					return false;
159 			}
160 			else if( !owner.equals( other.owner ) )
161 				return false;
162 			if( path == null )
163 			{
164 				if( other.path != null )
165 					return false;
166 			}
167 			else if( !path.equals( other.path ) )
168 				return false;
169 			return true;
170 		}
171 
172 		@SuppressWarnings( "unchecked" )
173 		private ResolveContext getOuterType()
174 		{
175 			return ResolveContext.this;
176 		}
177 
178 		public void setSolved( boolean solved )
179 		{
180 			this.resolved = solved;
181 		}
182 
183 		public boolean update()
184 		{
185 			for( Resolver resolver : resolvers )
186 			{
187 				if( resolver.isResolved() )
188 					return true;
189 			}
190 			return false;
191 		}
192 
193 	}
194 
195 	public interface Resolver
196 	{
197 		public boolean resolve();
198 
199 		public boolean isResolved();
200 
201 		public String getResolvedPath();
202 
203 		public Object getDescription();
204 
205 	}
206 
207 	public boolean isEmpty()
208 	{
209 		return pathsToResolve.isEmpty();
210 	}
211 
212 	public List<PathToResolve> getPathsToResolve()
213 	{
214 		return pathsToResolve;
215 	}
216 
217 	public int getUnresolvedCount()
218 	{
219 		int resultCnt = 0;
220 
221 		for( PathToResolve ptr : pathsToResolve )
222 		{
223 			if( ptr.getResolver() == null || !ptr.getResolver().isResolved() )
224 				resultCnt++ ;
225 		}
226 
227 		return resultCnt;
228 	}
229 
230 	public int apply()
231 	{
232 		int resultCnt = 0;
233 
234 		for( PathToResolve ptr : pathsToResolve )
235 		{
236 			if( ptr.resolve() )
237 				resultCnt++ ;
238 		}
239 
240 		return resultCnt;
241 	}
242 
243 	public abstract static class FileResolver implements Resolver
244 	{
245 		private String title;
246 		private String extension;
247 		private String fileType;
248 		private String current;
249 		private File result;
250 		private boolean resolved;
251 
252 		public FileResolver( String title, String extension, String fileType, String current )
253 		{
254 			super();
255 
256 			this.title = title;
257 			this.extension = extension;
258 			this.fileType = fileType;
259 			this.current = current;
260 		}
261 
262 		public boolean isResolved()
263 		{
264 			return resolved;
265 		}
266 
267 		public String getResolvedPath()
268 		{
269 			return result == null ? null : result.getAbsolutePath();
270 		}
271 
272 		public abstract boolean apply( File newFile );
273 
274 		public boolean resolve()
275 		{
276 			result = UISupport.getFileDialogs().open( this, title, extension, fileType, current );
277 			if( result != null )
278 				resolved = apply( result );
279 
280 			return resolved;
281 		}
282 
283 		public Object getDescription()
284 		{
285 			return title;
286 		}
287 
288 		@Override
289 		public String toString()
290 		{
291 			return ( String )getDescription();
292 		}
293 	}
294 
295 	public abstract static class DirectoryResolver implements Resolver
296 	{
297 		private String title;
298 		private String current;
299 		private File result;
300 		private boolean resolved;
301 
302 		public DirectoryResolver( String title, String current )
303 		{
304 			super();
305 
306 			this.title = title;
307 			this.current = current;
308 		}
309 
310 		public boolean isResolved()
311 		{
312 			return resolved;
313 		}
314 
315 		public String getResolvedPath()
316 		{
317 			return result == null ? null : result.getAbsolutePath();
318 		}
319 
320 		public abstract boolean apply( File newFile );
321 
322 		public boolean resolve()
323 		{
324 			result = UISupport.getFileDialogs().openDirectory( this, title,
325 					StringUtils.isNullOrEmpty( current ) ? null : new File( current ) );
326 			if( result != null )
327 				resolved = apply( result );
328 
329 			return resolved;
330 		}
331 
332 		public Object getDescription()
333 		{
334 			return title;
335 		}
336 
337 		public String toString()
338 		{
339 			return ( String )getDescription();
340 		}
341 	}
342 
343 	public boolean hasThisModelItem( AbstractWsdlModelItem<?> modelItem, String description, String pathName )
344 	{
345 		// if removed path is changed and turned to null. that is ok.
346 		if( pathName == null )
347 			return true;
348 		PathToResolve pathToCheck = new PathToResolve( modelItem, description, pathName );
349 		for( PathToResolve path : pathsToResolve )
350 		{
351 			if( path.equals( pathToCheck ) )
352 				return true;
353 		}
354 		return false;
355 	}
356 
357 	public PathToResolve getPath( AbstractWsdlModelItem<?> modelItem, String description, String pathName )
358 	{
359 		PathToResolve pathToCheck = new PathToResolve( modelItem, description, pathName );
360 		for( PathToResolve path : pathsToResolve )
361 		{
362 			if( path.equals( pathToCheck ) )
363 				return path;
364 		}
365 		return null;
366 	}
367 }