From 56363c4b223706142aba1c7f643918fa51ccc202 Mon Sep 17 00:00:00 2001 From: Markus Weigelt Date: Thu, 23 Sep 2021 17:25:05 +0200 Subject: [PATCH] #3408 fix remarks of review --- .../forms/createprocess/ProcessDataTab.java | 69 ++++++++++-------- .../production/process/TitleGenerator.java | 73 +++++++++++-------- .../services/data/ProcessService.java | 2 +- 3 files changed, 80 insertions(+), 64 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 2e1f98b4fba..b4495544c7e 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 @@ -14,13 +14,16 @@ import java.io.IOException; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; 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; @@ -53,7 +56,8 @@ public class ProcessDataTab { /** * Set docType. * - * @param docType as java.lang.String + * @param docType + * as java.lang.String */ public void setDocType(String docType) { if (Objects.isNull(allDocTypes) || allDocTypes.isEmpty()) { @@ -63,7 +67,7 @@ public void setDocType(String docType) { this.docType = docType; } else { this.docType = (String) allDocTypes.get(0).getValue(); - Helper.setErrorMessage("docTypeNotFound", new Object[] {docType}); + Helper.setErrorMessage("docTypeNotFound", new Object[] {docType }); } if (!this.createProcessForm.getProcesses().isEmpty()) { this.createProcessForm.getProcesses().get(0).getWorkpiece().getLogicalStructure().setType(this.docType); @@ -71,7 +75,8 @@ public void setDocType(String docType) { this.createProcessForm.getProcessMetadataTab().setProcessDetails(ProcessFieldedMetadata.EMPTY); } else { ProcessFieldedMetadata metadata = this.createProcessForm.getProcessMetadataTab() - .initializeProcessDetails(this.createProcessForm.getProcesses().get(0).getWorkpiece().getLogicalStructure()); + .initializeProcessDetails( + this.createProcessForm.getProcesses().get(0).getWorkpiece().getLogicalStructure()); this.createProcessForm.getProcessMetadataTab().setProcessDetails(metadata); } } @@ -107,7 +112,8 @@ public boolean isUsingTemplates() { /** * Set useTemplate. * - * @param usingTemplates as boolean + * @param usingTemplates + * as boolean */ public void setUsingTemplates(boolean usingTemplates) { ServiceManager.getImportService().setUsingTemplates(usingTemplates); @@ -125,7 +131,8 @@ public String getTiffHeaderImageDescription() { /** * Set tiffHeaderImageDescription. * - * @param tiffHeaderImageDescription as java.lang.String + * @param tiffHeaderImageDescription + * as java.lang.String */ public void setTiffHeaderImageDescription(String tiffHeaderImageDescription) { this.tiffHeaderImageDescription = tiffHeaderImageDescription; @@ -143,7 +150,8 @@ public String getTiffHeaderDocumentName() { /** * Set tiffHeaderDocumentName. * - * @param tiffHeaderDocumentName as java.lang.String + * @param tiffHeaderDocumentName + * as java.lang.String */ public void setTiffHeaderDocumentName(String tiffHeaderDocumentName) { this.tiffHeaderDocumentName = tiffHeaderDocumentName; @@ -161,7 +169,8 @@ public int getGuessedImages() { /** * Set guessedImages. * - * @param guessedImages as int + * @param guessedImages + * as int */ public void setGuessedImages(int guessedImages) { this.guessedImages = guessedImages; @@ -179,7 +188,8 @@ public List getAllDoctypes() { /** * Set allDocTypes. * - * @param allDocTypes as java.util.List + * @param allDocTypes + * as java.util.List */ void setAllDocTypes(List allDocTypes) { this.allDocTypes = allDocTypes; @@ -197,18 +207,18 @@ public void generateProcessTitleAndTiffHeader() { List processDetails = this.createProcessForm.getProcessMetadataTab().getProcessDetailsElements(); Process process = this.createProcessForm.getMainProcess(); try { - StructuralElementViewInterface docTypeView = createProcessForm.getRulesetManagement().getStructuralElementView( - docType, createProcessForm.getAcquisitionStage(), createProcessForm.getPriorityList()); + StructuralElementViewInterface docTypeView = createProcessForm.getRulesetManagement() + .getStructuralElementView(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() }); } + String currentTitle = TitleGenerator.getValueOfMetadataID(TitleGenerator.TITLE_DOC_MAIN, processDetails); - String currentTitle = TitleGenerator.getCurrentValue(TitleGenerator.TITLE_DOC_MAIN, processDetails); - - if ((Objects.isNull(currentTitle) || currentTitle.isEmpty())) { + if (StringUtils.isBlank(currentTitle)) { Process parentProcess = createProcessForm.getTitleRecordLinkTab().getTitleRecordProcess(); if (Objects.nonNull(parentProcess)) { @@ -224,17 +234,17 @@ public void generateProcessTitleAndTiffHeader() { } String atstsl = ProcessService.generateProcessTitleAndGetAtstsl(processDetails, processTitle, process, - currentTitle); + currentTitle); // document name is generally equal to process title this.tiffHeaderDocumentName = process.getTitle(); - this.tiffHeaderImageDescription = ProcessService.generateTiffHeader( - processDetails, 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 getTitleFromAncestors() { @@ -245,24 +255,19 @@ 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 + // get title of ancestors where TitleDocMain exists when several processes were + // imported for (TempProcess tempProcess : ancestors) { ProcessFieldedMetadata processFieldedMetadata = initializeTempProcessDetails(tempProcess); - AtomicReference tempTitle = new AtomicReference<>(); - processFieldedMetadata.getChildMetadata().parallelStream() - .filter(metadata -> TitleGenerator.TITLE_DOC_MAIN.equals(metadata.getKey())).findFirst() - .ifPresent(metadata -> { - if (metadata instanceof MetadataEntry) { - tempTitle.set(((MetadataEntry) metadata).getValue()); - } - }); - - if (Objects.nonNull(tempTitle.get()) && !tempTitle.get().isEmpty()) { - return tempTitle.get(); + Optional 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; + return tempTitle.get(); } /** @@ -273,8 +278,8 @@ private String getTitleFromAncestors() { */ private ProcessFieldedMetadata initializeTempProcessDetails(TempProcess tempProcess) { var metadata = ImportService.initializeProcessDetails(tempProcess.getWorkpiece().getLogicalStructure(), - createProcessForm.getRulesetManagement(), createProcessForm.getAcquisitionStage(), - createProcessForm.getPriorityList()); + createProcessForm.getRulesetManagement(), createProcessForm.getAcquisitionStage(), + createProcessForm.getPriorityList()); metadata.setMetadata(ImportService.importMetadata(tempProcess.getMetadataNodes(), MdSec.DMD_SEC)); return metadata; } diff --git a/Kitodo/src/main/java/org/kitodo/production/process/TitleGenerator.java b/Kitodo/src/main/java/org/kitodo/production/process/TitleGenerator.java index 95759fee869..802ca291a0f 100644 --- a/Kitodo/src/main/java/org/kitodo/production/process/TitleGenerator.java +++ b/Kitodo/src/main/java/org/kitodo/production/process/TitleGenerator.java @@ -23,13 +23,18 @@ public class TitleGenerator extends Generator { + /** + * Metadata identifier for title doc main + */ public static final String TITLE_DOC_MAIN = "TitleDocMain"; /** * Constructor for TitleGenerator. * - * @param atstsl field used for title generation - * @param processDetailsList fields used for title generation + * @param atstsl + * field used for title generation + * @param processDetailsList + * fields used for title generation */ public TitleGenerator(String atstsl, List processDetailsList) { super(atstsl, processDetailsList); @@ -38,23 +43,28 @@ public TitleGenerator(String atstsl, List processDetailsList) { /** * Generate title for process. * - * @param titleDefinition definition for title to generation - * @param genericFields Map of Strings + * @param titleDefinition + * definition for title to generation + * @param genericFields + * Map of Strings * @return String */ public String generateTitle(String titleDefinition, Map genericFields) throws ProcessGenerationException { - return generateTitle(titleDefinition, genericFields, getCurrentValue(TITLE_DOC_MAIN, processDetailsList)); + 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 + * @param titleDefinition + * definition for title to generation + * @param genericFields + * Map of Strings * @return String */ - public String generateTitle(String titleDefinition, Map genericFields, String title) throws ProcessGenerationException { + public String generateTitle(String titleDefinition, Map genericFields, String title) + throws ProcessGenerationException { String currentAuthors = ImportService.getListOfCreators(this.processDetailsList); StringBuilder newTitle = new StringBuilder(); @@ -86,15 +96,15 @@ public String generateTitle(String titleDefinition, Map genericF } /** - * Forms the author title key, or the title key (4/2/2/1) if no author is - * given. The author title key is a librarian sort criteria, composed out of - * the first four letters of the first author’s last name, followed by the - * first four letters of the title of the works. The title key (4/2/2/1) a - * librarian sort criteria composed of the title of the works, taking the - * first four letters of the first word, each the first two letters of the - * second and third word, and the first letter of the fourth word of the - * title. Note that this implementation removes non-word characters (any - * characters except A-Z, such as letters with diacritics). + * Forms the author title key, or the title key (4/2/2/1) if no author is given. + * The author title key is a librarian sort criteria, composed out of the first + * four letters of the first author’s last name, followed by the first four + * letters of the title of the works. The title key (4/2/2/1) a librarian sort + * criteria composed of the title of the works, taking the first four letters of + * the first word, each the first two letters of the second and third word, and + * the first letter of the fourth word of the title. Note that this + * implementation removes non-word characters (any characters except A-Z, such + * as letters with diacritics). * *

