From ffce3af309b06a18a5480ae19ee1e078834c0bbf Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Mon, 17 May 2021 11:11:53 +0200 Subject: [PATCH 01/15] feat: add parents / children relation for codes lists --- .../code_list/CodeListServiceImpl.java | 2 +- .../ConsultationGestionServiceImpl.java | 57 +++++++++++++++++-- .../consultation-gestion/getCodes.ftlh | 8 ++- .../consultation-gestion/getCodesList.ftlh | 4 +- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java index e3ed74a52..670836e54 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java @@ -84,7 +84,7 @@ public String getDetailedCodesList(String notation) throws RmesException { if(previousCode.has(Constants.PARENTS)){ parents = previousCode.getJSONArray(Constants.PARENTS); } - parents.put(tempCode.getString("parents")); + parents.put(tempCode.getString(Constants.PARENTS)); previousCode.put(Constants.PARENTS, parents); formattedCodes.put(code, previousCode); } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java index 3e7286268..7b221efa6 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Service; import java.util.HashMap; +import java.util.Iterator; @Service public class ConsultationGestionServiceImpl extends RdfService implements ConsultationGestionService { @@ -170,7 +171,7 @@ private void getStructureComponents(String id, JSONObject structure) throws Rmes JSONObject listCode = new JSONObject(); listCode.put("uri", component.getString("listeCodeUri")); - listCode.put("notation", component.getString("listeCodeNotation")); + listCode.put("id", component.getString("listeCodeNotation")); component.put("listCode", listCode); component.remove("listeCodeUri"); component.remove("listeCodeNotation"); @@ -255,14 +256,60 @@ private JSONArray getCodes(String notation) throws RmesException { JSONArray codes = repoGestion.getResponseAsArray(buildRequest("getCodes.ftlh", params)); + JSONObject childrenMapping = new JSONObject(); + + JSONObject formattedCodes = new JSONObject(); + for (int i = 0; i < codes.length(); i++) { JSONObject code = codes.getJSONObject(i); - code.put("label", this.formatLabel(code)); - code.remove("prefLabelLg1"); - code.remove("prefLabelLg2"); + + if(code.has("parents")){ + JSONArray children = new JSONArray(); + String parentCode = code.getString("parents"); + if(childrenMapping.has(parentCode)){ + children = childrenMapping.getJSONArray(parentCode); + } + children.put(code.get("code")); + childrenMapping.put(parentCode, children); + } + + + if(formattedCodes.has(code.getString("uri"))){ + JSONObject c = formattedCodes.getJSONObject(code.getString("uri")); + + if(code.has("parents")){ + JSONArray parents = c.getJSONArray("parents"); + parents.put(code.getString("parents")); + c.put("parents", parents); + } + } else { + code.put("label", this.formatLabel(code)); + code.remove("prefLabelLg1"); + code.remove("prefLabelLg2"); + + if(code.has("parents")){ + JSONArray parents = new JSONArray(); + parents.put(code.getString("parents")); + code.put("parents", parents); + } else { + code.put("parents", new JSONArray()); + } + formattedCodes.put(code.getString("uri"), code); + } } - return codes; + JSONArray result = new JSONArray(); + Iterator keys = formattedCodes.keys(); + + while(keys.hasNext()) { + String key = keys.next(); + JSONObject code = formattedCodes.getJSONObject(key); + if(childrenMapping.has(code.getString("code"))){ + code.put("enfants", childrenMapping.getJSONArray(code.getString("code"))); + } + result.put(code); + } + return result; } private JSONArray formatLabel(JSONObject obj) { diff --git a/src/main/resources/request/consultation-gestion/getCodes.ftlh b/src/main/resources/request/consultation-gestion/getCodes.ftlh index 18f49b73e..2ef434bbf 100644 --- a/src/main/resources/request/consultation-gestion/getCodes.ftlh +++ b/src/main/resources/request/consultation-gestion/getCodes.ftlh @@ -1,13 +1,17 @@ -SELECT ?uri ?notation ?prefLabelLg1 ?prefLabelLg2 +SELECT ?uri ?code ?prefLabelLg1 ?prefLabelLg2 ?parents FROM <${CODELIST_GRAPH}> WHERE { ?uriListe rdf:type skos:ConceptScheme . ?uriListe skos:notation "${NOTATION}" . ?uri rdf:type skos:Concept . ?uri skos:inScheme ?uriListe . - ?uri skos:notation ?notation . + ?uri skos:notation ?code . ?uri skos:prefLabel ?prefLabelLg1 . FILTER (lang(?prefLabelLg1) = '${LG1}') . ?uri skos:prefLabel ?prefLabelLg2 . FILTER (lang(?prefLabelLg2) = '${LG2}') . + OPTIONAL { + ?uri skos:broader ?parentsParent . + ?parentsParent skos:notation ?parents + } . } \ No newline at end of file diff --git a/src/main/resources/request/consultation-gestion/getCodesList.ftlh b/src/main/resources/request/consultation-gestion/getCodesList.ftlh index e77faf4f6..0ea3603df 100644 --- a/src/main/resources/request/consultation-gestion/getCodesList.ftlh +++ b/src/main/resources/request/consultation-gestion/getCodesList.ftlh @@ -1,9 +1,9 @@ -SELECT ?uri ?notation ?prefLabelLg1 ?prefLabelLg2 ?dateCréation ?dateMiseAJour ?dateFinValidité ?statutValidation +SELECT ?uri ?id ?prefLabelLg1 ?prefLabelLg2 ?dateCréation ?dateMiseAJour ?dateFinValidité ?statutValidation FROM <${CODELIST_GRAPH}> WHERE { ?uri rdf:type skos:ConceptScheme . ?uri skos:notation "${NOTATION}" . - BIND("${NOTATION}" AS ?notation) . + BIND("${NOTATION}" AS ?id) . ?uri skos:prefLabel ?prefLabelLg1 . FILTER (lang(?prefLabelLg1) = '${LG1}') . ?uri skos:prefLabel ?prefLabelLg2 . From 6a7816fff918b47b6eff3d56d476caf89998562d Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Mon, 17 May 2021 13:50:49 +0200 Subject: [PATCH 02/15] feat: solve issue when fetching attachment --- .../structures/utils/StructureUtils.java | 36 ++++++++++--------- .../structures/StructureQueries.java | 3 +- .../request/structures/getAttachment.ftlh | 7 ++-- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtils.java index 7d7c38e5c..55f57adee 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtils.java @@ -80,7 +80,7 @@ public JSONObject formatStructure(JSONObject structure, String id) throws RmesEx for (int i = 0; i < componentDefinitionsFlat.length(); i++) { JSONObject componentDefinitionFlat = componentDefinitionsFlat.getJSONObject(i); - JSONArray attachmentsArray = repoGestion.getResponseAsArray(StructureQueries.getStructuresAttachments(componentDefinitionFlat.getString(COMPONENT_DEFINITION_ID))); + JSONArray attachmentsArray = repoGestion.getResponseAsArray(StructureQueries.getStructuresAttachments(id, componentDefinitionFlat.getString(COMPONENT_DEFINITION_ID))); List attachments = new ArrayList<>(); for(int j = 0; j < attachmentsArray.length(); j++){ @@ -250,12 +250,12 @@ public void createRdfStructure(Structure structure, String structureId, IRI stru RdfUtils.addTripleString(structureIri, DC.CONTRIBUTOR, structure.getContributor(), model, graph); RdfUtils.addTripleUri(structureIri, INSEE.DISSEMINATIONSTATUS, structure.getDisseminationStatus(), model, graph); - createRdfComponentSpecifications(structure, structureIri, structure.getComponentDefinitions(), model, graph); - repoGestion.loadSimpleObject(structureIri, model, null); + + createRdfComponentSpecifications(structureIri, structure.getComponentDefinitions(), graph); } - public void createRdfComponentSpecifications(Structure structure, IRI structureIRI, List componentList, Model model, Resource graph) throws RmesException { + public void createRdfComponentSpecifications(IRI structureIRI, List componentList, Resource graph) throws RmesException { for (int i = 0; i < componentList.size(); i++) { ComponentDefinition componentDefinition = componentList.get(i); MutualizedComponent component = componentDefinition.getComponent(); @@ -273,7 +273,7 @@ public void createRdfComponentSpecifications(Structure structure, IRI structureI } componentDefinition.setModified(DateUtils.getCurrentDate()); componentDefinition.setId("cs" + (1000 + i) ); - createRdfComponentSpecification(structureIRI, model, componentDefinition, graph); + createRdfComponentSpecification(structureIRI, componentDefinition, graph); } } @@ -282,7 +282,8 @@ public void createMutualizedComponent(MutualizedComponent component) throws Rmes component.setId(id); } - public void createRdfComponentSpecification(IRI structureIRI, Model model, ComponentDefinition componentDefinition, Resource graph) { + public void createRdfComponentSpecification(IRI structureIRI, ComponentDefinition componentDefinition, Resource graph) throws RmesException { + Model model = new LinkedHashModel(); IRI componentSpecificationIRI; @@ -304,29 +305,30 @@ public void createRdfComponentSpecification(IRI structureIRI, Model model, Compo model.add(componentSpecificationIRI, QB.ORDER, RdfUtils.setLiteralInt(componentDefinition.getOrder()), graph); } - - for(String attachment : componentDefinition.getAttachment()){ - IRI attachmentIRI ; - try { - attachmentIRI = RdfUtils.createIRI(attachment); - } catch (Exception e){ - attachmentIRI = RdfUtils.structureComponentMeasureIRI(attachment); - } - - model.add(componentSpecificationIRI, QB.COMPONENT_ATTACHMENT, attachmentIRI, graph); - } MutualizedComponent component = componentDefinition.getComponent(); if (component.getType().equals(((SimpleIRI)QB.DIMENSION_PROPERTY).toString())) { model.add(componentSpecificationIRI, QB.DIMENSION, getDimensionIRI(component.getId()), graph); } if (component.getType().equals(((SimpleIRI)QB.ATTRIBUTE_PROPERTY).toString())) { + for(String attachment : componentDefinition.getAttachment()){ + IRI attachmentIRI ; + try { + attachmentIRI = RdfUtils.createIRI(attachment); + } catch (Exception e){ + attachmentIRI = RdfUtils.structureComponentMeasureIRI(attachment); + } + + model.add(componentSpecificationIRI, QB.COMPONENT_ATTACHMENT, attachmentIRI, graph); + } + model.add(componentSpecificationIRI, QB.ATTRIBUTE, getAttributeIRI(component.getId()), graph); model.add(componentSpecificationIRI, QB.COMPONENT_REQUIRED, RdfUtils.setLiteralBoolean(componentDefinition.getRequired()), graph); } if (component.getType().equals(((SimpleIRI)QB.MEASURE_PROPERTY).toString())) { model.add(componentSpecificationIRI, QB.MEASURE, getMeasureIRI(component.getId()), graph); } + repoGestion.loadSimpleObject(componentSpecificationIRI, model); } public IRI getComponentDefinitionIRI(String structureIRI, String componentDefinitionId) { diff --git a/src/main/java/fr/insee/rmes/persistance/sparql_queries/structures/StructureQueries.java b/src/main/java/fr/insee/rmes/persistance/sparql_queries/structures/StructureQueries.java index 780265e46..e0f97fdbe 100644 --- a/src/main/java/fr/insee/rmes/persistance/sparql_queries/structures/StructureQueries.java +++ b/src/main/java/fr/insee/rmes/persistance/sparql_queries/structures/StructureQueries.java @@ -19,8 +19,9 @@ public static String getValidationStatus(String id) throws RmesException { params.put("id", id); return buildRequest("getValidationStatus.ftlh", params); } - public static String getStructuresAttachments(String id) throws RmesException { + public static String getStructuresAttachments(String structureId, String id) throws RmesException { HashMap params = initParams(); + params.put("STRUCTURE_ID", structureId); params.put("COMPONENT_SPECIFICATION_ID", id); return buildRequest("getAttachment.ftlh", params); } diff --git a/src/main/resources/request/structures/getAttachment.ftlh b/src/main/resources/request/structures/getAttachment.ftlh index b18e35e8d..4e1d88892 100644 --- a/src/main/resources/request/structures/getAttachment.ftlh +++ b/src/main/resources/request/structures/getAttachment.ftlh @@ -2,7 +2,8 @@ SELECT DISTINCT ?attachment FROM <${STRUCTURES_COMPONENTS_GRAPH}> FROM <${STRUCTURES_GRAPH}> WHERE { - ?structure dcterms:identifier "${COMPONENT_SPECIFICATION_ID}" ; - qb:componentAttachment ?attachment . - + ?structure dcterms:identifier "${STRUCTURE_ID}" ; + qb:component ?componentSpecification . + ?componentSpecification dcterms:identifier "${COMPONENT_SPECIFICATION_ID}" ; + qb:componentAttachment ?attachment . } \ No newline at end of file From 22588f47c8c54d742582566de82a8078e23aba8b Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Mon, 17 May 2021 17:03:37 +0200 Subject: [PATCH 03/15] Try to fix too many files open issue --- .../operations/documentations/DocumentationExport.java | 5 +++-- .../operations/documentations/documents/DocumentsUtils.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java index 9f56f3cb0..2c0509ca4 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java @@ -1,7 +1,6 @@ package fr.insee.rmes.bauhaus_services.operations.documentations; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -128,9 +127,11 @@ public Response export(String simsXML,String operationXML,String indicatorXML,St try { return Response.ok( (StreamingOutput) out -> { - InputStream input = new FileInputStream( finalPath.toFile() ); + InputStream input = Files.newInputStream( finalPath); IOUtils.copy(input, out); out.flush(); + input.close(); + out.close(); } ).header( "Content-Disposition", content ).build(); } catch ( Exception e ) { logger.error(e.getMessage()); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java index 8bcc430e0..c0265cd9e 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsUtils.java @@ -1,7 +1,6 @@ package fr.insee.rmes.bauhaus_services.operations.documentations.documents; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -579,9 +578,11 @@ public Response downloadDocumentFile(String id) throws RmesException { ContentDisposition content = ContentDisposition.type("attachment").fileName(fileName).build(); try { return Response.ok( (StreamingOutput) output -> { - InputStream input = new FileInputStream( path.toFile() ); + InputStream input = Files.newInputStream(path); IOUtils.copy(input, output); + input.close(); output.flush(); + output.close(); } ).header( "Content-Disposition", content ).build(); } catch ( Exception e ) { logger.error(e.getMessage()); From fcaf0e7146389698ecd1443a422a2b7fc14eda9f Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Mon, 17 May 2021 18:32:19 +0200 Subject: [PATCH 04/15] fix: use constants instead of strings --- .../ConsultationGestionServiceImpl.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java index 7b221efa6..b714c9a67 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java @@ -1,10 +1,12 @@ package fr.insee.rmes.bauhaus_services.consutation_gestion; +import fr.insee.rmes.bauhaus_services.Constants; import fr.insee.rmes.bauhaus_services.rdf_utils.FreeMarkerUtils; import fr.insee.rmes.bauhaus_services.rdf_utils.RdfService; import fr.insee.rmes.config.Config; import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.model.ValidationStatus; +import jdk.vm.ci.meta.Constant; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.stereotype.Service; @@ -263,9 +265,9 @@ private JSONArray getCodes(String notation) throws RmesException { for (int i = 0; i < codes.length(); i++) { JSONObject code = codes.getJSONObject(i); - if(code.has("parents")){ + if(code.has(Constants.PARENTS)){ JSONArray children = new JSONArray(); - String parentCode = code.getString("parents"); + String parentCode = code.getString(Constants.PARENTS); if(childrenMapping.has(parentCode)){ children = childrenMapping.getJSONArray(parentCode); } @@ -274,27 +276,27 @@ private JSONArray getCodes(String notation) throws RmesException { } - if(formattedCodes.has(code.getString("uri"))){ - JSONObject c = formattedCodes.getJSONObject(code.getString("uri")); + if(formattedCodes.has(code.getString(Constants.URI))){ + JSONObject c = formattedCodes.getJSONObject(code.getString(Constants.URI)); - if(code.has("parents")){ - JSONArray parents = c.getJSONArray("parents"); - parents.put(code.getString("parents")); - c.put("parents", parents); + if(code.has(Constants.PARENTS)){ + JSONArray parents = c.getJSONArray(Constants.PARENTS); + parents.put(code.getString(Constants.PARENTS)); + c.put(Constants.PARENTS, parents); } } else { code.put("label", this.formatLabel(code)); - code.remove("prefLabelLg1"); - code.remove("prefLabelLg2"); + code.remove(Constants.PREF_LABEL_LG1); + code.remove(Constants.PREF_LABEL_LG2); - if(code.has("parents")){ + if(code.has(Constants.PARENTS)){ JSONArray parents = new JSONArray(); - parents.put(code.getString("parents")); - code.put("parents", parents); + parents.put(code.getString(Constants.PARENTS)); + code.put(Constants.PARENTS, parents); } else { - code.put("parents", new JSONArray()); + code.put(Constants.PARENTS, new JSONArray()); } - formattedCodes.put(code.getString("uri"), code); + formattedCodes.put(code.getString(Constants.URI), code); } } From c6f66795359c9897abace387bd925383aab12f72 Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Tue, 18 May 2021 08:49:29 +0200 Subject: [PATCH 05/15] fix: remove unused import --- .../consutation_gestion/ConsultationGestionServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java index b714c9a67..113ff088f 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java @@ -6,7 +6,6 @@ import fr.insee.rmes.config.Config; import fr.insee.rmes.exceptions.RmesException; import fr.insee.rmes.model.ValidationStatus; -import jdk.vm.ci.meta.Constant; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.stereotype.Service; From 74cb9a628bb728540f401550fb6e46a63240a96b Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Tue, 18 May 2021 09:27:26 +0200 Subject: [PATCH 06/15] Close database connexion... --- .../concepts/publication/ConceptsPublication.java | 8 +++++++- .../documentations/DocumentationPublication.java | 1 + .../documents/DocumentsPublication.java | 2 ++ .../operations/families/FamilyPublication.java | 1 + .../indicators/IndicatorPublication.java | 1 + .../operations/OperationPublication.java | 1 + .../operations/series/SeriesPublication.java | 1 + .../rdf_utils/RepositoryGestion.java | 15 +++++---------- .../rdf_utils/RepositoryPublication.java | 10 +++++----- .../structures/utils/ComponentPublication.java | 1 + .../structures/utils/StructurePublication.java | 1 + 11 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/publication/ConceptsPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/publication/ConceptsPublication.java index dfa15a689..2a6d190dd 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/publication/ConceptsPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/publication/ConceptsPublication.java @@ -105,6 +105,7 @@ else if (predicat.endsWith("narrower")) { repoGestion.closeStatements(statements); publishMemberLinks(concept, model, con); + con.close(); Resource conceptToPublish = PublicationUtils.tranformBaseURIToPublish(concept); RepositoryPublication.publishConcept(conceptToPublish, model, noteToClear, topConceptOfToDelete); @@ -165,9 +166,11 @@ public void publishExplanatoryNotes(RepositoryConnection con, Resource note, Mod } model.add(PublicationUtils.tranformBaseURIToPublish(subject), XKOS.PLAIN_TEXT, plainText, graph); } catch (RepositoryException e) { + repoGestion.closeStatements(statements); throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), Constants.REPOSITORY_EXCEPTION); } + repoGestion.closeStatements(statements); } public void publishMemberLinks(Resource concept, Model model, RepositoryConnection conn) throws RmesException { @@ -175,6 +178,7 @@ public void publishMemberLinks(Resource concept, Model model, RepositoryConnecti try { statements = conn.getStatements(null, SKOS.MEMBER, concept, false); } catch (RepositoryException e) { + repoGestion.closeStatements(statements); throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), Constants.REPOSITORY_EXCEPTION); } try { @@ -184,9 +188,10 @@ public void publishMemberLinks(Resource concept, Model model, RepositoryConnecti PublicationUtils.tranformBaseURIToPublish((Resource) st.getObject()), st.getContext()); } } catch (RepositoryException e) { + repoGestion.closeStatements(statements); throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), Constants.REPOSITORY_EXCEPTION); } - } + repoGestion.closeStatements(statements); } public void publishCollection(JSONArray collectionsToValidate) throws RmesException { @@ -223,6 +228,7 @@ public void publishCollection(JSONArray collectionsToValidate) throws RmesExcept } } finally { repoGestion.closeStatements(statements); + con.close(); } Resource collectionToPublish = PublicationUtils.tranformBaseURIToPublish(collection); RepositoryPublication.publishResource(collectionToPublish, model, "collection"); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationPublication.java index 99afd991e..489bd59b9 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationPublication.java @@ -79,6 +79,7 @@ public void publishSims(String simsId) throws RmesException { finally { repoGestion.closeStatements(metadataReportStatements); + con.close(); } RepositoryPublication.publishContext(graph, model, "sims"); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsPublication.java index 81e7fd1e2..6d9f01079 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/documents/DocumentsPublication.java @@ -129,6 +129,7 @@ private Model getModelToPublish(String documentId, String filename) throws RmesE if (documentStatements != null) { repoGestion.closeStatements(documentStatements); } + con.close(); } return model; } @@ -207,6 +208,7 @@ private Model getLinkModelToPublish(String linkId) throws RmesException { finally { repoGestion.closeStatements(linkStatements); + con.close(); } return model; } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/families/FamilyPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/families/FamilyPublication.java index ae7e48896..fe3f85983 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/families/FamilyPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/families/FamilyPublication.java @@ -66,6 +66,7 @@ public void publishFamily(String familyId) throws RmesException { } finally { repoGestion.closeStatements(statements); + con.close(); } Resource familyToPublishRessource = PublicationUtils.tranformBaseURIToPublish(family); RepositoryPublication.publishResource(familyToPublishRessource, model, Constants.FAMILY); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorPublication.java index dce935fcd..aa3a2d5db 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/indicators/IndicatorPublication.java @@ -73,6 +73,7 @@ public void publishIndicator(String indicatorId) throws RmesException { finally { repoGestion.closeStatements(statements); + con.close(); } Resource indicatorToPublishRessource = PublicationUtils.tranformBaseURIToPublish(indicator); RepositoryPublication.publishResource(indicatorToPublishRessource, model, "indicator"); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/operations/OperationPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/operations/OperationPublication.java index b0dd97d26..53d4682a0 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/operations/OperationPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/operations/OperationPublication.java @@ -72,6 +72,7 @@ public void publishOperation(String operationId) throws RmesException { } finally { repoGestion.closeStatements(statements); + con.close(); } Resource operationToPublishRessource = PublicationUtils.tranformBaseURIToPublish(operation); RepositoryPublication.publishResource(operationToPublishRessource, model, "operation"); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java index a942ee3d3..7ea39d942 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesPublication.java @@ -104,6 +104,7 @@ public void publishSeries(String seriesId) throws RmesException { } finally { repoGestion.closeStatements(statements); repoGestion.closeStatements(hasPartStatements); + con.close(); } Resource seriesToPublishRessource = PublicationUtils.tranformBaseURIToPublish(series); RepositoryPublication.publishResource(seriesToPublishRessource, model, "serie"); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryGestion.java b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryGestion.java index 7c7f6c543..7f14c5348 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryGestion.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryGestion.java @@ -159,8 +159,7 @@ public void closeStatements(RepositoryResult statements) throws RmesE public void loadConcept(IRI concept, Model model, List> notesToDeleteAndUpdate) throws RmesException { - try { - RepositoryConnection conn = REPOSITORY_GESTION.getConnection(); + try (RepositoryConnection conn = REPOSITORY_GESTION.getConnection() ){ // notes to delete for (IRI note : notesToDeleteAndUpdate.get(0)) { conn.remove(note, null, null); @@ -176,7 +175,7 @@ public void loadConcept(IRI concept, Model model, List> notesToDeleteA } catch (RepositoryException e) { throwsRmesException(e, "Failure load concept : " + concept); - } + } } /** @@ -239,8 +238,7 @@ public void replaceGraph(Resource graph, Model model, RepositoryConnection conn) } public void loadObjectWithReplaceLinks(IRI object, Model model) throws RmesException { - try { - RepositoryConnection conn = REPOSITORY_GESTION.getConnection(); + try (RepositoryConnection conn = REPOSITORY_GESTION.getConnection()){ clearReplaceLinks(object, conn); loadSimpleObject(object, model, conn); } catch (RepositoryException e) { @@ -304,8 +302,7 @@ private static void getStatementsAndRemove(Resource object, RepositoryConnection public void clearStructureNodeAndComponents(Resource structure) throws RmesException { List toRemove = new ArrayList<>(); - try { - RepositoryConnection conn = REPOSITORY_GESTION.getConnection(); + try (RepositoryConnection conn = REPOSITORY_GESTION.getConnection()){ RepositoryResult nodes = null; RepositoryResult specifications = null; nodes = conn.getStatements(structure, QB.COMPONENT, null, false); @@ -335,9 +332,7 @@ public void clearStructureNodeAndComponents(Resource structure) throws RmesExcep public void keepHierarchicalOperationLinks(Resource object, Model model) throws RmesException { List typeOfLink = Arrays.asList(DCTERMS.HAS_PART, DCTERMS.IS_PART_OF); - RepositoryConnection conn = null; - try { - conn = REPOSITORY_GESTION.getConnection(); + try (RepositoryConnection conn = REPOSITORY_GESTION.getConnection()){ getHierarchicalOperationLinksModel(object, model, typeOfLink, conn); } catch (RmesException e) { throw e; diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryPublication.java index 6ccca53e7..00737cfdb 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RepositoryPublication.java @@ -196,15 +196,14 @@ private static void publishContext(Resource context, Model model, String type, R } } - public static void clearStructureAndComponentForAllRepositories(Resource structure) { + public static void clearStructureAndComponentForAllRepositories(Resource structure) throws RmesException { clearStructureAndComponents(structure, REPOSITORY_PUBLICATION); clearStructureAndComponents(structure, REPOSITORY_PUBLICATION_INTERNE); } - public static void clearStructureAndComponents(Resource structure, Repository repository) { + public static void clearStructureAndComponents(Resource structure, Repository repository) throws RmesException { List toRemove = new ArrayList<>(); - try { - RepositoryConnection conn = repository.getConnection(); + try (RepositoryConnection conn = repository.getConnection()){ RepositoryResult nodes = null; RepositoryResult specifications = null; nodes = conn.getStatements(structure, QB.COMPONENT, null, false); @@ -223,12 +222,13 @@ public static void clearStructureAndComponents(Resource structure, Repository re try { RepositoryResult statements = conn.getStatements(res, null, null, false); conn.remove(statements); + statements.close(); } catch (RepositoryException e) { logger.error("RepositoryGestion Error {}", e.getMessage()); } }); } catch (RepositoryException e) { - new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), "Failure deletion : " + structure); + throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), "Failure deletion : " + structure); } } private static void clearConceptLinks(Resource concept, RepositoryConnection conn) throws RmesException { diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/ComponentPublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/ComponentPublication.java index 83511fff7..c1f11ee07 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/ComponentPublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/ComponentPublication.java @@ -58,6 +58,7 @@ public void publishComponent(Resource component, IRI type) throws RmesException } finally { repoGestion.closeStatements(statements); + con.close(); } Resource componentToPublishRessource = PublicationUtils.tranformBaseURIToPublish(component); RepositoryPublication.publishResource(componentToPublishRessource, model, ((SimpleIRI) type).toString()); diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructurePublication.java b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructurePublication.java index b29084902..f9ceacedf 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructurePublication.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructurePublication.java @@ -70,6 +70,7 @@ public void publish(Resource structure) throws RmesException { RepositoryConnection con = repoGestion.getConnection(); this.copyTriplet(structure, model, con, new String[]{"validationState", Constants.CREATOR, Constants.CONTRIBUTOR}); + con.close(); Resource structureToPublish = PublicationUtils.tranformBaseURIToPublish(structure); RepositoryPublication.clearStructureAndComponentForAllRepositories(structureToPublish); From 50d00c65496a3be202b1bf567dc7aa85e191ed52 Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Wed, 19 May 2021 15:54:51 +0200 Subject: [PATCH 07/15] Refactor for sonar --- .../documentations/DocumentationExport.java | 233 ++++++++++-------- .../documentations/DocumentationsUtils.java | 17 +- 2 files changed, 139 insertions(+), 111 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java index 2c0509ca4..4abaf01b7 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java @@ -11,6 +11,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.Map; import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; @@ -44,137 +45,151 @@ public class DocumentationExport { private static final Logger logger = LoggerFactory.getLogger(DocumentationExport.class); - - - public Response export(String simsXML,String operationXML,String indicatorXML,String seriesXML, - String organizationsXML, String codeListsXML, String targetType, - boolean includeEmptyMas, boolean lg1, boolean lg2, String goal) throws RmesException, IOException { + public Response export(Map xmlContent, String targetType, boolean includeEmptyMas, boolean lg1, + boolean lg2, String goal) throws RmesException { logger.debug("Begin To export documentation"); - String msdXML = documentationsUtils.buildShellSims(); - - String parametersXML = buildParams(lg1,lg2,includeEmptyMas,targetType); - File output = File.createTempFile(Constants.OUTPUT, ExportUtils.getExtension(Constants.XML)); - output.deleteOnExit(); - InputStream xslFileIS = getClass().getResourceAsStream("/xslTransformerFiles/sims2fodt.xsl"); + File output = null; + String fileName = "export.odt"; + ContentDisposition content = ContentDisposition.type("attachment").fileName(fileName).build(); + InputStream odtFileIS = null; + InputStream xslFileIS = null; InputStream zipToCompleteIS = null; - InputStream odtFileIS = null ; - - if(goal == Constants.GOAL_RMES){ - odtFileIS = getClass().getResourceAsStream("/xslTransformerFiles/rmesPatternContent.xml"); - zipToCompleteIS = getClass().getResourceAsStream("/xslTransformerFiles/toZipForRmes/export.zip"); - } - if(goal == Constants.GOAL_COMITE_LABEL){ - odtFileIS = getClass().getResourceAsStream("/xslTransformerFiles/labelPatternContent.xml"); - zipToCompleteIS = getClass().getResourceAsStream("/xslTransformerFiles/toZipForLabel/export.zip"); - } - - InputStream xslFile = getClass().getResourceAsStream("/xslTransformerFiles/sims2fodt.xsl"); - OutputStream osOutputFile = FileUtils.openOutputStream(output); - - InputStream odtFile = getClass().getResourceAsStream("/xslTransformerFiles/rmesPattern.fodt"); - PrintStream printStream= null; - - Path tempDir= Files.createTempDirectory("forExport"); - String fileName="export.odt"; - Path finalPath = Paths.get(tempDir.toString()+"/"+fileName); - - try{ - // prepare transformer - StreamSource xsrc = new StreamSource(xslFileIS); - TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl(); - transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - Transformer xsltTransformer = transformerFactory.newTransformer(xsrc); - - // Pass parameters in a file - addParameter ( xsltTransformer, "parametersFile", parametersXML,tempDir); - addParameter ( xsltTransformer, "simsFile", simsXML,tempDir); - addParameter ( xsltTransformer, "seriesFile", seriesXML,tempDir); - addParameter ( xsltTransformer, "operationFile", operationXML,tempDir); - addParameter ( xsltTransformer, "indicatorFile", indicatorXML,tempDir); - addParameter ( xsltTransformer, "msdFile", msdXML,tempDir); - addParameter ( xsltTransformer, "codeListsFile", codeListsXML,tempDir); - addParameter ( xsltTransformer, "organizationsFile", organizationsXML,tempDir); - + try { + xslFileIS = getClass().getResourceAsStream("/xslTransformerFiles/sims2fodt.xsl"); + + if (Constants.GOAL_RMES.equals(goal)) { + odtFileIS = getClass().getResourceAsStream("/xslTransformerFiles/rmesPatternContent.xml"); + zipToCompleteIS = getClass().getResourceAsStream("/xslTransformerFiles/toZipForRmes/export.zip"); + + } + if (Constants.GOAL_COMITE_LABEL.equals(goal)) { + odtFileIS = getClass().getResourceAsStream("/xslTransformerFiles/labelPatternContent.xml"); + zipToCompleteIS = getClass().getResourceAsStream("/xslTransformerFiles/toZipForLabel/export.zip"); + } + // prepare output - printStream = new PrintStream(osOutputFile); - // transformation - xsltTransformer.transform(new StreamSource(odtFileIS), new StreamResult(printStream)); + output = File.createTempFile(Constants.OUTPUT, ExportUtils.getExtension(Constants.XML)); + output.deleteOnExit(); + } catch (IOException ioe) { + logger.error(ioe.getMessage()); + } + + try (OutputStream osOutputFile = FileUtils.openOutputStream(output); + PrintStream printStream = new PrintStream(osOutputFile);) { + + Path tempDir = Files.createTempDirectory("forExport"); + Path finalPath = Paths.get(tempDir.toString() + "/" + fileName); - //create odt - Path contentPath = Paths.get(tempDir.toString()+"/content.xml"); - Files.copy(Paths.get(output.getAbsolutePath()), contentPath, - StandardCopyOption.REPLACE_EXISTING); - Path zipPath = Paths.get(tempDir.toString()+"/export.zip"); - Files.copy(zipToCompleteIS, zipPath, - StandardCopyOption.REPLACE_EXISTING); - FilesUtils.addFileToZipFolder(contentPath.toFile(),zipPath.toFile()); - Files.copy(zipPath, finalPath, - StandardCopyOption.REPLACE_EXISTING); - - } catch (TransformerException e) { - logger.error(e.getMessage()); + //Add two params to xmlContents + String msdXML = documentationsUtils.buildShellSims(); + xmlContent.put("msdFile", msdXML); + String parametersXML = buildParams(lg1, lg2, includeEmptyMas, targetType); + xmlContent.put("parametersFile", parametersXML); + + //transform + xsltTransform(xmlContent, odtFileIS, xslFileIS, printStream, tempDir); + + // create odt + createOdtFromXml(output, finalPath, zipToCompleteIS, tempDir); + + logger.debug("End To export documentation"); + + return Response.ok((StreamingOutput) out -> { + InputStream input = Files.newInputStream(finalPath); + IOUtils.copy(input, out); + out.flush(); + input.close(); + out.close(); + }).header("Content-Disposition", content).build(); + } catch (IOException | TransformerException e) { + throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), + e.getClass().getSimpleName()); } finally { - odtFileIS.close(); - xslFileIS.close(); - osOutputFile.close(); - printStream.close(); + try { + if (odtFileIS != null) + odtFileIS.close(); + if (xslFileIS != null) + xslFileIS.close(); + } catch (IOException ioe) { + logger.error(ioe.getMessage()); + } } - logger.debug("End To export documentation"); - - ContentDisposition content = ContentDisposition.type("attachment").fileName(fileName).build(); + } - try { - return Response.ok( (StreamingOutput) out -> { - InputStream input = Files.newInputStream( finalPath); - IOUtils.copy(input, out); - out.flush(); - input.close(); - out.close(); - } ).header( "Content-Disposition", content ).build(); - } catch ( Exception e ) { - logger.error(e.getMessage()); - throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), "Error downloading file"); - } + public void xsltTransform(Map xmlContent, InputStream odtFileIS, InputStream xslFileIS, + PrintStream printStream, Path tempDir) throws TransformerException { + // prepare transformer + StreamSource xsrc = new StreamSource(xslFileIS); + TransformerFactory transformerFactory = new net.sf.saxon.TransformerFactoryImpl(); + transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + Transformer xsltTransformer = transformerFactory.newTransformer(xsrc); + + // Pass parameters in a file to the transformer + xmlContent.forEach((paramName, xmlData) -> { + try { + addParameter(xsltTransformer, paramName, xmlData, tempDir); + } catch (RmesException e) { + logger.error(e.getMessageAndDetails2()); } + }); + // transformation + xsltTransformer.transform(new StreamSource(odtFileIS), new StreamResult(printStream)); + } + + private void createOdtFromXml(File output, Path finalPath, InputStream zipToCompleteIS, Path tempDir) + throws IOException { + Path contentPath = Paths.get(tempDir.toString() + "/content.xml"); + Files.copy(Paths.get(output.getAbsolutePath()), contentPath, StandardCopyOption.REPLACE_EXISTING); + Path zipPath = Paths.get(tempDir.toString() + "/export.zip"); + Files.copy(zipToCompleteIS, zipPath, StandardCopyOption.REPLACE_EXISTING); + FilesUtils.addFileToZipFolder(contentPath.toFile(), zipPath.toFile()); + Files.copy(zipPath, finalPath, StandardCopyOption.REPLACE_EXISTING); + } private String buildParams(Boolean lg1, Boolean lg2, Boolean includeEmptyMas, String targetType) { - String includeEmptyMasString=( Boolean.TRUE.equals(includeEmptyMas) ? "true" : "false"); - String parametersXML=""; - - parametersXML=parametersXML.concat(Constants.XML_OPEN_PARAMETERS_TAG); + String includeEmptyMasString = (Boolean.TRUE.equals(includeEmptyMas) ? "true" : "false"); + String parametersXML = ""; + + parametersXML = parametersXML.concat(Constants.XML_OPEN_PARAMETERS_TAG); - parametersXML=parametersXML.concat(Constants.XML_OPEN_LANGUAGES_TAG); - if(lg1) parametersXML=parametersXML.concat("1"); - if(lg2) parametersXML=parametersXML.concat("2"); - parametersXML=parametersXML.concat(Constants.XML_END_LANGUAGES_TAG); + parametersXML = parametersXML.concat(Constants.XML_OPEN_LANGUAGES_TAG); + if (Boolean.TRUE.equals(lg1)) + parametersXML = parametersXML.concat("1"); + if (Boolean.TRUE.equals(lg2)) + parametersXML = parametersXML.concat("2"); + parametersXML = parametersXML.concat(Constants.XML_END_LANGUAGES_TAG); - parametersXML=parametersXML.concat(Constants.XML_OPEN_INCLUDE_EMPTY_MAS_TAG); - parametersXML=parametersXML.concat(includeEmptyMasString); - parametersXML=parametersXML.concat(Constants.XML_END_INCLUDE_EMPTY_MAS_TAG); + parametersXML = parametersXML.concat(Constants.XML_OPEN_INCLUDE_EMPTY_MAS_TAG); + parametersXML = parametersXML.concat(includeEmptyMasString); + parametersXML = parametersXML.concat(Constants.XML_END_INCLUDE_EMPTY_MAS_TAG); - parametersXML=parametersXML.concat(Constants.XML_OPEN_TARGET_TYPE_TAG); - parametersXML=parametersXML.concat(targetType); - parametersXML=parametersXML.concat(Constants.XML_END_TARGET_TYPE_TAG); + parametersXML = parametersXML.concat(Constants.XML_OPEN_TARGET_TYPE_TAG); + parametersXML = parametersXML.concat(targetType); + parametersXML = parametersXML.concat(Constants.XML_END_TARGET_TYPE_TAG); - parametersXML=parametersXML.concat(Constants.XML_END_PARAMETERS_TAG); + parametersXML = parametersXML.concat(Constants.XML_END_PARAMETERS_TAG); return XMLUtils.encodeXml(parametersXML); } - private void addParameter (Transformer xsltTransformer, String paramName, String paramData, Path tempDir) throws IOException { + private void addParameter(Transformer xsltTransformer, String paramName, String paramData, Path tempDir) + throws RmesException { // Pass parameters in a file CopyOption[] options = { StandardCopyOption.REPLACE_EXISTING }; - Path tempFile = Files.createTempFile(tempDir, paramName,Constants.DOT_XML); - String absolutePath = tempFile.toFile().getAbsolutePath(); - InputStream is = IOUtils.toInputStream(paramData, StandardCharsets.UTF_8); - Files.copy(is, tempFile, options); - absolutePath = absolutePath.replace('\\', '/'); - xsltTransformer.setParameter(paramName, absolutePath); - } - - + try { + Path tempFile = Files.createTempFile(tempDir, paramName, Constants.DOT_XML); + String absolutePath = tempFile.toFile().getAbsolutePath(); + InputStream is = IOUtils.toInputStream(paramData, StandardCharsets.UTF_8); + Files.copy(is, tempFile, options); + absolutePath = absolutePath.replace('\\', '/'); + xsltTransformer.setParameter(paramName, absolutePath); + } catch (IOException e) { + throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), + "IOException - Can't create temp files for XSLT Transformer"); + } + } } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationsUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationsUtils.java index b0be9015a..4dedd9f57 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationsUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationsUtils.java @@ -2,7 +2,9 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import javax.ws.rs.core.Response; @@ -557,8 +559,19 @@ public Response exportMetadataReport(String id, Boolean includeEmptyMas, Boolean } codeListsXML=codeListsXML.concat(Constants.XML_END_CODELIST_TAG); - return docExport.export(simsXML,operationXML,indicatorXML,seriesXML, - organizationsXML,codeListsXML,targetType,includeEmptyMas,lg1,lg2,goal); + + + Map xmlContent = new HashMap<>(); + xmlContent.put("simsFile", simsXML); + xmlContent.put("seriesFile", seriesXML); + xmlContent.put("operationFile", operationXML); + xmlContent.put("indicatorFile", indicatorXML); + // xmlContent.put("msdFile", msdXML); + xmlContent.put("codeListsFile", codeListsXML); + xmlContent.put("organizationsFile", organizationsXML); + + + return docExport.export(xmlContent,targetType,includeEmptyMas,lg1,lg2,goal); } public MSD buildMSDFromJson(JSONArray jsonMsd) { From ae0a71308b7d94cd608e5b64ec38b30ac0149cc3 Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Wed, 19 May 2021 17:04:11 +0200 Subject: [PATCH 08/15] Refactor for sonar --- .../operations/series/SeriesUtils.java | 25 ++++++------------- .../structures/impl/StructureImpl.java | 8 +++--- .../utils/StructureComponentUtils.java | 22 ++++++++-------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java index e704cc233..94ddde797 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java @@ -244,27 +244,18 @@ private void createRdfSeries(Series series, IRI familyURI, ValidationStatus newS addCodeList(series.getAccrualPeriodicityList(), series.getAccrualPeriodicityCode(), DCTERMS.ACCRUAL_PERIODICITY, model, seriesURI); addOperationLinks(series.getSeeAlso(), RDFS.SEEALSO, model, seriesURI); - //addOperationLinks(series.getReplaces(), DCTERMS.REPLACES, model, seriesURI); List replaces = series.getReplaces(); - if (replaces != null) { - for (OperationsLink replace : replaces) { - if(!replace.isEmpty()) { - String replUri = ObjectType.getCompleteUriGestion(replace.getType(), replace.getId()); - addReplacesAndReplacedBy(model, RdfUtils.toURI(replUri), seriesURI); - } - } - } + replaces.stream().filter(repl -> !repl.isEmpty()).forEach(replace -> { + String replUri = ObjectType.getCompleteUriGestion(replace.getType(), replace.getId()); + addReplacesAndReplacedBy(model, RdfUtils.toURI(replUri), seriesURI); + }); List isReplacedBys = series.getIsReplacedBy(); - if (isReplacedBys != null) { - for (OperationsLink isRepl : isReplacedBys) { - if(!isRepl.isEmpty()) { - String isReplUri = ObjectType.getCompleteUriGestion(isRepl.getType(), isRepl.getId()); - addReplacesAndReplacedBy(model, seriesURI, RdfUtils.toURI(isReplUri)); - } - } - } + isReplacedBys.stream().filter(isRepl -> !isRepl.isEmpty()).forEach(isRepl -> { + String isReplUri = ObjectType.getCompleteUriGestion(isRepl.getType(), isRepl.getId()); + addReplacesAndReplacedBy(model, seriesURI, RdfUtils.toURI(isReplUri)); + }); if (familyURI != null) { //case CREATION : link series to family diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/structures/impl/StructureImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/structures/impl/StructureImpl.java index b5f042ec6..3c89b62d6 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/structures/impl/StructureImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/structures/impl/StructureImpl.java @@ -74,10 +74,10 @@ public String getStructureByIdWithDetails(String id) throws RmesException { if(type.equalsIgnoreCase(((SimpleIRI)QB.ATTRIBUTE_PROPERTY).toString())){ component.put("type", "attribute"); } - if(type.equalsIgnoreCase(((SimpleIRI)QB.MEASURE_PROPERTY).toString())){ + else if(type.equalsIgnoreCase(((SimpleIRI)QB.MEASURE_PROPERTY).toString())){ component.put("type", "measure"); } - if(type.equalsIgnoreCase(((SimpleIRI)QB.DIMENSION_PROPERTY).toString())){ + else if(type.equalsIgnoreCase(((SimpleIRI)QB.DIMENSION_PROPERTY).toString())){ component.put("type", "dimension"); } @@ -90,7 +90,7 @@ public String getStructureByIdWithDetails(String id) throws RmesException { try { codeList.put("codes", new JSONArray(this.codeListService.geCodesListByIRI(component.getString(Constants.CODELIST)))); } catch (RmesException e) { - logger.error("Cannot fetch code list of the structure " + id); + logger.error("Cannot fetch code list of the structure {}" , id); logger.error(e); } @@ -102,7 +102,7 @@ public String getStructureByIdWithDetails(String id) throws RmesException { JSONObject concept = repoGestion.getResponseAsObject(ConceptsQueries.conceptQueryForDetailStructure(component.getString(Constants.CONCEPT))); component.put(Constants.CONCEPT, concept); } catch (RmesException e) { - logger.error("Cannot fetch concept of the structure " + id); + logger.error("Cannot fetch concept of the structure {}" , id); logger.error(e); } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureComponentUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureComponentUtils.java index 690435dca..46062563d 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureComponentUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureComponentUtils.java @@ -289,24 +289,20 @@ public void deleteComponent(JSONObject component, String id, String type) throws public String publishComponent(JSONObject component) throws RmesException { - if(component.isNull("creator") || "".equals(component.getString("creator"))){ + if(jsonObjecthasPropertyNullOrEmpty(component, "creator")){ throw new RmesUnauthorizedException(ErrorCodes.COMPONENT_PUBLICATION_EMPTY_CREATOR, "The creator should not be empty", new JSONArray()); } - if(component.isNull("disseminationStatus") || "".equals(component.getString("disseminationStatus"))){ + if(jsonObjecthasPropertyNullOrEmpty(component, "disseminationStatus")){ throw new RmesUnauthorizedException(ErrorCodes.COMPONENT_PUBLICATION_EMPTY_STATUS, "The dissemination status should not be empty", new JSONArray()); } - if(!component.isNull("concept") && !"".equals(component.getString("concept"))){ - if(!repoGestion.getResponseAsBoolean(ConceptsQueries.isConceptValidated(component.getString("concept")))){ - throw new RmesUnauthorizedException(ErrorCodes.COMPONENT_PUBLICATION_VALIDATED_CONCEPT, "The concept should be validated", new JSONArray()); - } + if(!jsonObjecthasPropertyNullOrEmpty(component,"concept") && !repoGestion.getResponseAsBoolean(ConceptsQueries.isConceptValidated(component.getString("concept")))){ + throw new RmesUnauthorizedException(ErrorCodes.COMPONENT_PUBLICATION_VALIDATED_CONCEPT, "The concept should be validated", new JSONArray()); } - if(!component.isNull("codeList") && !"".equals(component.getString("codeList"))){ - if(!repoGestion.getResponseAsBoolean(CodeListQueries.isCodesListValidated(component.getString("codeList")))){ + if(!jsonObjecthasPropertyNullOrEmpty(component, "codeList") && !repoGestion.getResponseAsBoolean(CodeListQueries.isCodesListValidated(component.getString("codeList")))){ throw new RmesUnauthorizedException(ErrorCodes.COMPONENT_PUBLICATION_VALIDATED_CODESLIST, "The codes list should be validated", new JSONArray()); - } } @@ -324,10 +320,10 @@ public String publishComponent(JSONObject component) throws RmesException { if (type.equals(((SimpleIRI)QB.ATTRIBUTE_PROPERTY).toString())) { componentPublication.publishComponent(RdfUtils.structureComponentAttributeIRI(id), QB.ATTRIBUTE_PROPERTY); } - if (type.equals(((SimpleIRI)QB.MEASURE_PROPERTY).toString())) { + else if (type.equals(((SimpleIRI)QB.MEASURE_PROPERTY).toString())) { componentPublication.publishComponent(RdfUtils.structureComponentMeasureIRI(id), QB.MEASURE_PROPERTY); } - if (type.equals(((SimpleIRI)QB.DIMENSION_PROPERTY).toString())) { + else if (type.equals(((SimpleIRI)QB.DIMENSION_PROPERTY).toString())) { componentPublication.publishComponent(RdfUtils.structureComponentDimensionIRI(id), QB.DIMENSION_PROPERTY); } @@ -335,4 +331,8 @@ public String publishComponent(JSONObject component) throws RmesException { return id; } + + private boolean jsonObjecthasPropertyNullOrEmpty(JSONObject component, String property) { + return component.isNull(property) || "".equals(component.getString(property)); + } } From 49ba6c54323273c15defe69300ca300288677aae Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Mon, 24 May 2021 15:19:38 +0200 Subject: [PATCH 09/15] feat: remove parents if empty --- .../consutation_gestion/ConsultationGestionServiceImpl.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java index 113ff088f..ece72c2cf 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/consutation_gestion/ConsultationGestionServiceImpl.java @@ -308,6 +308,9 @@ private JSONArray getCodes(String notation) throws RmesException { if(childrenMapping.has(code.getString("code"))){ code.put("enfants", childrenMapping.getJSONArray(code.getString("code"))); } + if(code.getJSONArray(Constants.PARENTS).length() == 0){ + code.remove(Constants.PARENTS); + } result.put(code); } return result; From e6a0daa97ab639f9c07f5983a9a91050dceb0a15 Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Tue, 25 May 2021 16:16:41 +0200 Subject: [PATCH 10/15] Fix issue with null replaces or isReplacedBy List --- .../operations/series/SeriesUtils.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java index 94ddde797..45b7055b5 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java @@ -246,17 +246,21 @@ private void createRdfSeries(Series series, IRI familyURI, ValidationStatus newS addOperationLinks(series.getSeeAlso(), RDFS.SEEALSO, model, seriesURI); List replaces = series.getReplaces(); + if (replaces != null && replaces.isEmpty()) { replaces.stream().filter(repl -> !repl.isEmpty()).forEach(replace -> { String replUri = ObjectType.getCompleteUriGestion(replace.getType(), replace.getId()); addReplacesAndReplacedBy(model, RdfUtils.toURI(replUri), seriesURI); }); + } List isReplacedBys = series.getIsReplacedBy(); - isReplacedBys.stream().filter(isRepl -> !isRepl.isEmpty()).forEach(isRepl -> { - String isReplUri = ObjectType.getCompleteUriGestion(isRepl.getType(), isRepl.getId()); - addReplacesAndReplacedBy(model, seriesURI, RdfUtils.toURI(isReplUri)); - }); - + if (replaces != null && replaces.isEmpty()) { + isReplacedBys.stream().filter(isRepl -> !isRepl.isEmpty()).forEach(isRepl -> { + String isReplUri = ObjectType.getCompleteUriGestion(isRepl.getType(), isRepl.getId()); + addReplacesAndReplacedBy(model, seriesURI, RdfUtils.toURI(isReplUri)); + }); + } + if (familyURI != null) { //case CREATION : link series to family RdfUtils.addTripleUri(seriesURI, DCTERMS.IS_PART_OF, familyURI, model, RdfUtils.operationsGraph()); From 42f52d5734cd7786a1f617e3fff26d17daf0950f Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Tue, 25 May 2021 16:26:46 +0200 Subject: [PATCH 11/15] Fix empty list --- .../operations/series/SeriesUtils.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java index 45b7055b5..ebbf9d8ae 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/series/SeriesUtils.java @@ -1,7 +1,9 @@ package fr.insee.rmes.bauhaus_services.operations.series; import java.io.IOException; +import java.util.Collections; import java.util.List; +import java.util.Optional; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpStatus; @@ -246,20 +248,19 @@ private void createRdfSeries(Series series, IRI familyURI, ValidationStatus newS addOperationLinks(series.getSeeAlso(), RDFS.SEEALSO, model, seriesURI); List replaces = series.getReplaces(); - if (replaces != null && replaces.isEmpty()) { - replaces.stream().filter(repl -> !repl.isEmpty()).forEach(replace -> { + Optional.ofNullable(replaces) + .orElseGet(Collections::emptyList).stream().filter(repl -> !repl.isEmpty()).forEach(replace -> { String replUri = ObjectType.getCompleteUriGestion(replace.getType(), replace.getId()); addReplacesAndReplacedBy(model, RdfUtils.toURI(replUri), seriesURI); }); - } + List isReplacedBys = series.getIsReplacedBy(); - if (replaces != null && replaces.isEmpty()) { - isReplacedBys.stream().filter(isRepl -> !isRepl.isEmpty()).forEach(isRepl -> { + Optional.ofNullable(isReplacedBys) + .orElseGet(Collections::emptyList).stream().filter(isRepl -> !isRepl.isEmpty()).forEach(isRepl -> { String isReplUri = ObjectType.getCompleteUriGestion(isRepl.getType(), isRepl.getId()); addReplacesAndReplacedBy(model, seriesURI, RdfUtils.toURI(isReplUri)); }); - } if (familyURI != null) { //case CREATION : link series to family From 7714fb8ce969232059fbf46bb2248ed635d0af30 Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Tue, 8 Jun 2021 11:21:52 +0200 Subject: [PATCH 12/15] Fix issue with Sesame and rdf4j... --- .../sparql_queries/classifications/ItemsQueries.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/insee/rmes/persistance/sparql_queries/classifications/ItemsQueries.java b/src/main/java/fr/insee/rmes/persistance/sparql_queries/classifications/ItemsQueries.java index aa1beafe3..9d85f4ad1 100644 --- a/src/main/java/fr/insee/rmes/persistance/sparql_queries/classifications/ItemsQueries.java +++ b/src/main/java/fr/insee/rmes/persistance/sparql_queries/classifications/ItemsQueries.java @@ -56,9 +56,11 @@ public static String itemNotesQuery(String classificationId, String itemId, int + "?additionalContentNoteLg1 ?additionalContentNoteLg2 ?exclusionNoteLg1 ?exclusionNoteLg2 \n" + "?changeNoteLg1 ?changeNoteLg2 ?changeNoteDate \n" + "WHERE { \n" - + "?item skos:notation '" + itemId + "' . \n" + //+ "?item skos:notation '" + itemId + "' . \n" + "?item skos:inScheme ?classification . \n" + "FILTER(REGEX(STR(?classification),'/codes/" + classificationId + "/')) \n" + + "FILTER(STRENDS(STR(?item),'/" + itemId + "')) \n" + // definitionLg1 + "OPTIONAL {?item skos:definition ?defLg1 . \n" + "?defLg1 dcterms:language '" + Config.LG1 + "'^^xsd:language . \n" From f5dc229f73018f15c98c4b7e0dd19eecf14ed3f8 Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Tue, 8 Jun 2021 14:49:43 +0200 Subject: [PATCH 13/15] Fix issue when richtext has lg2 without lg1 --- .../documentations/getDocumentationRubricsQuery.ftlh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/resources/request/operations/documentations/getDocumentationRubricsQuery.ftlh b/src/main/resources/request/operations/documentations/getDocumentationRubricsQuery.ftlh index 0510cefde..5ad86860b 100644 --- a/src/main/resources/request/operations/documentations/getDocumentationRubricsQuery.ftlh +++ b/src/main/resources/request/operations/documentations/getDocumentationRubricsQuery.ftlh @@ -22,19 +22,21 @@ WHERE { <#-- RangeType.RICHTEXT : label, hasDoc --> UNION { + ?reportAttr ?attr ?temp . + ?temp rdf:type dcmitype:Text . + + OPTIONAL{ ?reportAttr ?attr ?textLg1 . ?textLg1 rdf:type dcmitype:Text . - ?textLg1 dcterms:language <${LG1_CL}> - - OPTIONAL{ - ?textLg1 rdf:value ?labelLg1 + ?textLg1 dcterms:language <${LG1_CL}> . + ?textLg1 rdf:value ?labelLg1 . } BIND(EXISTS{?textLg1 insee:additionalMaterial ?docLg1} AS ?hasDocLg1) OPTIONAL { ?reportAttr ?attr ?textLg2 . ?textLg2 rdf:type dcmitype:Text . - ?textLg2 dcterms:language <${LG2_CL}> + ?textLg2 dcterms:language <${LG2_CL}> . OPTIONAL{ ?textLg2 rdf:value ?labelLg2 From 049dd3ece912b23be5d0ba1196344f3c4cdc312f Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Fri, 11 Jun 2021 17:46:34 +0200 Subject: [PATCH 14/15] feat: be able to create / update codeslist --- .../bauhaus_services/CodeListService.java | 4 + .../code_list/CodeListServiceImpl.java | 86 ++++++++++++++++++- .../rdf_utils/ObjectType.java | 1 + .../bauhaus_services/rdf_utils/RdfUtils.java | 7 ++ .../rmes/webservice/CodeListsResources.java | 47 ++++++++-- 5 files changed, 132 insertions(+), 13 deletions(-) diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java b/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java index fb27e83e5..dcd8e6b65 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java @@ -18,4 +18,8 @@ public interface CodeListService { String getDetailedCodesList(String notation) throws RmesException; String getDetailedCodesListForSearch() throws RmesException; + + String setCodesList(String body) throws RmesException; + + String setCodesList(String id, String body) throws RmesException; } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java index 670836e54..2be8054cd 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java @@ -1,9 +1,26 @@ package fr.insee.rmes.bauhaus_services.code_list; +import com.fasterxml.jackson.databind.DeserializationFeature; +import fr.insee.rmes.bauhaus_services.rdf_utils.RdfUtils; +import fr.insee.rmes.config.Config; import fr.insee.rmes.model.ValidationStatus; +import fr.insee.rmes.model.structures.MutualizedComponent; +import fr.insee.rmes.persistance.ontologies.INSEE; +import fr.insee.rmes.persistance.ontologies.QB; +import fr.insee.rmes.persistance.sparql_queries.structures.StructureQueries; +import fr.insee.rmes.utils.DateUtils; import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Model; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.impl.LinkedHashModel; +import org.eclipse.rdf4j.model.impl.SimpleIRI; +import org.eclipse.rdf4j.model.vocabulary.DC; +import org.eclipse.rdf4j.model.vocabulary.DCTERMS; +import org.eclipse.rdf4j.model.vocabulary.RDF; +import org.eclipse.rdf4j.model.vocabulary.SKOS; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; @@ -110,6 +127,71 @@ public String getDetailedCodesListForSearch() throws RmesException { return lists.toString(); } + @Override + public String setCodesList(String body) throws RmesException { + ObjectMapper mapper = new ObjectMapper(); + mapper.configure( + DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JSONObject codesList = new JSONObject(body); + + IRI codeListIri = RdfUtils.codeListIRI(codesList.getString("id")); + repoGestion.clearStructureNodeAndComponents(codeListIri); + Model model = new LinkedHashModel(); + Resource graph = RdfUtils.codesListGraph(); + RdfUtils.addTripleDateTime(codeListIri, DCTERMS.CREATED, DateUtils.getCurrentDate(), model, graph); + RdfUtils.addTripleDateTime(codeListIri, DCTERMS.MODIFIED, DateUtils.getCurrentDate(), model, graph); + return this.createOrUpdateCodeList(model, graph, codesList, codeListIri); + } + + @Override + public String setCodesList(String id, String body) throws RmesException { + ObjectMapper mapper = new ObjectMapper(); + mapper.configure( + DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JSONObject codesList = new JSONObject(body); + + IRI codeListIri = RdfUtils.codeListIRI(codesList.getString("id")); + repoGestion.clearStructureNodeAndComponents(codeListIri); + Model model = new LinkedHashModel(); + Resource graph = RdfUtils.codesListGraph(); + + RdfUtils.addTripleDateTime(codeListIri, DCTERMS.CREATED, codesList.getString("created"), model, graph); + RdfUtils.addTripleDateTime(codeListIri, DCTERMS.MODIFIED, DateUtils.getCurrentDate(), model, graph); + + return this.createOrUpdateCodeList(model, graph, codesList, codeListIri); + } + + private String createOrUpdateCodeList(Model model, Resource graph, JSONObject codesList, IRI codeListIri) throws RmesException { + model.add(codeListIri, INSEE.VALIDATION_STATE, RdfUtils.setLiteralString(ValidationStatus.UNPUBLISHED), graph); + + if(codesList.has("disseminationStatus")){ + RdfUtils.addTripleUri(codeListIri, INSEE.DISSEMINATIONSTATUS, codesList.getString("disseminationStatus"), model, graph); + } + + if(codesList.has("labelLg1")){ + model.add(codeListIri, SKOS.PREF_LABEL, RdfUtils.setLiteralString(codesList.getString("labelLg1"), Config.LG1), graph); + } + if(codesList.has("labelLg2")){ + model.add(codeListIri, SKOS.PREF_LABEL, RdfUtils.setLiteralString(codesList.getString("labelLg2"), Config.LG2), graph); + } + if(codesList.has("descriptionLg1")){ + model.add(codeListIri, SKOS.DEFINITION, RdfUtils.setLiteralString(codesList.getString("descriptionLg1"), Config.LG1), graph); + } + if(codesList.has("descriptionLg2")){ + model.add(codeListIri, SKOS.DEFINITION, RdfUtils.setLiteralString(codesList.getString("descriptionLg2"), Config.LG2), graph); + } + if(codesList.has("creator")){ + RdfUtils.addTripleString(codeListIri, DC.CREATOR, codesList.getString("creator"), model, graph); + } + if(codesList.has("contributor")){ + RdfUtils.addTripleString(codeListIri, DC.CONTRIBUTOR, codesList.getString("contributor"), model, graph); + } + model.add(codeListIri, SKOS.NOTATION, RdfUtils.setLiteralString(codesList.getString("id")), graph); + repoGestion.loadSimpleObject(codeListIri, model, null); + return codeListIri.toString(); + } + + private JSONArray getCodesForList(JSONArray codes, JSONObject list) { JSONArray codesList = new JSONArray(); for (int i = 0 ; i < codes.length(); i++) { @@ -145,8 +227,4 @@ public String getAllCodesLists() throws RmesException { public String geCodesListByIRI(String IRI) throws RmesException { return repoGestion.getResponseAsArray(CodeListQueries.geCodesListByIRI(IRI)).toString(); } - - - - } diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/ObjectType.java b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/ObjectType.java index 3ebed8580..dcae217f2 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/ObjectType.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/ObjectType.java @@ -29,6 +29,7 @@ public enum ObjectType { GEO_STAT_TERRITORY("geoFeature", GEO.FEATURE, Config.DOCUMENTATIONS_GEO_BASE_URI), ORGANIZATION("organization",ORG.ORGANIZATION, ""), STRUCTURE("structure", QB.DATA_STRUCTURE_DEFINITION, Config.STRUCTURES_BASE_URI), + CODE_LIST("codeList", QB.CODE_LIST, Config.CODE_LIST_BASE_URI), MEASURE_PROPERTY("measureProperty", QB.MEASURE_PROPERTY, Config.STRUCTURES_COMPONENTS_BASE_URI + "mesure"), ATTRIBUTE_PROPERTY("attributeProperty", QB.ATTRIBUTE_PROPERTY, Config.STRUCTURES_COMPONENTS_BASE_URI + "attribut"), diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java index 9175a9d2b..6230ac16b 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/rdf_utils/RdfUtils.java @@ -66,6 +66,9 @@ public static Resource simsGeographyGraph(){ public static Resource structureGraph(){ return factory.createIRI(Config.STRUCTURES_GRAPH); } + public static Resource codesListGraph(){ + return factory.createIRI(Config.CODELIST_GRAPH); + } public static Resource structureComponentGraph(){ return factory.createIRI(Config.STRUCTURES_COMPONENTS_GRAPH); @@ -130,6 +133,10 @@ public static IRI structureIRI(String id) { return objectIRI(ObjectType.STRUCTURE, id); } + public static IRI codeListIRI(String id) { + return objectIRI(ObjectType.CODE_LIST, id); + } + public static IRI createIRI(String uri){ return factory.createIRI(uri); } diff --git a/src/main/java/fr/insee/rmes/webservice/CodeListsResources.java b/src/main/java/fr/insee/rmes/webservice/CodeListsResources.java index 61566ac2f..e7391bfc2 100644 --- a/src/main/java/fr/insee/rmes/webservice/CodeListsResources.java +++ b/src/main/java/fr/insee/rmes/webservice/CodeListsResources.java @@ -1,12 +1,11 @@ package fr.insee.rmes.webservice; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; +import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import fr.insee.rmes.bauhaus_services.Constants; +import io.swagger.v3.oas.annotations.parameters.RequestBody; import org.apache.http.HttpStatus; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -24,6 +23,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; +import static javax.ws.rs.core.MediaType.TEXT_PLAIN; + @Component @Path("/codeList") @Tag(name="Codes lists", description="Codes list API") @@ -43,6 +44,32 @@ public class CodeListsResources { @Autowired CodeListService codeListService; + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Operation(operationId = "setCodesList", summary = "Create a codes list") + public Response setCodesList(@RequestBody(description = "Code List", required = true) String body) { + String id = null; + try { + id = codeListService.setCodesList(body); + } catch (RmesException e) { + return Response.status(e.getStatus()).entity(e.getDetails()).type(TEXT_PLAIN).build(); + } + return Response.status(HttpStatus.SC_OK).entity(id).build(); + } + + @PUT + @Path("/{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Operation(operationId = "setCodesList", summary = "Create a codes list") + public Response updateCodesList(@PathParam(Constants.ID) String componentId, @RequestBody(description = "Code List", required = true) String body) { + String id = null; + try { + id = codeListService.setCodesList(id, body); + } catch (RmesException e) { + return Response.status(e.getStatus()).entity(e.getDetails()).type(TEXT_PLAIN).build(); + } + return Response.status(HttpStatus.SC_OK).entity(id).build(); + } @GET @Produces(MediaType.APPLICATION_JSON) @@ -53,7 +80,7 @@ public Response getallCodesLists() { try { jsonResultat = codeListService.getAllCodesLists(); } catch (RmesException e) { - return Response.status(e.getStatus()).entity(e.getDetails()).type(MediaType.TEXT_PLAIN).build(); + return Response.status(e.getStatus()).entity(e.getDetails()).type(TEXT_PLAIN).build(); } return Response.status(HttpStatus.SC_OK).entity(jsonResultat).build(); } @@ -68,7 +95,7 @@ public Response getDetailedCodesLisForSearch() { try { jsonResultat = codeListService.getDetailedCodesListForSearch(); } catch (RmesException e) { - return Response.status(e.getStatus()).entity(e.getDetails()).type(MediaType.TEXT_PLAIN).build(); + return Response.status(e.getStatus()).entity(e.getDetails()).type(TEXT_PLAIN).build(); } return Response.status(HttpStatus.SC_OK).entity(jsonResultat).build(); } @@ -83,7 +110,7 @@ public Response getDetailedCodesListByNotation(@PathParam("notation") String not try { jsonResultat = codeListService.getDetailedCodesList(notation); } catch (RmesException e) { - return Response.status(e.getStatus()).entity(e.getDetails()).type(MediaType.TEXT_PLAIN).build(); + return Response.status(e.getStatus()).entity(e.getDetails()).type(TEXT_PLAIN).build(); } return Response.status(HttpStatus.SC_OK).entity(jsonResultat).build(); } @@ -98,7 +125,7 @@ public Response getCodeListByNotation(@PathParam("notation") String notation) { try { jsonResultat = codeListService.getCodeListJson(notation); } catch (RmesException e) { - return Response.status(e.getStatus()).entity(e.getDetails()).type(MediaType.TEXT_PLAIN).build(); + return Response.status(e.getStatus()).entity(e.getDetails()).type(TEXT_PLAIN).build(); } return Response.status(HttpStatus.SC_OK).entity(jsonResultat).build(); } @@ -115,8 +142,10 @@ public Response getCodeByNotation(@PathParam("notation") String notation, @PathP try { jsonResultat = codeListService.getCode(notation, code); } catch (RmesException e) { - return Response.status(e.getStatus()).entity(e.getDetails()).type(MediaType.TEXT_PLAIN).build(); + return Response.status(e.getStatus()).entity(e.getDetails()).type(TEXT_PLAIN).build(); } return Response.status(HttpStatus.SC_OK).entity(jsonResultat).build(); } + + } From ef3626d3b0a2e240c6ed3d946e95766b2c5865ba Mon Sep 17 00:00:00 2001 From: Alice Lambois Date: Wed, 16 Jun 2021 11:02:25 +0200 Subject: [PATCH 15/15] Change version to 3.0.5 --- bauhaus-back-changeLog.txt | 3 +++ pom.xml | 2 +- src/main/java/fr/insee/rmes/config/swagger/SwaggerConfig.java | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/bauhaus-back-changeLog.txt b/bauhaus-back-changeLog.txt index e3388e5d6..121af36d4 100644 --- a/bauhaus-back-changeLog.txt +++ b/bauhaus-back-changeLog.txt @@ -1,3 +1,6 @@ +3.0.5 : - Correction d'un bug qui ouvrait une connexion à chaque requete + bugs mineurs + - Initialisation de dockerisation + - Changement du richtext dans les concepts (gras, italisque) 3.0.4 : Correction des liens dans la documentation SIMS (l'uri était celle d'un document) 3.0.3 : Mise à jour de spring-security + correction des droits de créations de documents/liens 3.0.2 : Corrections de l'url des documents à la publication diff --git a/pom.xml b/pom.xml index e495e9db3..419cd901a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ fr.insee.rmes Bauhaus-BO war - 3.0.4 + 3.0.5 Bauhaus-Back-Office Back-office services for Bauhaus https://github.com/InseeFr/Bauhaus-Back-Office diff --git a/src/main/java/fr/insee/rmes/config/swagger/SwaggerConfig.java b/src/main/java/fr/insee/rmes/config/swagger/SwaggerConfig.java index 8700002cc..5f021a00d 100644 --- a/src/main/java/fr/insee/rmes/config/swagger/SwaggerConfig.java +++ b/src/main/java/fr/insee/rmes/config/swagger/SwaggerConfig.java @@ -30,7 +30,7 @@ public SwaggerConfig(@Context ServletConfig servletConfig) { super(); OpenAPI openApi = new OpenAPI(); - Info info = new Info().title("Bauhaus API").version("3.0.4").description("Rest Endpoints and services Integration used by Bauhaus"); + Info info = new Info().title("Bauhaus API").version("3.0.5").description("Rest Endpoints and services Integration used by Bauhaus"); openApi.info(info); Server server = new Server();