Skip to content

Commit

Permalink
Merge pull request #128 from InseeFr/acceptance
Browse files Browse the repository at this point in the history
Acceptance
  • Loading branch information
alicela authored May 11, 2021
2 parents 830c8b7 + 67b7115 commit ff78a4d
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 23 deletions.
1 change: 1 addition & 0 deletions bauhaus-back-changeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
3.0.2 : Corrections de l'url des documents à la publication
3.0.1 : Rétablissement du swagger et des droits d'accès
3.0.0 : Module Opérations
2.1.2 : - Correction du bug à la publication des concepts (la modification supprimait les anciennes versions de notes)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>fr.insee.rmes</groupId>
<artifactId>Bauhaus-BO</artifactId>
<packaging>war</packaging>
<version>3.0.1</version>
<version>3.0.2</version>
<name>Bauhaus-Back-Office</name>
<description>Back-office services for Bauhaus</description>
<url>https://github.com/InseeFr/Bauhaus-Back-Office</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Response export(String simsXML,String operationXML,String indicatorXML,St


private String buildParams(Boolean lg1, Boolean lg2, Boolean includeEmptyMas, String targetType) {
String includeEmptyMasString=( includeEmptyMas ? "true" : "false");
String includeEmptyMasString=( Boolean.TRUE.equals(includeEmptyMas) ? "true" : "false");
String parametersXML="";

parametersXML=parametersXML.concat(Constants.XML_OPEN_PARAMETERS_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ private Model getModelToPublish(String documentId, String filename) throws RmesE
Resource document = RdfUtils.documentIRI(documentId);

RepositoryConnection con = repoGestion.getConnection();
RepositoryResult<Statement> documentStatements = repoGestion.getStatements(con, document);
RepositoryResult<Statement> documentStatements = null ;

try {
documentStatements = repoGestion.getStatements(con, document);

if (!documentStatements.hasNext()) {
throw new RmesNotFoundException(ErrorCodes.DOCUMENT_UNKNOWN_ID, "Document not found", documentId);
}
Expand All @@ -110,7 +112,7 @@ private Model getModelToPublish(String documentId, String filename) throws RmesE
Resource subject = PublicationUtils.tranformBaseURIToPublish(st.getSubject());
IRI predicate = RdfUtils
.createIRI(PublicationUtils.tranformBaseURIToPublish(st.getPredicate()).stringValue());
String newUrl = Config.DOCUMENTS_BASEURL + "/"+ filename;
String newUrl = Config.DOCUMENTS_BASEURL.trim() + "/"+ filename;
logger.info("Publishing document : {}",newUrl);
Value object = RdfUtils.toURI(newUrl);
model.add(subject, predicate, object, st.getContext());
Expand All @@ -119,15 +121,66 @@ private Model getModelToPublish(String documentId, String filename) throws RmesE
renameAndAddTripleToModel(model, st, subject);
}
}
} catch (RepositoryException e) {
throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(),
Constants.REPOSITORY_EXCEPTION);
} catch (RepositoryException |RmesException e) {
model = getModelWithErrorToPublish(documentId, filename);
}

finally {
repoGestion.closeStatements(documentStatements);
if (documentStatements != null) {
repoGestion.closeStatements(documentStatements);
}
}
return model;
}

private Model getModelWithErrorToPublish(String documentId, String filename) throws RmesException {
logger.error("PUBLISHING A DOCUMENT WITH RDF ERROR (URL)");
Model model = new LinkedHashModel();
Resource document = RdfUtils.documentIRI(documentId);

try {
JSONArray tuples = repoGestion.getResponseAsArray(""
+ "select ?predicat ?obj FROM <"+RdfUtils.documentsGraph()+"> "
+ "WHERE {"
+ "?document ?predicat ?obj . "
+ "FILTER (?document = <"+document+">) "
+ "}");


if (tuples.length()==0) {
throw new RmesNotFoundException(ErrorCodes.DOCUMENT_UNKNOWN_ID, "Document not found", documentId);
}

Resource newSubject = PublicationUtils.tranformBaseURIToPublish(document);
Value object ;

for (int i = 0; i < tuples.length(); i++) {
JSONObject tuple = (JSONObject) tuples.get(i);
String predicatString = tuple.getString("predicat");
IRI predicate = (SimpleIRI) PublicationUtils.tranformBaseURIToPublish(RdfUtils.toURI(predicatString));
if (predicatString.endsWith("url")) {
String newUrl = Config.DOCUMENTS_BASEURL.trim() + "/"+ filename;
logger.info("Publishing document : {}",newUrl);
object = RdfUtils.toURI(newUrl);
} else {
String objectString = tuple.getString("obj");
try {
object = RdfUtils.toURI(objectString);
object = PublicationUtils.tranformBaseURIToPublish((Resource) object);

}catch(IllegalArgumentException iAe) {
object = RdfUtils.setLiteralString(objectString);
}
}
model.add(newSubject, predicate, object, RdfUtils.documentsGraph());
}
} catch (RepositoryException e) {
logger.error(e.getMessage());
throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(),
Constants.REPOSITORY_EXCEPTION);
}
return model;

}

private Model getLinkModelToPublish(String linkId) throws RmesException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.insee.rmes.bauhaus_services.rdf_utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -27,6 +28,7 @@
import fr.insee.rmes.bauhaus_services.Constants;
import fr.insee.rmes.config.Config;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.persistance.ontologies.QB;

/**
* Getters only get on publication base
Expand Down Expand Up @@ -57,6 +59,17 @@ public class RepositoryPublication extends RepositoryUtils{
public static String getResponse(String query) throws RmesException {
return getResponse(query, REPOSITORY_PUBLICATION);
}

/**
* Method which aims to produce response from a sparql query for internal Repository
*
* @param query
* @return String
* @throws RmesException
*/
public static String getResponseInternalPublication(String query) throws RmesException {
return getResponse(query, REPOSITORY_PUBLICATION_INTERNE);
}

