Skip to content

Commit

Permalink
#554 update Get And Post parcelle to the same method
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrejego committed Jun 21, 2021
1 parent 04fa580 commit 55670e1
Showing 1 changed file with 89 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,85 +10,73 @@
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);

@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<Map<String, Object>> getParcelleGET(@Context HttpHeaders headers, @QueryParam("parcelle") final List<String> 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<String> 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<Map<String, Object>> getParcelleEntrypoint(
@RequestHeader(CadastrappConstants.HTTP_HEADER_ROLES) String rolesList,
@RequestParam(name = "parcelle") final List<String> 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<String> 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<Map<String, Object>> getParcellePOST(@Context HttpHeaders headers,
@FormParam("parcelle") final List<String> 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<String> 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
Expand Down Expand Up @@ -119,7 +107,7 @@ public List<Map<String, Object>> getParcellePOST(@Context HttpHeaders headers,
*
* @throws SQLException
*/
private List<Map<String, Object>> getParcelle(HttpHeaders headers,final List<String> parcelleList, int details,String cgoCommune,
private List<Map<String, Object>> getParcelle(String rolesList,final List<String> parcelleList, int details,String cgoCommune,
String ccopre, String ccosec, String dnupla, String dnvoiri, String dindic, String cconvo, String dvoilib, final List<String> comptecommunalList, int uf) throws SQLException
{
List<Map<String, Object>> parcellesResult = new ArrayList<Map<String, Object>>();
Expand All @@ -128,17 +116,17 @@ private List<Map<String, Object>> getParcelle(HttpHeaders headers,final List<Str
if (parcelleList != null && !parcelleList.isEmpty()) {

List<String> 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 {
Expand Down Expand Up @@ -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]";
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);
Expand Down Expand Up @@ -494,37 +491,43 @@ 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");
}

// 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
* @return list de dnupla
*
* @throws SQLException
*/
public List<Map<String, Object>> getDnuplaList(@Context HttpHeaders headers, @QueryParam("cgocommune") String cgoCommune, @QueryParam("ccopre") String ccopre, @QueryParam("ccosec") String ccosec) throws SQLException {
public List<Map<String, Object>> getDnuplaList(
@RequestParam(name = "cgocommune") String cgoCommune,
@RequestParam String ccopre,
@RequestParam String ccosec) throws SQLException {

List<Map<String, Object>> dnuplaList = null;
List<String> queryParams = new ArrayList<String>();
Expand All @@ -545,9 +548,8 @@ public List<Map<String, Object>> 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
*
Expand All @@ -558,12 +560,12 @@ public List<Map<String, Object>> 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";

Expand All @@ -576,15 +578,18 @@ public Response exportParcellesAsSCV(
logger.debug("Nb of parcelles to search in : " + parcelleList.size());

// Get value from database
List<Map<String,Object>> parcellesResult = getParcellesById(parcelleList, 0, getUserCNILLevel(headers));
List<Map<String,Object>> 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 {
Expand All @@ -598,7 +603,7 @@ public Response exportParcellesAsSCV(
logger.info("Parcelle Id List is empty nothing to search");
}

return response.build();
return response;
}

}

0 comments on commit 55670e1

Please sign in to comment.