Source Code Cross Referenced for PostGISAutoIncrementFIDMapper.java in  » GIS » GeoTools-2.4.1 » org » geotools » data » postgis » fidmapper » 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.data.postgis.fidmapper 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *    GeoTools - OpenSource mapping toolkit
003:         *    http://geotools.org
004:         *    (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005:         * 
006:         *    This library is free software; you can redistribute it and/or
007:         *    modify it under the terms of the GNU Lesser General Public
008:         *    License as published by the Free Software Foundation;
009:         *    version 2.1 of the License.
010:         *
011:         *    This library is distributed in the hope that it will be useful,
012:         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         *    Lesser General Public License for more details.
015:         */
016:        package org.geotools.data.postgis.fidmapper;
017:
018:        import java.io.IOException;
019:        import java.sql.Connection;
020:        import java.sql.ResultSet;
021:        import java.sql.Statement;
022:
023:        import org.geotools.data.jdbc.fidmapper.AutoIncrementFIDMapper;
024:        import org.geotools.data.jdbc.fidmapper.FIDMapper;
025:        import org.geotools.feature.Feature;
026:        import org.geotools.feature.IllegalAttributeException;
027:
028:        /**
029:         * Generate FID based on an auto increment function, the most stable approach for
030:         * use with editing.
031:         * 
032:         * @author Jesse Eichar, Refractions Research, Inc.
033:         * @author Cory Horner, Refractions Research, Inc.
034:         */
035:        public class PostGISAutoIncrementFIDMapper extends
036:                AutoIncrementFIDMapper implements  FIDMapper {
037:
038:            private static final long serialVersionUID = -6082930630426171079L;
039:
040:            /** Indicates that the pg_get_serial_sequence function exists, and works for this table */
041:            boolean can_usepg_get_serial_sequence = true;
042:
043:            /** Flag to indicate when we can't find the table's sequence */
044:            boolean hasSerialSequence = true;
045:
046:            /** The actual name of the sequence, if we have found it */
047:            String sequenceName = null;
048:
049:            public PostGISAutoIncrementFIDMapper(String tableSchemaName,
050:                    String tableName, String colName, int dataType) {
051:                super (tableSchemaName, tableName, colName, dataType);
052:            }
053:
054:            public PostGISAutoIncrementFIDMapper(String tableName,
055:                    String colName, int dataType,
056:                    boolean returnFIDColumnsAsAttributes) {
057:                super (tableName, colName, dataType);
058:                this .returnFIDColumnsAsAttributes = returnFIDColumnsAsAttributes;
059:            }
060:
061:            public String createID(Connection conn, Feature feature,
062:                    Statement statement) throws IOException {
063:                String id = retriveId(conn, feature, statement);
064:                if (id != null && returnFIDColumnsAsAttributes) {
065:                    // we have to udpate the attribute in the feature too
066:                    try {
067:                        feature.setAttribute(colNames[0], id);
068:                    } catch (IllegalAttributeException e) {
069:                        throw new IOException("Could not set generated key "
070:                                + id + " into attribute " + colNames[0]);
071:                    }
072:                }
073:                return id;
074:            }
075:
076:            /**
077:             * Attempts to determine the FID after it was inserted, using three techniques:
078:             * 1. SELECT currval(pg_get_serial_sequence(...))
079:             * 2. SELECT currval(sequence name) <-- using other methods to get name
080:             * 3. SELECT fid ... ORDER BY fid DESC LIMIT 1
081:             */
082:            public String retriveId(Connection conn, Feature feature,
083:                    Statement statement) throws IOException {
084:                ResultSet rs = null;
085:                if (can_usepg_get_serial_sequence) {
086:                    try {
087:                        //use pg_get_serial_sequence('"table name"','"column name"')
088:                        String sql = "SELECT currval(pg_get_serial_sequence('\"";
089:                        String schema = getTableSchemaName();
090:                        if (schema != null && !schema.equals("")) {
091:                            sql = sql + schema + "\".\"";
092:                        }
093:                        sql = sql + getTableName() + "\"','" + getColumnName()
094:                                + "'))";
095:                        rs = statement.executeQuery(sql);
096:                        if (rs.next() && rs.getString("currval") != null)
097:                            return rs.getString("currval");
098:                        else {
099:                            can_usepg_get_serial_sequence = false;
100:                        }
101:                    } catch (Exception e) {
102:                        can_usepg_get_serial_sequence = false;
103:                    } finally {
104:                        try {
105:                            if (rs != null)
106:                                rs.close();
107:                        } catch (Exception e) {
108:                            //oh well
109:                        }
110:                    }
111:                }
112:                //TODO: add logging
113:                if (hasSerialSequence) {
114:                    if (sequenceName == null) {
115:                        // try to find the sequence (this makes the assumption that
116:                        // the sequence name contains "tableName_columnName")
117:                        String sql = "SELECT relname FROM pg_catalog.pg_class WHERE relkind = 'S' AND relname LIKE '"
118:                                + getTableName()
119:                                + "_"
120:                                + getColumnName()
121:                                + "_seq'";
122:                        try {
123:                            rs = statement.executeQuery(sql);
124:                            if (rs.next() && rs.getString(1) != null) {
125:                                sequenceName = rs.getString(1);
126:                            } else {
127:                                hasSerialSequence = false;
128:                            }
129:                        } catch (Exception e) {
130:                            hasSerialSequence = false;
131:                        } finally {
132:                            try {
133:                                if (rs != null)
134:                                    rs.close();
135:                            } catch (Exception e) {
136:                                //oh well
137:                            }
138:                        }
139:                    }
140:
141:                    if (sequenceName != null) {
142:                        //get the sequence value
143:                        String sql = "SELECT currval('\"" + sequenceName
144:                                + "\"')";
145:                        try {
146:                            rs = statement.executeQuery(sql);
147:                            if (rs.next() && rs.getString("currval") != null)
148:                                return rs.getString("currval");
149:                            else {
150:                                hasSerialSequence = false;
151:                            }
152:                        } catch (Exception e) {
153:                            hasSerialSequence = false;
154:                        } finally {
155:                            try {
156:                                if (rs != null)
157:                                    rs.close();
158:                            } catch (Exception e) {
159:                                //oh well
160:                            }
161:                        }
162:                    }
163:                }
164:                return findInsertedFID(conn, feature, statement);
165:            }
166:
167:            /**
168:             * Our last resort method for getting the FID. 
169:             */
170:            private String findInsertedFID(Connection conn, Feature feature,
171:                    Statement statement) throws IOException {
172:                String sql = "SELECT \"" + getColumnName() + "\" FROM \"";
173:                String schema = getTableSchemaName();
174:                if (schema != null && !schema.equals("")) {
175:                    sql = sql + schema + "\".\"";
176:                }
177:                sql = sql + getTableName() + "\" ORDER BY \"" + getColumnName()
178:                        + "\" DESC LIMIT 1;";
179:                ResultSet rs = null;
180:                try {
181:                    statement.execute(sql);
182:                    rs = statement.getResultSet();
183:                    rs.next();
184:                    return rs.getString(getColumnName());
185:                } catch (Exception e) { //i surrender
186:                    return super .createID(conn, feature, statement);
187:                } finally {
188:                    try {
189:                        if (rs != null)
190:                            rs.close();
191:                    } catch (Exception e) {
192:                        //oh well
193:                    }
194:                }
195:            }
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.