Skip to content

Commit

Permalink
fix: review
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Nov 19, 2024
1 parent 60c54a0 commit b93a7ec
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,129 +11,123 @@
import fr.insee.rmes.exceptions.RmesNotFoundException;
import fr.insee.rmes.persistance.ontologies.DCTERMS;
import fr.insee.rmes.persistance.sparql_queries.operations.series.OpSeriesQueries;
import org.apache.http.HttpStatus;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.RepositoryResult;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Repository;

@Repository
public class SeriesPublication extends RdfService {

final
ParentUtils ownersUtils;

public SeriesPublication(ParentUtils ownersUtils) {
this.ownersUtils = ownersUtils;
}

public void publishSeries(String id, JSONObject series) throws RmesException {
String familyId = series.getJSONObject(Constants.FAMILY).getString(Constants.ID);
String status= ownersUtils.getValidationStatus(familyId);

if(PublicationUtils.isPublished(status)) {
throw new RmesBadRequestException(
ErrorCodes.SERIES_VALIDATION_UNPUBLISHED_FAMILY,
"This Series cannot be published before its family is published",
"Series: "+id+" ; Family: "+familyId);
}

Model model = new LinkedHashModel();
Resource resource = RdfUtils.seriesIRI(id);

RepositoryConnection con = repoGestion.getConnection();
RepositoryResult<Statement> statements = repoGestion.getStatements(con, resource);

RepositoryResult<Statement> hasPartStatements = repoGestion.getHasPartStatements(con, resource);
RepositoryResult<Statement> replacesStatements = repoGestion.getReplacesStatements(con, resource);
RepositoryResult<Statement> isReplacedByStatements = repoGestion.getIsReplacedByStatements(con, resource);

try {
try {
if (!statements.hasNext()) {
throw new RmesNotFoundException(ErrorCodes.SERIES_UNKNOWN_ID, "Series not found", id);
}
while (statements.hasNext()) {
Statement st = statements.next();
String pred = RdfUtils.toString(st.getPredicate());

// Other URI to transform
if (pred.endsWith("isPartOf") ||
pred.endsWith(Constants.SEEALSO) ||
pred.endsWith(Constants.REPLACES) ||
pred.endsWith(Constants.ISREPLACEDBY)||
pred.endsWith(Constants.DATA_COLLECTOR) ||
pred.endsWith(Constants.CONTRIBUTOR) ||
pred.endsWith(Constants.PUBLISHER) ||
pred.endsWith("accrualPeriodicity")||
pred.endsWith("type") ) {
transformSubjectAndObject(model, st);
} else if (pred.endsWith("isValidated")
|| pred.endsWith("validationState")
|| pred.endsWith("hasPart")) {
// nothing, wouldn't copy this attr
}
// Literals
else {
model.add(publicationUtils.tranformBaseURIToPublish(st.getSubject()),
st.getPredicate(),
st.getObject(),
st.getContext()
);
}
addStatementsToModel(model, hasPartStatements);
addStatementsToModel(model, replacesStatements);
addStatementsToModel(model, isReplacedByStatements);
}

/**
* We have to query all published operations linked to this series and publish all of them
*/
JSONArray operations = repoGestion.getResponseAsArray(OpSeriesQueries.getPublishedOperationsForSeries(resource.toString()));
for(var i = 0; i < operations.length(); i++){
JSONObject operation = operations.getJSONObject(i);
String iri = operation.getString("operation");
model.add(
publicationUtils.tranformBaseURIToPublish(resource),
DCTERMS.HAS_PART,
publicationUtils.tranformBaseURIToPublish(RdfUtils.createIRI(iri)),
RdfUtils.operationsGraph()
);
}

} catch (RepositoryException e) {
throw new RmesException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), Constants.REPOSITORY_EXCEPTION);
}

} finally {
repoGestion.closeStatements(statements);
repoGestion.closeStatements(hasPartStatements);
con.close();
}
Resource seriesToPublishRessource = publicationUtils.tranformBaseURIToPublish(resource);
repositoryPublication.publishResource(seriesToPublishRessource, model, "serie");

}

public void addStatementsToModel(Model model, RepositoryResult<Statement> statements) {
while (statements.hasNext()) {
Statement statement = statements.next();
transformSubjectAndObject(model, statement);
}
}

