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 : add withCodes queryparameter for GET /listeCode/{id} #123

Merged
merged 8 commits into from
Jan 26, 2024
27 changes: 22 additions & 5 deletions src/main/java/fr/insee/rmes/controller/CodeListsResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.List;
import java.util.Objects;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping(value="/",produces = {"application/json"})
@Tag(name = "Codes lists", description = "Consultation Gestion API - listes des codes")
Expand Down Expand Up @@ -53,33 +55,48 @@ public ResponseEntity<String> getallCodesLists() throws RmesException {
}

// en fait ici l'id correspond à la notation
@GetMapping("/listeCode/{id}")
@RequestMapping(path="/listeCode/{id}", method=GET, params="withCodes=true")
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getCodesList", summary = "Get one codes list", security = @SecurityRequirement(name = "bearerScheme"), responses = {@ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(type = "array", implementation = ListCodeByIdModelSwagger.class)))})
public ResponseEntity<String> getCodesList(
@PathVariable(Constants.NOTATION) String notation,
@RequestParam(name = "dateMiseAJour", defaultValue = "false") Boolean boolDateMiseAJour
@RequestParam(name = "dateMiseAJour", defaultValue = "false") Boolean boolDateMiseAJour,
@RequestParam(name = "withCodes") Boolean boolWithCodes
) throws RmesException {

if (!boolDateMiseAJour) {
String jsonResult = codeListsServices.getCodesList(notation);
if (Objects.isNull(jsonResult) || StringUtils.isEmpty(jsonResult)) {
return ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build();
} else {

return ResponseEntity.status(HttpStatus.SC_OK).body(jsonResult);
}
} else {
}
else {
String jsonResult = codeListsServices.getCodesListDateMiseAJour(notation);
if (Objects.isNull(jsonResult) || StringUtils.isEmpty(jsonResult)) {
return ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build();
} else {

return ResponseEntity.status(HttpStatus.SC_OK).body(jsonResult);
}
}
}

@RequestMapping(path="/listeCode/{id}", method=GET, params="withCodes=false")
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getCodesList", summary = "Get one codes list", security = @SecurityRequirement(name = "bearerScheme"), responses = {@ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(type = "array", implementation = ListCodeByIdModelSwagger.class)))})
public ResponseEntity<String> getCodesListWithoutCodes(
@PathVariable(Constants.NOTATION) String notation,
@RequestParam(name = "dateMiseAJour", defaultValue = "false") Boolean boolDateMiseAJour,
@RequestParam(name = "withCodes") Boolean boolWithCodes) throws RmesException {
String jsonResult = codeListsServices.getCodesListWithoutCodes(notation);
if (Objects.isNull(jsonResult) || StringUtils.isEmpty(jsonResult)) {
return ResponseEntity.status(HttpStatus.SC_NOT_FOUND).build();
} else {
return ResponseEntity.status(HttpStatus.SC_OK).body(jsonResult);
}
}


@GetMapping("/listeCode/{id}/pagination")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/fr/insee/rmes/services/codelists/CodeListImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.rmes.services.codelists;

