Source Code Cross Referenced for DBReaderWriter.java in  » GIS » GeoTools-2.4.1 » org » geotools » graph » io » standard » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoTools 2.4.1 » org.geotools.graph.io.standard 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.