Skip to content

Commit 02cea63

Browse files
committed
Merge pull request #629 from keeps/alindo-dev-tableName
Updated virtual table and context document table content import
2 parents 531ca15 + 80e2897 commit 02cea63

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

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

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import java.io.FileNotFoundException;
1212
import java.io.IOException;
1313
import java.math.BigInteger;
14+
import java.nio.file.Files;
1415
import java.nio.file.Path;
1516
import java.nio.file.Paths;
1617
import java.security.DigestInputStream;
18+
import java.util.ArrayList;
1719
import java.util.Arrays;
1820
import java.util.HashMap;
1921
import java.util.List;
@@ -33,7 +35,6 @@
3335

3436
import org.apache.commons.codec.Charsets;
3537
import org.apache.commons.codec.digest.DigestUtils;
36-
import org.apache.commons.io.FileUtils;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940
import org.w3c.dom.TypeInfo;
@@ -236,53 +237,60 @@ public void importContent(DatabaseExportModule dbExportHandler, SIARDArchiveCont
236237

237238
}
238239

239-
void populateContextDocumentationTable(TableStructure table)
240-
throws ModuleException, FileNotFoundException {
240+
void populateContextDocumentationTable(TableStructure table) throws ModuleException, FileNotFoundException {
241241
F contextDoc = loadContextDocTableContent();
242242
currentTable = table;
243243
this.dbExportHandler.handleDataOpenTable(table.getId());
244-
Cell[] cells = new Cell[table.getColumns().size()];
244+
int rowCounter = 0;
245245
for (S doc : getContextDocuments(contextDoc)) {
246-
int rowCounter = 0;
247-
int cellCounter = 0;
248246
Row row = new Row();
247+
List<Cell> lstCells = new ArrayList<>();
249248
row.setIndex(rowCounter);
249+
250+
// document id
250251
Cell documentIDCell = new SimpleCell(
251252
SIARDDKConstants.DOCUMENT_ID + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + rowCounter, getDocumentID(doc));
252-
cells[cellCounter] = documentIDCell;
253-
cellCounter++;
253+
lstCells.add(documentIDCell);
254+
255+
// document title
254256
Cell documentTitleCell = new SimpleCell(
255257
SIARDDKConstants.DOCUMENT_TITLE + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + rowCounter,
256258
getDocumentTitle(doc));
257-
cells[cellCounter] = documentTitleCell;
258-
cellCounter++;
259+
lstCells.add(documentTitleCell);
260+
261+
// document date
259262
Cell documentDateCell = new SimpleCell(
260263
SIARDDKConstants.DOCUMENT_DATE + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + rowCounter, getDocumentDate(doc));
261-
cells[cellCounter] = documentDateCell;
262-
cellCounter++;
264+
lstCells.add(documentDateCell);
265+
263266
try {
264-
String docPath = pathStrategy.getMainFolder().getPath().toString() + SIARDDKConstants.RESOURCE_FILE_SEPARATOR
265-
+ SIARDDKConstants.CONTEXT_DOCUMENTATION_FOLDER_NAME + SIARDDKConstants.RESOURCE_FILE_SEPARATOR
266-
+ SIARDDKConstants.DOC_COLLECTION_FOLDER_NAME + SIARDDKConstants.RESOURCE_FILE_SEPARATOR + getDocumentID(doc);
267+
// document blob
268+
Path docPath = pathStrategy.getMainFolder().getPath()
269+
.resolve(Paths.get(SIARDDKConstants.CONTEXT_DOCUMENTATION_FOLDER_NAME,
270+
SIARDDKConstants.DOC_COLLECTION_FOLDER_NAME, getDocumentID(doc)));
271+
267272
String digest = "";
268-
File docFolder = new File(docPath);
273+
File docFolder = new File(docPath.toString());
269274
if (docFolder.exists() && docFolder.isDirectory()) {
270275
File[] fileList = docFolder.listFiles();
271276
if (fileList != null && fileList.length == 1) {
272-
docPath = docPath + SIARDDKConstants.RESOURCE_FILE_SEPARATOR + fileList[0].getName();
273-
digest = DigestUtils.sha1Hex(FileUtils.readFileToByteArray(Paths.get(docPath).toFile()));
277+
docPath = docPath.resolve(Paths.get(fileList[0].getName()));
278+
digest = DigestUtils.sha1Hex(Files.newInputStream(docPath));
274279
}
275280
}
281+
276282
Cell blobCell = new BinaryCell(
277283
SIARDDKConstants.BLOB_EXTENSION + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + rowCounter,
278-
new DummyInputStreamProvider(), docPath, 0L, digest, DigestUtils.getSha1Digest().toString());
279-
cells[cellCounter] = blobCell;
280-
cellCounter++;
281-
rowCounter++;
282-
List<Cell> lstCells = Arrays.asList(cells);
284+
new DummyInputStreamProvider(), docPath.toString(), Files.size(docPath), digest,
285+
DigestUtils.getSha1Digest().toString());
286+
lstCells.add(blobCell);
287+
288+
// set and handle row
283289
assert !lstCells.contains(null);
284290
row.setCells(lstCells);
285291
this.dbExportHandler.handleDataRow(row);
292+
293+
rowCounter++;
286294
} catch (ModuleException | IOException e) {
287295
throw new ModuleException().withMessage("Error handling data row index:" + rowCounter).withCause(e);
288296
}
@@ -294,33 +302,34 @@ void populateVirtualTable(TableStructure table) throws ModuleException, FileNotF
294302
D docIndex = loadVirtualTableContent();
295303
currentTable = table;
296304
this.dbExportHandler.handleDataOpenTable(table.getId());
297-
Cell[] cells = new Cell[table.getColumns().size()];
305+
int rowCounter = 0;
298306
for (T doc : getDocuments(docIndex)) {
299-
int rowCounter = 0;
300-
int cellCounter = 0;
301307
Row row = new Row();
308+
List<Cell> lstCells = new ArrayList<>();
302309
row.setIndex(rowCounter);
310+
311+
// document id
303312
Cell dIDCell = new SimpleCell(SIARDDKConstants.DID + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + rowCounter,
304313
getDID(doc).toString());
305-
cells[cellCounter] = dIDCell;
306-
cellCounter++;
307-
String binPath = pathStrategy.getMainFolder().getPath().toString() + SIARDDKConstants.RESOURCE_FILE_SEPARATOR
308-
+ SIARDDKConstants.DOCUMENTS_FOLDER_NAME + SIARDDKConstants.RESOURCE_FILE_SEPARATOR + getDCf(doc)
309-
+ SIARDDKConstants.RESOURCE_FILE_SEPARATOR + getDID(doc) + SIARDDKConstants.RESOURCE_FILE_SEPARATOR
310-
+ getMID(doc) + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + getAFt(doc);
314+
lstCells.add(dIDCell);
315+
316+
// document blob
317+
Path binPath = pathStrategy.getMainFolder().getPath().resolve(Paths.get(SIARDDKConstants.DOCUMENTS_FOLDER_NAME,
318+
getDCf(doc), getDID(doc).toString(), getMID(doc) + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + getAFt(doc)));
319+
311320
try {
312321
Cell blobCell = new BinaryCell(
313322
SIARDDKConstants.BLOB_EXTENSION + SIARDDKConstants.FILE_EXTENSION_SEPARATOR + rowCounter,
314-
new DummyInputStreamProvider(), binPath, 0L,
315-
DigestUtils.sha1Hex(FileUtils.readFileToByteArray(Paths.get(binPath).toFile())),
316-
DigestUtils.getSha1Digest().toString());
317-
cells[cellCounter] = blobCell;
318-
cellCounter++;
319-
rowCounter++;
320-
List<Cell> lstCells = Arrays.asList(cells);
323+
new DummyInputStreamProvider(), binPath.toString(), Files.size(binPath),
324+
DigestUtils.sha1Hex(Files.newInputStream(binPath)), DigestUtils.getSha1Digest().toString());
325+
lstCells.add(blobCell);
326+
327+
// set and handle row
321328
assert !lstCells.contains(null);
322329
row.setCells(lstCells);
323330
this.dbExportHandler.handleDataRow(row);
331+
332+
rowCounter++;
324333
} catch (ModuleException | IOException e) {
325334
throw new ModuleException().withMessage("Error handling data row index:" + rowCounter).withCause(e);
326335
}

0 commit comments

Comments
 (0)