001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.io.standard;
018:
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.ResultSet;
022: import java.sql.Statement;
023: import java.util.Iterator;
024:
025: import org.geotools.graph.build.GraphGenerator;
026: import org.geotools.graph.structure.Edge;
027: import org.geotools.graph.structure.Graph;
028: import org.geotools.graph.structure.Node;
029:
030: /**
031: * An implementation of GraphReaderWriter used for reading and writing graph
032: * objects to and from a database. <BR>
033: * <BR>
034: * Upon reading, the database is queried using
035: * the getQuery() template method, and a representation of the objects to be
036: * modelled by the graph are returned through a standard ResultSet. From each
037: * tuple in the result set, the object is recreated via the template method
038: * readInternal(ResultSet). The object is then passed to an underlying
039: * graph generator and the graph components used to model the object are
040: * constructed.<BR>
041: * <BR>
042: * Upon writing, the graph is read component by component based
043: * on set properties. If the NODES property is set, nodes will be written. If
044: * the EDGES property is set, edges will be written as well. As each component
045: * is processed, it is passed to the repspective template methods
046: * writeNode(Statement,Node) and writeEdge(Statement,Edge). The methods then
047: * execute a statement to create the database representation of the graph
048: * component.
049: *
050: * @author Justin Deoliveira, Refractions Research Inc, jdeolive@refractions.net
051: *
052: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/graph/src/main/java/org/geotools/graph/io/standard/DBReaderWriter.java $
053: */
054: public abstract class DBReaderWriter extends AbstractReaderWriter {
055:
056: /** JDBC driver class name key **/
057: public static final String DRIVERCLASS = "DRIVERCLASS";
058:
059: /** JDBC driver url **/
060: public static final String DRIVERURL = "DRIVERURL";
061:
062: /** Database server key **/
063: public static final String SERVER = "SERVER";
064:
065: /** Database port key **/
066: public static final String PORT = "PORT";
067:
068: /** Database name key **/
069: public static final String DBNAME = "DBNAME";
070:
071: /** User name key **/
072: public static final String USERNAME = "USERNAME";
073:
074: /** Table key **/
075: public static final String TABLENAME = "TABLENAME";
076:
077: /**
078: * Performs a graph read by querying the database and processing each tuple
079: * returned in the query. As each tuple is processed, the graph components
080: * represented by the tuple are created by an underlying GraphGenerator.
081: *
082: * @see org.geotools.graph.io.GraphReaderWriter#read()
083: */
084: public Graph read() throws Exception {
085: //get underlying generator
086: GraphGenerator generator = (GraphGenerator) getProperty(GENERATOR);
087:
088: //create database connection
089: Connection conn = getConnection();
090:
091: //query database to obtain graph information
092: Statement st = conn.createStatement();
093: ResultSet rs = st.executeQuery(getQuery());
094: // System.out.println(getQuery());
095:
096: while (rs.next()) {
097: generator.add(readInternal(rs));
098: }
099:
100: //close database connection
101: rs.close();
102: st.close();
103: conn.close();
104:
105: return (generator.getGraph());
106: }
107:
108: /**
109: * Performs a write on the graph out to the database. If the NODES property
110: * is set, the nodes of the graph will be written, and if the EDGES property
111: * is set, the edges of the graph will be written.
112: *
113: * * @see GraphGenerator#write()
114: */
115: public void write(Graph g) throws Exception {
116: //get database connection
117: Connection conn = getConnection();
118: Statement st = conn.createStatement();
119:
120: //write nodes if property set
121: if (getProperty(NODES) != null) {
122: for (Iterator itr = g.getNodes().iterator(); itr.hasNext();) {
123: writeNode(st, (Node) itr.next());
124: }
125: }
126:
127: //write edges if property set
128: if (getProperty(EDGES) != null) {
129: for (Iterator itr = g.getEdges().iterator(); itr.hasNext();) {
130: writeEdge(st, (Edge) itr.next());
131: }
132: }
133:
134: //close database connection
135: st.close();
136: conn.close();
137: }
138:
139: /**
140: * Opens a connection to the database, based on set properties.
141: *
142: * @return Connection to the database.
143: *
144: * @throws Exception
145: */
146: protected Connection getConnection() throws Exception {
147: //read database + driver properties
148: String driverclass = (String) getProperty(DRIVERCLASS);
149: String driverurl = (String) getProperty(DRIVERURL);
150: String server = (String) getProperty(SERVER);
151: String port = (String) getProperty(PORT);
152: String dbname = (String) getProperty(DBNAME);
153: String username = (String) getProperty(USERNAME);
154:
155: Class.forName(driverclass);
156: return (DriverManager.getConnection(driverurl + server + ":"
157: + port + "/" + dbname + "?user=" + username));
158: }
159:
160: /**
161: * Template method used to write a node into the database.
162: *
163: * @param st Statement used to execute write statement.
164: * @param node Node to write.
165: */
166: protected void writeNode(Statement st, Node node) {
167: }
168:
169: /**
170: * Template method used to write an edge into the database.
171: *
172: * @param st Statement used to execute write statement.
173: * @param edge Edge to write.
174: */
175: protected void writeEdge(Statement st, Edge edge) {
176: }
177:
178: /**
179: * Template method which returns the query to execute in order to read
180: * a graph from the database.
181: *
182: * @return SQL query.
183: */
184: protected abstract String getQuery();
185:
186: /**
187: * Template method used to create the object represented by a tuple returned
188: * by the database query.
189: *
190: * @param rs ResultSet of query.
191: *
192: * @return Object represented by current tuple of result set.
193: */
194: protected abstract Object readInternal(ResultSet rs)
195: throws Exception;
196:
197: }
|