From 55670e1ad835b164c6c96acb321f63d14ed3c7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20J=C3=A9go?= Date: Mon, 21 Jun 2021 22:38:25 +0200 Subject: [PATCH] #554 update Get And Post parcelle to the same method --- .../service/ParcelleController.java | 173 +++++++++--------- 1 file changed, 89 insertions(+), 84 deletions(-) diff --git a/cadastrapp/src/main/java/org/georchestra/cadastrapp/service/ParcelleController.java b/cadastrapp/src/main/java/org/georchestra/cadastrapp/service/ParcelleController.java index 0f2db7d7..c92fa94a 100644 --- a/cadastrapp/src/main/java/org/georchestra/cadastrapp/service/ParcelleController.java +++ b/cadastrapp/src/main/java/org/georchestra/cadastrapp/service/ParcelleController.java @@ -10,36 +10,36 @@ import java.util.List; import java.util.Map; -import javax.ws.rs.Consumes; -import javax.ws.rs.DefaultValue; -import javax.ws.rs.FormParam; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; - import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.georchestra.cadastrapp.model.pdf.ExtFormResult; import org.georchestra.cadastrapp.service.export.ExportHelper; +import org.georchestra.cadastrapp.service.constants.CadastrappConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + /** * Parcelle controller expose all rest service for plots information */ +@Controller public class ParcelleController extends CadController { static final Logger logger = LoggerFactory.getLogger(ParcelleController.class); @@ -47,48 +47,36 @@ public class ParcelleController extends CadController { @Autowired ExportHelper exportHelper; - @GET - @Path("/getParcelle") - @Produces(MediaType.APPLICATION_JSON) + @GetMapping + @PostMapping + @RequestMapping(path = "/getParcelle", produces = {MediaType.APPLICATION_JSON_VALUE}) /** * Works like {@link #getParcelle} */ - public List> getParcelleGET(@Context HttpHeaders headers, @QueryParam("parcelle") final List parcelleList, @DefaultValue("0") @QueryParam("details") int details, @QueryParam("cgocommune") String cgoCommune, @QueryParam("ccopre") String ccopre, @QueryParam("ccosec") String ccosec, @QueryParam("dnupla") String dnupla, - @QueryParam("dnvoiri") String dnvoiri, @QueryParam("dlindic") String dindic, @QueryParam("cconvo") String cconvo, @QueryParam("dvoilib") String dvoilib, @QueryParam("comptecommunal") final List comptecommunalList, @DefaultValue("0") @QueryParam("unitefonciere") int uf) throws SQLException { - - return getParcelle(headers, parcelleList, details, cgoCommune, ccopre, ccosec, dnupla, dnvoiri, dindic, cconvo, dvoilib, comptecommunalList, uf); + public List> getParcelleEntrypoint( + @RequestHeader(CadastrappConstants.HTTP_HEADER_ROLES) String rolesList, + @RequestParam(name = "parcelle") final List parcelleList, + @RequestParam(defaultValue = "0") int details, + @RequestParam(name ="cgocommune") String cgoCommune, + @RequestParam String ccopre, + @RequestParam String ccosec, + @RequestParam String dnupla, + @RequestParam String dnvoiri, + @RequestParam(name = "dlindic") String dindic, + @RequestParam String cconvo, + @RequestParam String dvoilib, + @RequestParam(name = "comptecommunal") final List comptecommunalList, + @RequestParam(name = "unitefonciere", defaultValue = "0") int uf) throws SQLException { + + return getParcelle(rolesList, parcelleList, details, cgoCommune, ccopre, ccosec, dnupla, dnvoiri, dindic, cconvo, dvoilib, comptecommunalList, uf); } - - @POST - @Path("/getParcelle") - @Produces(MediaType.APPLICATION_JSON) - /** - * Works like {@link #getParcelle} - */ - public List> getParcellePOST(@Context HttpHeaders headers, - @FormParam("parcelle") final List parcelleList, - @DefaultValue("0") @FormParam("details") int details, - @FormParam("cgocommune") String cgoCommune, - @FormParam("ccopre") String ccopre, - @FormParam("ccosec") String ccosec, - @FormParam("dnupla") String dnupla, - @FormParam("dnvoiri") String dnvoiri, - @FormParam("dlindic") String dindic, - @FormParam("cconvo") String cconvo, - @FormParam("dvoilib") String dvoilib, - @FormParam("comptecommunal") final List comptecommunalList, - @DefaultValue("0") @FormParam("unitefonciere") int uf) throws SQLException { - - return getParcelle(headers, parcelleList, details, cgoCommune, ccopre, ccosec, dnupla, dnvoiri, dindic, cconvo, dvoilib, comptecommunalList, uf); - - } - + /** * * getParcelle * - * @param headers http headers, used to get ldap role information about the user group + * @param rolesList used to get ldap role information about the user group * @param parcelleList * could be LIST if one or more element, if only one in the list, * this element could contains list of parcelleids separated by @@ -119,7 +107,7 @@ public List> getParcellePOST(@Context HttpHeaders headers, * * @throws SQLException */ - private List> getParcelle(HttpHeaders headers,final List parcelleList, int details,String cgoCommune, + private List> getParcelle(String rolesList,final List parcelleList, int details,String cgoCommune, String ccopre, String ccosec, String dnupla, String dnvoiri, String dindic, String cconvo, String dvoilib, final List comptecommunalList, int uf) throws SQLException { List> parcellesResult = new ArrayList>(); @@ -128,17 +116,17 @@ private List> getParcelle(HttpHeaders headers,final List parsedParcelleList = prepareParcelleList(parcelleList); - parcellesResult = getParcellesById(parsedParcelleList, details, getUserCNILLevel(headers)); + parcellesResult = getParcellesById(parsedParcelleList, details, getUserCNILLevel(rolesList)); // Search by Proprietaire } else if (comptecommunalList != null && !comptecommunalList.isEmpty()) { - parcellesResult = getParcellesByProprietaire(comptecommunalList, details, getUserCNILLevel(headers)); + parcellesResult = getParcellesByProprietaire(comptecommunalList, details, getUserCNILLevel(rolesList)); // Search by unitefonciere } else if (uf != 0) { - parcellesResult = getParcellesByUniteFonciere(uf, details, getUserCNILLevel(headers)); + parcellesResult = getParcellesByUniteFonciere(uf, details, getUserCNILLevel(rolesList)); // Search by attributes } else { @@ -381,17 +369,18 @@ private String createSelectParcelleQuery(int details) { /** * Service witch use csv file as input * - * @param headers http headers information + * @param roleList fromhttp headers information * @param fileContent * parcelleId separated by space, ',' or ';' * * @return Json data, with corresponding parcelleId information * */ - @POST - @Path("/fromParcellesFile") - @Consumes(MediaType.MULTIPART_FORM_DATA) - public Response getFromParcellesFile(@Context HttpHeaders headers, @FormParam("filePath") String fileContent) { + @PostMapping + @RequestMapping(path = "/fromParcellesFile", consumes = {"multipart/form-data" }) + public ResponseEntity getFromParcellesFile(@RequestHeader(CadastrappConstants.HTTP_HEADER_ROLES) String rolesList, @RequestParam(name = "filePath") String fileContent) { + + ResponseEntity response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); // space, , or ; String delimitersRegex = "[\\s\\;\\,\\n]"; @@ -426,7 +415,7 @@ public Response getFromParcellesFile(@Context HttpHeaders headers, @FormParam("f // Avoid make request if no parcelle id is given if (parcelleList != null && !parcelleList.isEmpty()) { - parcellesResult = getParcellesById(parcelleList, 0, getUserCNILLevel(headers)); + parcellesResult = getParcellesById(parcelleList, 0, getUserCNILLevel(rolesList)); } else { logger.warn("No information given to create csv"); } @@ -435,32 +424,40 @@ public Response getFromParcellesFile(@Context HttpHeaders headers, @FormParam("f // success=true) ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(new ExtFormResult(true, parcellesResult)); - return Response.ok(json, MediaType.TEXT_HTML).build(); + + // Create response + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.TEXT_HTML); + response = new ResponseEntity<>(json, headers, HttpStatus.OK); } catch (IOException e) { logger.error("Error while trying to read input data ", e); - return Response.serverError().build(); } catch (SQLException e) { logger.error("Error while trying to get information from database ", e); - return Response.serverError().build(); } + return response; } /** * Get export of parcelle for given comptecommunal * - * @param headers + * @param rolesList * make sure user have CNIL level * @param details 1 to have detailed information * @param city cgocommune information from form * @param fileContent file content * @return form validation */ - @POST - @Path("/fromProprietairesFile") - @Consumes(MediaType.MULTIPART_FORM_DATA) - public Response getFromProprietairesFile(@Context HttpHeaders headers, @DefaultValue("0") @FormParam("details") int details, @FormParam("cgocommune") String city, @FormParam("filePath") String fileContent) { + @PostMapping + @RequestMapping(path = "/fromProprietairesFile", consumes = {"multipart/form-data"}) + public ResponseEntity getFromProprietairesFile( + @RequestHeader(CadastrappConstants.HTTP_HEADER_ROLES) String rolesList, + @RequestParam(defaultValue = "0") int details, + @RequestParam(name = "cgocommune") String city, + @RequestParam(name = "filePath") String fileContent) { + + ResponseEntity response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); if (logger.isDebugEnabled()) { logger.debug("csv content : " + fileContent); @@ -494,7 +491,7 @@ public Response getFromProprietairesFile(@Context HttpHeaders headers, @DefaultV // Avoid call without parameter if (proprietaireList != null && !proprietaireList.isEmpty()) { - ownersResult = getParcellesByProprietaire(proprietaireList, details, getUserCNILLevel(headers)); + ownersResult = getParcellesByProprietaire(proprietaireList, details, getUserCNILLevel(rolesList)); } else { logger.warn("No information given to get CompteCommunal information"); } @@ -502,21 +499,24 @@ public Response getFromProprietairesFile(@Context HttpHeaders headers, @DefaultV // les forms ExtJs attendent du JSON sous format TEXT/HTML... (avec success=true) ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json = ow.writeValueAsString(new ExtFormResult(true, ownersResult)); - return Response.ok(json, MediaType.TEXT_HTML).build(); + + // Create response + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.TEXT_HTML); + response = new ResponseEntity<>(json, headers, HttpStatus.OK); + } catch (IOException e) { logger.error("Error while trying to read input data ", e); - return Response.serverError().build(); } + return response; } - @GET - @Path("/getDnuplaList") - @Produces("application/json") + @GetMapping + @RequestMapping(path = "/getDnuplaList", produces = {MediaType.APPLICATION_JSON_VALUE}) /** * Return only dnupla list from a section of a commune * - * @param headers http headers, used to get ldap role information about the user group * @param cgocommune code commune INSEE * @param ccopre prefix de section * @param ccosec code de section @@ -524,7 +524,10 @@ public Response getFromProprietairesFile(@Context HttpHeaders headers, @DefaultV * * @throws SQLException */ - public List> getDnuplaList(@Context HttpHeaders headers, @QueryParam("cgocommune") String cgoCommune, @QueryParam("ccopre") String ccopre, @QueryParam("ccosec") String ccosec) throws SQLException { + public List> getDnuplaList( + @RequestParam(name = "cgocommune") String cgoCommune, + @RequestParam String ccopre, + @RequestParam String ccosec) throws SQLException { List> dnuplaList = null; List queryParams = new ArrayList(); @@ -545,9 +548,8 @@ public List> getDnuplaList(@Context HttpHeaders headers, @Qu return dnuplaList; } - @POST - @Path("/exportParcellesAsCSV") - @Produces("text/csv") + @PostMapping + @RequestMapping(path = "/exportParcellesAsCSV", produces = {"text/csv;charset=utf-8"}) /** * Create a csv file from given parcelles id * @@ -558,12 +560,12 @@ public List> getDnuplaList(@Context HttpHeaders headers, @Qu * * @throws SQLException */ - public Response exportParcellesAsSCV( - @Context HttpHeaders headers, - @FormParam("parcelles") String parcelles) throws SQLException { + public ResponseEntity exportParcellesAsSCV( + @RequestHeader(CadastrappConstants.HTTP_HEADER_ROLES) String rolesList, + @RequestParam String parcelles) throws SQLException { // Create empty content - ResponseBuilder response = Response.noContent(); + ResponseEntity response = new ResponseEntity<>(HttpStatus.NO_CONTENT); String entete = "parcelle; commune;voie_adr;voie_adr_cplmt;voie_type;voie_nom;section_prefixe;section;parcelle_num;contenance"; @@ -576,15 +578,18 @@ public Response exportParcellesAsSCV( logger.debug("Nb of parcelles to search in : " + parcelleList.size()); // Get value from database - List> parcellesResult = getParcellesById(parcelleList, 0, getUserCNILLevel(headers)); + List> parcellesResult = getParcellesById(parcelleList, 0, getUserCNILLevel(rolesList)); File file = null; try{ file = exportHelper.createCSV(parcellesResult, entete); // build csv response - response = Response.ok((Object) file); - response.header("Content-Disposition", "attachment; filename=" + file.getName()); + // build csv response + HttpHeaders headers = new HttpHeaders(); + headers.setContentDispositionFormData("filename", file.getName()); + response = new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); + }catch (IOException e) { logger.error("Error while creating CSV files ", e); } finally { @@ -598,7 +603,7 @@ public Response exportParcellesAsSCV( logger.info("Parcelle Id List is empty nothing to search"); } - return response.build(); + return response; } }