Skip to content

Commit

Permalink
refactor (mutulualize, separate concerns, remove useless, remove redo…
Browse files Browse the repository at this point in the history
…ndant, remove elastic search) WIP

- fr.insee.rmes.utils.export.XDocReport.getCodeBookExportV2 in progress
  • Loading branch information
Fabrice B committed Dec 20, 2024
1 parent 6ce3320 commit 27ce69c
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 607 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
public class ControllerUtils {
private ControllerUtils() {}

static ResponseEntity<Resource> xmltoResponseEntity(Resource response, String fileNameWithExtension, MediaType contentType) throws IOException {
public static ResponseEntity<Resource> xmltoResponseEntity(Resource response, String fileNameWithExtension, MediaType contentType) throws IOException {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.attachment().filename(fileNameWithExtension).build().toString())
.contentLength(response.contentLength())
.contentType(contentType)
.body(response);
}

static ResponseEntity<String> xmltoResponseEntity(String xmlContent) {
public static ResponseEntity<String> xmltoResponseEntity(String xmlContent) {
return ResponseEntity.ok(xmlContent.replace("\\\"", "\"").replace("", ""));
}

Expand Down
124 changes: 5 additions & 119 deletions src/main/java/fr/insee/rmes/tocolecticaapi/controller/PostItem.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package fr.insee.rmes.tocolecticaapi.controller;


import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.exceptions.ExceptionColecticaUnreachable;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.model.DDIItemType;
import fr.insee.rmes.tocolecticaapi.models.TransactionType;
import fr.insee.rmes.tocolecticaapi.service.ColecticaService;
import fr.insee.rmes.utils.FileExtension;
import fr.insee.rmes.utils.FilesUtils;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -20,23 +18,20 @@
import org.apache.logging.log4j.Logger;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

import static fr.insee.rmes.tocolecticaapi.controller.ControllerUtils.xmltoResponseEntity;

Expand All @@ -57,7 +52,6 @@
public class PostItem {

static final Logger logger = LogManager.getLogger(PostItem.class);
public static final MediaType APPLICATION_ODT = MediaType.valueOf("application/vnd.oasis.opendocument.text");
private final ColecticaService colecticaService;

@Autowired
Expand Down Expand Up @@ -95,117 +89,9 @@ public ResponseEntity<String> uploadItem(@RequestParam("file") MultipartFile fil
@Operation(summary = "Get JSON for a type of DDI item", description = "Get a JSON list of item for a type of DDI items .")
@PreAuthorize("hasRole('Administrateur_RMESGOPS')")
public ResponseEntity<String> byType(
@PathVariable("type") DDIItemType type, @AuthenticationPrincipal
OidcUser principal)
@PathVariable("type") DDIItemType type)
throws IOException, ExceptionColecticaUnreachable, ParseException {

return colecticaService.getByType(type);
}


@PostMapping(value = "/operation/codebook",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, "application/vnd.oasis.opendocument.text"},
produces = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, "application/vnd.oasis.opendocument.text"}
)
@PreAuthorize("hasRole('Administrateur_RMESGOPS')")
@Operation(operationId = "getCodeBook", summary = "Produce a codebook from a DDI")

public ResponseEntity<Resource> getCodeBook(

@Parameter(schema = @Schema(type = "string", format = "String", description = "Accept"))
@RequestHeader(required = false) String accept,

@Parameter(schema = @Schema(type = "string", format = "binary", description = "file in DDI"))
@RequestParam(value = "file") MultipartFile isDDI, // InputStream isDDI,

@Parameter(schema = @Schema(type = "string", format = "binary", description = "file for structure"))
@RequestParam(value = "dicoVar") MultipartFile isCodeBook //InputStream isCodeBook

)
throws RmesException {
logger.info("Generate CodeBook from DDI {}, {}", isDDI.getOriginalFilename(), isCodeBook.getOriginalFilename());
String ddi;
File codeBookFile;
try {
ddi = new String(isDDI.getBytes(), StandardCharsets.UTF_8);
codeBookFile = FilesUtils.streamToFile(isCodeBook.getInputStream(), "dicoVar", ".odt");
Resource response = colecticaService.exportCodeBookAsOdt(ddi, codeBookFile);
logger.debug("Codebook is generated");
return xmltoResponseEntity(response, "Codebook" + FileExtension.forHeader(accept).extension(), APPLICATION_ODT);
} catch (IOException e) {
throw new RmesException(HttpStatus.BAD_REQUEST, e.getMessage(), "Files can't be read");
}
}

@PostMapping(value = "/operation/codebook/V2",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, "application/vnd.oasis.opendocument.text"}
)
@PreAuthorize("hasRole('Administrateur_RMESGOPS')")
@Operation(operationId = "getCodeBookV2", summary = "Produce a codebook from a DDI")
public ResponseEntity<?> getCodeBookV2(

@Parameter(schema = @Schema(type = "string", format = "String", description = "Accept"))
@RequestHeader(required = false) String accept,

@Parameter(schema = @Schema(type = "string", allowableValues = {"concis", "concis avec expression", "scindable", "non scindable"}))
@RequestParam(value = "dicoVar") String isCodeBook, //InputStream isCodeBook,

@RequestParam(value = "file") MultipartFile isDDI // InputStream isDDI,

)
throws Exception {
if (isDDI == null)
throw new RmesException(HttpStatus.INTERNAL_SERVER_ERROR, "Can't generate codebook", "Stream is null");
InputStream ddiInputStream = new BufferedInputStream(isDDI.getInputStream());
String ddi = new String(ddiInputStream.readAllBytes(), StandardCharsets.UTF_8);
String xslPatternFile = null;
switch (isCodeBook) {
case "concis":
String xmlFileConcis = "/xslTransformerFiles/dicoCodes/dicoConcisPatternContent.xml";
xslPatternFile = xmlFileConcis;
break;
case "concis avec expression":
String xmlFileConcisAvecExpression = "/xslTransformerFiles/dicoCodes/dicoConcisDescrPatternContent.xml";
xslPatternFile = xmlFileConcisAvecExpression;
break;
case "scindable":
String xmlFileScindable = "/xslTransformerFiles/dicoCodes/dicoScindablePatternContent.xml";
xslPatternFile = xmlFileScindable;
break;
case "non scindable":
String xmlFileNonScindable = "/xslTransformerFiles/dicoCodes/dicoNonScindablePatternContent.xml";
xslPatternFile = xmlFileNonScindable;
break;
default:
logger.error("Choix incorrect");
break;
}

return colecticaService.getCodeBookExportV2(ddi, xslPatternFile);
}

@PostMapping(value = "/operation/codebook/checkCodeBookContent",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, "application/vnd.oasis.opendocument.text"},
produces = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, "application/vnd.oasis.opendocument.text"}
)
@PreAuthorize("hasRole('Administrateur_RMESGOPS')")
@Operation(operationId = "getCodeBookCheck", summary = "Check the DDI before made the codebook export")

public ResponseEntity<?> getCodeBookCheck(

@RequestParam(value = "file") MultipartFile isCodeBook // InputStream isDDI,

)
throws Exception {

return colecticaService.getCodeBookCheck(isCodeBook);
}

protected ResponseEntity<Object> returnRmesException(RmesException e) {
logger.error(e.getMessageAndDetails(), e);
return ResponseEntity.status(e.getStatus()).contentType(MediaType.TEXT_PLAIN).body(e.getMessage());
}


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package fr.insee.rmes.tocolecticaapi.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.exceptions.ExceptionColecticaUnreachable;
import fr.insee.rmes.exceptions.RmesException;
import fr.insee.rmes.exceptions.RmesExceptionIO;
import fr.insee.rmes.model.DDIItemType;
import fr.insee.rmes.tocolecticaapi.models.TransactionType;
import fr.insee.rmes.exceptions.RmesExceptionIO;
import org.json.JSONArray;
import org.json.simple.parser.ParseException;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public interface ColecticaService {
String findFragmentByUuid(String uuid);

String sendDeleteColectica(String uuid, TransactionType transactionType) throws JsonProcessingException, ExceptionColecticaUnreachable, RmesExceptionIO, ParseException;
String sendDeleteColectica(String uuid, TransactionType transactionType) throws JsonProcessingException, ExceptionColecticaUnreachable, RmesExceptionIO, ParseException, RmesException;
String searchColecticaInstanceByUuid(String uuid) throws RmesException;
JSONArray findFragmentByUuidWithChildren(String uuid) throws Exception;
String filteredSearchText(String index, String texte) throws RmesException;
Expand All @@ -36,10 +32,4 @@ public interface ColecticaService {

void sendUpdateColectica(String ddiUpdatingInJson, TransactionType transactionType) throws RmesException;

Resource exportCodeBookAsOdt(String ddiFile, File dicoVar) throws RmesException;

ResponseEntity<Resource> getCodeBookExportV2(String ddiFile, String xslPatternFile) throws RmesException, Exception;

ResponseEntity<?> getCodeBookCheck(MultipartFile isCodeBook) throws RmesException, Exception;

}
Loading

0 comments on commit 27ce69c

Please sign in to comment.