Skip to content

Commit

Permalink
kitodo#3408 fix remarks of review
Browse files Browse the repository at this point in the history
  • Loading branch information
markusweigelt committed Sep 23, 2021
1 parent d280baf commit 56363c4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -63,15 +67,16 @@ 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);
if (this.docType.isEmpty()) {
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);
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -179,7 +188,8 @@ public List<SelectItem> getAllDoctypes() {
/**
* Set allDocTypes.
*
* @param allDocTypes as java.util.List
* @param allDocTypes
* as java.util.List
*/
void setAllDocTypes(List<SelectItem> allDocTypes) {
this.allDocTypes = allDocTypes;
Expand All @@ -197,18 +207,18 @@ public void generateProcessTitleAndTiffHeader() {
List<ProcessDetail> 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)) {
Expand All @@ -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() {
Expand All @@ -245,24 +255,19 @@ 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
// get title of ancestors where TitleDocMain exists when several processes were
// imported
for (TempProcess tempProcess : ancestors) {
ProcessFieldedMetadata processFieldedMetadata = initializeTempProcessDetails(tempProcess);
AtomicReference<String> 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<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;
return tempTitle.get();
}

/**
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessDetail> processDetailsList) {
super(atstsl, processDetailsList);
Expand All @@ -38,23 +43,28 @@ public TitleGenerator(String atstsl, List<ProcessDetail> 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<String, String> 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<String, String> genericFields, String title) throws ProcessGenerationException {
public String generateTitle(String titleDefinition, Map<String, String> genericFields, String title)
throws ProcessGenerationException {
String currentAuthors = ImportService.getListOfCreators(this.processDetailsList);
StringBuilder newTitle = new StringBuilder();

Expand Down Expand Up @@ -86,15 +96,15 @@ public String generateTitle(String titleDefinition, Map<String, String> 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).
*
* <p>
* <u>Examples:</u><br>
Expand All @@ -105,8 +115,7 @@ public String generateTitle(String titleDefinition, Map<String, String> 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) {
Expand Down Expand Up @@ -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<ProcessDetail> 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<ProcessDetail> 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)
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2287,7 +2287,7 @@ private HashSet<Process> getProcessesLinkedInLogicalDivision(
public static String generateProcessTitleAndGetAtstsl(List<ProcessDetail> 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));
}

/**
Expand Down

0 comments on commit 56363c4

Please sign in to comment.