import fr.insee.rmes.persistence.RdfService;
import fr.insee.rmes.services.utils.CommonMethods;
import fr.insee.rmes.utils.Constants;
import fr.insee.rmes.utils.config.Config;
import fr.insee.rmes.utils.exceptions.RmesException;
Expand Down Expand Up @@ -44,8 +45,7 @@ public String getCodesList(String notation) throws RmesException {
JSONObject codesList = repoGestion.getResponseAsObject(buildRequest(Constants.CODELISTS_QUERIES_PATH,"getCodesList.ftlh", params));

codesList.put("label", this.formatLabel(codesList));
codesList.remove("prefLabelLg1");
codesList.remove("prefLabelLg2");
CommonMethods.removePrefLabels(codesList);

if(codesList.has(STATUT_VALIDATION)){
String validationState = codesList.getString(STATUT_VALIDATION);
Expand Down Expand Up @@ -258,12 +258,28 @@ public Integer getMaxpage(String notation) throws RmesException {
}
return total;
}

public String getCodesListWithoutCodes(String notation) throws RmesException{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nouvelle méthode non couverte par un test


Map<String, Object> params = initParamsNotation(notation);

JSONObject codesList = repoGestion.getResponseAsObject(buildRequest(Constants.CODELISTS_QUERIES_PATH,"getCodesList.ftlh", params));

codesList.put("label", this.formatLabel(codesList));
CommonMethods.removePrefLabels(codesList);

if(codesList.has(STATUT_VALIDATION)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Séquence de code dupliquée :

  • dans codeLIstImpl lignes 202, 50, 227, 237, 272, 285
  • dans SutrctureImpl lignes 114, 218, 260, 414

Mieux vaut factoriser dans une méthode

String validationState = codesList.getString(STATUT_VALIDATION);
codesList.put(STATUT_VALIDATION, this.getValidationState(validationState));
}
codesList.remove(Constants.URI);
return codesList.toString();
}
public String getCodesListPagination(String notation, int pageNumber)throws RmesException{
Map<String, Object> params = initParamsNotation(notation);
JSONObject result = repoGestion.getResponseAsObject(buildRequest(Constants.CODELISTS_QUERIES_PATH,"getCodesList.ftlh",params));
result.put("label", this.formatLabel(result));
result.remove("prefLabelLg1");
result.remove("prefLabelLg2");
CommonMethods.removePrefLabels(result);
if(result.has(STATUT_VALIDATION)){
String validationState = result.getString(STATUT_VALIDATION);
result.put(STATUT_VALIDATION, this.getValidationState(validationState));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface CodeListsServices {
Integer getMaxpage(String notation) throws RmesException;

String getCodesListPagination(String notation, int pageNumber) throws RmesException;

String getCodesListWithoutCodes(String notation) throws RmesException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fr.insee.rmes.persistence.RdfService;
import fr.insee.rmes.persistence.ontologies.IGEO;
import fr.insee.rmes.persistence.ontologies.QB;
import fr.insee.rmes.services.utils.CommonMethods;
import fr.insee.rmes.utils.Constants;
import fr.insee.rmes.utils.config.Config;
import fr.insee.rmes.utils.exceptions.RmesException;
Expand Down Expand Up @@ -80,8 +81,7 @@ public String getStructure(String id) throws RmesException, JsonProcessingExcept
JSONObject structure = (JSONObject) structureArray.get(0);

structure.put(LABEL, this.formatLabel(structure));
structure.remove("prefLabelLg1");
structure.remove("prefLabelLg2");
CommonMethods.removePrefLabels(structure);


if(structureArray.length() > 1){
Expand Down Expand Up @@ -240,8 +240,8 @@ public JSONObject getComponent(String id) throws RmesException {
component.put(NOM,this.formatNom(component));
component.remove("altLabelLg1");
component.remove("altLabelLg2");
component.remove("prefLabelLg1");
component.remove("prefLabelLg2");
CommonMethods.removePrefLabels(component);


if(component.has(URI_COMPONENT_PARENT_ID)){
JSONObject parent = new JSONObject();
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/fr/insee/rmes/services/utils/CommonMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.insee.rmes.services.utils;

import org.json.JSONObject;

public class CommonMethods {


public static void removePrefLabels(JSONObject jsonObject){
if (jsonObject.has("prefLabelLg1") && jsonObject.has("prefLabelLg2")){
jsonObject.remove("prefLabelLg1");
jsonObject.remove("prefLabelLg2");
}
}
}
8 changes: 8 additions & 0 deletions src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__ __
| \/ |
| \ / | __ _ __ _ _ __ ___ __ _
| |\/| |/ _` |/ _` | '_ ` _ \ / _` |
| | | | (_| | (_| | | | | | | (_| |
|_| |_|\__,_|\__, |_| |_| |_|\__,_|
__/ |
|___/
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.insee.rmes.services.codelists;

import fr.insee.rmes.persistence.RdfService;
import fr.insee.rmes.services.utils.CommonMethods;
import fr.insee.rmes.utils.Constants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static fr.insee.rmes.utils.Constants.STATUT_VALIDATION;
import static org.junit.jupiter.api.Assertions.*;

class CodeListImplTest {

public static final String CODELIST_TEST_WITH_STATUT_VALIDATION = "{\"dateMiseAJour\":\"2023-01-01T00:00:00\",\"dateCreation\":\"2023-01-01T00:00:00\",\"statutValidation\":\"Provisoire, jamais publiée\",\"id\":\"CL_TEST\"}";
public static final String CODELIST_TEST_WITHOUT_STATUT_VALIDATION = "{\"dateMiseAJour\":\"2023-01-01T00:00:00\",\"dateCreation\":\"2023-01-01T00:00:00\",\"id\":\"CL_TEST\"}";


public static final String LABEL = "[{\"langue\":\"fr\",\"contenu\":\"test\"},{\"langue\":\"en\",\"contenu\":\"test\"}]";
public static final String CODELIST_EXPECTED_WITH_STATUT_VALIDATION = "{\"dateMiseAJour\":\"2023-01-01T00:00:00\",\"dateCreation\":\"2023-01-01T00:00:00\",\"statutValidation\":\"Provisoire, jamais publiée\",\"id\":\"CL_TEST\",\"label\":[{\"langue\":\"fr\",\"contenu\":\"test\"},{\"langue\":\"en\",\"contenu\":\"test\"}]}";
public static final String CODELIST_EXPECTED_WITHOUT_STATUT_VALIDATION = "{\"dateMiseAJour\":\"2023-01-01T00:00:00\",\"dateCreation\":\"2023-01-01T00:00:00\",\"id\":\"CL_TEST\",\"label\":[{\"langue\":\"fr\",\"contenu\":\"test\"},{\"langue\":\"en\",\"contenu\":\"test\"}]}";

public static final String STATUT_VALIDATION_PUBLIEE = "Publiée";

@Test
void getCodesListWithoutCodes_With_Statut_Validation() throws JSONException {

JSONObject codesList = new JSONObject(CODELIST_TEST_WITH_STATUT_VALIDATION);
JSONArray label = new JSONArray(LABEL);
codesList.put("label",label);

if(codesList.has(STATUT_VALIDATION)){
String validationState = codesList.getString(STATUT_VALIDATION);
codesList.put(STATUT_VALIDATION, validationState);
}
codesList.remove(Constants.URI);
assertTrue(codesList.toString().equals(CODELIST_EXPECTED_WITH_STATUT_VALIDATION));

}

@Test
void getCodesListWithoutCodes_Without_Statut_Validation() throws JSONException {

JSONObject codesList = new JSONObject(CODELIST_TEST_WITHOUT_STATUT_VALIDATION);
JSONArray label = new JSONArray(LABEL);
codesList.put("label",label);

if(codesList.has(STATUT_VALIDATION)){
String validationState = codesList.getString(STATUT_VALIDATION);
codesList.put(STATUT_VALIDATION, STATUT_VALIDATION_PUBLIEE);
}
codesList.remove(Constants.URI);
assertTrue(codesList.toString().equals(CODELIST_EXPECTED_WITHOUT_STATUT_VALIDATION));

}

}