package fr.insee.rmes.api.classifications;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import fr.insee.rmes.api.MetadataApi;
import fr.insee.rmes.modeles.classification.correspondence.Associations;
import fr.insee.rmes.modeles.classification.correspondence.RawCorrespondence;
import fr.insee.rmes.queries.classifications.CorrespondencesQueries;

@Path("/correspondance")
public class CorrespondenceApi extends MetadataApi {

    @GET
    @Path("/{idCorrespondance}")
    @Produces({
        MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
    })
    public Response getCorrespondencesById(
        @PathParam("idCorrespondance") String idCorrespondance,
        @HeaderParam(value = HttpHeaders.ACCEPT) String header) {

        String csvResult =
            sparqlUtils
                .executeSparqlQuery(CorrespondencesQueries.getCorrespondenceById(idCorrespondance.toLowerCase()));

        /* RawCorrespondence direct mapping from sparql request */
        List<RawCorrespondence> rawItemsList = csvUtils.populateMultiPOJO(csvResult, RawCorrespondence.class);

        if (rawItemsList != null && ! rawItemsList.isEmpty()) {

            /* raw sparql result fields order must be got in shape 1 source -> many targets */
            Associations itemsList =
                CorrespondencesUtils.getCorrespondenceByCorrespondenceId(idCorrespondance, rawItemsList);

            return Response.ok(responseUtils.produceResponse(itemsList, header)).build();

        }

        else {

            return Response.status(Status.NOT_FOUND).entity("").build();

        }
    }

    @GET
    @Path("/{idNomenclatureSource}/{idNomenclatureCible}")
    @Produces({
        MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
    })
    public Response getCorrespondenceByIds(
        @PathParam("idNomenclatureSource") String codeClassification,
        @PathParam("idNomenclatureCible") String targetCodeClassification,
        @HeaderParam(value = HttpHeaders.ACCEPT) String header) {

        String csvResult =
            sparqlUtils
                .executeSparqlQuery(
                    CorrespondencesQueries
                        .getCorrespondenceByIds(
                            codeClassification.toLowerCase(),
                            targetCodeClassification.toLowerCase()));

        /* RawCorrespondence direct mapping from sparql request - correspondences are not symetrical in RDF model */
        List<RawCorrespondence> rawItemsList = csvUtils.populateMultiPOJO(csvResult, RawCorrespondence.class);

        if (rawItemsList != null && ! rawItemsList.isEmpty()) {

            /* raw sparql result fields order must be handled according to source / target classifications */
            Associations itemsList =
                CorrespondencesUtils
                    .getCorrespondenceByclassificationIds(codeClassification, targetCodeClassification, rawItemsList);

            return Response.ok(responseUtils.produceResponse(itemsList, header)).build();

        }

        else {

            return Response.status(Status.NOT_FOUND).entity("").build();

        }
    }

}

@@ -611,266 +611,998 @@
 es;%0A
-%0A@Path(%22/correspondance%22)%0Apublic class CorrespondenceApi extends MetadataApi %7B%0A%0A    @GET%0A    @Path(%22/%7BidCorrespondance%7D%22)%0A    @Produces(%7B%0A        MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML%0A    %7D)%0A    public Response getCorrespondencesById(%0A       
+import io.swagger.v3.oas.annotations.Operation;%0Aimport io.swagger.v3.oas.annotations.Parameter;%0Aimport io.swagger.v3.oas.annotations.media.Content;%0Aimport io.swagger.v3.oas.annotations.media.Schema;%0Aimport io.swagger.v3.oas.annotations.responses.ApiResponse;%0Aimport io.swagger.v3.oas.annotations.tags.Tag;%0A%0A@Path(%22/correspondance%22)%0A@Tag(name = %22nomenclatures%22, description = %22Nomenclatures API%22)%0Apublic class CorrespondenceApi extends MetadataApi %7B%0A%0A    @GET%0A    @Path(%22/%7BidCorrespondance%7D%22)%0A    @Produces(%7B%0A        MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML%0A    %7D)%0A    @Operation(%0A        operationId = %22getCorrespondenceByCode%22,%0A        summary = %22Correspondance entre deux nomenclatures%22,%0A        responses = %7B%0A            @ApiResponse(content = @Content(schema = @Schema(implementation = Associations.class)))%0A        %7D)%0A    public Response getCorrespondencesById(%0A        @Parameter(%0A            required = true,%0A            description = %22Identifiant de la correspondance%22)
  @Pa
@@ -1653,32 +1653,58 @@
 ondance,%0A       
+ @Parameter(hidden = true)
  @HeaderParam(va
@@ -2122,33 +2122,32 @@
 ist != null && !
- 
 rawItemsList.isE
@@ -2766,125 +2766,641 @@
     
-public Response getCorrespondenceByIds(%0A        @PathParam(%22idNomenclatureSource%22) String codeClassification,%0A       
+@Operation(%0A        operationId = %22getCorrespondenceByClassificationCodes%22,%0A        summary = %22 Liste des associations de correspondance entre deux nomenclatures%22,%0A        responses = %7B%0A            @ApiResponse(content = @Content(schema = @Schema(implementation = Associations.class)))%0A        %7D)%0A    public Response getCorrespondenceByIds(%0A        @Parameter(%0A            required = true,%0A            description = %22Identifiant de la nomenclature source%22) @PathParam(%22idNomenclatureSource%22) String codeClassification,%0A        @Parameter(%0A            required = true,%0A            description = %22Identifiant de la nomenclature cible%22)
  @Pa
@@ -3470,16 +3470,42 @@
 %0A       
+ @Parameter(hidden = true)
  @Header

Update CorrespondenceApi.java