From 408ffc3fcfe1cf5af18e5e29e3779d2654b8aa51 Mon Sep 17 00:00:00 2001 From: cboucheIGN Date: Thu, 8 Sep 2022 11:25:43 +0200 Subject: [PATCH] validator-plugin-cnig: control at least one assiette/generateur file for SUP validation --- .../src/main/resources/error-code.json | 12 ++++ .../fr/ign/validator/cnig/CnigPlugin.java | 32 +++++++--- .../validator/cnig/error/CnigErrorCodes.java | 5 ++ .../cnig/model/DocumentModelName.java | 17 ++++++ .../document/AtLeastOneAssietteValidator.java | 59 +++++++++++++++++++ .../AtLeastOneGenerateurValidator.java | 59 +++++++++++++++++++ .../cnig/model/DocumentModelNameTest.java | 16 ++++- .../regress/CnigValidatorRegressTest.java | 11 ++++ 8 files changed, 199 insertions(+), 12 deletions(-) create mode 100644 validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneAssietteValidator.java create mode 100644 validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneGenerateurValidator.java diff --git a/validator-core/src/main/resources/error-code.json b/validator-core/src/main/resources/error-code.json index ad43d255..66eab41e 100644 --- a/validator-core/src/main/resources/error-code.json +++ b/validator-core/src/main/resources/error-code.json @@ -651,6 +651,18 @@ "message": "Le code de territoire '{EMPRISE}' est invalide dans le nom de dossier.", "documentation": "Les départements doivent être codés sur 3 caractères." }, + { + "name": "CNIG_GENERATEUR_SUP_NOT_FOUND", + "level": "ERROR", + "message": "Aucun générateur trouvée.", + "documentation": "Selon la catégorie de SUP des géométries sont autorisées, au moins une doit être présente." + }, + { + "name": "CNIG_ASSIETTE_SUP_NOT_FOUND", + "level": "ERROR", + "message": "Aucune assiette trouvée.", + "documentation": "Selon la catégorie de SUP des géométries sont autorisées, au moins une doit être présente." + }, { "name": "DGPR_DOCUMENT_PREFIX_ERROR", "level": "WARNING", diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java index 20fb70e6..9973fba8 100644 --- a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/CnigPlugin.java @@ -9,6 +9,8 @@ import fr.ign.validator.cnig.process.SupRelationsPostProcess; import fr.ign.validator.cnig.validation.attribute.GeometryComplexityValidator; import fr.ign.validator.cnig.validation.attribute.InseeValidator; +import fr.ign.validator.cnig.validation.document.AtLeastOneAssietteValidator; +import fr.ign.validator.cnig.validation.document.AtLeastOneGenerateurValidator; import fr.ign.validator.cnig.validation.document.AtLeastOneWritingMaterialValidator; import fr.ign.validator.cnig.validation.document.DocumentNameValidator; import fr.ign.validator.cnig.validation.metadata.CnigMetadataDateOfLastRevisionValidator; @@ -43,16 +45,23 @@ public void setup(Context context) { context.setNormalizeEnabled(true); /* - * Join SUP files to add a column "fichiers" and "nomsuplitt" (must run before - * CreateShapefilesPostProcess) + * PreProcess - Customize idurba + */ + context.addListener(new CustomizeIdurbaPreProcess()); + + /* + * PostProcess - Join SUP files to add a column "fichiers" and "nomsuplitt" + * (must run before CreateShapefilesPostProcess) */ context.addListener(new SupRelationsPostProcess()); /* - * Converts DATA/*.csv to DATA/*.shp (must follow ReferenceActeSupPostProcess) + * PostProcess- Converts DATA/*.csv to DATA/*.shp (must follow + * ReferenceActeSupPostProcess) */ context.addListener(new CreateShapefilesPostProcess()); /* - * Compute document.tag.typeref (must run before document-info.json generation) + * PostProcess - Compute document.tag.typeref (must run before + * document-info.json generation) */ context.addListenerBefore( new DocUrbaPostProcess(), @@ -60,22 +69,27 @@ public void setup(Context context) { ); /* - * Extends DOC_URBA_COM validation + * PostProcess - DOC_URBA_COM validation for PLUi */ context.addListener(new DocUrbaComPostProcess()); /* - * Extends PERIMETRE_SCOT validation + * PostProcess - Extends PERIMETRE_SCOT validation */ context.addListener(new PerimetreScotPostProcess()); + /* + * Extends document validation + */ + context.addListener(new DocumentNameValidator()); + context.addListener(new AtLeastOneWritingMaterialValidator()); + context.addListener(new AtLeastOneGenerateurValidator()); + context.addListener(new AtLeastOneAssietteValidator()); + /* * Extends attribute validation */ context.addListener(new InseeValidator()); - context.addListener(new AtLeastOneWritingMaterialValidator()); - context.addListener(new DocumentNameValidator()); - context.addListener(new CustomizeIdurbaPreProcess()); context.addListener(new GeometryComplexityValidator()); /* diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java index f18da6fd..8b86de92 100644 --- a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/error/CnigErrorCodes.java @@ -64,6 +64,11 @@ public class CnigErrorCodes { public static final ErrorCode CNIG_SUP_IDGEN_NOT_FOUND = ErrorCode.valueOf( "CNIG_SUP_IDGEN_NOT_FOUND" ); + /** + * SUP - reported when no generateur / assiette file found + */ + public static final ErrorCode CNIG_GENERATEUR_SUP_NOT_FOUND = ErrorCode.valueOf("CNIG_GENERATEUR_SUP_NOT_FOUND"); + public static final ErrorCode CNIG_ASSIETTE_SUP_NOT_FOUND = ErrorCode.valueOf("CNIG_ASSIETTE_SUP_NOT_FOUND"); public static final ErrorCode CNIG_METADATA_SPECIFICATION_NOT_FOUND = ErrorCode.valueOf( "CNIG_METADATA_SPECIFICATION_NOT_FOUND" diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/model/DocumentModelName.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/model/DocumentModelName.java index 66168128..b197948b 100644 --- a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/model/DocumentModelName.java +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/model/DocumentModelName.java @@ -14,6 +14,10 @@ public class DocumentModelName { private static final String REGEXP_YYYY = "[0-9]{4}"; + public static final String TYPE_SUP = "SUP"; + public static final String TYPE_SCOT = "SCOT"; + public static final String TYPE_PLU = "PLU"; + /** * Test if document model is a CNIG standard * @@ -38,6 +42,19 @@ public static String getDocumentType(String documentModelName) { return parts[1]; } + /** + * Check that document name belong to SUP type + * + * @param documentModelName + * @return + */ + public static boolean isDocumentModelSup(String documentModelName) { + if (getDocumentType(documentModelName) != null) { + return getDocumentType(documentModelName).toUpperCase().equals(TYPE_SUP); + } + return false; + } + /** * Get version * diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneAssietteValidator.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneAssietteValidator.java new file mode 100644 index 00000000..f05282b4 --- /dev/null +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneAssietteValidator.java @@ -0,0 +1,59 @@ +package fr.ign.validator.cnig.validation.document; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; + +import fr.ign.validator.Context; +import fr.ign.validator.ValidatorListener; +import fr.ign.validator.cnig.error.CnigErrorCodes; +import fr.ign.validator.cnig.model.DocumentModelName; +import fr.ign.validator.data.Document; +import fr.ign.validator.data.DocumentFile; +import fr.ign.validator.model.TableModel; +import fr.ign.validator.validation.Validator; + +public class AtLeastOneAssietteValidator implements Validator, ValidatorListener { + + public static final Logger log = LogManager.getRootLogger(); + public static final Marker MARKER = MarkerManager.getMarker("AtLeastOneAssietteValidator"); + + @Override + public void validate(Context context, Document document) { + if (!DocumentModelName.isDocumentModelSup(document.getDocumentModel().getName())) { + log.info(MARKER, "Skip control if document model is not a SUP."); + return; + } + log.info(MARKER, "Ensure that document contains at least one assiete file..."); + int count = 0; + for (DocumentFile documentFile : document.getDocumentFiles()) { + if (!(documentFile.getFileModel() instanceof TableModel)) { + continue; + } + if (documentFile.getFileModel().getName().contains("ASSIETTE")) { + count++; + } + } + log.info(MARKER, "Found {} ASSIETTE file(s).", count); + if (count == 0) { + context.report(CnigErrorCodes.CNIG_ASSIETTE_SUP_NOT_FOUND); + } + } + + @Override + public void beforeMatching(Context context, Document document) throws Exception { + + } + + @Override + public void beforeValidate(Context context, Document document) throws Exception { + document.getDocumentModel().addValidator(this); + } + + @Override + public void afterValidate(Context context, Document document) throws Exception { + + } + +} diff --git a/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneGenerateurValidator.java b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneGenerateurValidator.java new file mode 100644 index 00000000..285ccb7c --- /dev/null +++ b/validator-plugin-cnig/src/main/java/fr/ign/validator/cnig/validation/document/AtLeastOneGenerateurValidator.java @@ -0,0 +1,59 @@ +package fr.ign.validator.cnig.validation.document; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; + +import fr.ign.validator.Context; +import fr.ign.validator.ValidatorListener; +import fr.ign.validator.cnig.error.CnigErrorCodes; +import fr.ign.validator.cnig.model.DocumentModelName; +import fr.ign.validator.data.Document; +import fr.ign.validator.data.DocumentFile; +import fr.ign.validator.model.TableModel; +import fr.ign.validator.validation.Validator; + +public class AtLeastOneGenerateurValidator implements Validator, ValidatorListener { + + public static final Logger log = LogManager.getRootLogger(); + public static final Marker MARKER = MarkerManager.getMarker("AtLeastOneGenerateurValidator"); + + @Override + public void validate(Context context, Document document) { + if (!DocumentModelName.isDocumentModelSup(document.getDocumentModel().getName())) { + log.info(MARKER, "Skip control if document model is not a SUP."); + return; + } + log.info(MARKER, "Ensure that document contains at least one generateur file..."); + int count = 0; + for (DocumentFile documentFile : document.getDocumentFiles()) { + if (!(documentFile.getFileModel() instanceof TableModel)) { + continue; + } + if (documentFile.getFileModel().getName().contains("GENERATEUR")) { + count++; + } + } + log.info(MARKER, "Found {} GENERATEUR file(s).", count); + if (count == 0) { + context.report(CnigErrorCodes.CNIG_GENERATEUR_SUP_NOT_FOUND); + } + } + + @Override + public void beforeMatching(Context context, Document document) throws Exception { + + } + + @Override + public void beforeValidate(Context context, Document document) throws Exception { + document.getDocumentModel().addValidator(this); + } + + @Override + public void afterValidate(Context context, Document document) throws Exception { + + } + +} diff --git a/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/model/DocumentModelNameTest.java b/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/model/DocumentModelNameTest.java index cd06452b..2dd5e2ef 100644 --- a/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/model/DocumentModelNameTest.java +++ b/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/model/DocumentModelNameTest.java @@ -22,9 +22,19 @@ public void testIsCnigStandard() { public void testGetDocumentType() { assertNull(DocumentModelName.getDocumentType("cnig")); - assertEquals("SCOT", DocumentModelName.getDocumentType("cnig_SCOT_2013")); - assertEquals("PLU", DocumentModelName.getDocumentType("cnig_PLU_2013")); - assertEquals("SUP", DocumentModelName.getDocumentType("cnig_SUP_AC2_2013")); + assertEquals(DocumentModelName.TYPE_SCOT, DocumentModelName.getDocumentType("cnig_SCOT_2013")); + assertEquals(DocumentModelName.TYPE_PLU, DocumentModelName.getDocumentType("cnig_PLU_2013")); + assertEquals(DocumentModelName.TYPE_SUP, DocumentModelName.getDocumentType("cnig_SUP_AC2_2013")); + } + + @Test + public void testIsDocumentSup() { + assertFalse(DocumentModelName.isDocumentModelSup("cnig")); + + assertFalse(DocumentModelName.isDocumentModelSup("cnig_SCOT_2013")); + assertFalse(DocumentModelName.isDocumentModelSup("cnig_PLU_2013")); + assertTrue(DocumentModelName.isDocumentModelSup("cnig_SUP_AC2_2013")); + assertTrue(DocumentModelName.isDocumentModelSup("cnig_sup_ac2_2013")); } @Test diff --git a/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java b/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java index c4402139..ed2d48bb 100644 --- a/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java +++ b/validator-plugin-cnig/src/test/java/fr/ign/validator/cnig/regress/CnigValidatorRegressTest.java @@ -29,6 +29,9 @@ import fr.ign.validator.cnig.CnigRegressHelper; import fr.ign.validator.cnig.ReportAssert; import fr.ign.validator.cnig.error.CnigErrorCodes; +import fr.ign.validator.cnig.model.DocumentModelName; +import fr.ign.validator.cnig.model.DocumentName; +import fr.ign.validator.cnig.model.DocumentType; import fr.ign.validator.data.Document; import fr.ign.validator.error.CoreErrorCodes; import fr.ign.validator.error.ErrorLevel; @@ -659,6 +662,9 @@ public void test213000896_INT1_83044_20160130() throws Exception { */ Assert.assertEquals(StandardCharsets.UTF_8, context.getEncoding()); Assert.assertEquals("213000896_INT1_83044_20160130", document.getDocumentName()); + DocumentName documentName = new DocumentName(document.getDocumentName()); + Assert.assertEquals(DocumentType.SUP, documentName.getDocumentType()); + Assert.assertTrue(DocumentModelName.isDocumentModelSup(document.getDocumentModel().getName())); /* * check document-info.json @@ -682,6 +688,9 @@ public void test213000896_INT1_83044_20160130() throws Exception { } } + ReportAssert.assertCount(0, CnigErrorCodes.CNIG_GENERATEUR_SUP_NOT_FOUND, report); + ReportAssert.assertCount(0, CnigErrorCodes.CNIG_ASSIETTE_SUP_NOT_FOUND, report); + } /** @@ -717,6 +726,8 @@ public void test30014_PLU_20171013() throws Exception { ReportAssert.assertCount(0, CoreErrorCodes.DATABASE_CONSTRAINT_MISMATCH, report); ReportAssert.assertCount(1, CoreErrorCodes.TABLE_FOREIGN_KEY_NOT_FOUND, report); ReportAssert.assertCount(20, ErrorLevel.ERROR, report); + ReportAssert.assertCount(0, CnigErrorCodes.CNIG_GENERATEUR_SUP_NOT_FOUND, report); + ReportAssert.assertCount(0, CnigErrorCodes.CNIG_ASSIETTE_SUP_NOT_FOUND, report); { ValidatorError error = report.getErrorsByCode(CoreErrorCodes.TABLE_FOREIGN_KEY_NOT_FOUND).get(0);