Skip to content

Commit

Permalink
33817 Allow to delete exports
Browse files Browse the repository at this point in the history
Delegate getPath for a specific export to config class
  • Loading branch information
cgendreau committed May 21, 2024
1 parent b1b0ddc commit 33dbe3c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package ca.gc.aafc.dina.export.api.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.validation.annotation.Validated;

import ca.gc.aafc.dina.export.api.entity.DataExport;

import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
Expand All @@ -9,12 +17,6 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.validation.annotation.Validated;

/**
* Configured on DinaExportModuleApiLauncher
*/
Expand All @@ -35,6 +37,9 @@ public class DataExportConfig {
public static final String PAYLOAD_KEY = "payload";

public static final String TEXT_CSV_VALUE = MediaType.parseMediaType("text/csv").toString();
public static final String DATA_EXPORT_CSV_FILENAME = "export.csv";

public static final String ZIP_EXT = "zip";

public static final String PDF_REPORT_FILENAME = "report.pdf";
public static final String CSV_REPORT_FILENAME = "report.csv";
Expand Down Expand Up @@ -65,4 +70,11 @@ public Path getGeneratedDataExportsPath() {
return Path.of(workingFolder).resolve(GENERATED_DATA_EXPORTS);
}

public Path getPathForDataExport(DataExport dataExport) {
return switch (dataExport.getExportType()) {
case TABULAR_DATA -> getGeneratedDataExportsPath().resolve(dataExport.getUuid().toString()).resolve(DATA_EXPORT_CSV_FILENAME);
case OBJECT_ARCHIVE -> getGeneratedDataExportsPath().resolve(dataExport.getUuid().toString() + "." + ZIP_EXT);
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import ca.gc.aafc.dina.export.api.service.DataExportService;
import ca.gc.aafc.dina.export.api.service.TransactionWrapper;

import static ca.gc.aafc.dina.export.api.generator.TabularDataExportGenerator.DATA_EXPORT_CSV_FILENAME;
import static ca.gc.aafc.dina.export.api.config.DataExportConfig.DATA_EXPORT_CSV_FILENAME;

@RestController
@RequestMapping("/api/v1")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ca.gc.aafc.dina.export.api.generator;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand All @@ -22,7 +21,6 @@
public class ObjectStoreExportGenerator extends DataExportGenerator {

private final FileDownloader fileDownloader;
private final Path workingFolder;
private final DataExportConfig dataExportConfig;

public ObjectStoreExportGenerator(DataExportConfig dataExportConfig,
Expand All @@ -33,31 +31,28 @@ public ObjectStoreExportGenerator(DataExportConfig dataExportConfig,

this.fileDownloader = fileDownloader;
this.dataExportConfig = dataExportConfig;
this.workingFolder = dataExportConfig.getGeneratedDataExportsPath();
}

@Async(DataExportConfig.DINA_THREAD_POOL_BEAN_NAME)
@Override
public CompletableFuture<UUID> export(DataExport dinaExport) throws IOException {

// make sure the destination folder exists
if(!workingFolder.toFile().exists()) {
Files.createDirectories(workingFolder);
}

DataExport.ExportStatus currStatus = waitForRecord(dinaExport.getUuid());

if(currStatus == DataExport.ExportStatus.NEW) {

Path exportPath = dataExportConfig.getPathForDataExport(dinaExport);
//Create the directory if it doesn't exist
Files.createDirectories(exportPath.getParent());

updateStatus(dinaExport.getUuid(), DataExport.ExportStatus.RUNNING);

String downloadUrl = StringUtils.appendIfMissing(dataExportConfig.getObjectStoreDownloadUrl(), "/")
+ dinaExport.getTransitiveData().get(DataExportConfig.OBJECT_STORE_TOA);

try {
// call download
fileDownloader.downloadFile(downloadUrl,
filename -> generatePath(dinaExport.getUuid(), filename));
fileDownloader.downloadFile(downloadUrl, filename -> exportPath);
updateStatus(dinaExport.getUuid(), DataExport.ExportStatus.COMPLETED);
} catch (IOException | IllegalStateException ex) {
updateStatus(dinaExport.getUuid(), DataExport.ExportStatus.ERROR);
Expand All @@ -70,12 +65,12 @@ public CompletableFuture<UUID> export(DataExport dinaExport) throws IOException
return CompletableFuture.completedFuture(dinaExport.getUuid());
}

private Path generatePath(UUID exportUuid, String downloadFilename) {
String ext = "";
if (StringUtils.isNotBlank(downloadFilename)) {
ext = FilenameUtils.getExtension(downloadFilename);
}
return workingFolder.resolve(exportUuid.toString() + "." + StringUtils.defaultIfBlank(ext, "tmp"));
}
// private Path generatePath(UUID exportUuid, String downloadFilename) {
// String ext = "";
// if (StringUtils.isNotBlank(downloadFilename)) {
// ext = FilenameUtils.getExtension(downloadFilename);
// }
// return workingFolder.resolve(exportUuid.toString() + "." + StringUtils.defaultIfBlank(ext, "tmp"));
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@
@Service
@Log4j2
public class TabularDataExportGenerator extends DataExportGenerator {
public static final String DATA_EXPORT_CSV_FILENAME = "export.csv";

private final ObjectMapper objectMapper;
private final ElasticSearchDataSource elasticSearchDataSource;
private final Configuration jsonPathConfiguration;

private final Path workingFolder;
private final DataExportConfig dataExportConfig;

public TabularDataExportGenerator(
DataExportStatusService dataExportStatusService,
Expand All @@ -70,8 +69,7 @@ public TabularDataExportGenerator(
this.jsonPathConfiguration = jsonPathConfiguration;
this.elasticSearchDataSource = elasticSearchDataSource;
this.objectMapper = objectMapper;

workingFolder = dataExportConfig.getGeneratedDataExportsPath();
this.dataExportConfig = dataExportConfig;
}

/**
Expand All @@ -89,10 +87,11 @@ public CompletableFuture<UUID> export(DataExport dinaExport) throws IOException
updateStatus(dinaExport.getUuid(), DataExport.ExportStatus.RUNNING);

try {
Path tmpDirectory =
Files.createDirectories(workingFolder.resolve(dinaExport.getUuid().toString()));
Path exportPath = dataExportConfig.getPathForDataExport(dinaExport);
//Create the directory
Files.createDirectories(exportPath.getParent());
// csv output
try (Writer w = new FileWriter(tmpDirectory.resolve(DATA_EXPORT_CSV_FILENAME).toFile(),
try (Writer w = new FileWriter(exportPath.toFile(),
StandardCharsets.UTF_8);
CsvOutput<JsonNode> output =
CsvOutput.create(Arrays.asList(dinaExport.getColumns()), new TypeReference<>() {
Expand Down

0 comments on commit 33dbe3c

Please sign in to comment.