]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-6949: Adding code that will create a uniqueness constraint on the shortidentif...
authorremillet <remillet@yahoo.com>
Thu, 5 May 2016 02:24:25 +0000 (19:24 -0700)
committerremillet <remillet@yahoo.com>
Thu, 5 May 2016 02:24:25 +0000 (19:24 -0700)
services/common/src/main/java/org/collectionspace/services/common/context/ServiceBindingUtils.java
services/common/src/main/java/org/collectionspace/services/common/init/AddIndices.java
services/config/src/main/resources/service.xsd

index 69c134c83aaca7b69eb312aca3bee539ed923984..84b8f2cc51feefb07c79f5b9390fd7ea5e7d1ea5 100644 (file)
@@ -36,6 +36,7 @@ public class ServiceBindingUtils {
        public static final String SERVICE_TYPE_AUTHORITY = "authority";
        public static final String SERVICE_TYPE_UTILITY = "utility";
        public static final String SERVICE_TYPE_SECURITY = "security";
+       public static final String SERVICE_COMMONPART_ID = "1";
        
        private static final String TENANT_EXTENSION_PATTERN = "(.*)"+ServiceContext.TENANT_SUFFIX+"[\\d]+$";
        private static final String TENANT_REPLACEMENT_PATTERN = "$1";
index 2f18d305b502d52aa149b5f0c68ee4cbf901cc84..f4c26f6ce0b6c2c66738aaaf08c35b7583d0faac 100644 (file)
@@ -26,12 +26,13 @@ import java.util.List;
 import javax.sql.DataSource;
 
 import org.collectionspace.services.common.api.Tools;
+import org.collectionspace.services.common.context.ServiceBindingUtils;
 import org.collectionspace.services.common.storage.DatabaseProductType;
 import org.collectionspace.services.common.storage.JDBCTools;
 import org.collectionspace.services.config.service.InitHandler.Params.Field;
 import org.collectionspace.services.config.service.InitHandler.Params.Property;
+import org.collectionspace.services.config.service.ObjectPartType;
 import org.collectionspace.services.config.service.ServiceBindingType;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -114,6 +115,47 @@ public class AddIndices extends InitHandler implements IInitHandler {
                 rows = addOneIndex(dataSourceName, repositoryName, cspaceInstanceId, tableName, fieldName);
             }
         }
+        //
+        // Add a uniqueness constraint on the short ID field of authority and authority item tables
+        //
+        if (sbt != null && sbt.isRequiresUniqueShortId()) {
+               ensureShortIdConstraintOnAuthority(dataSourceName,
+                       repositoryName,
+                       cspaceInstanceId,
+                       sbt);
+        }
+    }
+    
+    /**
+     * 
+     * Add a DB level uniqueness constraint on the short ID column of this service's common part table.
+     * 
+     * @param dataSourceName
+     * @param repositoryName
+     * @param cspaceInstanceId
+     * @param sbt
+     */
+    private void ensureShortIdConstraintOnAuthority(String dataSourceName,
+               String repositoryName,
+               String cspaceInstanceId,
+               ServiceBindingType sbt) {
+       
+//     #TEST:
+//             SELECT constraint_name FROM information_schema.constraint_column_usage WHERE table_name = 'persons_common' AND constraint_name = 'persons_shortid_unique';
+//
+//             #IF: emptyResult
+//                     ALTER TABLE persons_common add CONSTRAINT persons_shortid_unique UNIQUE (shortidentifier);
+       
+       String tableName = null;
+       
+        List<ObjectPartType> objectPartTypes = sbt.getObject().getPart();
+        for (ObjectPartType objectPartType : objectPartTypes) {
+               if (objectPartType.getId().equalsIgnoreCase(ServiceBindingUtils.SERVICE_COMMONPART_ID) == true) {
+                       tableName = objectPartType.getLabel();
+               }
+        }
+       System.out.println(String.format("Added uniqueness constraint on short ID of repo:%s%s service:%s tablename:%s",
+               repositoryName, cspaceInstanceId, sbt.getName(), tableName));
     }
 
     private int addOneIndex(String dataSourceName,
index a42c00fbe0a379485c5b02a801f348f61068c886..158c1bb3fa6af1bfc79e29615442ff290fc8112f 100644 (file)
@@ -61,6 +61,7 @@
         <xs:attribute name="type" type="xs:string" use="optional"/>
         <xs:attribute name="version" type="types:VersionType" use="required"/>
         <xs:attribute name="supportsReplicating" type="xs:boolean" default="false"/>
+        <xs:attribute name="requiresUniqueShortId" type="xs:boolean" default="false"/>
     </xs:complexType>
 
     <!--