1 | /** |
---|
2 | * Copyright (c) 2007 Orkney, Inc. <http://www.orkney.co.jp/> |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | |
---|
18 | package jp.co.orkney.restlet.geo; |
---|
19 | |
---|
20 | import java.sql.ResultSet; |
---|
21 | import java.sql.SQLException; |
---|
22 | |
---|
23 | import org.antlr.stringtemplate.StringTemplate; |
---|
24 | import org.json.JSONException; |
---|
25 | |
---|
26 | import jp.co.orkney.restlet.util.IOHelper; |
---|
27 | import jp.co.orkney.restlet.util.Log; |
---|
28 | import jp.co.orkney.restlet.util.Point; |
---|
29 | import jp.co.orkney.restlet.util.Configuration; |
---|
30 | import jp.co.orkney.restlet.util.DatabaseConnection; |
---|
31 | |
---|
32 | /** |
---|
33 | * <b>TravelingSalesPerson is a class which represents a "traveling sales |
---|
34 | * person" algorithm.</b> |
---|
35 | * <p> |
---|
36 | * "traveling sales person" algorithm follows these steps: |
---|
37 | * <ul> |
---|
38 | * <li>Extract X and Y for start and each steps</li> |
---|
39 | * <li>Start the database connection</li> |
---|
40 | * <li>Make the query</li> |
---|
41 | * <li>Send the query to the database</li> |
---|
42 | * <li>Transform the result in the specified format</li> |
---|
43 | * <li>Close the database connection</li> |
---|
44 | * </ul> |
---|
45 | * To do these steps, TravelingSalesPerson class calls some method in IOHelper |
---|
46 | * and DatabaseConnection |
---|
47 | * </p> |
---|
48 | * |
---|
49 | * @author Matthieu Bilbille - Orkney Inc. |
---|
50 | * @version 1.0 |
---|
51 | * @see IOHelper |
---|
52 | * @see DatabaseConnection |
---|
53 | */ |
---|
54 | public class TravelingSalesPerson extends GeoAction |
---|
55 | { |
---|
56 | |
---|
57 | private Point startPoint; |
---|
58 | private Point stepPoints[]; |
---|
59 | |
---|
60 | |
---|
61 | /** |
---|
62 | * Constructor TravelingSalesPerson |
---|
63 | * <p> |
---|
64 | * Creates a new TravelingSalesPerson object using the specified |
---|
65 | * Configuration object and extracts X and Y from the data provides by GET |
---|
66 | * or POST request into a new point (for start and each steps). |
---|
67 | * </p> |
---|
68 | * |
---|
69 | * @param configuration |
---|
70 | * the current configuration |
---|
71 | */ |
---|
72 | public TravelingSalesPerson(Configuration configuration, DatabaseConnection databaseConnection, IOHelper ioHelper, Log log) |
---|
73 | { |
---|
74 | super(configuration, databaseConnection, ioHelper, log); |
---|
75 | |
---|
76 | name = "TSP"; |
---|
77 | |
---|
78 | // extract method for Geojson format |
---|
79 | if (this.configuration.getFormatIn().getName().equals("geojson")) |
---|
80 | { |
---|
81 | startPoint = ioHelper.extractGEOJSONPxPy(this.configuration.getService().getParameter("point_start").getValue()); |
---|
82 | String stepTexts[] = ioHelper.extractPoints(this.configuration.getService().getParameter("points_step").getValue(), "::"); |
---|
83 | stepPoints = new Point[stepTexts.length]; |
---|
84 | for (int i = 0; i < stepTexts.length; i++) |
---|
85 | { |
---|
86 | stepPoints[i] = ioHelper.extractGEOJSONPxPy(stepTexts[i]); |
---|
87 | } |
---|
88 | } |
---|
89 | // extract method for Gml format |
---|
90 | else if (this.configuration.getFormatIn().getName().equals("gml")) |
---|
91 | { |
---|
92 | } |
---|
93 | // extract method for lonlat format |
---|
94 | else if (this.configuration.getFormatIn().getName().equals("lonlat")) |
---|
95 | { |
---|
96 | } |
---|
97 | // extract method for wkt format |
---|
98 | else if (this.configuration.getFormatIn().getName().equals("wkt")) |
---|
99 | { |
---|
100 | } |
---|
101 | // extract method for kml format |
---|
102 | else if (this.configuration.getFormatIn().getName().equals("kml")) |
---|
103 | { |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Gets the result of the traveling sales person algorithm |
---|
109 | * |
---|
110 | * @return this traveling sales person's result |
---|
111 | */ |
---|
112 | public String get() |
---|
113 | { |
---|
114 | return result; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Starts the "traveling sales person" algorithm |
---|
119 | * |
---|
120 | * @throws SQLException |
---|
121 | * error to send the query to the database |
---|
122 | * @throws JSONException |
---|
123 | * Error to transform the result in Geojson format |
---|
124 | */ |
---|
125 | public void start() throws SQLException, JSONException |
---|
126 | { |
---|
127 | StringTemplate query = ioHelper.getQuery(); |
---|
128 | float bboxSize = Float.parseFloat(configuration.getService().getParameter("bbox").getValue()); |
---|
129 | |
---|
130 | // Makes the SQL query |
---|
131 | query.setAttribute("sonStartX", startPoint.getX()); |
---|
132 | query.setAttribute("sonStartY", startPoint.getY()); |
---|
133 | query.setAttribute("bbox", bboxSize); |
---|
134 | query.setAttribute("sesSteps", ioHelper.pointsToString(stepPoints)); |
---|
135 | query.setAttribute("sridProvider", configuration.getService().getDataProjection()); |
---|
136 | query.setAttribute("sridIn", configuration.getService().getParameter("sridInput").getValue()); |
---|
137 | query.setAttribute("sridOut", configuration.getService().getParameter("sridOutput").getValue()); |
---|
138 | |
---|
139 | // System.out.println(query.toString()); |
---|
140 | log.write(query.toString(), 1); |
---|
141 | ResultSet rs = databaseConnection.getResult(configuration.getService(), query.toString()); |
---|
142 | |
---|
143 | String reqId = configuration.getService().getParameter("request_id").getValue(); |
---|
144 | |
---|
145 | StringTemplate template = this.configuration.getFormatOut().getTemplate(); |
---|
146 | |
---|
147 | result = this.ioHelper.fillTemplate(rs, this.configuration.getFormatOut().getName(), |
---|
148 | template, reqId); |
---|
149 | |
---|
150 | /* |
---|
151 | // Converts the SQL Result in Geojson |
---|
152 | if (configuration.getFormatOut().getName().equals("geojson")) |
---|
153 | { |
---|
154 | result = ioHelper.travelingSalesPersonGEOJSONResult(rs, reqId); |
---|
155 | } |
---|
156 | // Converts the SQL Result in HTML |
---|
157 | else if (configuration.getFormatOut().getName().equals("html")) |
---|
158 | { |
---|
159 | result = ioHelper.travelingSalesPersonHTMLResult(rs, startPoint, reqId); |
---|
160 | } |
---|
161 | // Converts the SQL Result in XML |
---|
162 | else if (configuration.getFormatOut().getName().equals("xml")) |
---|
163 | { |
---|
164 | result = ioHelper.travelingSalesPersonXMLResult(rs, reqId); |
---|
165 | } |
---|
166 | // Converts the SQL Result in GML |
---|
167 | else if (configuration.getFormatOut().getName().equals("gml")) |
---|
168 | { |
---|
169 | result = ioHelper.travelingSalesPersonGMLResult(rs, reqId); |
---|
170 | } |
---|
171 | // Converts the SQL Result in WKT |
---|
172 | else if (configuration.getFormatOut().getName().equals("wkt")) |
---|
173 | { |
---|
174 | result = ioHelper.travelingSalesPersonWKTResult(rs); |
---|
175 | } |
---|
176 | // Converts the SQL Result in KML |
---|
177 | else if (configuration.getFormatOut().getName().equals("kml")) |
---|
178 | { |
---|
179 | result = ioHelper.travelingSalesPersonKMLResult(rs); |
---|
180 | } |
---|
181 | */ |
---|
182 | } |
---|
183 | |
---|
184 | } |
---|