public void transformSubjectAndObject(Model model, Statement statement) {
model.add(publicationUtils.tranformBaseURIToPublish(statement.getSubject()),
statement.getPredicate(),
publicationUtils.tranformBaseURIToPublish((Resource) statement.getObject()),
statement.getContext());
}

final
ParentUtils ownersUtils;

public SeriesPublication(ParentUtils ownersUtils) {
this.ownersUtils = ownersUtils;
}

public void publishSeries(String id, JSONObject series) throws RmesException {
String familyId = series.getJSONObject(Constants.FAMILY).getString(Constants.ID);
String status = ownersUtils.getValidationStatus(familyId);

if (PublicationUtils.isPublished(status)) {
throw new RmesBadRequestException(
ErrorCodes.SERIES_VALIDATION_UNPUBLISHED_FAMILY,
"This Series cannot be published before its family is published",
"Series: " + id + " ; Family: " + familyId);
}

Model model = new LinkedHashModel();
Resource resource = RdfUtils.seriesIRI(id);

RepositoryConnection con = repoGestion.getConnection();
RepositoryResult<Statement> statements = repoGestion.getStatements(con, resource);

RepositoryResult<Statement> hasPartStatements = repoGestion.getHasPartStatements(con, resource);
RepositoryResult<Statement> replacesStatements = repoGestion.getReplacesStatements(con, resource);
RepositoryResult<Statement> isReplacedByStatements = repoGestion.getIsReplacedByStatements(con, resource);

try {
if (!statements.hasNext()) {
throw new RmesNotFoundException(ErrorCodes.SERIES_UNKNOWN_ID, "Series not found", id);
}
while (statements.hasNext()) {
Statement st = statements.next();
String pred = RdfUtils.toString(st.getPredicate());

// Other URI to transform
if (pred.endsWith("isPartOf") ||
pred.endsWith(Constants.SEEALSO) ||
pred.endsWith(Constants.REPLACES) ||
pred.endsWith(Constants.ISREPLACEDBY) ||
pred.endsWith(Constants.DATA_COLLECTOR) ||
pred.endsWith(Constants.CONTRIBUTOR) ||
pred.endsWith(Constants.PUBLISHER) ||
pred.endsWith("accrualPeriodicity") ||
pred.endsWith("type")) {
transformSubjectAndObject(model, st);
} else if (pred.endsWith("isValidated")
|| pred.endsWith("validationState")
|| pred.endsWith("hasPart")) {
// nothing, wouldn't copy this attr
}
// Literals
else {
model.add(publicationUtils.tranformBaseURIToPublish(st.getSubject()),
st.getPredicate(),
st.getObject(),
st.getContext()
);
}
addStatementsToModel(model, hasPartStatements);
addStatementsToModel(model, replacesStatements);
addStatementsToModel(model, isReplacedByStatements);
}

/**
* We have to query all published operations linked to this series and publish all of them
*/
JSONArray operations = repoGestion.getResponseAsArray(OpSeriesQueries.getPublishedOperationsForSeries(resource.toString()));
for (var i = 0; i < operations.length(); i++) {
JSONObject operation = operations.getJSONObject(i);
String iri = operation.getString("operation");
model.add(
publicationUtils.tranformBaseURIToPublish(resource),
DCTERMS.HAS_PART,
publicationUtils.tranformBaseURIToPublish(RdfUtils.createIRI(iri)),
RdfUtils.operationsGraph()
);
}


} finally {
repoGestion.closeStatements(statements);
repoGestion.closeStatements(hasPartStatements);
con.close();
}
Resource seriesToPublishRessource = publicationUtils.tranformBaseURIToPublish(resource);
repositoryPublication.publishResource(seriesToPublishRessource, model, "serie");

}

public void addStatementsToModel(Model model, RepositoryResult<Statement> statements) {
while (statements.hasNext()) {
Statement statement = statements.next();
transformSubjectAndObject(model, statement);
}
}

public void transformSubjectAndObject(Model model, Statement statement) {
model.add(publicationUtils.tranformBaseURIToPublish(statement.getSubject()),
statement.getPredicate(),
publicationUtils.tranformBaseURIToPublish((Resource) statement.getObject()),
statement.getContext());
}

}

Loading

0 comments on commit b93a7ec

Please sign in to comment.