From 5faaf8fab38db326fb785f927a4c5d2a9f3c5559 Mon Sep 17 00:00:00 2001 From: Steffen Hankiewicz Date: Fri, 5 Jul 2024 09:26:57 +0200 Subject: [PATCH] export for positives finished --- .../plugins/exporters/PositiveExporter.java | 82 ++++++++++++------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/module-base/src/main/java/de/intranda/goobi/plugins/exporters/PositiveExporter.java b/module-base/src/main/java/de/intranda/goobi/plugins/exporters/PositiveExporter.java index 0c299ff..971333b 100644 --- a/module-base/src/main/java/de/intranda/goobi/plugins/exporters/PositiveExporter.java +++ b/module-base/src/main/java/de/intranda/goobi/plugins/exporters/PositiveExporter.java @@ -3,6 +3,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -20,7 +22,6 @@ import de.intranda.goobi.plugins.AdmBsmeExportHelper; import de.sub.goobi.helper.StorageProvider; -import de.sub.goobi.helper.StorageProviderInterface; import de.sub.goobi.helper.VariableReplacer; import de.sub.goobi.helper.exceptions.DAOException; import de.sub.goobi.helper.exceptions.SwapException; @@ -85,13 +86,8 @@ public boolean startExport() { log.debug("Export directory for AdmBsmeExportPlugin: " + targetFolder); DocStruct topStruct = dd.getLogicalDocStruct(); - // prepare xml document - Document doc = new Document(); - doc.setRootElement(new Element("Envelope")); - - // add volume information + // prepare process information Element info = new Element("envelopeInfo"); - doc.getRootElement().addContent(info); String identifier = AdmBsmeExportHelper.getMetdata(topStruct, config.getString("/metadata/identifier")); String rightsToUse = vr.replace(config.getString("/rightsToUse")); @@ -148,13 +144,23 @@ public boolean startExport() { info.addContent(new Element("Technical_Notes").setText("- no entry available -")); } - // add file information - Element files = new Element("Images"); - doc.getRootElement().addContent(files); - List refs = topStruct.getAllToReferences("logical_physical"); if (refs != null) { + + // EACH IMAGE - START for (Reference ref : refs) { + + // prepare xml document + Document doc = new Document(); + doc.setRootElement(new Element("Envelope")); + info.detach(); + doc.getRootElement().addContent(info); + + // add file information + Element files = new Element("Images"); + doc.getRootElement().addContent(files); + + // Image details DocStruct page = ref.getTarget(); String realFileName = page.getImageName(); String realFileNameWithoutExtension = realFileName.substring(0, realFileName.indexOf(".")); @@ -222,29 +228,45 @@ public boolean startExport() { master.addContent(new Element("file").setText(exportFileName + ".tif")); file.addContent(master); + + try { + // copy image file to target folder + Path in = Paths.get(process.getImagesOrigDirectory(false), realFileNameWithoutExtension + ".tif"); + Path out = Paths.get(targetFolder, exportFileName + ".tif"); + StorageProvider.getInstance().copyFile(in, out); + + // copy plaintext file to target folder and add it to xml + Path ocrPlaintextPath = Paths.get(process.getOcrTxtDirectory(), realFileNameWithoutExtension + ".txt"); + if (StorageProvider.getInstance().isFileExists(ocrPlaintextPath)) { + file.addContent(new Element("text").setText(exportFileName + ".txt").setAttribute("Format", "text/plain")); + out = Paths.get(targetFolder, exportFileName + ".txt"); + StorageProvider.getInstance().copyFile(ocrPlaintextPath, out); + } else { + file.addContent(new Element("text").setAttribute("Format", "text/plain")); + } + + } catch (IOException | SwapException | DAOException e) { + log.error("Error while copying the image and ocr files to export folder", e); + return false; + } + files.addContent(file); - } - } - // write the xml file - XMLOutputter xmlOutputter = new XMLOutputter(); - xmlOutputter.setFormat(Format.getPrettyFormat()); - File xmlfile = new File(targetFolder + identifier + ".xml"); - try (FileOutputStream fileOutputStream = new FileOutputStream(xmlfile)) { - xmlOutputter.output(doc, fileOutputStream); - } catch (IOException e) { - log.error("Error writing the simple xml file", e); - return false; - } + // write the xml file + XMLOutputter xmlOutputter = new XMLOutputter(); + xmlOutputter.setFormat(Format.getPrettyFormat()); - try { - // copy all important files to target folder - AdmBsmeExportHelper.copyFolderContent(process.getImagesOrigDirectory(false), "tif", fileMap, targetFolder); - StorageProviderInterface sp = StorageProvider.getInstance(); + File xmlfile = new File(targetFolder + identifier + "-" + String.format("%03d", fileCounter) + ".xml"); + try (FileOutputStream fileOutputStream = new FileOutputStream(xmlfile)) { + xmlOutputter.output(doc, fileOutputStream); + } catch (IOException e) { + log.error("Error writing the simple xml file", e); + return false; + } + + } + // EACH IMAGE - END - } catch (IOException | SwapException | DAOException e) { - log.error("Error while copying the image files to export folder", e); - return false; } return true;