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