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

Improve newspaper creation #4924

Merged
merged 6 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,44 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.production.converter;

import java.util.List;
import java.util.Optional;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.inject.Named;

import org.kitodo.production.forms.createprocess.ProcessDetail;
import org.kitodo.production.forms.createprocess.ProcessFieldedMetadata;

@Named
public class ProcessDetailConverter implements Converter<ProcessDetail> {

@Override
public ProcessDetail getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
List<ProcessDetail> list = (List<ProcessDetail>) component.getAttributes().get("allMetadataTypes");
Optional<ProcessDetail> processDetail = list.parallelStream()
.filter(metadata -> metadata.getMetadataID().equals(value)).findFirst();
return processDetail.orElseGet(ProcessFieldedMetadata::new);
}

@Override
public String getAsString(FacesContext context, UIComponent component, ProcessDetail value) throws ConverterException {
if (value != null) {
return value.getMetadataID();
}
return "";
}
}
75 changes: 66 additions & 9 deletions Kitodo/src/main/java/org/kitodo/production/forms/CalendarForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.naming.ConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewInterface;
import org.kitodo.config.ConfigCore;
import org.kitodo.config.enums.ParameterCore;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.DoctypeMissingException;
import org.kitodo.exceptions.ProcessGenerationException;
import org.kitodo.production.forms.createprocess.ProcessDetail;
import org.kitodo.production.forms.createprocess.ProcessTextMetadata;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.helper.XMLUtils;
import org.kitodo.production.helper.tasks.GeneratesNewspaperProcessesThread;
Expand All @@ -58,8 +62,10 @@
import org.kitodo.production.model.bibliography.course.IndividualIssue;
import org.kitodo.production.model.bibliography.course.Issue;
import org.kitodo.production.model.bibliography.course.metadata.CountableMetadata;
import org.kitodo.production.process.NewspaperProcessesGenerator;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.calendar.CalendarService;
import org.kitodo.production.services.data.ImportService;
import org.primefaces.PrimeFaces;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
Expand Down Expand Up @@ -110,7 +116,7 @@ public class CalendarForm implements Serializable {
private UploadedFile uploadedFile;
private LocalDate selectedDate;
private Block selectedBlock = null;
private List<MetadataViewInterface> metadataTypes = null;
private List<ProcessDetail> metadataTypes = null;

/**
* The field course holds the course of appearance currently under edit by
Expand Down Expand Up @@ -138,6 +144,7 @@ public class CalendarForm implements Serializable {
public CalendarForm() {
issueColours = ConfigCore.getParameterOrDefaultValue(ParameterCore.ISSUE_COLOURS).split(";");
course = new Course();

}

/**
Expand Down Expand Up @@ -596,7 +603,8 @@ public void removeBlock(Block block) {
*/
public void addIssue(Block block) {
if (Objects.nonNull(block)) {
Issue issue = block.addIssue();
block.addIssue();
block.checkIssuesWithSameHeading();
}
}

Expand Down Expand Up @@ -745,7 +753,7 @@ public void addMetadata(Issue issue) {
if (!selectedBlock.getIssues().isEmpty() && Objects.nonNull(selectedIssue)) {
CountableMetadata metadata = new CountableMetadata(selectedBlock, Pair.of(selectedIssue.getDate(), selectedIssue.getIssue()));
if (!getAllMetadataTypes().isEmpty()) {
metadata.setMetadataType(getAllMetadataTypes().get(0).getLabel());
metadata.setMetadataDetail(getAllMetadataTypes().get(0));
}
selectedBlock.addMetadata(metadata);
} else {
Expand Down Expand Up @@ -818,11 +826,11 @@ public List<IndividualIssue> getIndividualIssues(Block block) {
*
* @return the map of metadata types
*/
public List<MetadataViewInterface> getAllMetadataTypes() {
public List<ProcessDetail> getAllMetadataTypes() {
if (Objects.isNull(metadataTypes)) {
try {
Process process = ServiceManager.getProcessService().getById(parentId);
metadataTypes = CalendarService.getAddableMetadata(process);
metadataTypes = CalendarService.getAddableMetadataTable(process);
} catch (DAOException | DataException | IOException e) {
Helper.setErrorMessage("Unable to load metadata types: " + e.getMessage());
}
Expand Down Expand Up @@ -863,8 +871,8 @@ public void setLastIssue(CountableMetadata metadata, LocalDate date, Issue issue
* @param issue the issue to calculate the metadata for
* @return the metadata value as java.lang.String
*/
public String getMetadataValue(CountableMetadata metadata, LocalDate date, Issue issue) {
if (Objects.nonNull(metadata)) {
public String getTextMetadataValue(CountableMetadata metadata, LocalDate date, Issue issue) {
if (Objects.nonNull(metadata) && metadata.getMetadataDetail() instanceof ProcessTextMetadata) {
return metadata.getValue(new ImmutablePair<>(date, issue), course.getYearStart());
}
return "";
Expand All @@ -877,7 +885,7 @@ public String getMetadataValue(CountableMetadata metadata, LocalDate date, Issue
* @param block the block to get the metadata for
* @return list of pairs containing the metadata type and the date of its earliest occurrence
*/
public List<Pair<String, LocalDate>> getMetadataSummary(Block block) {
public List<Pair<ProcessDetail, LocalDate>> getMetadataSummary(Block block) {
return CalendarService.getMetadataSummary(block);
}

Expand All @@ -895,4 +903,53 @@ public String getMetadataTranslation(String key) {
return key;
}
}

/**
* Get the value of the given processDetail.
*
* @param processDetail
* as ProcessDetail
* @return the value as a java.lang.String
*/
public String getMetadataValue(ProcessDetail processDetail) {
return ImportService.getProcessDetailValue(processDetail);
}

/**
* Check if process with the same processtitle already exists.
*/
public void checkDuplicatedTitles() throws ProcessGenerationException, DataException, DAOException,
ConfigurationException, IOException, DoctypeMissingException {
if (course.parallelStream().noneMatch(block -> Objects.equals(block.checkIssuesWithSameHeading(), true))) {
Process process = ServiceManager.getProcessService().getById(parentId);
NewspaperProcessesGenerator newspaperProcessesGenerator = new NewspaperProcessesGenerator(process, course);
newspaperProcessesGenerator.initialize();
if (!newspaperProcessesGenerator.isDuplicatedTitles()) {
PrimeFaces.current().executeScript("PF('createProcessesConfirmDialog').show();");
}
}
}

/**
* Get first issue that's appear on the selected Date.
* @return issue
*/
public Issue getFirstMatchIssue() {
if (selectedDate != null) {
return getCalendarSheet().get(selectedDate.getDayOfMonth() - 1).get(selectedDate.getMonthValue() - 1).getIssues()
.parallelStream()
.filter(issue -> issue.isMatch(selectedDate))
.findFirst().orElse(null);
}
return null;
}

/**
* add Metadata to all Issues that's appear on the selected Date.
*/
public void addMetadataToAllMatchIssues() {
if (getFirstMatchIssue() != null) {
addMetadata(getFirstMatchIssue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.Pair;
import org.kitodo.production.helper.Helper;
Expand Down Expand Up @@ -136,7 +137,7 @@ public Issue addIssue() {
* metadata to add
*/
public void addMetadata(CountableMetadata countableMetadata) {
metadata.add(countableMetadata);
metadata.add(0, countableMetadata);
}

/**
Expand Down Expand Up @@ -453,6 +454,24 @@ public void removeIssue(Issue issue) {
issues.remove(issue);
}

/**
* Check if block has issues with same heading.
* @return 'true' if duplicates are found anf 'false' if not.
*/
public boolean checkIssuesWithSameHeading() {
List<String> issuesTitles = issues.stream().map(Issue::getHeading).collect(Collectors.toList());
List<String> titles = new ArrayList<>();
for (String title : issuesTitles) {
if (titles.contains(title)) {
Helper.setErrorMessage("duplicatedTitles", " (Block " + (course.indexOf(this) + 1) + ")" );
return true;
} else {
titles.add(title);
}
}
return false;
}

/**
* Sets a LocalDate as day of first
* appearance for this Block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.kitodo.production.helper.XMLUtils;
import org.kitodo.production.model.bibliography.course.metadata.CountableMetadata;
import org.kitodo.production.model.bibliography.course.metadata.RecoveredMetadata;
import org.kitodo.production.services.data.ImportService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -875,7 +876,7 @@ private void addMetadataToIssue(Document xml, IndividualIssue issue, Element iss
if (metaDatum.matches(metaDatum.getMetadataType(), issueId, false)) {
metadataNode.setAttribute(ATTRIBUTE_VALUE, "");
} else {
metadataNode.setAttribute(ATTRIBUTE_VALUE, metaDatum.getStartValue());
metadataNode.setAttribute(ATTRIBUTE_VALUE, ImportService.getProcessDetailValue(metaDatum.getMetadataDetail()));
if (metaDatum.getStepSize() != null) {
metadataNode.setAttribute(ATTRIBUTE_INCREMENT, metaDatum.getStepSize().toString().toLowerCase());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kitodo.api.Metadata;
import org.kitodo.api.MetadataEntry;
import org.kitodo.exceptions.InvalidMetadataValueException;
import org.kitodo.exceptions.UnreachableCodeException;
import org.kitodo.production.model.bibliography.course.metadata.CountableMetadata;

Expand All @@ -32,6 +38,8 @@
* <em>type</em> of issue.
*/
public class IndividualIssue {
private static final Logger logger = LogManager.getLogger(IndividualIssue.class);

/**
* The constant DAY holds a DateTimeFormatter used to get the a two-digit
* day (01—31) from the newspaper’s date.
Expand Down Expand Up @@ -229,7 +237,7 @@ public Map<String, String> getGenericFields() {
* @return a list of pairs, each consisting of the metadata type name and
* the value
*/
public Iterable<MetadataEntry> getMetadata(int monthOfYear, int dayOfMonth) {
public Iterable<Metadata> getMetadata(int monthOfYear, int dayOfMonth) {
return getMetadata(MonthDay.of(monthOfYear, dayOfMonth));
}

Expand All @@ -242,15 +250,25 @@ public Iterable<MetadataEntry> getMetadata(int monthOfYear, int dayOfMonth) {
* @return a list of pairs, each consisting of the metadata type name and
* the value
*/
public Iterable<MetadataEntry> getMetadata(MonthDay yearStart) {
List<MetadataEntry> result = new ArrayList<>();
public Iterable<Metadata> getMetadata(MonthDay yearStart) {
List<Metadata> result = new ArrayList<>();
Pair<LocalDate, Issue> selectedIssue = Pair.of(date, issue);
for (CountableMetadata counter : block.getMetadata(selectedIssue, null)) {
String value = counter.getValue(selectedIssue, yearStart);
MetadataEntry entry = new MetadataEntry();
entry.setKey(counter.getMetadataType());
entry.setValue(value);
result.add(entry);
Collection<Metadata> metadata = Collections.emptyList();
try {
metadata = counter.getMetadataDetail().getMetadata();
} catch (InvalidMetadataValueException e) {
logger.error(e.getLocalizedMessage());
}
if (counter.getMetadataDetail().getInput().equals("inputText")
|| counter.getMetadataDetail().getInput().equals("inputTextarea")) {
String value = counter.getValue(selectedIssue, yearStart);
if (metadata.stream().findFirst().isPresent()
&& metadata.stream().findFirst().get() instanceof MetadataEntry) {
((MetadataEntry) metadata.stream().findFirst().get()).setValue(value);
}
}
result.addAll(metadata);
}
return result;
}
Expand Down
Loading