Skip to content

Commit

Permalink
export rows with lobs on SIARDDK (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioG70 authored Dec 3, 2024
1 parent e8abac4 commit 97cd819
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
package com.databasepreservation.common.api.v1.utils;

import com.databasepreservation.common.client.models.structure.ViewerLobStoreType;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -19,6 +21,7 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.zip.ZipFile;

import com.databasepreservation.common.api.utils.ExtraMediaType;
Expand All @@ -37,7 +40,6 @@
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.IOUtils;
import org.roda.core.data.v2.index.sublist.Sublist;

import com.databasepreservation.common.client.models.status.collection.CollectionStatus;
import com.databasepreservation.common.client.models.status.collection.TableStatus;
Expand Down Expand Up @@ -120,22 +122,31 @@ protected ColumnStatus findLobColumn(final List<ColumnStatus> columns, final Str

protected void writeToZipFile(ZipFile siardArchive, ZipArchiveOutputStream out, ViewerRow row,
List<ColumnStatus> binaryColumns) throws IOException {
writeToZipFile(siardArchive, out, row, binaryColumns, false);
}

protected void writeToZipFile(ZipFile siardArchive, ZipArchiveOutputStream out, ViewerRow row,
List<ColumnStatus> binaryColumns, boolean isSiardDK) throws IOException {

for (Map.Entry<String, ViewerCell> cellEntry : row.getCells().entrySet()) {
final ColumnStatus binaryColumn = findLobColumn(binaryColumns, cellEntry.getKey());

if (binaryColumn != null) {
if (ViewerType.dbTypes.CLOB.equals(binaryColumn.getType())) {
handleWriteClob(out, binaryColumn, row);
} else if (configurationCollection.getConsolidateProperty()
.equals(LargeObjectConsolidateProperty.CONSOLIDATED)) {
handleWriteConsolidateLobs(out, binaryColumn, row);
if (isSiardDK) {
handleWriteSIARDDKLobs(out, cellEntry.getValue());
} else {
if (ViewerLobStoreType.EXTERNALLY.equals(
row.getCells().get(configTable.getColumnByIndex(binaryColumn.getColumnIndex()).getId()).getStoreType())) {
handleWriteExternalLobs(out, binaryColumn, row, cellEntry.getValue());
if (ViewerType.dbTypes.CLOB.equals(binaryColumn.getType())) {
handleWriteClob(out, binaryColumn, row);
} else if (configurationCollection.getConsolidateProperty()
.equals(LargeObjectConsolidateProperty.CONSOLIDATED)) {
handleWriteConsolidateLobs(out, binaryColumn, row);
} else {
handleWriteInternalLobs(out, siardArchive, binaryColumn, row);
if (ViewerLobStoreType.EXTERNALLY.equals(
row.getCells().get(configTable.getColumnByIndex(binaryColumn.getColumnIndex()).getId()).getStoreType())) {
handleWriteExternalLobs(out, binaryColumn, row, cellEntry.getValue());
} else {
handleWriteInternalLobs(out, siardArchive, binaryColumn, row);
}
}
}
}
Expand Down Expand Up @@ -182,6 +193,21 @@ private void handleWriteExternalLobs(ZipArchiveOutputStream out, ColumnStatus bi
addEntryToZip(out, inputStream, templateFilename);
}

private void handleWriteSIARDDKLobs(ZipArchiveOutputStream out, ViewerCell cell) throws IOException {
final String lobLocation = cell.getValue();
final Path lobPath = Paths.get(lobLocation);

if (lobPath.toFile().isDirectory()) {
for (File file : Objects.requireNonNull(lobPath.toFile().listFiles())) {
InputStream inputStream = Files.newInputStream(file.toPath());
addEntryToZip(out, inputStream, file.getName());
}
} else {
InputStream inputStream = Files.newInputStream(lobPath);
addEntryToZip(out, inputStream, lobPath.getFileName().toString());
}
}

private void handleWriteClob(ZipArchiveOutputStream out, ColumnStatus binaryColumn, ViewerRow row)
throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.zip.ZipFile;

import com.databasepreservation.common.client.ViewerConstants;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
Expand Down Expand Up @@ -47,7 +48,13 @@ public void consumeOutputStream(OutputStream out) throws IOException {
zipArchiveOutputStream.setMethod(ZipArchiveOutputStream.DEFLATED);

final List<ColumnStatus> binaryColumns = getConfigTable().getLobColumns();
writeToZipFile(new ZipFile(getDatabase().getPath()), zipArchiveOutputStream, row, binaryColumns);

if (getDatabase().getVersion().equals(ViewerConstants.SIARD_DK_1007)
|| getDatabase().getVersion().equals(ViewerConstants.SIARD_DK_128)) {
writeToZipFile(null, zipArchiveOutputStream, row, binaryColumns, true);
} else {
writeToZipFile(new ZipFile(getDatabase().getPath()), zipArchiveOutputStream, row, binaryColumns);
}

final ByteArrayOutputStream byteArrayOutputStream = writeCSVFile();
zipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(getCsvFilename()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static Path zipDirectory(Path dirPath, String databasePath, String handle
zipFilePath = dirPath.resolveSibling(dirPath.getFileName().toString() + ExtraMediaType.ZIP_FILE_EXTENSION);
} else {
if (handlebarsFilename != null) {
zipFilePath = buildSIARDKZipLobPath(databasePath, handlebarsFilename);
zipFilePath = buildSiardDKZipLobPath(databasePath, handlebarsFilename);
} else {
zipFilePath = Files.createTempFile(ViewerConstants.TEMP_PREFIX, ExtraMediaType.ZIP_FILE_EXTENSION);
}
Expand All @@ -98,7 +98,7 @@ public static Path zipDirectory(Path dirPath, String databasePath, String handle
return zipFilePath;
}

public static final Path buildSIARDKZipLobPath(String databasePath, String handlebarsFilename) {
public static final Path buildSiardDKZipLobPath(String databasePath, String handlebarsFilename) {
Path dbPath = Paths.get(databasePath);
String dbName = dbPath.getFileName().toString();
return dbPath.getParent().getParent().resolve(ViewerConstants.VIEWER_LOBS_FOLDER)
Expand Down

0 comments on commit 97cd819

Please sign in to comment.