|
| 1 | +package fr.insee.rmes.api.geo.territoire; |
| 2 | + |
| 3 | +import javax.ws.rs.GET; |
| 4 | +import javax.ws.rs.HeaderParam; |
| 5 | +import javax.ws.rs.Path; |
| 6 | +import javax.ws.rs.PathParam; |
| 7 | +import javax.ws.rs.Produces; |
| 8 | +import javax.ws.rs.QueryParam; |
| 9 | +import javax.ws.rs.core.HttpHeaders; |
| 10 | +import javax.ws.rs.core.MediaType; |
| 11 | +import javax.ws.rs.core.Response; |
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
| 14 | +import fr.insee.rmes.api.geo.AbstractGeoApi; |
| 15 | +import fr.insee.rmes.api.geo.ConstGeoApi; |
| 16 | +import fr.insee.rmes.modeles.geo.territoire.CollectiviteDOutreMer; |
| 17 | +import fr.insee.rmes.modeles.geo.territoire.Commune; |
| 18 | +import fr.insee.rmes.modeles.geo.territoire.Territoire; |
| 19 | +import fr.insee.rmes.modeles.geo.territoires.CollectivitesDOutreMer; |
| 20 | +import fr.insee.rmes.modeles.geo.territoires.Territoires; |
| 21 | +import fr.insee.rmes.queries.geo.GeoQueries; |
| 22 | +import fr.insee.rmes.utils.Constants; |
| 23 | +import io.swagger.v3.oas.annotations.Operation; |
| 24 | +import io.swagger.v3.oas.annotations.Parameter; |
| 25 | +import io.swagger.v3.oas.annotations.media.Content; |
| 26 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 27 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 28 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 29 | + |
| 30 | + |
| 31 | + @Path(ConstGeoApi.PATH_GEO) |
| 32 | + @Tag(name = ConstGeoApi.TAG_NAME, description = ConstGeoApi.TAG_DESCRIPTION) |
| 33 | + |
| 34 | + public class CollectivitesDOutreMerAPI extends AbstractGeoApi { |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + private static Logger logger = LogManager.getLogger(CollectivitesDOutreMerAPI.class); |
| 39 | + private static final String CODE_PATTERNCOM = "/{code: " + ConstGeoApi.PATTERN_COM + "}"; |
| 40 | + private static final String LITTERAL_ID_OPERATION = "getcogcom"; |
| 41 | + private static final String LITTERAL_OPERATION_SUMMARY = |
| 42 | + "Informations sur une collectivité d'outre-mer identifiée par son code (cinq caractères)"; |
| 43 | + private static final String LITTERAL_RESPONSE_DESCRIPTION = "collectivité d'outre-mer"; |
| 44 | + private static final String LITTERAL_PARAMETER_DATE_DESCRIPTION = |
| 45 | + "Filtre pour renvoyer la collectivite d'outre-mer active à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')"; |
| 46 | + private static final String LITTERAL_PARAMETER_TYPE_DESCRIPTION = "Filtre sur le type de territoire renvoyé."; |
| 47 | + private static final String LITTERAL_CODE_EXAMPLE = "988"; |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + @Path(ConstGeoApi.PATH_LISTE_COM) |
| 52 | + @GET |
| 53 | + @Produces({ |
| 54 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML |
| 55 | + }) |
| 56 | + |
| 57 | + @Operation( |
| 58 | + operationId = LITTERAL_ID_OPERATION + ConstGeoApi.ID_OPERATION_LISTE, |
| 59 | + summary = "Informations sur toutes les collectivités d'outre-mer actives à la date donnée. Par défaut, c’est la date courante.", |
| 60 | + responses = { |
| 61 | + @ApiResponse( |
| 62 | + content = @Content(schema = @Schema(type = ARRAY, implementation = Commune.class)), |
| 63 | + description = LITTERAL_RESPONSE_DESCRIPTION) |
| 64 | + }) |
| 65 | + |
| 66 | + public Response getListe( |
| 67 | + @Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header, |
| 68 | + @Parameter( |
| 69 | + description = "Filtre pour renvoyer les collectivités d'outre-mer actives à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')" + LITTERAL_PARAMETER_DATE_WITH_HISTORY, |
| 70 | + required = false, |
| 71 | + schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam( |
| 72 | + value = Constants.PARAMETER_DATE) String date) |
| 73 | + |
| 74 | + { |
| 75 | + |
| 76 | + logger.debug("Received GET request for all collectivités d'outre-mer"); |
| 77 | + |
| 78 | + if ( ! this.verifyParameterDateIsRightWithHistory(date)) { |
| 79 | + return this.generateBadRequestResponse(); |
| 80 | + } |
| 81 | + else { |
| 82 | + return this |
| 83 | + .generateResponseListOfTerritoire( |
| 84 | + sparqlUtils.executeSparqlQuery(GeoQueries.getListCollectivitesDOutreMer(this.formatValidParameterDateIfIsNull(date))), |
| 85 | + header, |
| 86 | + CollectivitesDOutreMer.class, |
| 87 | + CollectiviteDOutreMer.class ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + @Path(ConstGeoApi.PATH_COM + CODE_PATTERNCOM) |
| 93 | + @GET |
| 94 | + @Produces({ |
| 95 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML |
| 96 | + }) |
| 97 | + @Operation( |
| 98 | + operationId = LITTERAL_ID_OPERATION, |
| 99 | + summary = LITTERAL_OPERATION_SUMMARY, |
| 100 | + responses = { |
| 101 | + @ApiResponse( |
| 102 | + content = @Content(schema = @Schema(implementation = CollectiviteDOutreMer.class)), |
| 103 | + description = LITTERAL_RESPONSE_DESCRIPTION) |
| 104 | + }) |
| 105 | + public Response getByCode( |
| 106 | + @Parameter( |
| 107 | + description = ConstGeoApi.PATTERN_COM_DESCRIPTION, |
| 108 | + required = true, |
| 109 | + schema = @Schema( |
| 110 | + pattern = ConstGeoApi.PATTERN_COM, |
| 111 | + type = Constants.TYPE_STRING, example=LITTERAL_CODE_EXAMPLE)) @PathParam(Constants.CODE) String code, |
| 112 | + @Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header, |
| 113 | + @Parameter( |
| 114 | + description = LITTERAL_PARAMETER_DATE_DESCRIPTION, |
| 115 | + required = false, |
| 116 | + schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam( |
| 117 | + value = Constants.PARAMETER_DATE) String date) { |
| 118 | + |
| 119 | + logger.debug("Received GET request for collectivite d'outre-mer {}", code); |
| 120 | + |
| 121 | + if ( ! this.verifyParameterDateIsRightWithoutHistory(date)) { |
| 122 | + return this.generateBadRequestResponse(); |
| 123 | + } |
| 124 | + else { |
| 125 | + return this |
| 126 | + .generateResponseATerritoireByCode( |
| 127 | + sparqlUtils |
| 128 | + .executeSparqlQuery( |
| 129 | + GeoQueries.getCollectiviteDOutreMerByCodeAndDate(code, this.formatValidParameterDateIfIsNull(date))), |
| 130 | + header, |
| 131 | + new CollectiviteDOutreMer(code)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + @Path(ConstGeoApi.PATH_COM + CODE_PATTERNCOM + ConstGeoApi.PATH_DESCENDANT) |
| 137 | + @GET |
| 138 | + @Produces({ |
| 139 | + MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML |
| 140 | + }) |
| 141 | + @Operation( |
| 142 | + operationId = LITTERAL_ID_OPERATION + ConstGeoApi.ID_OPERATION_DESCENDANTS, |
| 143 | + summary = "Informations concernant les territoires inclus dans la collectivite d'outre-mer", |
| 144 | + responses = { |
| 145 | + @ApiResponse( |
| 146 | + content = @Content(schema = @Schema(type = ARRAY, implementation = Territoire.class)), |
| 147 | + description = LITTERAL_RESPONSE_DESCRIPTION) |
| 148 | + }) |
| 149 | + public Response getDescendants( |
| 150 | + @Parameter( |
| 151 | + description = "code de la collectivité d'outre-mer (3 caractères)", |
| 152 | + required = true, |
| 153 | + schema = @Schema( |
| 154 | + pattern = ConstGeoApi.PATTERN_COM, |
| 155 | + type = Constants.TYPE_STRING, example=LITTERAL_CODE_EXAMPLE)) @PathParam(Constants.CODE) String code, |
| 156 | + @Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header, |
| 157 | + @Parameter( |
| 158 | + description ="Filtre pour renvoyer les territoires inclus dans la collectivité d'outre-mer active à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')", |
| 159 | + required = false, |
| 160 | + schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam( |
| 161 | + value = Constants.PARAMETER_DATE) String date, |
| 162 | + @Parameter( |
| 163 | + description = LITTERAL_PARAMETER_TYPE_DESCRIPTION+ "(Commune ou District)", |
| 164 | + required = false, |
| 165 | + schema = @Schema(type = Constants.TYPE_STRING)) @QueryParam( |
| 166 | + value = Constants.PARAMETER_TYPE) String typeTerritoire, |
| 167 | + @Parameter( |
| 168 | + description = "Filtre sur le nom du ou des territoires renvoyés", |
| 169 | + required = false, |
| 170 | + schema = @Schema(type = Constants.TYPE_STRING)) @QueryParam( |
| 171 | + value = Constants.PARAMETER_FILTRE) String filtreNom) { |
| 172 | + |
| 173 | + logger.debug("Received GET request for descendants of collectivite d'outre-mer {}", code); |
| 174 | + |
| 175 | + if ( ! this.verifyParametersTypeAndDateAreValid(typeTerritoire, date)) { |
| 176 | + return this.generateBadRequestResponse(); |
| 177 | + } |
| 178 | + else { |
| 179 | + return this.generateResponseListOfTerritoire( |
| 180 | + sparqlUtils.executeSparqlQuery(GeoQueries.getDescendantsCollectiviteDOutreMer(code, |
| 181 | + this.formatValidParameterDateIfIsNull(date), |
| 182 | + this.formatValidParametertypeTerritoireIfIsNull(typeTerritoire), |
| 183 | + this.formatValidParameterFiltreIfIsNull(filtreNom))), |
| 184 | + header, Territoires.class, Territoire.class); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + } |
0 commit comments