Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generation tsl ats #4668

Merged
merged 14 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kitodo.api.MdSec;
import org.kitodo.api.Metadata;
import org.kitodo.api.MetadataEntry;
import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface;
import org.kitodo.data.database.beans.Process;
import org.kitodo.exceptions.DoctypeMissingException;
import org.kitodo.exceptions.ProcessGenerationException;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.helper.TempProcess;
import org.kitodo.production.process.TitleGenerator;
import org.kitodo.production.services.ServiceManager;
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 All @@ -35,7 +44,6 @@ public class ProcessDataTab {
private List<SelectItem> allDocTypes;
private final CreateProcessForm createProcessForm;
private String docType;
private String atstsl = "";
private String tiffHeaderImageDescription = "";
private String tiffHeaderDocumentName = "";
private int guessedImages = 0;
Expand Down Expand Up @@ -192,23 +200,85 @@ public void generateProcessTitleAndTiffHeader() {
Process process = this.createProcessForm.getMainProcess();
try {
StructuralElementViewInterface docTypeView = createProcessForm.getRulesetManagement().getStructuralElementView(
docType, createProcessForm.getAcquisitionStage(), createProcessForm.getPriorityList());
docType, createProcessForm.getAcquisitionStage(), createProcessForm.getPriorityList());
String processTitle = docTypeView.getProcessTitle().orElse("");
if (processTitle.isEmpty()) {
Helper.setErrorMessage("newProcess.titleGeneration.creationRuleNotFound",
new Object[] {getDocTypeLabel(docType), process.getRuleset().getTitle() });
}
this.atstsl = ProcessService.generateProcessTitle(this.atstsl, processDetails,
processTitle, process);

String currentTitle = TitleGenerator.getValueOfMetadataID(TitleGenerator.TITLE_DOC_MAIN, processDetails);

if (StringUtils.isBlank(currentTitle)) {
Process parentProcess = createProcessForm.getTitleRecordLinkTab().getTitleRecordProcess();
if (Objects.nonNull(parentProcess)) {
currentTitle = getTitleFromMetaXML(parentProcess);
} else {
currentTitle = getTitleFromAncestors();
}
}

String atstsl = ProcessService.generateProcessTitleAndGetAtstsl(processDetails, processTitle, process,
currentTitle);

// document name is generally equal to process title
this.tiffHeaderDocumentName = process.getTitle();
this.tiffHeaderImageDescription = ProcessService.generateTiffHeader(
processDetails, this.atstsl, ServiceManager.getImportService().getTiffDefinition(), this.docType);
this.tiffHeaderImageDescription = ProcessService.generateTiffHeader(processDetails, atstsl,
ServiceManager.getImportService().getTiffDefinition(), this.docType);
} catch (ProcessGenerationException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
Ajax.update("editForm:processFromTemplateTabView:processDataEditGrid",
"editForm:processFromTemplateTabView:processMetadata");
"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();
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
}

private String getTitleFromAncestors() {
int processesSize = createProcessForm.getProcesses().size();

if (processesSize <= 1) {
return null;
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
}

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

// get title of ancestors where TitleDocMain exists when several processes were
// imported
for (TempProcess tempProcess : ancestors) {
ProcessFieldedMetadata processFieldedMetadata = initializeTempProcessDetails(tempProcess);
Optional<Metadata> metadataOptional = processFieldedMetadata.getChildMetadata().parallelStream()
.filter(metadata -> TitleGenerator.TITLE_DOC_MAIN.equals(metadata.getKey())).findFirst();
if (metadataOptional.isPresent() && metadataOptional.get() instanceof MetadataEntry) {
return ((MetadataEntry) metadataOptional.get()).getValue();
}
}
return null;
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* initialize process details table.
*
* @param tempProcess
* whose metadata should be queried
*/
private ProcessFieldedMetadata initializeTempProcessDetails(TempProcess tempProcess) {
var metadata = ImportService.initializeProcessDetails(tempProcess.getWorkpiece().getLogicalStructure(),
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
createProcessForm.getRulesetManagement(), createProcessForm.getAcquisitionStage(),
createProcessForm.getPriorityList());
metadata.setMetadata(ImportService.importMetadata(tempProcess.getMetadataNodes(), MdSec.DMD_SEC));
return metadata;
}

private String getDocTypeLabel(String docType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

public class TitleGenerator extends Generator {

private static final String TITLE_DOC_MAIN = "TitleDocMain";
/**
* Metadata identifier for title doc main.
*/
public static final String TITLE_DOC_MAIN = "TitleDocMain";
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved

/**
* Constructor for TitleGenerator.
Expand All @@ -44,9 +47,21 @@ public TitleGenerator(String atstsl, List<ProcessDetail> processDetailsList) {
*/
public String generateTitle(String titleDefinition, Map<String, String> genericFields)
throws ProcessGenerationException {
String currentAuthors = ImportService.getListOfCreators(this.processDetailsList);
String currentTitle = getCurrentValue(TITLE_DOC_MAIN);
return generateTitle(titleDefinition, genericFields, getValueOfMetadataID(TITLE_DOC_MAIN, processDetailsList));
}

/**
* Generate title for process.
*
* @param titleDefinition
* definition for title to generation
* @param genericFields
* Map of Strings
* @return String
*/
public String generateTitle(String titleDefinition, Map<String, String> genericFields, String title)
throws ProcessGenerationException {
String currentAuthors = ImportService.getListOfCreators(this.processDetailsList);
StringBuilder newTitle = new StringBuilder();

StringTokenizer tokenizer = new StringTokenizer(titleDefinition, "+");
Expand All @@ -65,7 +80,7 @@ public String generateTitle(String titleDefinition, Map<String, String> genericF
}
}
} else {
newTitle.append(evaluateAdditionalDetailsRows(currentTitle, currentAuthors, token));
newTitle.append(evaluateAdditionalDetailsRows(title, currentAuthors, token));
}
}

Expand Down Expand Up @@ -130,22 +145,23 @@ public static String createAtstsl(String title, String author) {
return result.toString().replaceAll("[\\W]", ""); // delete umlauts etc.
}

private String getCurrentValue(String metadataTag) {
//int counter = 0;
for (ProcessDetail row : this.processDetailsList) {
// TODO: check how to set "autogenerated" flag for metadata in ruleset!
/* if (row.isAutogenerated() && metadataValue.isEmpty()) {
row.setValue(String.valueOf(System.currentTimeMillis() + counter));
ProcessMetadataTab.setAdditionalDetailsRow(row,
String.valueOf(System.currentTimeMillis() + counter));
counter++;
}*/
/**
* Get the value of metadata identifier from process details list.
*
* @param metadataID
* The metadata identifier
* @param processDetailsList
* The process detail list that contains the potential value
* @return The value of metadata identifier or null
*/
public static String getValueOfMetadataID(String metadataID, List<ProcessDetail> processDetailsList) {
for (ProcessDetail row : processDetailsList) {
String metadata = row.getMetadataID();
if (Objects.nonNull(metadata) && metadata.equals(metadataTag)) {
if (Objects.nonNull(metadata) && metadata.equals(metadataID)) {
return ImportService.getProcessDetailValue(row);
}
}
return "";
return null;
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
}

private String evaluateAdditionalDetailsRows(String currentTitle, String currentAuthors, String token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ public static void createProcessTitle(TempProcess tempProcess,
StructuralElementViewInterface docTypeView = rulesetManagementInterface
.getStructuralElementView(docType, acquisitionStage, priorityList);
String processTitle = docTypeView.getProcessTitle().orElse("");
ProcessService.generateProcessTitle("", processDetails,
ProcessService.generateProcessTitleAndGetAtstsl(processDetails,
processTitle, tempProcess.getProcess());
}

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 @@ -2280,12 +2290,28 @@ private HashSet<Process> getProcessesLinkedInLogicalDivision(
}

/**
* Generate process title.
* Generate and set the title to process and gets the atstsl.
*
* @return String atstsl
*/
public static String generateProcessTitleAndGetAtstsl(List<ProcessDetail> processDetails, String titleDefinition,
Process process) throws ProcessGenerationException {
return generateProcessTitleAndGetAtstsl(processDetails, titleDefinition, process,
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
TitleGenerator.getValueOfMetadataID(TitleGenerator.TITLE_DOC_MAIN, processDetails));
}

/**
* Generate and set the title to process using current title parameter and gets
* the atstsl.
*
* @param title
* of the work to generate atstsl
* @return String atstsl
*/
public static String generateProcessTitle(String atstsl, List<ProcessDetail> processDetails, String titleDefinition,
Process process) throws ProcessGenerationException {
TitleGenerator titleGenerator = new TitleGenerator(atstsl, processDetails);
String newTitle = titleGenerator.generateTitle(titleDefinition, null);
public static String generateProcessTitleAndGetAtstsl(List<ProcessDetail> processDetails, String titleDefinition,
Process process, String title) throws ProcessGenerationException {
TitleGenerator titleGenerator = new TitleGenerator(null, processDetails);
String newTitle = titleGenerator.generateTitle(titleDefinition, null, (Objects.nonNull(title) ? title : ""));
process.setTitle(newTitle);
// atstsl is created in title generator and next used in tiff header generator
return titleGenerator.getAtstsl();
Expand Down Expand Up @@ -2407,6 +2433,31 @@ public static void deleteSymlinksFromUserHomes(Task task) {
}
}

/**
* Get the note list from metadata file by the xpath.
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
*
* @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