From 0bae5a925de899f970c8fa8d03c8733a10a3b263 Mon Sep 17 00:00:00 2001
From: webrian <webrian@users.noreply.github.com>
Date: Mon, 27 May 2024 10:54:46 +0200
Subject: [PATCH] Support for parameter metaConfig (#162)

* [Ili2pgImportSchema] Add support for metaConfig

* [Ili2pgImportSchema] Remove remote dependencies in integration tests

* start-gretl.sh: use tagged version

---------

Co-authored-by: Stefan Ziegler <edi.gonzales@gmail.com>
---
 .../gretl/jobs/Ili2pgImportSchemaTest.java    | 54 +++++++++++++++++++
 .../SimpleTable.ili                           | 21 ++++++++
 .../build.gradle                              | 18 +++++++
 .../settings.gradle                           |  0
 .../simple_table_ini_20240502.ini             | 24 +++++++++
 .../SimpleTable.ili                           | 21 ++++++++
 .../build.gradle                              | 18 +++++++
 .../files/simple_table_ini_20240502.ini       | 24 +++++++++
 .../ilidata.xml                               | 53 ++++++++++++++++++
 .../settings.gradle                           |  0
 .../agi/gretl/tasks/Ili2pgImportSchema.java   | 21 ++++++--
 11 files changed, 250 insertions(+), 4 deletions(-)
 create mode 100644 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/SimpleTable.ili
 create mode 100755 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/build.gradle
 create mode 100644 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/settings.gradle
 create mode 100644 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/simple_table_ini_20240502.ini
 create mode 100644 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/SimpleTable.ili
 create mode 100755 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/build.gradle
 create mode 100644 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/files/simple_table_ini_20240502.ini
 create mode 100644 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/ilidata.xml
 create mode 100644 gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/settings.gradle

diff --git a/gretl/src/integrationTest/java/ch/so/agi/gretl/jobs/Ili2pgImportSchemaTest.java b/gretl/src/integrationTest/java/ch/so/agi/gretl/jobs/Ili2pgImportSchemaTest.java
index 225cccc2..0a87999a 100644
--- a/gretl/src/integrationTest/java/ch/so/agi/gretl/jobs/Ili2pgImportSchemaTest.java
+++ b/gretl/src/integrationTest/java/ch/so/agi/gretl/jobs/Ili2pgImportSchemaTest.java
@@ -113,4 +113,58 @@ public void schemaImport_Options1_Ok() throws Exception {
         }
     }
 