/**
* Method which aims to produce response from a sparql query
Expand Down Expand Up @@ -182,8 +195,42 @@ private static void publishContext(Resource context, Model model, String type, R
throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), CONNECTION_TO + repo + FAILED);
}
}



public static void clearStructureAndComponentForAllRepositories(Resource structure) {
clearStructureAndComponents(structure, REPOSITORY_PUBLICATION);
clearStructureAndComponents(structure, REPOSITORY_PUBLICATION_INTERNE);
}

public static void clearStructureAndComponents(Resource structure, Repository repository) {
List<Resource> toRemove = new ArrayList<>();
try {
RepositoryConnection conn = repository.getConnection();
RepositoryResult<Statement> nodes = null;
RepositoryResult<Statement> specifications = null;
nodes = conn.getStatements(structure, QB.COMPONENT, null, false);
while (nodes.hasNext()) {
Resource node = (Resource) nodes.next().getObject();
toRemove.add(node);
specifications = conn.getStatements(node, QB.COMPONENT, null, false);
while (specifications.hasNext()) {
toRemove.add((Resource) specifications.next().getObject());
}
specifications.close();

}
nodes.close();
toRemove.forEach(res -> {
try {
RepositoryResult<Statement> statements = conn.getStatements(res, null, null, false);
conn.remove(statements);
} catch (RepositoryException e) {
logger.error("RepositoryGestion Error {}", e.getMessage());
}
});
} catch (RepositoryException e) {
new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), "Failure deletion : " + structure);
}
}
private static void clearConceptLinks(Resource concept, RepositoryConnection conn) throws RmesException {
List<IRI> typeOfLink = Arrays.asList(SKOS.BROADER, SKOS.NARROWER, SKOS.MEMBER, DCTERMS.REFERENCES,
DCTERMS.REPLACES, SKOS.RELATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ public void publish(Resource structure) throws RmesException {
RepositoryConnection con = repoGestion.getConnection();

this.copyTriplet(structure, model, con, new String[]{"validationState", Constants.CREATOR, Constants.CONTRIBUTOR});
Resource componentToPublishRessource = PublicationUtils.tranformBaseURIToPublish(structure);
RepositoryPublication.publishResource(componentToPublishRessource, model, "Structure");

Resource structureToPublish = PublicationUtils.tranformBaseURIToPublish(structure);

RepositoryPublication.clearStructureAndComponentForAllRepositories(structureToPublish);
RepositoryPublication.publishResource(structureToPublish, model, "Structure");

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SwaggerConfig(@Context ServletConfig servletConfig) {
super();
OpenAPI openApi = new OpenAPI();

Info info = new Info().title("Bauhaus API").version("3.0.1").description("Rest Endpoints and services Integration used by Bauhaus");
Info info = new Info().title("Bauhaus API").version("3.0.2").description("Rest Endpoints and services Integration used by Bauhaus");
openApi.info(info);

Server server = new Server();
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/fr/insee/rmes/webservice/HealthcheckApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Response getHealthcheck() {

//Test database connexion
stateResult.add("Database connexion \n");
stateResult = testDatabaseConnexions(errorMessage, stateResult);
checkDatabaseConnexions(errorMessage, stateResult);

//Test access to storage
stateResult = stateResult.add("Document storage \n");
Expand Down Expand Up @@ -98,25 +98,33 @@ public Response getHealthcheck() {
}
}

public StringJoiner testDatabaseConnexions(StringJoiner errorMessage, StringJoiner stateResult) {
public void checkDatabaseConnexions(StringJoiner errorMessage, StringJoiner stateResult) {
try {
if (StringUtils.isEmpty(RepositoryPublication.getResponse(sparlQuery))){
errorMessage.add("- Repository publication doesn't return statement \n");
stateResult = stateResult.add(" - Connexion publication").add(KO_STATE);
stateResult.add(" - Connexion publication Z").add(KO_STATE);
}else {
stateResult = stateResult.add(" - Connexion publication").add(OK_STATE);
stateResult.add(" - Connexion publication Z").add(OK_STATE);
}
if (StringUtils.isEmpty( repoGestion.getResponse(sparlQuery))) {
if (StringUtils.isEmpty(RepositoryPublication.getResponseInternalPublication(sparlQuery))){
errorMessage.add("- Repository publication interne doesn't return statement \n");
stateResult.add(" - Connexion publication I").add(KO_STATE);
}else {
stateResult.add(" - Connexion publication I").add(OK_STATE);
}
if (StringUtils.isEmpty( repoGestion.getResponse(sparlQuery))) {
errorMessage.add("- Repository gestion doesn't return statement \n");
stateResult = stateResult.add(" - Connexion gestion").add(KO_STATE);
stateResult.add(" - Connexion gestion").add(KO_STATE);
}else {
stateResult = stateResult.add(" - Connexion gestion").add(OK_STATE);
stateResult.add(" - Connexion gestion").add(OK_STATE);
}
} catch (RmesException e) {
errorMessage.add("- "+e.getMessage()+ " \n");
stateResult = stateResult.add(" - Connexion database").add(KO_STATE);
stateResult.add(" - Connexion database").add(KO_STATE);
} catch (Exception e) {
errorMessage.add("- "+e.getClass().getSimpleName() +e.getMessage()+ " \n");
stateResult.add(" - Connexion database").add(KO_STATE);
}
return stateResult;
}

private void checkDocumentStorage(String pathToStorage, String storageType, StringJoiner stateResult, StringJoiner errorMessage) {
Expand Down

0 comments on commit ff78a4d

Please sign in to comment.