Skip to content

Commit

Permalink
Merge pull request #4124 from Kathrin-Huber/realize_parent_creation_f…
Browse files Browse the repository at this point in the history
…rom_xml

enable parentExtraction from record
  • Loading branch information
Kathrin-Huber authored Jan 7, 2021
2 parents 8aea4da + 4da7713 commit 754f5d2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
18 changes: 18 additions & 0 deletions Kitodo-API/src/main/java/org/kitodo/config/OPACConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public static List<String> getXsltMappingFiles(String catalogName) {
.map(c -> c.getRoot().getValue().toString()).collect(Collectors.toList());
}

/**
* Retrieve the "parentMappingFile" of the catalog identified by its title.
* @param catalogName String identifying the catalog by its title
* @return String containing given catalogs "parentMappingFile"
*/
public static String getXsltMappingFileForParentInRecord(String catalogName) {
return getCatalog(catalogName).getString("parentMappingFile");
}

/**
* Retrieve the "queryDelimiter" of the catalog identified by its title.
* @param catalogName String identifying the catalog by its title
Expand Down Expand Up @@ -247,6 +256,15 @@ public static String getFtpPassword(String catalogName) {
return getCatalog(catalogName).configurationAt("credentials").getString("password");
}

/**
* If a mappingFile for parentInRecord is configured.
* @param catalogName OPAC for witch to get the config
* @return true, if mapping file is configured
*/
public static boolean isParentInRecord(String catalogName) {
return StringUtils.isNotBlank(getXsltMappingFileForParentInRecord(catalogName));
}