+    @Test
+    public void schemaImport_MetaConfigIliData_Ok() throws Exception {
+        Connection con = null;
+        try {
+            GradleVariable[] gvs = {GradleVariable.newGradleProperty(IntegrationTestUtilSql.VARNAME_PG_CON_URI, postgres.getJdbcUrl())};
+            IntegrationTestUtil.runJob("src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData", gvs);
+
+            // check results
+            con = IntegrationTestUtilSql.connectPG(postgres);
+            Statement s = con.createStatement();
+            ResultSet rs = s.executeQuery("SELECT setting FROM simple_table_ilidata.t_ili2db_settings WHERE tag ILIKE 'ch.ehi.ili2db.metaConfigFileName'");
+
+            if(!rs.next()) {
+                fail();
+            }
+
+            assertTrue(rs.getString(1).contains("ilidata:metaconfig_simple_table_ini_20240502"));
+
+            if(rs.next()) {
+                fail();
+            }
+
+        } finally {
+            IntegrationTestUtilSql.closeCon(con);
+        }
+    }
+
+    @Test
+    public void schemaImport_MetaConfigFile_Ok() throws Exception {
+        Connection con = null;
+        try {
+            GradleVariable[] gvs = {GradleVariable.newGradleProperty(IntegrationTestUtilSql.VARNAME_PG_CON_URI, postgres.getJdbcUrl())};
+            IntegrationTestUtil.runJob("src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile", gvs);
+
+            // check results
+            con = IntegrationTestUtilSql.connectPG(postgres);
+            Statement s = con.createStatement();
+            ResultSet rs = s.executeQuery("SELECT setting FROM simple_table_metaconfigfile.t_ili2db_settings WHERE tag ILIKE 'ch.ehi.ili2db.metaConfigFileName'");
+
+            if(!rs.next()) {
+                fail();
+            }
+
+            assertTrue(rs.getString(1).contains("simple_table_ini_20240502.ini"));
+
+            if(rs.next()) {
+                fail();
+            }
+
+        } finally {
+            IntegrationTestUtilSql.closeCon(con);
+        }
+    }
+
 }
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/SimpleTable.ili b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/SimpleTable.ili
new file mode 100644
index 00000000..76e35c36
--- /dev/null
+++ b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/SimpleTable.ili
@@ -0,0 +1,21 @@
+INTERLIS 2.3;
+
+MODEL SimpleTable (de)
+AT "mailto:agi@bd.so.ch"
+VERSION "2024-05-02"  =
+
+  DOMAIN
+
+    LKoord = COORD 2460000.000 .. 2870000.000 [INTERLIS.M], 1045000.000 .. 1310000.000 [INTERLIS.M] ,ROTATION 2 -> 1;
+
+  TOPIC SimpleTopic =
+
+    CLASS Table =
+      Anzahl : MANDATORY 1 .. 1000;
+      Bezeichnung : MANDATORY MTEXT*64;
+      Geometrie : MANDATORY SimpleTable.LKoord;
+    END Table;
+
+  END SimpleTopic;
+
+END SimpleTable.
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/build.gradle b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/build.gradle
new file mode 100755
index 00000000..d32a5bd6
--- /dev/null
+++ b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/build.gradle
@@ -0,0 +1,18 @@
+import ch.so.agi.gretl.tasks.*
+
+apply plugin: 'ch.so.agi.gretl'
+
+def GRETLTEST_DBURI = 'gretltest_dburi_pg'
+def db_uri = findProperty(GRETLTEST_DBURI) != null ? findProperty(GRETLTEST_DBURI) : 'jdbc:postgresql://localhost:5432/gretl'
+
+def db_user = "ddluser"
+def db_pass = "ddluser"
+
+defaultTasks 'ili2pgschemaimport'
+
+task ili2pgschemaimport(type: Ili2pgImportSchema){
+    database = [db_uri, db_user, db_pass]
+    dbschema = "simple_table_metaconfigfile"
+    modeldir = "%ILI_FROM_DB;" + rootProject.projectDir.toString() + ";https://geo.so.ch/models;http://models.interlis.ch"
+    metaConfig = "simple_table_ini_20240502.ini"
+}
\ No newline at end of file
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/settings.gradle b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/settings.gradle
new file mode 100644
index 00000000..e69de29b
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/simple_table_ini_20240502.ini b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/simple_table_ini_20240502.ini
new file mode 100644
index 00000000..e062f691
--- /dev/null
+++ b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigFile/simple_table_ini_20240502.ini
@@ -0,0 +1,24 @@
+[ch.ehi.ili2db]
+models = SimpleTable
+beautifyEnumDispName = True
+coalesceArray = True
+coalesceCatalogueRef = True
+coalesceMultiLine = True
+coalesceMultiPoint = True
+coalesceMultiSurface = True
+createBasketCol = False
+createEnumTabs = True
+createEnumTabsWithId = False
+createNumChecks = False
+createUnique = False
+createFk = True
+createFkIdx = True
+createMetaInfo = True
+createTidCol = True
+createTypeConstraint = False
+defaultSrsAuth = EPSG
+defaultSrsCode = 2056
+expandMultilingual = True
+smart2Inheritance = True
+sqlEnableNull = True
+strokeArcs = True
\ No newline at end of file
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/SimpleTable.ili b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/SimpleTable.ili
new file mode 100644
index 00000000..76e35c36
--- /dev/null
+++ b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/SimpleTable.ili
@@ -0,0 +1,21 @@
+INTERLIS 2.3;
+
+MODEL SimpleTable (de)
+AT "mailto:agi@bd.so.ch"
+VERSION "2024-05-02"  =
+
+  DOMAIN
+
+    LKoord = COORD 2460000.000 .. 2870000.000 [INTERLIS.M], 1045000.000 .. 1310000.000 [INTERLIS.M] ,ROTATION 2 -> 1;
+
+  TOPIC SimpleTopic =
+
+    CLASS Table =
+      Anzahl : MANDATORY 1 .. 1000;
+      Bezeichnung : MANDATORY MTEXT*64;
+      Geometrie : MANDATORY SimpleTable.LKoord;
+    END Table;
+
+  END SimpleTopic;
+
+END SimpleTable.
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/build.gradle b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/build.gradle
new file mode 100755
index 00000000..372537ae
--- /dev/null
+++ b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/build.gradle
@@ -0,0 +1,18 @@
+import ch.so.agi.gretl.tasks.*
+
+apply plugin: 'ch.so.agi.gretl'
+
+def GRETLTEST_DBURI = 'gretltest_dburi_pg'
+def db_uri = findProperty(GRETLTEST_DBURI) != null ? findProperty(GRETLTEST_DBURI) : 'jdbc:postgresql://localhost:5432/gretl'
+
+def db_user = "ddluser"
+def db_pass = "ddluser"
+
+defaultTasks 'ili2pgschemaimport'
+
+task ili2pgschemaimport(type: Ili2pgImportSchema){
+    database = [db_uri, db_user, db_pass]
+    dbschema = "simple_table_ilidata"
+    modeldir = "%ILI_FROM_DB;" + rootProject.projectDir.toString() + ";https://geo.so.ch/models;http://models.interlis.ch"
+    metaConfig = "ilidata:metaconfig_simple_table_ini_20240502"
+}
\ No newline at end of file
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/files/simple_table_ini_20240502.ini b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/files/simple_table_ini_20240502.ini
new file mode 100644
index 00000000..e062f691
--- /dev/null
+++ b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/files/simple_table_ini_20240502.ini
@@ -0,0 +1,24 @@
+[ch.ehi.ili2db]
+models = SimpleTable
+beautifyEnumDispName = True
+coalesceArray = True
+coalesceCatalogueRef = True
+coalesceMultiLine = True
+coalesceMultiPoint = True
+coalesceMultiSurface = True
+createBasketCol = False
+createEnumTabs = True
+createEnumTabsWithId = False
+createNumChecks = False
+createUnique = False
+createFk = True
+createFkIdx = True
+createMetaInfo = True
+createTidCol = True
+createTypeConstraint = False
+defaultSrsAuth = EPSG
+defaultSrsCode = 2056
+expandMultilingual = True
+smart2Inheritance = True
+sqlEnableNull = True
+strokeArcs = True
\ No newline at end of file
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/ilidata.xml b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/ilidata.xml
new file mode 100644
index 00000000..b2e03fe9
--- /dev/null
+++ b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/ilidata.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?><TRANSFER xmlns="http://www.interlis.ch/INTERLIS2.3">
+    <HEADERSECTION SENDER="SOGIS-INTERLIS-REPOSITORY-CREATOR" VERSION="2.3">
+        <MODELS>
+            <MODEL NAME="DatasetIdx16" VERSION="2022-10-10" URI="mailto:ce@eisenhutinformatik.ch"></MODEL>
+        </MODELS>
+    </HEADERSECTION>
+    <DATASECTION>
+        <DatasetIdx16.DataIndex BID="b1">
+            <DatasetIdx16.DataIndex.DatasetMetadata TID="037f8229-ac14-42de-a299-81038f229790">
+                <id>metaconfig_simple_table_ini_20240502</id>
+                <version>2024-02-18</version>
+                <publishingDate>2024-02-18</publishingDate>
+                <owner>mailto:bjsvwweb</owner>
+                <title>
+                    <DatasetIdx16.MultilingualText>
+                        <LocalisedText>
+                            <DatasetIdx16.LocalisedText>
+                                <Text>simple_table_ini_20240502</Text>
+                            </DatasetIdx16.LocalisedText>
+                        </LocalisedText>
+                    </DatasetIdx16.MultilingualText>
+                </title>
+                <shortDescription>
+                    <DatasetIdx16.MultilingualMText>
+                        <LocalisedText>
+                            <DatasetIdx16.LocalisedMText>
+                                <Text>Metaconfig file for simple_table_ini_20240502 - ('simple_table_ini_20240502', '.ini')</Text>
+                            </DatasetIdx16.LocalisedMText>
+                        </LocalisedText>
+                    </DatasetIdx16.MultilingualMText>
+                </shortDescription>
+                <categories>
+                    <DatasetIdx16.Code_>
+                        <value>http://codes.interlis.ch/type/metaconfig</value>
+                    </DatasetIdx16.Code_>
+                    <DatasetIdx16.Code_>
+                        <value>http://codes.interlis.ch/model/SO_AFU_Schutzbauten_20231212</value>
+                    </DatasetIdx16.Code_>
+                </categories>
+                <files>
+                    <DatasetIdx16.DataFile>
+                        <fileFormat>text/plain</fileFormat>
+                        <file>
+                            <DatasetIdx16.File>
+                                <path>files/simple_table_ini_20240502.ini</path>
+                            </DatasetIdx16.File>
+                        </file>
+                    </DatasetIdx16.DataFile>
+                </files>
+            </DatasetIdx16.DataIndex.DatasetMetadata>
+        </DatasetIdx16.DataIndex>
+    </DATASECTION>
+</TRANSFER>
\ No newline at end of file
diff --git a/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/settings.gradle b/gretl/src/integrationTest/jobs/Ili2pgImportSchema_MetaConfigIliData/settings.gradle
new file mode 100644
index 00000000..e69de29b
diff --git a/gretl/src/main/java/ch/so/agi/gretl/tasks/Ili2pgImportSchema.java b/gretl/src/main/java/ch/so/agi/gretl/tasks/Ili2pgImportSchema.java
index 03ca2181..ce92d173 100644
--- a/gretl/src/main/java/ch/so/agi/gretl/tasks/Ili2pgImportSchema.java
+++ b/gretl/src/main/java/ch/so/agi/gretl/tasks/Ili2pgImportSchema.java
@@ -79,10 +79,10 @@ public class Ili2pgImportSchema extends Ili2pgAbstractTask {
     public boolean expandMultilingual = false;
     @Input
     @Optional
-    public boolean coalesceJson = false;    
+    public boolean coalesceJson = false;
     @Input
     @Optional
-    public boolean coalesceArray = false; 
+    public boolean coalesceArray = false;
     @Input
     @Optional
     public boolean createTypeConstraint = false;
@@ -158,6 +158,9 @@ public class Ili2pgImportSchema extends Ili2pgAbstractTask {
     @Input
     @Optional
     public boolean createMetaInfo = false;
+    @Input
+    @Optional
+    public String metaConfig = null;
 
     @TaskAction
     public void importSchema() {
@@ -174,12 +177,12 @@ public void importSchema() {
             }
         }
         settings.setXtffile(iliFilename);
-        
+
         if (iliMetaAttrs != null) {
             String iliMetaAttrsFilename = this.getProject().file(iliMetaAttrs).getPath();
             settings.setIliMetaAttrsFile(iliMetaAttrsFilename);
         }
-        
+
         init(settings);
         run(function, settings);
     }
@@ -330,5 +333,15 @@ private void init(Config settings) {
         if (createMetaInfo) {
             settings.setCreateMetaInfo(true);
         }
+        if (metaConfig != null) {
+            String metaConfigFile = null;
+            if (metaConfig.startsWith("ilidata")) {
+                metaConfigFile = metaConfig;
+            } else {
+                java.io.File file = this.getProject().file(metaConfig);
+                metaConfigFile = file.getAbsolutePath();
+            }
+            settings.setMetaConfigFile(metaConfigFile);
+        }
     }
 }