Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw an error if a indicator is not linked to a series #827

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface ConceptsCollectionService {

ResponseEntity<?> getCollectionExportODT(String id, String accept, ConceptsCollectionsResources.Language lg, boolean withConcepts, HttpServletResponse response) throws RmesException;

ResponseEntity<?> getCollectionExportODS(String id, String accept, boolean withConcepts, HttpServletResponse response) throws RmesException;
ResponseEntity<?> getCollectionExportODS(String id, String accept, boolean withConcepts, HttpServletResponse response) throws RmesException;

void exportZipCollection(String id, String accept, HttpServletResponse response, ConceptsCollectionsResources.Language lg, String type, boolean withConcepts) throws RmesException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.insee.rmes.model.concepts.MembersLg;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;

import java.io.InputStream;
Expand Down Expand Up @@ -39,7 +40,7 @@ public interface ConceptsService {

void setConceptsValidation(String body) throws RmesException ;

ResponseEntity<?> exportConcept(String id, String acceptHeader) throws RmesException;
ResponseEntity<Resource> exportConcept(String id, String acceptHeader) throws RmesException;

void exportZipConcept(String id, String acceptHeader, HttpServletResponse response, ConceptsCollectionsResources.Language lg, String type, boolean withConcepts) throws RmesException;

Expand All @@ -49,11 +50,11 @@ public interface ConceptsService {

void setCollectionsValidation(String body) throws RmesException ;

ResponseEntity<?> getCollectionExport(String id, String acceptHeader) throws RmesException ;
ResponseEntity<Resource> getCollectionExport(String id, String acceptHeader) throws RmesException ;

String getRelatedConcepts(String id) throws RmesException;

String deleteConcept(String id) throws RmesException;
void deleteConcept(String id) throws RmesException;

Map<String, InputStream> getConceptsExportIS(List<String> ids, List<MembersLg> members) throws RmesException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,16 @@ public interface OperationsService {

/**
* UPDATE
* @param id
* @param body
* @throws RmesException
*/
void setIndicator(String id, String body) throws RmesException;

/**
* PUBLISH
* @param body
* @throws RmesException
*/

String setIndicatorValidation(String body) throws RmesException;

/**
* CREATE
* @param body
* @return
* @throws RmesException
*/
String setIndicator(String body) throws RmesException;


/**
* PUBLISH
*/
String setIndicatorValidation(String body) throws RmesException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public class StampAuthorizationChecker extends StampsRestrictionServiceImpl {
private static final Logger logger = LoggerFactory.getLogger(StampAuthorizationChecker.class);
public static final String CHECKING_AUTHORIZATION_ERROR_MESSAGE = "Error while checking authorization for user with stamp {} to modify or delete {}";
public static final String ERROR_AUTHORIZATION = "Error while checking authorization for user with stamp {} to modify {}";
private final String baseInternalUri;

@Autowired
public StampAuthorizationChecker(RepositoryGestion repoGestion, AuthorizeMethodDecider authorizeMethodDecider, UserProvider userProvider, @Value("${fr.insee.rmes.bauhaus.sesame.gestion.baseInternalURI}") String baseInternalUri) {
public StampAuthorizationChecker(RepositoryGestion repoGestion, AuthorizeMethodDecider authorizeMethodDecider, UserProvider userProvider) {
super(repoGestion, authorizeMethodDecider, userProvider);
this.baseInternalUri = baseInternalUri;
}

public boolean isSeriesManagerWithStamp(String seriesId, Stamp stamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import fr.insee.rmes.utils.XMLUtils;
import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.text.CaseUtils;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -89,7 +88,7 @@ public ResponseEntity<?> getCollectionExportODT(String id, String accept, Concep

String fileName = getFileNameForExport(collection, lg);
if(conceptsIds.isEmpty()){
return collectionExport.exportAsResponseODT(fileName, xmlContent,true,true,true, lg);
return collectionExport.exportAsResponseODT(fileName, xmlContent,true, lg);
}

Map<String, InputStream> concepts = conceptsService.getConceptsExportIS(conceptsIds, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -101,7 +102,7 @@ public String getRelatedConcepts(String id) throws RmesException{
* @throws RmesException
*/
@Override
public String deleteConcept(String id) throws RmesException {
public void deleteConcept(String id) throws RmesException {
String uriConcept = RdfUtils.toString(RdfUtils.objectIRI(ObjectType.CONCEPT,id));
JSONArray graphArray = conceptsUtils.getGraphsWithConcept(uriConcept);

Expand Down Expand Up @@ -139,8 +140,6 @@ public String deleteConcept(String id) throws RmesException {
String successMessage=THE_CONCEPT+id+" has been deleted from graph "+RdfUtils.conceptGraph();
if (result!= HttpStatus.OK) {
throw new RmesException(HttpStatus.INTERNAL_SERVER_ERROR.value(),"Unexpected return message: ",result.toString());
} else {
return successMessage;
}
}

Expand Down Expand Up @@ -214,14 +213,8 @@ public void setConceptsValidation(String body) throws RmesException {
* Export concept(s)
*/
@Override
public ResponseEntity<?> exportConcept(String id, String acceptHeader) throws RmesException {
ConceptForExport concept;
try {
concept = conceptsExport.getConceptData(id);
} catch (RmesException e) {
return ResponseEntity.status(e.getStatus()).contentType(MediaType.TEXT_PLAIN).body(e.getDetails());
}

public ResponseEntity<Resource> exportConcept(String id, String acceptHeader) throws RmesException {
ConceptForExport concept = conceptsExport.getConceptData(id);
Map<String, String> xmlContent = convertConceptInXml(concept);
String fileName = getFileNameForExport(concept);
return conceptsExport.exportAsResponse(fileName,xmlContent,true,true,true);
Expand Down Expand Up @@ -339,13 +332,8 @@ public void setCollectionsValidation(String body) throws RmesException {
}

@Override
public ResponseEntity<?> getCollectionExport(String id, String acceptHeader) throws RmesException{
CollectionForExportOld collection;
try {
collection = collectionExport.getCollectionDataOld(id);
} catch (RmesException e) {
return ResponseEntity.status(e.getStatus()).contentType(MediaType.TEXT_PLAIN).body(e.getDetails());
}
public ResponseEntity<Resource> getCollectionExport(String id, String acceptHeader) throws RmesException{
CollectionForExportOld collection= collectionExport.getCollectionDataOld(id);
Map<String, String> xmlContent = convertCollectionInXml(collection);
String fileName = FilesUtils.generateFinalFileNameWithoutExtension(collection.getId() + "-" + collection.getPrefLabelLg1(), maxLength);
return collectionExport.exportAsResponse(fileName,xmlContent,true,true,true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ResponseEntity<Resource> exportAsResponse(String fileName, Map<String, St
}


public ResponseEntity<Resource> exportAsResponseODT(String fileName, Map<String, String> xmlContent, boolean lg1, boolean lg2, boolean includeEmptyFields, ConceptsCollectionsResources.Language lg) throws RmesException {
public ResponseEntity<Resource> exportAsResponseODT(String fileName, Map<String, String> xmlContent, boolean includeEmptyFields, ConceptsCollectionsResources.Language lg) throws RmesException {
String parametersXML = XsltUtils.buildParams(true, true, includeEmptyFields, Constants.COLLECTION);
xmlContent.put(Constants.PARAMETERS_FILE, parametersXML);
String xmlPattern = lg == ConceptsCollectionsResources.Language.lg1 ? xmlPatternFR : xmlPatternEN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fr.insee.rmes.bauhaus_services.rdf_utils.*;
import fr.insee.rmes.config.auth.security.restrictions.StampsRestrictionsService;
import fr.insee.rmes.exceptions.*;
import fr.insee.rmes.exceptions.errors.IndicatorErrorCodes;
import fr.insee.rmes.model.ValidationStatus;
import fr.insee.rmes.model.links.OperationsLink;
import fr.insee.rmes.model.operations.Indicator;
Expand Down Expand Up @@ -89,18 +90,17 @@ public IndicatorsUtils(
}

private void validate(Indicator indicator) throws RmesException {
if(indicator.isWasGenerattedByEmpty()){
throw new RmesBadRequestException(IndicatorErrorCodes.EMPTY_WAS_GENERATED_BY, "An indicator should be linked to a series.");
}
if(repositoryGestion.getResponseAsBoolean(IndicatorsQueries.checkPrefLabelUnicity(indicator.getId(), indicator.getPrefLabelLg1(), lg1))){
throw new RmesBadRequestException(ErrorCodes.OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1, "This prefLabelLg1 is already used by another indicator.");
throw new RmesBadRequestException(IndicatorErrorCodes.EXISTING_PREF_LABEL_LG1, "This prefLabelLg1 is already used by another indicator.");
}
if(repositoryGestion.getResponseAsBoolean(IndicatorsQueries.checkPrefLabelUnicity(indicator.getId(), indicator.getPrefLabelLg2(), lg2))){
throw new RmesBadRequestException(ErrorCodes.OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2, "This prefLabelLg2 is already used by another indicator.");
throw new RmesBadRequestException(IndicatorErrorCodes.EXISTING_PREF_LABEL_LG2, "This prefLabelLg2 is already used by another indicator.");
}
}

public Indicator getIndicatorById(String id) throws RmesException{
return buildIndicatorFromJson(getIndicatorJsonById(id), false);
}

public Indicator getIndicatorById(String id, boolean forXML) throws RmesException{
return buildIndicatorFromJson(getIndicatorJsonById(id), forXML);
}
Expand Down Expand Up @@ -434,7 +434,4 @@ public String createID() throws RmesException {
public boolean checkIfIndicatorExists(String id) throws RmesException {
return repositoryGestion.getResponseAsBoolean(IndicatorsQueries.checkIfExists(id));
}



}
3 changes: 0 additions & 3 deletions src/main/java/fr/insee/rmes/exceptions/ErrorCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ public class ErrorCodes {
public static final String OPERATION_OPERATION_EXISTING_PREF_LABEL_LG1 = "406_OPERATION_OPERATION_OPERATION_OPERATION_EXISTING_PREF_LABEL_LG1";
public static final String OPERATION_OPERATION_EXISTING_PREF_LABEL_LG2 = "406_OPERATION_OPERATION_OPERATION_OPERATION_EXISTING_PREF_LABEL_LG2";

public static final String OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1";
public static final String OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2";

// SIMS
public static final int SIMS_INCORRECT = 861;
public static final int SIMS_DELETION_FOR_NON_SERIES = 862;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.insee.rmes.exceptions;

import fr.insee.rmes.webservice.ConceptsCollectionsResources;
import fr.insee.rmes.webservice.ConceptsResources;
import fr.insee.rmes.webservice.UserResources;
import fr.insee.rmes.webservice.codesLists.CodeListsResources;
import fr.insee.rmes.webservice.dataset.DatasetResources;
Expand All @@ -23,6 +25,8 @@
OperationsResources.class,
DocumentsResources.class,
UserResources.class,
ConceptsResources.class,
ConceptsCollectionsResources.class

})
public class RmesExceptionHandler extends ResponseEntityExceptionHandler {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.insee.rmes.exceptions.errors;

public class IndicatorErrorCodes {

private IndicatorErrorCodes() {
}

public static final String EMPTY_WAS_GENERATED_BY = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EMPTY_WAS_GENERATED_BY";
public static final String EXISTING_PREF_LABEL_LG1 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG1";
public static final String EXISTING_PREF_LABEL_LG2 = "406_OPERATION_INDICATOR_OPERATION_INDICATOR_EXISTING_PREF_LABEL_LG2";
}
4 changes: 4 additions & 0 deletions src/main/java/fr/insee/rmes/model/operations/Indicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public List<OperationsLink> getWasGeneratedBy() {
return wasGeneratedBy;
}

public boolean isWasGenerattedByEmpty() {
return this.wasGeneratedBy == null || this.wasGeneratedBy.isEmpty();
}

public String getIdSims() {
return idSims;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -256,7 +257,7 @@ public ResponseEntity<?> getCollectionExport(@PathVariable(Constants.ID) String

@GetMapping(value = "/concept/export/{id}", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE, "application/zip" })
@Operation(operationId = "exportConcept", summary = "Blob of concept")
public ResponseEntity<?> exportConcept(@PathVariable(Constants.ID) String id, @RequestHeader(required=false) String accept) throws RmesException {
public ResponseEntity<Resource> exportConcept(@PathVariable(Constants.ID) String id, @RequestHeader(required=false) String accept) throws RmesException {
return conceptsService.exportConcept(id, accept);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -135,13 +136,13 @@ void exportConceptTest() throws RmesException, IOException, URISyntaxException {
.reduce((a,b)->a+"\n"+b).get();

// WHEN
ResponseEntity<ByteArrayResource> result = (ResponseEntity<ByteArrayResource>) conceptsImpl.exportConcept(idConcept, MediaType.APPLICATION_OCTET_STREAM_VALUE);
ResponseEntity<Resource> result = conceptsImpl.exportConcept(idConcept, MediaType.APPLICATION_OCTET_STREAM_VALUE);
// THEN
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getHeaders().getContentDisposition().getFilename()).isEqualTo("c1116accid.odt");

Diff inputsDiffs = DiffBuilder.compare(expectedXmlForOdtContent)
.withTest(getOdtContent(result.getBody().getByteArray()))
.withTest(getOdtContent(((ByteArrayResource)result.getBody()).getByteArray()))
.ignoreWhitespace()
.ignoreComments()
.build();
Expand Down
Loading
Loading