Skip to content

Commit 1f0aeed

Browse files
AntonioG70hmiguim
authored andcommitted
Created fallback for lob path
1 parent 3104957 commit 1f0aeed

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/content/SIARD2ContentImportStrategy.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class SIARD2ContentImportStrategy extends DefaultHandler implements Conte
103103
private int currentColumnIndex;
104104
private Row row;
105105
private long rowIndex;
106+
private boolean useLobPathFallback = false;
106107

107108
public SIARD2ContentImportStrategy(ReadStrategy readStrategy, ContentPathImportStrategy contentPathStrategy,
108109
SIARDArchiveContainer lobContainer, boolean ignoreLobs) {
@@ -322,16 +323,16 @@ public void startElement(String uri, String localName, String qName, Attributes
322323
new DummyInputStreamProvider(), lobPath, optionalLength.orElse(0L), optionalDigest.orElse(null),
323324
optionalDigestType.orElse(null));
324325
} else {
326+
inputStream = createInputStream(container, lobPath, lobDir);
325327
currentBlobCell = new BinaryCell(
326-
currentTable.getColumns().get(currentColumnIndex - 1).getId() + "." + rowIndex,
327-
readStrategy.createInputStream(container, lobPath));
328+
currentTable.getColumns().get(currentColumnIndex - 1).getId() + "." + rowIndex, inputStream);
328329
}
329330
}
330331

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

478+
private InputStream createInputStream(SIARDArchiveContainer container, String lobPath, String lobDir) throws ModuleException {
479+
String lobName = Paths.get(lobDir).getFileName().toString();
480+
if (useLobPathFallback) {
481+
lobPath = contentPathStrategy.getLobPathFallback(null,
482+
currentTable.getColumns().get(currentColumnIndex - 1).getId(), lobName);
483+
}
484+
try {
485+
return readStrategy.createInputStream(container, lobPath);
486+
} catch (ModuleException e) {
487+
useLobPathFallback = true;
488+
lobPath = contentPathStrategy.getLobPathFallback(null,
489+
currentTable.getColumns().get(currentColumnIndex - 1).getId(), lobName);
490+
return readStrategy.createInputStream(container, lobPath);
491+
}
492+
}
493+
477494
private List<Integer> getArrayCellPositions(Integer current, Deque<String> parentTags) {
478495
List<Integer> positions = new ArrayList<>();
479496

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/path/ContentPathImportStrategy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
public interface ContentPathImportStrategy {
1616
String getLobPath(String basePath, String schemaName, String tableId, String columnId, String lobFileName);
1717

18+
String getLobPathFallback(String basePath, String columnId, String lobFileName);
19+
1820
/**
1921
* Stores the relationship between the schema and it's folder
2022
*

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/path/SIARD1ContentPathImportStrategy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public String getLobPath(String basePath, String schemaName, String tableId, Str
4848
throw new NotImplementedException("getLobPath in ContentPathImportStrategy method is not needed for SIARD1");
4949
}
5050

51+
@Override
52+
public String getLobPathFallback(String basePath, String columnId, String lobFileName) {
53+
throw new NotImplementedException("getLobPathFallback in ContentPathImportStrategy method is not needed for SIARD1");
54+
}
55+
5156
@Override
5257
public void associateSchemaWithFolder(String schemaName, String schemaFolder) {
5358
schemaFolders.put(schemaName, schemaFolder);

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/path/SIARD2ContentPathImportStrategy.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ public String getLobPath(String basePath, String schemaName, String tableId, Str
8787
}
8888
}
8989

90+
@Override
91+
public String getLobPathFallback(String basePath, String columnId, String lobFileName) {
92+
if (StringUtils.isBlank(basePath)) {
93+
basePath = metadataLobFolder;
94+
}
95+
96+
String columnPart = columnFolders.get(columnId);
97+
98+
if (columnPart == null) {
99+
columnPart = ".";
100+
}
101+
102+
if (".".equals(basePath) && ".".equals(columnPart) && lobFileName.startsWith("..")) {
103+
return lobFileName.substring(3);
104+
} else if (".".equals(columnPart)) {
105+
return lobFileName;
106+
} else {
107+
return basePath + RESOURCE_FILE_SEPARATOR + columnPart + RESOURCE_FILE_SEPARATOR + lobFileName;
108+
}
109+
}
110+
90111
@Override
91112
public void associateSchemaWithFolder(String schemaName, String schemaFolder) {
92113
schemaFolders.put(schemaName, schemaFolder);

dbptk-modules/dbptk-module-siard/src/main/java/com/databasepreservation/modules/siard/in/path/SIARDDKPathImportStrategy.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import jakarta.xml.bind.JAXBException;
1111
import jakarta.xml.bind.Unmarshaller;
1212
import java.util.List;
13+
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
1516
import org.xml.sax.SAXException;
@@ -169,6 +170,11 @@ public String getLobPath(String basePath, String schemaName, String tableId, Str
169170
throw new UnsupportedOperationException("Invoking getLobPath(...) is not relevant for SIARDDK.");
170171
}
171172

173+
@Override
174+
public String getLobPathFallback(String basePath, String columnId, String lobFileName) {
175+
throw new UnsupportedOperationException("Invoking getLobPath(...) is not relevant for SIARDDK.");
176+
}
177+
172178
@Override
173179
public void associateSchemaWithFolder(String schemaName, String schemaFolder) {
174180
throw new UnsupportedOperationException("Invoking associateSchemaWithFolder(...) is not relevant for SIARDDK.");

0 commit comments

Comments
 (0)