source: gs3-extensions/atlas-src/trunk/src/org/greenstone/atlas/client/Place.java@ 23906

Last change on this file since 23906 was 23906, checked in by sjm84, 13 years ago

Committing most recent version of ATLAS

File size: 9.8 KB
Line 
1package org.greenstone.client;
2
3import java.io.Serializable;
4import java.util.ArrayList;
5
6import com.google.gwt.user.client.rpc.AsyncCallback;
7
8public class Place implements Serializable
9{
10 private static final long serialVersionUID = 6106265145261780206L;
11
12 protected ArrayList<String> _childrenPlaceNames = new ArrayList<String>();
13
14 protected String _placeName = null;
15 protected String _parentPlaceName = null;
16 protected String _placeType = null;
17
18 protected Float _latitude = null;
19 protected Float _longitude = null;
20
21 protected long _population = 0;
22 protected long _id = -1;
23 protected int _score = 0;
24
25 protected boolean _directlyReferenced = false;
26 protected static boolean _loopDone = false;
27
28 protected static Place _tempPlace = null;
29
30 public Place()
31 {
32
33 }
34
35 public Place(String placeName)
36 {
37 _placeName = placeName;
38 }
39
40 public void setParentPlaceName(String parentPlaceName)
41 {
42 _parentPlaceName = parentPlaceName;
43 }
44
45 public boolean addChildPlaceName(String placeName)
46 {
47 if (_childrenPlaceNames.contains(placeName))
48 {
49 return false;
50 }
51 else
52 {
53 _childrenPlaceNames.add(placeName);
54 return true;
55 }
56 }
57
58 public boolean equals(Object placeToCompare)
59 {
60 if (placeToCompare == null)
61 {
62 return false;
63 }
64 /*
65 if (placeToCompare instanceof ServerPlace)
66 {
67 ServerPlace otherPlace = (ServerPlace)(placeToCompare);
68 if(_placeName != null && otherPlace.getName() != null && _parentPlaceName != null && otherPlace.getParentPlaceName() != null && _placeName.equals(otherPlace.getName()))
69 {
70 String[] cols = _parentPlaceName.split(", ");
71 String[] otherCols = otherPlace.getParentPlaceName().split(", ");
72
73 if(cols.length == 3 && otherCols.length == 2)
74 {
75 if(cols[0].equals(otherPlace.getName()) && cols[1].equals(otherCols[0]) && cols[2].equals(otherCols[1]))
76 {
77 return true;
78 }
79 }
80 else if(cols.length == 2 && otherCols.length == 3)
81 {
82 if(otherCols[0].equals(_placeName) && otherCols[1].equals(cols[0]) && otherCols[2].equals(cols[1]))
83 {
84 return true;
85 }
86 }
87 else if(cols.length == 2 && otherCols.length == 1)
88 {
89 if(cols[0].equals(otherPlace.getName()) && cols[1].equals(otherCols[0]))
90 {
91 return true;
92 }
93 }
94 else if(cols.length == 1 && otherCols.length == 2)
95 {
96 if(otherCols[0].equals(_placeName) && otherCols[1].equals(cols[0]))
97 {
98 return true;
99 }
100 }
101 }
102
103 if (_id != -1 && ((ServerPlace)(placeToCompare)).getId() != -1 && _id == otherPlace.getId())
104 {
105 return true;
106 }
107
108 String otherPlaceName = otherPlace.getName();
109 String otherParentName = otherPlace.getParentPlaceName();
110
111 if ((_placeName == null && otherPlaceName == null) && (_parentPlaceName == null && otherParentName == null))
112 {
113 return true;
114 }
115
116 if ((_placeName == null && otherPlaceName == null) && _parentPlaceName != null && otherParentName != null && _parentPlaceName.equals(otherParentName))
117 {
118 return true;
119 }
120
121 if ((_parentPlaceName == null && otherParentName == null) && _placeName != null && otherPlaceName != null && _placeName.equals(otherPlaceName))
122 {
123 return true;
124 }
125
126 if (_placeName != null && otherPlaceName != null && _parentPlaceName != null && otherParentName != null && _parentPlaceName.equals(otherPlace.getParentPlaceName()) && _placeName.equals(otherPlace.getName()))
127 {
128 return true;
129 }
130 }
131*/
132 if (placeToCompare instanceof Place)
133 {
134 Place otherPlace = (Place)(placeToCompare);
135 if(_placeName != null && otherPlace.getName() != null && _parentPlaceName != null && otherPlace.getParentPlaceName() != null && _placeName.equals(otherPlace.getName()))
136 {
137 String[] cols = _parentPlaceName.split(", ");
138 String[] otherCols = otherPlace.getParentPlaceName().split(", ");
139
140 if(cols.length == 3 && otherCols.length == 2)
141 {
142 if(cols[0].equals(otherPlace.getName()) && cols[1].equals(otherCols[0]) && cols[2].equals(otherCols[1]))
143 {
144 return true;
145 }
146 }
147 else if(cols.length == 2 && otherCols.length == 3)
148 {
149 if(otherCols[0].equals(_placeName) && otherCols[1].equals(cols[0]) && otherCols[2].equals(cols[1]))
150 {
151 return true;
152 }
153 }
154 else if(cols.length == 2 && otherCols.length == 1)
155 {
156 if(cols[0].equals(otherPlace.getName()) && cols[1].equals(otherCols[0]))
157 {
158 return true;
159 }
160 }
161 else if(cols.length == 1 && otherCols.length == 2)
162 {
163 if(otherCols[0].equals(_placeName) && otherCols[1].equals(cols[0]))
164 {
165 return true;
166 }
167 }
168 }
169
170 if (_id != -1 && ((Place)(placeToCompare)).getId() != -1 && _id == ((Place)(placeToCompare)).getId())
171 {
172 return true;
173 }
174
175 String otherPlaceName = otherPlace.getName();
176 String otherParentName = otherPlace.getParentPlaceName();
177
178 if ((_placeName == null && otherPlaceName == null) && (_parentPlaceName == null && otherParentName == null))
179 {
180 return true;
181 }
182
183 if ((_placeName == null && otherPlaceName == null) && _parentPlaceName != null && otherParentName != null && _parentPlaceName.equals(otherParentName))
184 {
185 return true;
186 }
187
188 if ((_parentPlaceName == null && otherParentName == null) && _placeName != null && otherPlaceName != null && _placeName.equals(otherPlaceName))
189 {
190 return true;
191 }
192
193 if (_placeName != null && otherPlaceName != null && _parentPlaceName != null && otherParentName != null && _parentPlaceName.equals(otherPlace.getParentPlaceName()) && _placeName.equals(otherPlace.getName()))
194 {
195 return true;
196 }
197 }
198 return false;
199 }
200
201 public String toString()
202 {
203 String info = "*************************";
204
205 info += "\nPlace name = " + _placeName + "\nParents = " + _parentPlaceName + "\nChildren = ";
206
207 for (String p : _childrenPlaceNames)
208 {
209 info += "->" + p;
210 }
211
212 info += "\nPlace type = " + _placeType;
213 info += "\nPopulation = " + _population;
214 info += "\nLatitude = " + _latitude + " Longitude = " + _longitude;
215 info += "\n*************************";
216
217 return info;
218 }
219
220 // Get Methods
221
222 public int getScore()
223 {
224 return _score;
225 }
226
227 public String getPlaceType()
228 {
229 return _placeType;
230 }
231
232 public String getParentPlaceName()
233 {
234 return _parentPlaceName;
235 }
236
237 public ArrayList<String> getChildrenPlaceNames()
238 {
239 return _childrenPlaceNames;
240 }
241
242 public String getName()
243 {
244 return _placeName;
245 }
246
247 public Float getLatitude()
248 {
249 return _latitude;
250 }
251
252 public Float getLongitude()
253 {
254 return _longitude;
255 }
256
257 public long getPopulation()
258 {
259 return _population;
260 }
261
262 public long getId()
263 {
264 return _id;
265 }
266
267 public boolean isDirectlyReferenced()
268 {
269 return _directlyReferenced;
270 }
271
272 // Set Methods
273
274 public void setScore(int score)
275 {
276 _score = score;
277 }
278
279 public void setPlaceType(String placeType)
280 {
281 _placeType = placeType;
282 }
283
284 public void setLatitude(Float latitude)
285 {
286 _latitude = latitude;
287 }
288
289 public void setLongitude(Float longitude)
290 {
291 _longitude = longitude;
292 }
293
294 public void setPopulation(long population)
295 {
296 _population = population;
297 }
298
299 public void setId(long id)
300 {
301 _id = id;
302 }
303
304 public void directReference()
305 {
306 _directlyReferenced = true;
307 }
308
309 /**
310 * Figures out what sort of place the line in the file is refering to (e.g.
311 * locality, region, etc.)
312 *
313 * @param placeName
314 * is the name of the place
315 * @param subRegionName
316 * is the possible sub region the place is in
317 * @param regionName
318 * is the possible region the place is in
319 * @param countryName
320 * is the country the place is in
321 * @param placeType
322 * is the type of place
323 * @return returns the place
324 */
325 public static Place createPlaceFromInformation(long id, String placeName, String subRegionName, String regionName, String countryName, String placeType, Float longitude, Float latitude, long population)
326 {
327 Place newPlace = new Place(placeName);
328
329 String parentPlaceName = null;
330 if (!(subRegionName == null))
331 {
332 parentPlaceName = subRegionName;
333 }
334 if (!(regionName == null))
335 {
336 if (parentPlaceName == null)
337 {
338 parentPlaceName = regionName;
339 }
340 else
341 {
342 parentPlaceName += ", " + regionName;
343 }
344 }
345 if (!(countryName == null))
346 {
347 if (parentPlaceName == null)
348 {
349 parentPlaceName = countryName;
350 }
351 else
352 {
353 parentPlaceName += ", " + countryName;
354 }
355 }
356
357 if (parentPlaceName == null && countryName == null)
358 {
359 if (placeType.equals("country"))
360 {
361 countryName = placeName;
362 }
363 else
364 {
365 return null;
366 }
367 }
368
369 newPlace.setId(id);
370 newPlace.setParentPlaceName(parentPlaceName);
371 newPlace.setLatitude(latitude);
372 newPlace.setLongitude(longitude);
373 newPlace.setPopulation(population);
374 newPlace.setPlaceType(placeType);
375 return newPlace;
376 }
377
378 public boolean isIn(Place p)
379 {
380 if(_parentPlaceName == null)
381 {
382 return false;
383 }
384
385 String[] cols = _parentPlaceName.split(", ");
386
387 if(p.getParentPlaceName() == null)
388 {
389 if(cols[cols.length-1].equals(p.getName()))
390 {
391 return true;
392 }
393 return false;
394 }
395 String[] otherCols = p.getParentPlaceName().split(", ");
396
397 if(cols.length <= otherCols.length)
398 {
399 return false;
400 }
401
402 else if(otherCols.length == 1)
403 {
404 if(cols[cols.length-1].equals(otherCols[0]) && cols[cols.length-2].equals(p.getName()))
405 {
406 return true;
407 }
408 }
409 else if(otherCols.length == 2)
410 {
411 if(cols[cols.length-1].equals(otherCols[1]) && cols[cols.length-2].equals(otherCols[0]) && cols[cols.length-3].equals(p.getName()))
412 {
413 return true;
414 }
415 }
416
417 return false;
418 }
419
420 public static Place createPlaceFromId(long id)
421 {
422 _loopDone = false;
423 GS3MapLibrary.getServerConnection().getPlaceById(id, new AsyncCallback<Place>()
424 {
425 @Override
426 public void onSuccess(Place p)
427 {
428 _tempPlace = p;
429 _loopDone = true;
430 }
431
432 @Override
433 public void onFailure(Throwable arg0)
434 {
435 _tempPlace = null;
436 _loopDone = true;
437 }
438 });
439
440 while(!_loopDone);
441
442 return _tempPlace;
443 }
444
445 public void unDirectReference()
446 {
447 _directlyReferenced = false;
448 }
449}
Note: See TracBrowser for help on using the repository browser.