source: gs3-extensions/atlas-src/trunk/src/org/greenstone/server/ServerPlace.java@ 22272

Last change on this file since 22272 was 22272, checked in by sjm84, 14 years ago

Initial version of ATLAS as an extension

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