root/tools/routingservice/trunk/src/jp/co/orkney/restlet/util/DatabaseConnection.java

Revision 264, 5.1 KB (checked in by anton, 21 months ago)

Client id is now case sensitive

Line 
1package jp.co.orkney.restlet.util;
2
3/**
4 * Copyright (c) 2007 Orkney, Inc. <http://www.orkney.co.jp/>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20import java.io.File;
21import java.math.BigDecimal;
22import java.sql.Connection;
23import java.sql.ResultSet;
24import java.sql.SQLException;
25import java.sql.Statement;
26
27import javax.sql.rowset.WebRowSet;
28
29import org.antlr.stringtemplate.StringTemplate;
30import org.restlet.Client;
31import org.restlet.data.MediaType;
32import org.restlet.data.Protocol;
33import org.restlet.data.Request;
34import org.restlet.data.Response;
35import org.restlet.resource.Representation;
36import org.restlet.resource.StringRepresentation;
37
38import com.noelios.restlet.ext.jdbc.JdbcClientHelper;
39import com.noelios.restlet.ext.jdbc.RowSetRepresentation;
40import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
41
42import jp.co.orkney.restlet.util.Log;
43
44/**
45 * <b>To access to a database</b>
46 * <p>
47 * Provides methods to open, request and close a database connection <br />
48 * DatabaseConnection object is characterized by the following information:
49 * <ul>
50 * <li>A Connection object</li>
51 * <li>A Statement object</li>
52 * <li>A boolean to know if the connection is started or not</li>
53 * <li>An DatabaseConfiguration object to provide information about "how to
54 * connect"</li>
55 * </ul>
56 * </p>
57 *
58 * @author Matthieu Bilbille - Orkney Inc.
59 * @version 1.0
60 * @see Connection
61 * @see Statement
62 * @see DatabaseConfiguration
63 */
64public class DatabaseConnection
65{
66    private IOHelper ioHelper;
67
68    private Log log;
69
70    private DatabaseConfiguration databaseConfiguration;
71
72    private Configuration configuration;
73
74    /**
75         * Constructor DatabaseConnection
76         * <p>
77         * Creates a new DatabaseConnection object with the specified
78         * Configuration object
79         * </p>
80         *
81         * @param configuration
82         *                The current WebRouting Service's configuration
83         */
84    public DatabaseConnection(Configuration configuration)
85    {
86        this.configuration = configuration;
87        log = configuration.createLog();
88        // databaseConfiguration =
89        // configuration.getService().getDatabaseConfiguration();
90        ioHelper = new IOHelper(configuration);
91    }
92
93    /**
94         * Creates and sends the query using pool connection
95         *
96         * @param query
97         *                theSQL query
98         * @return the result of the request
99         */
100    public ResultSet getResult(Service service, String query)
101    {
102        StringTemplate queryTemplate = new StringTemplate(ioHelper
103                .loadFile(new File(configuration.getUrlXMLTemplateQuery())));
104
105        databaseConfiguration = service.getDatabaseConfiguration();
106
107        // Makes the SQL query
108        queryTemplate.setAttribute("user", databaseConfiguration.getUser());
109        queryTemplate.setAttribute("password", databaseConfiguration
110                .getPassword());
111        queryTemplate.setAttribute("query", query);
112
113        ResultSet result = null;
114
115        Client client = new Client(Protocol.JDBC);
116        JdbcClientHelper helper = new JdbcClientHelper(client);
117        try
118        {
119            Class.forName(databaseConfiguration.getDriver());
120        }
121        catch (ClassNotFoundException e)
122        {
123            log.write("--ERROR: Can not find the driver", 0);
124        }
125        Representation req = new StringRepresentation(queryTemplate.toString()
126                .replace("&", "&amp;"), MediaType.TEXT_XML);
127        Request request = JdbcClientHelper.create(databaseConfiguration
128                .getURL(), req);
129        Response response = new Response(request);
130        helper.handle(request, response);
131
132        WebRowSet rowSet = ((RowSetRepresentation) response.getEntity())
133                .getWebRowSet();
134
135        try
136        {
137            result = rowSet;
138        }
139        catch (Exception ex)
140        {
141            log
142                    .write(
143                            "--ERROR: Can not cast the result from WebRowSet to ResultSet",
144                            0);
145        }
146        return result;
147    }
148
149    public void fillKeysTable(Service service, Hashtable keys)
150            throws SQLException
151    {
152    String q = service.getSQL()[0];     
153        ResultSet rs = getResult(service, q);
154        while (rs != null && rs.next())
155            keys.put(rs.getString("client_id"), rs
156                    .getString("key"));
157    }
158
159    public Point transform(Service service, Point point, String sridIn, String sridOut)
160            throws SQLException
161    {
162        Point res = point;
163
164        double x = point.getX().doubleValue();
165        double y = point.getY().doubleValue();
166       
167        String query = "select x(a.g), y(a.g) from (select transform(geometryfromtext('POINT("
168                + x + " " + y + ")', " + sridIn + "), " + sridOut
169                + ") as g) as a";
170       
171        this.log.write(query, 2);
172
173        ResultSet rs = getResult(service, query);
174
175        while (rs != null && rs.next())
176            res = new Point(BigDecimal.valueOf(rs.getDouble("x")),
177                    BigDecimal.valueOf(rs.getDouble("y")));
178
179        return res;
180    }
181}
Note: See TracBrowser for help on using the browser.