Skip to content

Commit

Permalink
Created fallback for lob path
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioG70 committed Jan 24, 2025
1 parent b818522 commit d55a3df
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class SIARD2ContentImportStrategy extends DefaultHandler implements Conte
private int currentColumnIndex;
private Row row;
private long rowIndex;
private boolean useLobPathFallback = false;

public SIARD2ContentImportStrategy(ReadStrategy readStrategy, ContentPathImportStrategy contentPathStrategy,
SIARDArchiveContainer lobContainer, boolean ignoreLobs) {
Expand Down Expand Up @@ -322,16 +323,16 @@ public void startElement(String uri, String localName, String qName, Attributes
new DummyInputStreamProvider(), lobPath, optionalLength.orElse(0L), optionalDigest.orElse(null),
optionalDigestType.orElse(null));
} else {
inputStream = createInputStream(container, lobPath, lobDir);
currentBlobCell = new BinaryCell(
currentTable.getColumns().get(currentColumnIndex - 1).getId() + "." + rowIndex,
readStrategy.createInputStream(container, lobPath));
currentTable.getColumns().get(currentColumnIndex - 1).getId() + "." + rowIndex, inputStream);
}
}

LOGGER.debug(
String.format("BLOB cell %s on row #%d with lob dir %s", currentBlobCell.getId(), rowIndex, lobDir));
} else if (lobDir.endsWith(SIARD2ContentPathExportStrategy.CLOB_EXTENSION)) {
inputStream = readStrategy.createInputStream(container, lobPath);
inputStream = createInputStream(container, lobPath, lobDir);
String data = IOUtils.toString(inputStream);
currentClobCell = new SimpleCell(
currentTable.getColumns().get(currentColumnIndex - 1).getId() + "." + rowIndex, data);
Expand Down Expand Up @@ -474,6 +475,22 @@ public void characters(char buf[], int offset, int len) {
tempVal.append(buf, offset, len);
}

private InputStream createInputStream(SIARDArchiveContainer container, String lobPath, String lobDir) throws ModuleException {
String lobName = Paths.get(lobDir).getFileName().toString();
if (useLobPathFallback) {
lobPath = contentPathStrategy.getLobPathFallback(null,
currentTable.getColumns().get(currentColumnIndex - 1).getId(), lobName);
}
try {
return readStrategy.createInputStream(container, lobPath);
} catch (ModuleException e) {
useLobPathFallback = true;
lobPath = contentPathStrategy.getLobPathFallback(null,
currentTable.getColumns().get(currentColumnIndex - 1).getId(), lobName);
return readStrategy.createInputStream(container, lobPath);
}
}

private List<Integer> getArrayCellPositions(Integer current, Deque<String> parentTags) {
List<Integer> positions = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
public interface ContentPathImportStrategy {
String getLobPath(String basePath, String schemaName, String tableId, String columnId, String lobFileName);

String getLobPathFallback(String basePath, String columnId, String lobFileName);

/**
* Stores the relationship between the schema and it's folder
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public String getLobPath(String basePath, String schemaName, String tableId, Str
throw new NotImplementedException("getLobPath in ContentPathImportStrategy method is not needed for SIARD1");
}

@Override
public String getLobPathFallback(String basePath, String columnId, String lobFileName) {
throw new NotImplementedException("getLobPathFallback in ContentPathImportStrategy method is not needed for SIARD1");
}

@Override
public void associateSchemaWithFolder(String schemaName, String schemaFolder) {
schemaFolders.put(schemaName, schemaFolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ public String getLobPath(String basePath, String schemaName, String tableId, Str
}
}

@Override
public String getLobPathFallback(String basePath, String columnId, String lobFileName) {
if (StringUtils.isBlank(basePath)) {
basePath = metadataLobFolder;
}

String columnPart = columnFolders.get(columnId);

if (columnPart == null) {
columnPart = ".";
}

if (".".equals(basePath) && ".".equals(columnPart) && lobFileName.startsWith("..")) {
return lobFileName.substring(3);
} else if (".".equals(columnPart)) {
return lobFileName;
} else {
return basePath + RESOURCE_FILE_SEPARATOR + columnPart + RESOURCE_FILE_SEPARATOR + lobFileName;
}
}

@Override
public void associateSchemaWithFolder(String schemaName, String schemaFolder) {
schemaFolders.put(schemaName, schemaFolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import java.util.List;

import org.apache.commons.lang3.NotImplementedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -169,6 +171,11 @@ public String getLobPath(String basePath, String schemaName, String tableId, Str
throw new UnsupportedOperationException("Invoking getLobPath(...) is not relevant for SIARDDK.");
}

@Override
public String getLobPathFallback(String basePath, String columnId, String lobFileName) {
throw new NotImplementedException("Invoking getLobPath(...) is not relevant for SIARDDK.");
}

@Override
public void associateSchemaWithFolder(String schemaName, String schemaFolder) {
throw new UnsupportedOperationException("Invoking associateSchemaWithFolder(...) is not relevant for SIARDDK.");
Expand Down

0 comments on commit d55a3df

Please sign in to comment.