You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2017/05/27 12:54:50 UTC

svn commit: r1796389 - in /sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql: MetadataSource.java MetadataWriter.java

Author: desruisseaux
Date: Sat May 27 12:54:50 2017
New Revision: 1796389

URL: http://svn.apache.org/viewvc?rev=1796389&view=rev
Log:
Support enumerations in the same way than code lists (both are controlled vocabularies).

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataSource.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataWriter.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataSource.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataSource.java?rev=1796389&r1=1796388&r2=1796389&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataSource.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataSource.java [UTF-8] Sat May 27 12:54:50 2017
@@ -45,6 +45,7 @@ import java.sql.SQLNonTransientException
 import java.sql.PreparedStatement;
 import org.opengis.annotation.UML;
 import org.opengis.util.CodeList;
+import org.opengis.util.ControlledVocabulary;
 import org.apache.sis.metadata.MetadataStandard;
 import org.apache.sis.metadata.KeyNamePolicy;
 import org.apache.sis.metadata.ValueExistencePolicy;
@@ -614,8 +615,8 @@ public class MetadataSource implements A
              * be present in the database in order to ensure foreigner key constraints, but
              * those tables are not used in any way by the org.apache.sis.metadata.sql package.
              */
-            if (metadata instanceof CodeList<?>) {
-                identifier = ((CodeList<?>) metadata).name();
+            if (metadata instanceof ControlledVocabulary) {
+                identifier = Types.getCodeName((ControlledVocabulary) metadata);
             } else {
                 final String table;
                 final Map<String,Object> asMap;
@@ -677,8 +678,8 @@ public class MetadataSource implements A
              * Note that if a metadata dependency is not found, we can stop the whole process immediately.
              */
             if (value != null) {
-                if (value instanceof CodeList<?>) {
-                    value = ((CodeList<?>) value).name();
+                if (value instanceof ControlledVocabulary) {
+                    value = Types.getCodeName((ControlledVocabulary) value);
                 } else {
                     String dependency = proxy(value);
                     if (dependency != null) {
@@ -788,11 +789,12 @@ public class MetadataSource implements A
 
     /**
      * Returns an implementation of the specified metadata interface filled with the data referenced
-     * by the specified identifier. Alternatively, this method can also return a {@link CodeList} element.
+     * by the specified identifier. Alternatively, this method can also return a {@link CodeList} or
+     * {@link Enum} element.
      *
      * @param  <T>         the parameterized type of the {@code type} argument.
      * @param  type        the interface to implement (e.g. {@link org.opengis.metadata.citation.Citation}),
-     *                     or the {@link CodeList} type.
+     *                     or the {@link ControlledVocabulary} type ({@link CodeList} or {@link Enum}).
      * @param  identifier  the identifier of the record for the metadata entity to be created.
      *                     This is usually the primary key of the record to search for.
      * @return an implementation of the required interface, or the code list element.
@@ -802,7 +804,7 @@ public class MetadataSource implements A
         ArgumentChecks.ensureNonNull("type", type);
         ArgumentChecks.ensureNonEmpty("identifier", identifier);
         Object value;
-        if (CodeList.class.isAssignableFrom(type)) {
+        if (ControlledVocabulary.class.isAssignableFrom(type)) {
             value = getCodeList(type, identifier);
         } else {
             final CacheKey key = new CacheKey(type, identifier);
@@ -974,9 +976,13 @@ public class MetadataSource implements A
      * Returns the code of the given type and name. This method is defined for avoiding the compiler warning
      * message when the actual class is unknown (it must have been checked dynamically by the caller however).
      */
-    @SuppressWarnings({"unchecked","rawtypes"})
-    private static CodeList<?> getCodeList(final Class<?> type, final String name) {
-        return Types.forCodeName((Class) type, name, true);
+    @SuppressWarnings("unchecked")
+    private static ControlledVocabulary getCodeList(final Class<?> type, final String name) {
+        if (type.isEnum()) {
+            return (ControlledVocabulary) Types.forEnumName(type.asSubclass(Enum.class), name);
+        } else {
+            return Types.forCodeName(type.asSubclass(CodeList.class), name, true);
+        }
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataWriter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataWriter.java?rev=1796389&r1=1796388&r2=1796389&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataWriter.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/sql/MetadataWriter.java [UTF-8] Sat May 27 12:54:50 2017
@@ -30,7 +30,6 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import javax.sql.DataSource;
 
-import org.opengis.util.CodeList;
 import org.opengis.metadata.Identifier;
 import org.opengis.metadata.citation.Citation;
 
@@ -39,6 +38,7 @@ import org.apache.sis.util.ArgumentCheck
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.resources.Messages;
 import org.apache.sis.util.iso.DefaultNameSpace;
+import org.apache.sis.util.iso.Types;
 import org.apache.sis.util.collection.Containers;
 import org.apache.sis.metadata.MetadataStandard;
 import org.apache.sis.metadata.KeyNamePolicy;
@@ -48,6 +48,9 @@ import org.apache.sis.metadata.TitleProp
 import org.apache.sis.metadata.iso.citation.Citations;
 import org.apache.sis.internal.metadata.sql.SQLBuilder;
 
+// Branch-dependent imports
+import org.opengis.util.ControlledVocabulary;
+
 
 /**
  * A connection to a metadata database with write capabilities. The database must have a schema of the given name,
@@ -185,8 +188,8 @@ public class MetadataWriter extends Meta
                 boolean success = false;
                 try {
                     try (Statement stmt = connection.createStatement()) {
-                        if (metadata instanceof CodeList<?>) {
-                            identifier = addCode(stmt, (CodeList<?>) metadata);
+                        if (metadata instanceof ControlledVocabulary) {
+                            identifier = addCode(stmt, (ControlledVocabulary) metadata);
                         } else {
                             identifier = add(stmt, metadata, new IdentityHashMap<>(), null);
                         }
@@ -306,7 +309,7 @@ public class MetadataWriter extends Meta
                  */
                 int maxLength = maximumValueLength;
                 Class<?> rt = colTypes.get(column);
-                if (CodeList.class.isAssignableFrom(rt) || standard.isMetadata(rt)) {
+                if (ControlledVocabulary.class.isAssignableFrom(rt) || standard.isMetadata(rt)) {
                     /*
                      * Found a reference to an other metadata. Remind that
                      * column for creating a foreign key constraint later.
@@ -388,8 +391,8 @@ public class MetadataWriter extends Meta
         for (final Map.Entry<String,Object> entry : asSingletons.entrySet()) {
             Object value = entry.getValue();
             final Class<?> type = value.getClass();
-            if (CodeList.class.isAssignableFrom(type)) {
-                value = addCode(stmt, (CodeList<?>) value);
+            if (ControlledVocabulary.class.isAssignableFrom(type)) {
+                value = addCode(stmt, (ControlledVocabulary) value);
             } else if (standard.isMetadata(type)) {
                 String dependency = proxy(value);
                 if (dependency == null) {
@@ -464,7 +467,7 @@ public class MetadataWriter extends Meta
             for (final Map.Entry<String,FKey> entry : foreigners.entrySet()) {
                 final FKey fkey = entry.getValue();
                 Class<?> rt = fkey.tableType;
-                final boolean isCodeList = CodeList.class.isAssignableFrom(rt);
+                final boolean isCodeList = ControlledVocabulary.class.isAssignableFrom(rt);
                 final String primaryKey;
                 if (isCodeList) {
                     primaryKey = CODE_COLUMN;
@@ -475,9 +478,9 @@ public class MetadataWriter extends Meta
                 final String column = entry.getKey();
                 final String target = getTableName(rt);
                 stmt.executeUpdate(helper.createForeignKey(
-                        schema(), fkey.tableName, column,               // Source (schema.table.column)
-                        target, primaryKey,                             // Target (table.column)
-                        !isCodeList));                                  // CASCADE if metadata, RESTRICT if CodeList.
+                        schema(), fkey.tableName, column,       // Source (schema.table.column)
+                        target, primaryKey,                     // Target (table.column)
+                        !isCodeList));                          // CASCADE if metadata, RESTRICT if CodeList or Enum.
                 /*
                  * In a classical object-oriented model, the constraint would be inherited by child tables.
                  * However this is not yet supported as of PostgreSQL 9.6. If inheritance is not supported,
@@ -624,9 +627,11 @@ public class MetadataWriter extends Meta
     /**
      * Adds a code list if it is not already present. This is used only in order to enforce
      * foreigner key constraints in the database. The value of CodeList tables are not used
-     * at parsing time.
+     * at parsing time. Enumerations are handled as if they were CodeLists; we do not use
+     * the native SQL {@code ENUM} type for making easier to add new values when a standard
+     * is updated.
      */
-    private String addCode(final Statement stmt, final CodeList<?> code) throws SQLException {
+    private String addCode(final Statement stmt, final ControlledVocabulary code) throws SQLException {
         assert Thread.holdsLock(this);
         final String table = getTableName(code.getClass());
         final Set<String> columns = getExistingColumns(table);
@@ -634,7 +639,7 @@ public class MetadataWriter extends Meta
             stmt.executeUpdate(createTable(table, CODE_COLUMN));
             columns.add(CODE_COLUMN);
         }
-        final String identifier = code.name();
+        final String identifier = Types.getCodeName(code);
         final String query = helper().clear().append("SELECT ").append(CODE_COLUMN)
                 .append(" FROM ").appendIdentifier(schema(), table).append(" WHERE ")
                 .append(CODE_COLUMN).appendCondition(identifier).toString();