* Examples:
@@ -105,8 +115,7 @@ public String generateTitle(String titleDefinition, Map genericF * @param title * the title of the work * @param author - * the last name of the (first) author, may be {@code null} or - * empty + * the last name of the (first) author, may be {@code null} or empty * @return the author title key, or the title key (4/2/2/1) */ public static String createAtstsl(String title, String author) { @@ -139,22 +148,23 @@ public static String createAtstsl(String title, String author) { return result.toString().replaceAll("[\\W]", ""); // delete umlauts etc. } - public static String getCurrentValue(String metadataTag, List processDetailsList) { - //int counter = 0; + /** + * 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 processDetailsList) { for (ProcessDetail row : 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++; - }*/ 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; } private String evaluateAdditionalDetailsRows(String currentTitle, String currentAuthors, String token) @@ -163,7 +173,8 @@ private String evaluateAdditionalDetailsRows(String currentTitle, String current for (ProcessDetail row : this.processDetailsList) { String rowMetadataID = row.getMetadataID(); String rowValue = ImportService.getProcessDetailValue(row); - // if it is the ATS or TSL field, then use the calculated atstsl if it does not already exist + // if it is the ATS or TSL field, then use the calculated atstsl if it does not + // already exist if ("TSL_ATS".equals(rowMetadataID)) { if (StringUtils.isBlank(rowValue)) { this.atstsl = createAtstsl(currentTitle, currentAuthors); 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 23ec89f309d..37bc2595616 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 @@ -2287,7 +2287,7 @@ private HashSet getProcessesLinkedInLogicalDivision( public static String generateProcessTitleAndGetAtstsl(List processDetails, String titleDefinition, Process process) throws ProcessGenerationException { return generateProcessTitleAndGetAtstsl(processDetails, titleDefinition, process, - TitleGenerator.getCurrentValue(TitleGenerator.TITLE_DOC_MAIN, processDetails)); + TitleGenerator.getValueOfMetadataID(TitleGenerator.TITLE_DOC_MAIN, processDetails)); } /**