|
| 1 | +package fr.insee.rmes.api.geo.territoire; |
| 2 | + |
| 3 | +import fr.insee.rmes.api.geo.AbstractGeoApi; |
| 4 | +import fr.insee.rmes.api.geo.ConstGeoApi; |
| 5 | +import fr.insee.rmes.modeles.geo.territoire.CodeIris; |
| 6 | +import fr.insee.rmes.modeles.geo.territoire.Iris; |
| 7 | +import fr.insee.rmes.modeles.geo.territoire.PseudoIris; |
| 8 | +import fr.insee.rmes.modeles.geo.territoire.Territoire; |
| 9 | +import fr.insee.rmes.modeles.geo.territoires.Territoires; |
| 10 | +import fr.insee.rmes.queries.geo.GeoQueries; |
| 11 | +import fr.insee.rmes.utils.*; |
| 12 | +import io.swagger.v3.oas.annotations.Operation; |
| 13 | +import io.swagger.v3.oas.annotations.Parameter; |
| 14 | +import io.swagger.v3.oas.annotations.media.Content; |
| 15 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 16 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 17 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 18 | + |
| 19 | +import javax.ws.rs.*; |
| 20 | +import javax.ws.rs.core.HttpHeaders; |
| 21 | +import javax.ws.rs.core.MediaType; |
| 22 | +import javax.ws.rs.core.Response; |
| 23 | + |
| 24 | + |
| 25 | +@Path(ConstGeoApi.PATH_GEO) |
| 26 | +@Tag(name = ConstGeoApi.TAG_NAME, description = ConstGeoApi.TAG_DESCRIPTION) |
| 27 | +public class IrisApi extends AbstractGeoApi { |
| 28 | + private static final String CODE_PATTERN = "/{code:}"; |
| 29 | + private static final String LITTERAL_PARAMETER_TYPE_DESCRIPTION = "Filtre sur le type de territoire renvoyé."; |
| 30 | + |
| 31 | + private static final String LITTERAL_RESPONSE_DESCRIPTION = "Iris"; |
| 32 | + private static final String LITTERAL_ID_OPERATION = "getcogiris"; |
| 33 | + private static final String LITTERAL_CODE_EXAMPLE = "010040101"; |
| 34 | + private static final String LITTERAL_PARAMETER_DATE_DESCRIPTION = |
| 35 | + "Filtre pour renvoyer l'Iris active à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')"; |
| 36 | + private static final String LITTERAL_OPERATION_SUMMARY = |
| 37 | + "Informations sur un Iris identifié par son code (neuf chiffres pour la métropole ou 2A/2B plus 7 chiffres pour la Corse)"; |
| 38 | + |
| 39 | + |
| 40 | + private final IrisUtils irisUtils; |
| 41 | + |
| 42 | + public IrisApi() { |
| 43 | + // Constructeur par défaut |
| 44 | + this.irisUtils = new IrisUtils(); |
| 45 | + } |
| 46 | + |
| 47 | + protected IrisApi(SparqlUtils sparqlUtils, CSVUtils csvUtils, ResponseUtils responseUtils, IrisUtils irisUtils) { |
| 48 | + super(sparqlUtils, csvUtils, responseUtils); |
| 49 | + this.irisUtils = irisUtils; |
| 50 | + } |
| 51 | + |
| 52 | + @Path(ConstGeoApi.PATH_IRIS + CODE_PATTERN) |
| 53 | + @GET |
| 54 | + @Produces({ |
| 55 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML |
| 56 | + }) |
| 57 | + @Operation(operationId = LITTERAL_ID_OPERATION, summary = LITTERAL_OPERATION_SUMMARY, responses = { |
| 58 | + @ApiResponse( |
| 59 | + content = @Content(schema = @Schema(implementation = Iris.class)), |
| 60 | + description = LITTERAL_RESPONSE_DESCRIPTION) |
| 61 | + |
| 62 | + }) |
| 63 | + public Response getByCode( |
| 64 | + @Parameter( |
| 65 | + description = ConstGeoApi.PATTERN_IRIS_DESCRIPTION, |
| 66 | + required = true, |
| 67 | + schema = @Schema( |
| 68 | + pattern = ConstGeoApi.PATTERN_IRIS, |
| 69 | + type = Constants.TYPE_STRING, example = LITTERAL_CODE_EXAMPLE)) @PathParam(Constants.CODE) String code, |
| 70 | + @Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header, |
| 71 | + @Parameter( |
| 72 | + description = LITTERAL_PARAMETER_DATE_DESCRIPTION, |
| 73 | + schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam( |
| 74 | + value = Constants.PARAMETER_DATE) String date) { |
| 75 | + |
| 76 | + var codeIris = CodeIris.of(code); |
| 77 | + if (codeIris.isInvalid()) { |
| 78 | + return generateBadRequestResponse(ConstGeoApi.ERREUR_PATTERN); |
| 79 | + } |
| 80 | + if (!this.verifyParameterDateIsRightWithoutHistory(date)) { |
| 81 | + return this.generateBadRequestResponse(); |
| 82 | + } |
| 83 | + |
| 84 | + return getResponseForIrisOrPseudoIris(codeIris, header, date); |
| 85 | + } |
| 86 | + |
| 87 | + private Response getResponseForIrisOrPseudoIris(CodeIris codeIris, String header, String date) { |
| 88 | + if (irisUtils.hasIrisDescendant(codeIris.codeCommune())) { |
| 89 | + return getResponseForIris(codeIris, header, date); |
| 90 | + } else { |
| 91 | + return getResponseForPseudoIris(codeIris, header, date); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + private Response getResponseForPseudoIris(CodeIris codeIris, String header, String date) { |
| 97 | + if (codeIris.isPseudoIrisCode()) { |
| 98 | + return this.generateResponseATerritoireByCode( |
| 99 | + sparqlUtils.executeSparqlQuery( |
| 100 | + GeoQueries.getIrisByCodeAndDate(codeIris.code(), this.formatValidParameterDateIfIsNull(date))), |
| 101 | + header, |
| 102 | + new PseudoIris(codeIris.code())); |
| 103 | + } else { |
| 104 | + return Response.status(Response.Status.NOT_FOUND).entity("").build(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private Response getResponseForIris(CodeIris codeIris, String header, String date) { |
| 109 | + if (codeIris.isPseudoIrisCode()) { |
| 110 | + return Response.status(Response.Status.NOT_FOUND).entity("").build(); |
| 111 | + } else { |
| 112 | + Territoire territoire = new Iris(codeIris.code()); |
| 113 | + return this.generateResponseATerritoireByCode( |
| 114 | + sparqlUtils.executeSparqlQuery( |
| 115 | + GeoQueries.getIrisByCodeAndDate(codeIris.code(), this.formatValidParameterDateIfIsNull(date))), |
| 116 | + header, |
| 117 | + territoire); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + @Path(ConstGeoApi.PATH_LISTE_IRIS) |
| 123 | + @GET |
| 124 | + @Produces({ |
| 125 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML |
| 126 | + }) |
| 127 | + @Operation( |
| 128 | + operationId = LITTERAL_ID_OPERATION + ConstGeoApi.ID_OPERATION_LISTE, |
| 129 | + summary = "Informations sur toutes les iris actifs à la date donnée. Par défaut, c’est la date courante.", |
| 130 | + responses = { |
| 131 | + @ApiResponse( |
| 132 | + content = @Content(schema = @Schema(type = ARRAY, implementation = Territoire.class)), |
| 133 | + description = LITTERAL_RESPONSE_DESCRIPTION) |
| 134 | + }) |
| 135 | + public Response getListe( |
| 136 | + @Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header, |
| 137 | + @Parameter( |
| 138 | + description = "Filtre pour renvoyer les Iris ou faux-Iris à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')", |
| 139 | + schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam( |
| 140 | + value = Constants.PARAMETER_DATE) String date, |
| 141 | + @Parameter(description = "les Iris (et pseudo-iris) des collectivités d'outre-mer", |
| 142 | + required = true, |
| 143 | + schema = @Schema(type = Constants.TYPE_BOOLEAN, allowableValues = {"true", "false"}, example = "false", defaultValue = "false")) |
| 144 | + @QueryParam( |
| 145 | + value = Constants.PARAMETER_STRING) Boolean com |
| 146 | + ) { |
| 147 | + |
| 148 | + if (!this.verifyParameterDateIsRightWithHistory(date)) { |
| 149 | + return this.generateBadRequestResponse(); |
| 150 | + } else { |
| 151 | + return this |
| 152 | + .generateResponseListOfTerritoire( |
| 153 | + sparqlUtils |
| 154 | + .executeSparqlQuery(GeoQueries.getListIris(this.formatValidParameterDateIfIsNull(date), this.formatValidParameterBooleanIfIsNull(com))), |
| 155 | + header, |
| 156 | + Territoires.class, |
| 157 | + Territoire.class); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + @Path(ConstGeoApi.PATH_IRIS + CODE_PATTERN + ConstGeoApi.PATH_ASCENDANT) |
| 163 | + @GET |
| 164 | + @Produces({ |
| 165 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML |
| 166 | + }) |
| 167 | + @Operation( |
| 168 | + operationId = LITTERAL_ID_OPERATION + ConstGeoApi.ID_OPERATION_ASCENDANTS, |
| 169 | + summary = "Informations concernant les territoires qui contiennent l'Iris", |
| 170 | + responses = { |
| 171 | + @ApiResponse( |
| 172 | + content = @Content(schema = @Schema(type = ARRAY, implementation = Territoire.class)), |
| 173 | + description = LITTERAL_RESPONSE_DESCRIPTION) |
| 174 | + }) |
| 175 | + public Response getAscendants( |
| 176 | + @Parameter( |
| 177 | + description = ConstGeoApi.PATTERN_IRIS_DESCRIPTION, |
| 178 | + required = true, |
| 179 | + schema = @Schema( |
| 180 | + pattern = ConstGeoApi.PATTERN_IRIS, |
| 181 | + type = Constants.TYPE_STRING, example = LITTERAL_CODE_EXAMPLE)) @PathParam(Constants.CODE) String code, |
| 182 | + @Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header, |
| 183 | + @Parameter( |
| 184 | + description = "Filtre pour renvoyer les territoires contenant l'iris actif à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')", |
| 185 | + schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam( |
| 186 | + value = Constants.PARAMETER_DATE) String date, |
| 187 | + @Parameter( |
| 188 | + description = LITTERAL_PARAMETER_TYPE_DESCRIPTION, |
| 189 | + schema = @Schema(type = Constants.TYPE_STRING)) @QueryParam( |
| 190 | + value = Constants.PARAMETER_TYPE) String typeTerritoire) { |
| 191 | + |
| 192 | + if (!this.verifyParametersTypeAndDateAreValid(typeTerritoire, date)) { |
| 193 | + return this.generateBadRequestResponse(); |
| 194 | + } else { |
| 195 | + return this |
| 196 | + .generateResponseListOfTerritoire( |
| 197 | + sparqlUtils |
| 198 | + .executeSparqlQuery( |
| 199 | + GeoQueries |
| 200 | + .getAscendantsIris( |
| 201 | + code, |
| 202 | + this.formatValidParameterDateIfIsNull(date), |
| 203 | + this.formatValidParametertypeTerritoireIfIsNull(typeTerritoire))), |
| 204 | + header, |
| 205 | + Territoires.class, |
| 206 | + Territoire.class); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments