Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: review doc export #675

Merged
merged 5 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/main/java/fr/insee/rmes/bauhaus_services/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public class Constants {
public static final String DOCUMENT = "document";
public static final String DOCUMENTS_LG1 = "documentsLg1";
public static final String DOCUMENTS_LG2 = "documentsLg2";
public static final String DOT_XML = ".xml";
public static final String DOT_ZIP = ".zip";

EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved

/*E*/
public static final String EDITORIAL_NOTE_LG1 = "editorialNoteLg1";
Expand Down Expand Up @@ -103,9 +102,7 @@ public class Constants {
public static final String SEEALSO = "seeAlso";
public static final String SERIES_UP = "SERIES";
public static final String STAMP = "stamp";
public static final String STATUT_VALIDATION = "statutValidation";


/*T*/
public static final String TEXT = "text";
public static final String TEXT_LG1 = "texte";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.insee.rmes.bauhaus_services.OrganizationsService;
import fr.insee.rmes.bauhaus_services.code_list.CodeList;
import fr.insee.rmes.bauhaus_services.operations.ParentUtils;
import fr.insee.rmes.bauhaus_services.operations.documentations.documents.DocumentsUtils;
import fr.insee.rmes.bauhaus_services.operations.indicators.IndicatorsUtils;
import fr.insee.rmes.bauhaus_services.operations.operations.OperationsUtils;
import fr.insee.rmes.bauhaus_services.operations.series.SeriesUtils;
Expand All @@ -13,26 +14,34 @@
import fr.insee.rmes.model.operations.Operation;
import fr.insee.rmes.model.operations.Series;
import fr.insee.rmes.model.operations.documentations.MSD;
import fr.insee.rmes.utils.EncodingType;
import fr.insee.rmes.utils.ExportUtils;
import fr.insee.rmes.utils.XMLUtils;
import fr.insee.rmes.utils.XsltUtils;
import fr.insee.rmes.utils.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.stream.Collectors;

import static fr.insee.rmes.bauhaus_services.Constants.GOAL_COMITE_LABEL;
import static fr.insee.rmes.bauhaus_services.Constants.GOAL_RMES;

@Component
public class DocumentationExport {
static final Logger logger = LoggerFactory.getLogger(DocumentationExport.class);

public static final String DOCUMENTATION = "documentation";
final ExportUtils exportUtils;
Expand All @@ -50,15 +59,16 @@ public class DocumentationExport {
final OrganizationsService organizationsServiceImpl;

final DocumentationsUtils documentationsUtils;
final DocumentsUtils documentsUtils;
static final String xslFile = "/xslTransformerFiles/sims2fodt.xsl";
static final String xmlPatternRmes = "/xslTransformerFiles/simsRmes/rmesPatternContent.xml";
static final String zipRmes = "/xslTransformerFiles/simsRmes/toZipForRmes.zip";

static String xslFile = "/xslTransformerFiles/sims2fodt.xsl";
static String xmlPatternRmes = "/xslTransformerFiles/simsRmes/rmesPatternContent.xml";
static String zipRmes = "/xslTransformerFiles/simsRmes/toZipForRmes.zip";

static String xmlPatternLabel = "/xslTransformerFiles/simsLabel/labelPatternContent.xml";
static String zipLabel = "/xslTransformerFiles/simsLabel/toZipForLabel.zip";
static final String xmlPatternLabel = "/xslTransformerFiles/simsLabel/labelPatternContent.xml";
static final String zipLabel = "/xslTransformerFiles/simsLabel/toZipForLabel.zip";
private final int maxLength;

public DocumentationExport(ExportUtils exportUtils, SeriesUtils seriesUtils, OperationsUtils operationsUtils, IndicatorsUtils indicatorsUtils, ParentUtils parentUtils, CodeListService codeListServiceImpl, OrganizationsService organizationsServiceImpl, DocumentationsUtils documentationsUtils) {
public DocumentationExport(@Value("${fr.insee.rmes.bauhaus.filenames.maxlength}") int maxLength, DocumentsUtils documentsUtils, ExportUtils exportUtils, SeriesUtils seriesUtils, OperationsUtils operationsUtils, IndicatorsUtils indicatorsUtils, ParentUtils parentUtils, CodeListService codeListServiceImpl, OrganizationsService organizationsServiceImpl, DocumentationsUtils documentationsUtils) {
this.exportUtils = exportUtils;
this.seriesUtils = seriesUtils;
this.operationsUtils = operationsUtils;
Expand All @@ -67,6 +77,8 @@ public DocumentationExport(ExportUtils exportUtils, SeriesUtils seriesUtils, Ope
this.codeListServiceImpl = codeListServiceImpl;
this.organizationsServiceImpl = organizationsServiceImpl;
this.documentationsUtils = documentationsUtils;
this.documentsUtils = documentsUtils;
this.maxLength = maxLength;
}

/**
Expand All @@ -86,13 +98,98 @@ public ResponseEntity<Resource> exportAsResponse(String id, Map<String, String>
Exporter exporter;
if (documents) {
JSONObject sims = this.documentationsUtils.getDocumentationByIdSims(id);
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportUtils.exportAsZip(sims, xml, xsl, xmlPattern, zip, documentation );
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportAsZip(sims, xml, xsl, xmlPattern, zip, documentation );
} else{
exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportUtils.exportAsODT(id, xml, xsl, xmlPattern, zip, documentation );
}
return export(exporter, xmlContent, patternAndZip);
}

public ResponseEntity<Resource> exportAsZip(JSONObject sims, Map<String, String> xmlContent, String xslFile, String xmlPattern, String zip, String objectType) throws RmesException {
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
String simsId = sims.getString("id");
logger.debug("Begin to download the SIMS {} with its documents", simsId);
String fileName = sims.getString("labelLg1");

try {

Path directory = Files.createTempDirectory("sims");
logger.debug("Creating tempory directory {}", directory.toString());
Path simsDirectory = Files.createDirectory(Path.of(directory.toString(), fileName));
logger.debug("Creating tempory directory {}", simsDirectory);

logger.debug("Generating the InputStream for the SIMS {}", simsId);

InputStream input = exportUtils.exportAsInputStream(fileName, xmlContent, xslFile, xmlPattern, zip, objectType, FilesUtils.ODT_EXTENSION);
if (input == null){
logger.debug("Error when creating the export of the SIMS {}", simsId);
throw new RmesException(HttpStatus.INTERNAL_SERVER_ERROR, "Can't export this object", "");
}

logger.debug("Creating the .odt file for the SIMS {}", simsId);
Path tempFile = Files.createFile(Path.of(simsDirectory.toString(), fileName + FilesUtils.ODT_EXTENSION));
Files.write(tempFile, input.readAllBytes(), StandardOpenOption.APPEND);
logger.debug("Finishing the creation of the .odt file for the SIMS {}", simsId);


logger.debug("Starting downloading documents for the SIMS {}", simsId);
Set<String> missingDocuments = this.exportRubricsDocuments(sims, simsDirectory);
logger.debug("Ending downloading documents for the SIMS {}", simsId);

logger.debug("Zipping the folder for the SIMS {}", simsId);
FilesUtils.zipDirectory(simsDirectory.toFile());

logger.debug("Zip created for the SIMS {}", simsId);
HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(sims.getString("labelLg1"), FilesUtils.ZIP_EXTENSION, this.maxLength);
responseHeaders.setAccessControlExposeHeaders(List.of("X-Missing-Documents", HttpUtils.CONTENT_DISPOSITION));
responseHeaders.set("X-Missing-Documents", String.join(",", missingDocuments));
Resource resource = new UrlResource(Paths.get(simsDirectory.toString(), simsDirectory.getFileName() + FilesUtils.ZIP_EXTENSION).toUri());
return ResponseEntity.ok()
.headers(responseHeaders)
.body(resource);
}
catch (Exception exception) {
throw new RmesException(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), exception.getClass().getSimpleName());
}
}

private Set<String> exportRubricsDocuments(JSONObject sims, Path directory) throws IOException, RmesException {
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
Set<String> history = new HashSet<>();
JSONArray documents = documentsUtils.getDocumentsUriAndUrlForSims(sims.getString("id"));
Set<String> missingDocuments = new HashSet<>();

for (int i = 0; i < documents.length(); i++) {
JSONObject document = documents.getJSONObject(i);
String url = document.getString("url").replace("file://", "");
if(!history.contains(url)){
history.add(url);
logger.debug("Extracting document {}", url);


Path documentPath = Path.of(url);

if(!Files.exists(documentPath)){
missingDocuments.add(document.getString("id"));
} else {
String documentFileName = FilesUtils.reduceFileNameSize(UriUtils.getLastPartFromUri(url), maxLength);
try (InputStream inputStream = Files.newInputStream(documentPath)){
Path documentDirectory = Path.of(directory.toString(), "documents");
if (!Files.exists(documentDirectory)) {
logger.debug("Creating the documents folder");
Files.createDirectory(documentDirectory);
}

logger.debug("Writing the document {} with the name {} into the folder {}", url, documentFileName, directory.toString());
Path documentTempFile = Files.createFile(Path.of(documentDirectory.toString(), documentFileName));
Files.write(documentTempFile, inputStream.readAllBytes(), StandardOpenOption.APPEND);
}
}

}
}

return missingDocuments;
}

private ResponseEntity<Resource> export(Exporter exporter, Map<String, String> xmlContent, PatternAndZip patternAndZip) throws RmesException {
return exporter.export(xmlContent, xslFile, patternAndZip.xmlPattern(), patternAndZip.zip(), DOCUMENTATION);
}
Expand All @@ -104,7 +201,6 @@ public ResponseEntity<Object> exportXmlFiles(Map<String, String> xmlContent, Str
xmlContent.put(Constants.PARAMETERS_FILE, parametersXML);

return exportUtils.exportFilesAsResponse(xmlContent);

}


Expand Down
Loading
Loading