From 50ed1839284c8626e1f6dde38985ea2898bd4b5a Mon Sep 17 00:00:00 2001 From: Markus Weigelt Date: Fri, 24 Sep 2021 10:46:30 +0200 Subject: [PATCH] #3408 read titledocmain from meta.xml if title record process is selected --- .../forms/createprocess/ProcessDataTab.java | 27 ++++++---- .../services/data/ProcessService.java | 51 ++++++++++++++++--- .../workflow/WorkflowControllerService.java | 28 +--------- 3 files changed, 60 insertions(+), 46 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDataTab.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDataTab.java index 09f9d70f5b5..eedae06fb9f 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDataTab.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/ProcessDataTab.java @@ -15,7 +15,6 @@ import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.concurrent.atomic.AtomicReference; import javax.faces.model.SelectItem; @@ -27,7 +26,6 @@ import org.kitodo.api.MetadataEntry; import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface; import org.kitodo.data.database.beans.Process; -import org.kitodo.data.database.beans.Property; import org.kitodo.exceptions.DoctypeMissingException; import org.kitodo.exceptions.ProcessGenerationException; import org.kitodo.production.helper.Helper; @@ -37,6 +35,7 @@ import org.kitodo.production.services.data.ImportService; import org.kitodo.production.services.data.ProcessService; import org.omnifaces.util.Ajax; +import org.w3c.dom.NodeList; public class ProcessDataTab { @@ -212,14 +211,8 @@ public void generateProcessTitleAndTiffHeader() { if (StringUtils.isBlank(currentTitle)) { Process parentProcess = createProcessForm.getTitleRecordLinkTab().getTitleRecordProcess(); - if (Objects.nonNull(parentProcess)) { - - // get title of workpiece property "Haupttitle" from chosen title record process - currentTitle = parentProcess.getWorkpieces().stream() - .filter(property -> "Haupttitel".equals(property.getTitle())).findFirst() - .map(Property::getValue).orElse(currentTitle); - + currentTitle = getTitleFromMetaXML(parentProcess); } else { currentTitle = getTitleFromAncestors(); } @@ -239,6 +232,19 @@ public void generateProcessTitleAndTiffHeader() { "editForm:processFromTemplateTabView:processMetadata"); } + private String getTitleFromMetaXML(Process process) { + String xpath = "//kitodo:metadata[@name='" + TitleGenerator.TITLE_DOC_MAIN + "']"; + try { + NodeList nodeList = ServiceManager.getProcessService().getNodeListFromMetadataFile(process, xpath); + if (nodeList.getLength() > 0) { + return nodeList.item(0).getTextContent(); + } + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + return null; + } + private String getTitleFromAncestors() { int processesSize = createProcessForm.getProcesses().size(); @@ -247,7 +253,6 @@ private String getTitleFromAncestors() { } List ancestors = createProcessForm.getProcesses().subList(1, processesSize); - AtomicReference tempTitle = new AtomicReference<>(); // get title of ancestors where TitleDocMain exists when several processes were // imported @@ -259,7 +264,7 @@ private String getTitleFromAncestors() { return ((MetadataEntry) metadataOptional.get()).getValue(); } } - return tempTitle.get(); + return null; } /** diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java index 37bc2595616..6c5802db0c6 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java @@ -18,14 +18,6 @@ import static org.kitodo.data.database.enums.CorrectionComments.NO_OPEN_CORRECTION_COMMENTS; import static org.kitodo.data.database.enums.CorrectionComments.OPEN_CORRECTION_COMMENTS; -import com.itextpdf.text.Document; -import com.itextpdf.text.DocumentException; -import com.itextpdf.text.PageSize; -import com.itextpdf.text.Paragraph; -import com.itextpdf.text.Rectangle; -import com.itextpdf.text.pdf.PdfPTable; -import com.itextpdf.text.pdf.PdfWriter; - import java.io.File; import java.io.FileInputStream; import java.io.FilenameFilter; @@ -62,6 +54,13 @@ import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.io.IOUtils; @@ -149,6 +148,7 @@ import org.kitodo.production.services.data.base.ProjectSearchService; import org.kitodo.production.services.file.FileService; import org.kitodo.production.services.workflow.WorkflowControllerService; +import org.kitodo.production.workflow.KitodoNamespaceContext; import org.kitodo.serviceloader.KitodoServiceLoader; import org.primefaces.model.charts.ChartData; import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearAxes; @@ -158,6 +158,16 @@ import org.primefaces.model.charts.optionconfig.tooltip.Tooltip; import org.primefaces.model.charts.pie.PieChartDataSet; import org.primefaces.model.charts.pie.PieChartModel; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import com.itextpdf.text.Document; +import com.itextpdf.text.DocumentException; +import com.itextpdf.text.PageSize; +import com.itextpdf.text.Paragraph; +import com.itextpdf.text.Rectangle; +import com.itextpdf.text.pdf.PdfPTable; +import com.itextpdf.text.pdf.PdfWriter; public class ProcessService extends ProjectSearchService { private final FileService fileService = ServiceManager.getFileService(); @@ -2423,6 +2433,31 @@ public static void deleteSymlinksFromUserHomes(Task task) { } } + /** + * Get the note list from metadata file by the xpath + * + * @param process + * The process for which the metadata file is searched for + * @param xpath + * The xpath to get to the node list + * @return The node list of process by the of xpath + */ + public NodeList getNodeListFromMetadataFile(Process process, String xpath) throws IOException { + try (InputStream fileInputStream = ServiceManager.getFileService().readMetadataFile(process)) { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + builderFactory.setNamespaceAware(true); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + org.w3c.dom.Document xmlDocument = builder.parse(fileInputStream); + + XPath xPath = XPathFactory.newInstance().newXPath(); + xPath.setNamespaceContext(new KitodoNamespaceContext()); + return (NodeList) xPath.compile(xpath).evaluate(xmlDocument, XPathConstants.NODESET); + } catch (ParserConfigurationException | SAXException | XPathExpressionException e) { + logger.error(e.getMessage(), e); + throw new IOException(e); + } + } + /** * Export Mets. * diff --git a/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java b/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java index 751962d8920..11ff3043f11 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/workflow/WorkflowControllerService.java @@ -13,7 +13,6 @@ import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.net.URI; import java.nio.file.Paths; import java.util.ArrayList; @@ -24,14 +23,6 @@ import java.util.Objects; import java.util.concurrent.locks.ReentrantLock; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.kitodo.api.command.CommandResult; @@ -62,10 +53,6 @@ import org.kitodo.production.services.ServiceManager; import org.kitodo.production.services.data.TaskService; import org.kitodo.production.thread.TaskScriptThread; -import org.kitodo.production.workflow.KitodoNamespaceContext; -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; public class WorkflowControllerService { @@ -685,20 +672,7 @@ private boolean runScriptCondition(String script, Process process) throws IOExce } private boolean runXPathCondition(Process process, String xpath) throws IOException { - try (InputStream fileInputStream = ServiceManager.getFileService().readMetadataFile(process)) { - DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); - builderFactory.setNamespaceAware(true); - DocumentBuilder builder = builderFactory.newDocumentBuilder(); - Document xmlDocument = builder.parse(fileInputStream); - - XPath xPath = XPathFactory.newInstance().newXPath(); - xPath.setNamespaceContext(new KitodoNamespaceContext()); - NodeList nodeList = (NodeList) xPath.compile(xpath).evaluate(xmlDocument, XPathConstants.NODESET); - return nodeList.getLength() > 0; - } catch (ParserConfigurationException | SAXException | XPathExpressionException e) { - logger.error(e.getMessage(), e); - throw new IOException(e); - } + return ServiceManager.getProcessService().getNodeListFromMetadataFile(process, xpath).getLength() > 0; } private void verifyTask(Task task) {