1 | package org.greenstone.atlas.server;
|
---|
2 |
|
---|
3 | import java.net.URLConnection;
|
---|
4 | import java.sql.Connection;
|
---|
5 | import java.sql.DriverManager;
|
---|
6 | import java.sql.ResultSet;
|
---|
7 | import java.sql.Statement;
|
---|
8 | import java.util.ArrayList;
|
---|
9 |
|
---|
10 | import org.greenstone.atlas.client.FindPlaceService;
|
---|
11 | import org.greenstone.atlas.client.Place;
|
---|
12 |
|
---|
13 | import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * The server side implementation of the RPC service.
|
---|
17 | */
|
---|
18 | @SuppressWarnings("serial")
|
---|
19 | public class FindPlaceServiceImpl extends RemoteServiceServlet implements FindPlaceService
|
---|
20 | {
|
---|
21 | protected PageScanner _pageScanner = null;
|
---|
22 | protected Statement _database = null;
|
---|
23 | protected ArrayList<Place> _places = null;
|
---|
24 |
|
---|
25 | public Boolean loadGazetteer()
|
---|
26 | {
|
---|
27 | System.err.println("Loading Gazetteer... ");
|
---|
28 | _pageScanner = new PageScanner(this.getServletContext().getRealPath(""));
|
---|
29 | System.err.println("... Done!");
|
---|
30 | return true;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public String getMarkedUpText()
|
---|
34 | {
|
---|
35 | return _pageScanner.getMarkedUpText();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public Boolean findPlacesInText(String text, String html)
|
---|
39 | {
|
---|
40 | _pageScanner.examineTextWithGate(text, html);
|
---|
41 | return true;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public ArrayList<Place> getPlaces(double maxScore, double minScore, long minPopulation, boolean locality, boolean region, boolean country, int numberOfPlacesToGet)
|
---|
45 | {
|
---|
46 | return _pageScanner.getPlacesWithParams(minScore, maxScore, minPopulation, locality, region, country, numberOfPlacesToGet);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public Boolean adjustScoringParams(double penalty, double parentBonus, double indirectReferencePercent)
|
---|
50 | {
|
---|
51 | _pageScanner.setScoringParams(penalty, parentBonus, indirectReferencePercent);
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public Boolean isGazetteerLoaded()
|
---|
56 | {
|
---|
57 | return _pageScanner != null && _pageScanner.isGazetteerLoaded();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public ArrayList<ArrayList<Place>> findPlacesInMultipleTexts(String[] texts)
|
---|
61 | {
|
---|
62 | return _pageScanner.examineMultipleTexts(texts);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public ArrayList<Place> updateGazetteer(int start, int end)
|
---|
66 | {
|
---|
67 | if(_places == null)
|
---|
68 | {
|
---|
69 | if(_database == null)
|
---|
70 | {
|
---|
71 | try
|
---|
72 | {
|
---|
73 | Class.forName("org.postgresql.Driver");
|
---|
74 | Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/GS3MapDB", "sam","password");
|
---|
75 | _database = c.createStatement();
|
---|
76 | }
|
---|
77 | catch (Exception ex)
|
---|
78 | {
|
---|
79 | ex.printStackTrace();
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 | System.out.println("Got connection");
|
---|
85 |
|
---|
86 | try
|
---|
87 | {
|
---|
88 | ResultSet results = _database.executeQuery("SELECT * FROM places WHERE latitude is null;");
|
---|
89 |
|
---|
90 | System.out.println("Got results");
|
---|
91 |
|
---|
92 | _places = new ArrayList<Place>();
|
---|
93 |
|
---|
94 | while(results.next())
|
---|
95 | {
|
---|
96 | _places.add(Place.createPlaceFromInformation(results.getLong("id"), results.getString("placename"), results.getString("subregion"), results.getString("region"), results.getString("country"), results.getString("placetype"), null, null, results.getLong("population")));
|
---|
97 | }
|
---|
98 | }
|
---|
99 | catch(Exception ex)
|
---|
100 | {
|
---|
101 | ex.printStackTrace();
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | System.out.println("Making subset");
|
---|
106 |
|
---|
107 | ArrayList<Place> placeSubset = new ArrayList<Place>();
|
---|
108 | for(int i = start; i <= end; i++)
|
---|
109 | {
|
---|
110 | if(i < _places.size())
|
---|
111 | {
|
---|
112 | System.out.println("Adding place to subset " + i);
|
---|
113 | placeSubset.add(_places.get(i));
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | System.out.println("Done making subset, places size = " + placeSubset.size());
|
---|
118 |
|
---|
119 | return placeSubset;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public Boolean addToDatabase(Place p, float lat, float lng)
|
---|
123 | {
|
---|
124 | if(_database == null)
|
---|
125 | {
|
---|
126 | try
|
---|
127 | {
|
---|
128 | Class.forName("org.postgresql.Driver");
|
---|
129 | Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/GS3MapDB", "sam","password");
|
---|
130 | _database = c.createStatement();
|
---|
131 | }
|
---|
132 | catch (Exception ex)
|
---|
133 | {
|
---|
134 | ex.printStackTrace();
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | String placeName = p.getName();
|
---|
139 | String parentPlaceName = p.getParentPlaceName();
|
---|
140 |
|
---|
141 | String firstParent = null;
|
---|
142 | String secondParent = null;
|
---|
143 | String thirdParent = null;
|
---|
144 |
|
---|
145 | //Separate the parents
|
---|
146 | if(parentPlaceName != null)
|
---|
147 | {
|
---|
148 | String[] parents = parentPlaceName.split(", ");
|
---|
149 |
|
---|
150 | if(parents.length == 1)
|
---|
151 | {
|
---|
152 | firstParent = parents[0];
|
---|
153 | }
|
---|
154 | else if(parents.length == 2)
|
---|
155 | {
|
---|
156 | firstParent = parents[0];
|
---|
157 | secondParent = parents[1];
|
---|
158 | }
|
---|
159 | else
|
---|
160 | {
|
---|
161 | firstParent = parents[0];
|
---|
162 | secondParent = parents[1];
|
---|
163 | thirdParent = parents[2];
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | try
|
---|
168 | {
|
---|
169 | //If the place does not have a parent
|
---|
170 | if(parentPlaceName == null)
|
---|
171 | {
|
---|
172 | _database.executeQuery("UPDATE places SET latitude = " + lat + ", longitude = " + lng + " WHERE placename = \'" + placeName.replaceAll("\'", "\\\\\'") + "\' " +
|
---|
173 | "and subregion is null " +
|
---|
174 | "and region is null " +
|
---|
175 | "and country is null;");
|
---|
176 | }
|
---|
177 | //If the place has one parent
|
---|
178 | else if(secondParent == null)
|
---|
179 | {
|
---|
180 | _database.executeQuery("UPDATE places SET latitude = " + lat + ", longitude = " + lng + " WHERE placename = \'" + placeName.replaceAll("\'", "\\\\\'") + "\' " +
|
---|
181 | "and subregion is null " +
|
---|
182 | "and region is null " +
|
---|
183 | "and country = \'" + firstParent.replaceAll("\'", "\\\\\'") + "\';");
|
---|
184 | }
|
---|
185 | //If the place has two parents
|
---|
186 | else if(thirdParent == null)
|
---|
187 | {
|
---|
188 | _database.executeQuery("UPDATE places SET latitude = " + lat + ", longitude = " + lng + " WHERE placename = \'" + placeName.replaceAll("\'", "\\\\\'") + "\' " +
|
---|
189 | "and subregion is null " +
|
---|
190 | "and region = \'" + firstParent.replaceAll("\'", "\\\\\'") + "\' " +
|
---|
191 | "and country = \'" + secondParent.replaceAll("\'", "\\\\\'") + "\';");
|
---|
192 | }
|
---|
193 | //If the place has three parents
|
---|
194 | else
|
---|
195 | {
|
---|
196 | _database.executeQuery("UPDATE places SET latitude = " + lat + ", longitude = " + lng + " WHERE placename = \'" + placeName.replaceAll("\'", "\\\\\'") + "\' " +
|
---|
197 | "and subregion = \'" + firstParent.replaceAll("\'", "\\\\\'") + "\' " +
|
---|
198 | "and region = \'" + secondParent.replaceAll("\'", "\\\\\'") + "\' " +
|
---|
199 | "and country = \'" + thirdParent.replaceAll("\'", "\\\\\'") + "\';");
|
---|
200 | }
|
---|
201 | _database.close();
|
---|
202 | _database = null;
|
---|
203 | }
|
---|
204 | catch(Exception ex)
|
---|
205 | {
|
---|
206 | ex.printStackTrace();
|
---|
207 | }
|
---|
208 |
|
---|
209 | return true;
|
---|
210 | }
|
---|
211 |
|
---|
212 | public ArrayList<String> spatialSearch(ArrayList<Float[]> points)
|
---|
213 | {
|
---|
214 | try
|
---|
215 | {
|
---|
216 | System.out.println("Connecting to database for spatial search");
|
---|
217 | if(_database == null)
|
---|
218 | {
|
---|
219 | Class.forName("org.postgresql.Driver");
|
---|
220 | Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/GS3MapDB", "sam","password");
|
---|
221 | _database = c.createStatement();
|
---|
222 | }
|
---|
223 | System.out.println("Done connecting to database");
|
---|
224 |
|
---|
225 | StringBuilder polygonString = new StringBuilder("'POLYGON((");
|
---|
226 |
|
---|
227 | for(int j = 0; j < points.size(); j++)
|
---|
228 | {
|
---|
229 | Float[] current = points.get(j);
|
---|
230 | if(j != 0)
|
---|
231 | {
|
---|
232 | polygonString.append(", " + current[0] + " " + current[1]);
|
---|
233 | }
|
---|
234 | else
|
---|
235 | {
|
---|
236 | polygonString.append(current[0] + " " + current[1]);
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | polygonString.append(", " + points.get(0)[0] + " " + points.get(0)[1]);
|
---|
241 |
|
---|
242 | polygonString.append("))'");
|
---|
243 |
|
---|
244 | System.out.println("Polystring = " + polygonString);
|
---|
245 |
|
---|
246 | ResultSet results = _database.executeQuery("select placename from places where id in (select id from placepoints where ST_Within(point, " + polygonString + "));");
|
---|
247 |
|
---|
248 | ArrayList<String> placeNames = new ArrayList<String>();
|
---|
249 | while(results.next())
|
---|
250 | {
|
---|
251 | placeNames.add(results.getString("placename"));
|
---|
252 | System.out.println(results.getString("placename"));
|
---|
253 | }
|
---|
254 |
|
---|
255 | _database.close();
|
---|
256 | _database = null;
|
---|
257 |
|
---|
258 | return placeNames;
|
---|
259 | }
|
---|
260 | catch (Exception ex)
|
---|
261 | {
|
---|
262 | ex.printStackTrace();
|
---|
263 | return null;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | public Place getPlaceById(long id)
|
---|
268 | {
|
---|
269 | try
|
---|
270 | {
|
---|
271 | if(_database == null)
|
---|
272 | {
|
---|
273 | System.out.print("Loading postgresql driver... ");
|
---|
274 | Class.forName("org.postgresql.Driver");
|
---|
275 | Connection c = DriverManager.getConnection("jdbc:postgresql://localhost/GS3MapDB", "sam", "password");//DriverManager.getConnection("jdbc:postgresql://localhost/test", "sjm84", "password");
|
---|
276 | System.out.println("Done!");
|
---|
277 | _database = c.createStatement();
|
---|
278 | }
|
---|
279 |
|
---|
280 | ResultSet results = _database.executeQuery("SELECT * FROM places WHERE id = " + id);
|
---|
281 | if(!results.next())
|
---|
282 | {
|
---|
283 | return null;
|
---|
284 | }
|
---|
285 |
|
---|
286 | Place p = Place.createPlaceFromInformation(results.getLong("id"), results.getString("placename"), results.getString("subregion"), results.getString("region"), results.getString("country"), results.getString("placetype"), (Float) (results.getObject("longitude")), (Float) (results.getObject("latitude")), results.getLong("population"));
|
---|
287 |
|
---|
288 | results.close();
|
---|
289 |
|
---|
290 | return p;
|
---|
291 | }
|
---|
292 | catch (Exception ex)
|
---|
293 | {
|
---|
294 | ex.printStackTrace();
|
---|
295 | return null;
|
---|
296 | }
|
---|
297 | }
|
---|
298 | } |
---|