Skip to content

Commit

Permalink
kitodo#3408 read titledocmain from meta.xml if title record process i…
Browse files Browse the repository at this point in the history
…s selected
  • Loading branch information
markusweigelt committed Sep 24, 2021
1 parent a728b1b commit 50ed183
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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();
}
Expand All @@ -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();

Expand All @@ -247,7 +253,6 @@ private String getTitleFromAncestors() {
}

List<TempProcess> ancestors = createProcessForm.getProcesses().subList(1, processesSize);
AtomicReference<String> tempTitle = new AtomicReference<>();

// get title of ancestors where TitleDocMain exists when several processes were
// imported
Expand All @@ -259,7 +264,7 @@ private String getTitleFromAncestors() {
return ((MetadataEntry) metadataOptional.get()).getValue();
}
}
return tempTitle.get();
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<Process, ProcessDTO, ProcessDAO> {
private final FileService fileService = ServiceManager.getFileService();
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 50ed183

Please sign in to comment.