svn - r34364 - trunk/modules/library/jdbc/src/main/java/org/geotools/jdbc

1 message Options
Embed this post
Permalink
svn_geotools

svn - r34364 - trunk/modules/library/jdbc/src/main/java/org/geotools/jdbc

Reply Threaded More More options
Print post
Permalink
Author: jdeolive
Date: 2009-11-11 19:58:27 -0500 (Wed, 11 Nov 2009)
New Revision: 34364

Modified:
   trunk/modules/library/jdbc/src/main/java/org/geotools/jdbc/JDBCDataStore.java
Log:
GEOT-2825, relaxing jdbc class to sql type mapping

Modified: trunk/modules/library/jdbc/src/main/java/org/geotools/jdbc/JDBCDataStore.java
===================================================================
--- trunk/modules/library/jdbc/src/main/java/org/geotools/jdbc/JDBCDataStore.java 2009-11-12 00:58:15 UTC (rev 34363)
+++ trunk/modules/library/jdbc/src/main/java/org/geotools/jdbc/JDBCDataStore.java 2009-11-12 00:58:27 UTC (rev 34364)
@@ -32,11 +32,13 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.Map.Entry;
 import java.util.logging.Level;
 
 import javax.sql.DataSource;
@@ -488,6 +490,41 @@
         Integer mapping = getClassToSqlTypeMappings().get(clazz);
 
         if (mapping == null) {
+            //no match, try a "fuzzy" match in which we find the super class which matches best
+            List<Map.Entry<Class<?>, Integer>> matches = new ArrayList();
+            for (Map.Entry<Class<?>, Integer> e : getClassToSqlTypeMappings().entrySet()) {
+                if (e.getKey().isAssignableFrom(clazz) ) {
+                    matches.add(e);
+                }
+            }
+            if (!matches.isEmpty()) {
+                if (matches.size() == 1) {
+                    //single match, great, use it
+                    mapping = matches.get(0).getValue();
+                }
+                else {
+                    // sort to match lowest class in type hierarchy, if we end up with a list like:
+                    // A, B where B is a super class of A, then chose A since it is the closest
+                    // subclass to match
+                    
+                    Collections.sort(matches, new Comparator<Map.Entry<Class<?>, Integer>>() {
+                        public int compare(Entry<Class<?>, Integer> o1, Entry<Class<?>, Integer> o2) {
+                            if (o1.getKey().isAssignableFrom(o2.getKey())) {
+                                return 1;
+                            }
+                            if (o2.getKey().isAssignableFrom(o1.getKey())) {
+                                return -1;
+                            }
+                            return 0;
+                        }
+                    });
+                    if (matches.get(1).getKey().isAssignableFrom(matches.get(0).getKey())) {
+                        mapping = matches.get(0).getValue();
+                    }
+                }
+            }
+        }
+        if (mapping == null) {
             mapping = Types.OTHER;
             LOGGER.warning("No mapping for " + clazz.getName());
         }


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
GeoTools-commits mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-commits