/**
* Retrieve the list of catalogs' titles from config file.
* @return List of Strings containing all catalog titles.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ private TempProcess createTempProcessFromDocument(Document document, int templat
}

private String importProcessAndReturnParentID(String recordId, LinkedList<TempProcess> allProcesses, String opac,
int projectID, int templateID)
int projectID, int templateID, boolean isParentInRecord)
throws IOException, ProcessGenerationException, XPathExpressionException, ParserConfigurationException,
NoRecordFoundException, UnsupportedFormatException, URISyntaxException, SAXException {

Document internalDocument = importDocument(opac, recordId, allProcesses.isEmpty());
Document internalDocument = importDocument(opac, recordId, allProcesses.isEmpty(), isParentInRecord);
TempProcess tempProcess = createTempProcessFromDocument(internalDocument, templateID, projectID);

// Workaround for classifying MultiVolumeWorks with insufficient information
Expand All @@ -384,7 +384,7 @@ private String importProcessAndReturnParentID(String recordId, LinkedList<TempPr
}

allProcesses.add(tempProcess);
return getParentID(internalDocument);
return isParentInRecord ? null : getParentID(internalDocument);
}

/**
Expand Down Expand Up @@ -432,11 +432,19 @@ public LinkedList<TempProcess> importProcessHierarchy(String recordId, String op
parentXpath = parentXpath.replace(REPLACE_ME, parentIdMetadata.toArray()[0].toString());
}

String parentID = importProcessAndReturnParentID(recordId, processes, opac, projectId, templateId);
String parentID = importProcessAndReturnParentID(recordId, processes, opac, projectId, templateId, false);
Template template = ServiceManager.getTemplateService().getById(templateId);
if (Objects.isNull(template.getRuleset())) {
throw new ProcessGenerationException("Ruleset of template " + template.getId() + " is null!");
}
importParents(recordId, opac, projectId, templateId, importDepth, processes, parentID, template);
return processes;
}

private void importParents(String recordId, String opac, int projectId, int templateId, int importDepth,
LinkedList<TempProcess> processes, String parentID, Template template)
throws ProcessGenerationException, IOException, XPathExpressionException, ParserConfigurationException,
NoRecordFoundException, UnsupportedFormatException, URISyntaxException, SAXException, DAOException {
int level = 1;
this.parentTempProcess = null;
while (Objects.nonNull(parentID) && level < importDepth) {
Expand All @@ -445,7 +453,13 @@ public LinkedList<TempProcess> importProcessHierarchy(String recordId, String op
try {
Process parentProcess = loadParentProcess(parentIDMetadata, template.getRuleset().getId(), projectId);
if (Objects.isNull(parentProcess)) {
parentID = importProcessAndReturnParentID(parentID, processes, opac, projectId, templateId);
if (OPACConfig.isParentInRecord(opac)) {
parentID = importProcessAndReturnParentID(recordId, processes, opac, projectId, templateId,
true);
} else {
parentID = importProcessAndReturnParentID(parentID, processes, opac, projectId, templateId,
false);
}
level++;
} else {
logger.info("Process with ID '" + parentID + "' already in database. Stop hierarchical import.");
Expand All @@ -455,17 +469,18 @@ public LinkedList<TempProcess> importProcessHierarchy(String recordId, String op
break;
}
} catch (SAXParseException | DAOException e) {
// this happens for example if a document is part of a "Virtueller Bestand" in Kalliope for which a
// this happens for example if a document is part of a "Virtueller Bestand" in
// Kalliope for which a
// proper "record" is not returned from its SRU interface
logger.error(e.getLocalizedMessage());
break;
}
}
// always try to find a parent for last imported process (e.g. level == importDepth) in the database!
// always try to find a parent for last imported process (e.g. level ==
// importDepth) in the database!
if (Objects.nonNull(parentID) && level == importDepth) {
this.parentTempProcess = checkForParent(parentID, template.getRuleset().getId(), projectId);
}
return processes;
}

private TempProcess checkForParent(String parentID, int rulesetID, int projectID) throws DAOException, IOException,
Expand Down Expand Up @@ -548,9 +563,9 @@ public LinkedList<TempProcess> getChildProcesses(String opac, String elementID,
}
}

private Document importDocument(String opac, String identifier, boolean extractExemplars) throws NoRecordFoundException,
UnsupportedFormatException, URISyntaxException, IOException, XPathExpressionException,
ParserConfigurationException, SAXException {
private Document importDocument(String opac, String identifier, boolean extractExemplars, boolean isParentInRecord)
throws NoRecordFoundException, UnsupportedFormatException, URISyntaxException, IOException,
XPathExpressionException, ParserConfigurationException, SAXException {
// ################ IMPORT #################
importModule = initializeImportModule();
DataRecord dataRecord = importModule.getFullRecordById(opac, identifier);
Expand All @@ -563,7 +578,7 @@ private Document importDocument(String opac, String identifier, boolean extractE
// depending on metadata and return form, call corresponding schema converter module!
SchemaConverterInterface converter = getSchemaConverter(dataRecord);

List<File> mappingFiles = getMappingFiles(opac);
List<File> mappingFiles = getMappingFiles(opac, isParentInRecord);

// transform dataRecord to Kitodo internal format using appropriate SchemaConverter!
File debugFolder = ConfigCore.getKitodoDebugDirectory();
Expand Down Expand Up @@ -594,10 +609,17 @@ private NodeList extractMetadataNodeList(Document document) throws ProcessGenera
return kitodoNode.getChildNodes();
}

private List<File> getMappingFiles(String opac) throws URISyntaxException {
private List<File> getMappingFiles(String opac, boolean forParentInRecord) throws URISyntaxException {
List<File> mappingFiles = new ArrayList<>();

List<String> mappingFileNames;
try {
for (String mappingFileName : OPACConfig.getXsltMappingFiles(opac)) {
if (forParentInRecord) {
mappingFileNames = Collections.singletonList(OPACConfig.getXsltMappingFileForParentInRecord(opac));
} else {
mappingFileNames = OPACConfig.getXsltMappingFiles(opac);
}
for (String mappingFileName : mappingFileNames) {
URI xsltFile = Paths.get(ConfigCore.getParameter(ParameterCore.DIR_XSLT)).toUri()
.resolve(new URI(mappingFileName.trim()));
mappingFiles.add(ServiceManager.getFileService().getFile(xsltFile));
Expand All @@ -608,6 +630,10 @@ private List<File> getMappingFiles(String opac) throws URISyntaxException {
return mappingFiles;
}

private List<File> getMappingFiles(String opac) throws URISyntaxException {
return getMappingFiles(opac, false);
}

/**
* Converts DOM node list of Kitodo metadata elements to metadata objects.
*
Expand Down Expand Up @@ -1101,7 +1127,7 @@ public Process importProcess(String ppn, int projectId, int templateId, String s
String metadataLanguage = ServiceManager.getUserService().getCurrentUser().getMetadataLanguage();
List<Locale.LanguageRange> priorityList = Locale.LanguageRange
.parse(metadataLanguage.isEmpty() ? "en" : metadataLanguage);
importProcessAndReturnParentID(ppn, processList, selectedCatalog, projectId, templateId);
importProcessAndReturnParentID(ppn, processList, selectedCatalog, projectId, templateId, false);
tempProcess = processList.get(0);
processTempProcess(tempProcess, template,
ServiceManager.getRulesetService().openRuleset(template.getRuleset()), "create", priorityList);
Expand Down
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/kitodo_opac.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<mappingFiles>
<file>mods2kitodo.xsl</file>
</mappingFiles>
<parentMappingFile>parentMapping.xsl</parentMappingFile>
<config>
<param name="host" value="kalliope-verbund.info" />
<param name="scheme" value="http" />
Expand Down

0 comments on commit 754f5d2

Please sign in to comment.