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

[3.8] Fix NPE when importing newspaper course with metadata #6393

Open
wants to merge 2 commits into
base: 3.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions Kitodo/src/main/java/org/kitodo/production/forms/CalendarForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
Expand All @@ -48,6 +50,7 @@
import org.kitodo.exceptions.DoctypeMissingException;
import org.kitodo.exceptions.ProcessGenerationException;
import org.kitodo.production.forms.createprocess.ProcessDetail;
import org.kitodo.production.forms.createprocess.ProcessSimpleMetadata;
import org.kitodo.production.forms.createprocess.ProcessTextMetadata;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.helper.XMLUtils;
Expand Down Expand Up @@ -614,12 +617,17 @@ public void upload() {
return;
}
Document xml = XMLUtils.load(uploadedFile.getInputStream());
course = new Course(xml);
List<ProcessDetail> processDetails = CalendarService
.getAddableMetadataTable(ServiceManager.getProcessService().getById(parentId));
Map<String, ProcessSimpleMetadata> processDetailsByMetadataID = processDetails.stream()
.filter(ProcessSimpleMetadata.class::isInstance).map(ProcessSimpleMetadata.class::cast)
.collect(Collectors.toMap(ProcessDetail::getMetadataID, Function.identity()));
course = new Course(xml, processDetailsByMetadataID);
Helper.removeManagedBean("GranularityForm");
navigate(course.get(0));
} catch (SAXException e) {
Helper.setErrorMessage(UPLOAD_ERROR, "errorSAXException", logger, e);
} catch (IOException e) {
} catch (IOException | DataException | DAOException e) {
Helper.setErrorMessage(UPLOAD_ERROR, e.getLocalizedMessage(), logger, e);
} catch (IllegalArgumentException e) {
Helper.setErrorMessage("calendar.upload.overlappingDateRanges", logger, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private ProcessBooleanMetadata(ProcessBooleanMetadata template) {
}

@Override
ProcessBooleanMetadata getClone() {
public ProcessBooleanMetadata getClone() {
return new ProcessBooleanMetadata(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private ProcessDateMetadata(ProcessDateMetadata template) {
}

@Override
ProcessTextMetadata getClone() {
public ProcessTextMetadata getClone() {
return new ProcessDateMetadata(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private ProcessSelectMetadata(ProcessSelectMetadata template) {
}

@Override
ProcessSelectMetadata getClone() {
public ProcessSelectMetadata getClone() {
return new ProcessSelectMetadata(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.kitodo.api.dataformat.LogicalDivision;
import org.kitodo.api.dataformat.PhysicalDivision;

abstract class ProcessSimpleMetadata extends ProcessDetail implements Serializable {
public abstract class ProcessSimpleMetadata extends ProcessDetail implements Serializable {

static final List<Class<? extends Division<?>>> PARENT_CLASSES = Arrays.asList(LogicalDivision.class,
PhysicalDivision.class);
Expand All @@ -50,7 +50,7 @@ protected ProcessSimpleMetadata(ProcessFieldedMetadata container, SimpleMetadata
*
* @return an independently mutable copy
*/
abstract ProcessSimpleMetadata getClone();
public abstract ProcessSimpleMetadata getClone();

/**
* Returns a simpler string representation of the Metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private String addLeadingZeros(String value) {
}

@Override
ProcessTextMetadata getClone() {
public ProcessTextMetadata getClone() {
return new ProcessTextMetadata(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.kitodo.exceptions.MetadataException;
import org.kitodo.production.forms.createprocess.ProcessSimpleMetadata;
import org.kitodo.production.helper.XMLUtils;
import org.kitodo.production.model.bibliography.course.metadata.CountableMetadata;
import org.kitodo.production.model.bibliography.course.metadata.RecoveredMetadata;
Expand Down Expand Up @@ -279,14 +281,16 @@ public Course() {
*
* @param xml
* XML document data structure
* @param possibleProcessDetails
* possible process details from the process creation form
* @throws NoSuchElementException
* if ELEMENT_COURSE or ELEMENT_PROCESSES cannot be found
* @throws IllegalArgumentException
* if the dates of two blocks do overlap
* @throws NullPointerException
* if a mandatory element is absent
*/
public Course(Document xml) {
public Course(Document xml, Map<String, ProcessSimpleMetadata> possibleProcessDetails) {
super();
processesAreVolatile = false;
Element rootNode = XMLUtils.getFirstChildWithTagName(xml, ELEMENT_COURSE);
Expand All @@ -308,7 +312,7 @@ public Course(Document xml) {
}
initialCapacity = processProcessNode(initialCapacity, lastIssueForDate, recoveredMetadata, processNode);
}
processRecoveredMetadata(recoveredMetadata);
processRecoveredMetadata(recoveredMetadata, possibleProcessDetails);
recalculateRegularityOfIssues();
processesAreVolatile = true;
}
Expand Down Expand Up @@ -362,7 +366,8 @@ private void processBlockNode(Map<LocalDate, IndividualIssue> lastIssueForDate,
}
}

private void processRecoveredMetadata(List<RecoveredMetadata> recoveredMetadata) {
private void processRecoveredMetadata(List<RecoveredMetadata> recoveredMetadata,
Map<String, ProcessSimpleMetadata> possibleProcessDetails) {
Map<Pair<Block, String>, CountableMetadata> last = new HashMap<>();
for (RecoveredMetadata metaDatum : recoveredMetadata) {
Block foundBlock = null;
Expand All @@ -382,6 +387,12 @@ private void processRecoveredMetadata(List<RecoveredMetadata> recoveredMetadata)
}
CountableMetadata metadata = new CountableMetadata(foundBlock, Pair.of(metaDatum.getDate(), foundIssue));
metadata.setMetadataType(metaDatum.getMetadataType());
ProcessSimpleMetadata processSimpleMetadata = possibleProcessDetails.get(metaDatum.getMetadataType());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential null pointer exception if possibleProcessDetails ist set to null like in the test execution (see CourseTest.java line 34). The test code execution did not cover this peace of code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field is not nullable when a course is loaded with metadata, and it is the result of a stream collect operation, which can return empty, but never returns null. In the test situation, it works with null because there is no known in the test course. But, I will input emptySet() in the test for improvement, it is definitely cleaner.

if (Objects.isNull(processSimpleMetadata)) {
throw new MetadataException("Cannot add metadata key " + metaDatum.getMetadataType(),
new NoSuchElementException(metaDatum.getMetadataType()));
}
metadata.setMetadataDetail(processSimpleMetadata.getClone());
metadata.setStartValue(metaDatum.getValue());
metadata.setStepSize(metaDatum.getStepSize());
foundBlock.addMetadata(metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CourseTest {
public void testCloneMethod() throws IOException, ParserConfigurationException, SAXException {
String xmlString = new String(Files.readAllBytes(new File("src/test/resources/newspaper-course.xml").toPath()));
Document xmlDocument = XMLUtils.parseXMLString(xmlString);
Course course = new Course(xmlDocument);
Course course = new Course(xmlDocument, null);

// assert / check some data from the xml file
assertEquals(23L, course.getIndividualIssues().size());
Expand Down