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

Prevent NPE when creating unknown metadata group and metadata #5603

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

solth
Copy link
Member

@solth solth commented Mar 27, 2023

This PR fixes #5217 by preventing the NullPointerException that is currently being displayed to the user when the import mapping xslt file creates a metadata group that is undefined in the used ruleset.

@BartChris
Copy link
Collaborator

BartChris commented Mar 27, 2023

This might be about a special scenario but nevertheless: I tried the changes with the scenario described in the linked issue (importing an entry from K10 plus without having all keys defined in the ruleset). The first NPE does not happen anymore. I still get an NPE in this specific scenario because "Person" was not defined in my ruleset and the construction of the ATS does fail, since it identifies a Person key in the data, but not the other values which are required here.

java.lang.NullPointerException
at org.kitodo.production.forms.createprocess.ProcessTextMetadata.getMetadataID(ProcessTextMetadata.java:62)
at org.kitodo.production.services.data.ImportService.getListOfCreators(ImportService.java:827)

Edit: This seem to stem from the specific functionality attached to keys named as "Person", it does not happen if if define a "Person" key in the ruleset, even with different subkeys than the one coming from K10-plus. I have not tested it yet with other datasets yet where the ATS related NPE does not happen.

Copy link
Collaborator

@matthias-ronge matthias-ronge left a comment

Choose a reason for hiding this comment

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

Seems neither wrong nor right, see comments.

@@ -43,7 +43,7 @@ public ProcessTextMetadata(ProcessTextMetadata template) {
}

private String addLeadingZeros(String value) {
if (Objects.equals(super.settings.getInputType(), InputType.INTEGER)) {
if (Objects.nonNull(super.settings) && InputType.INTEGER.equals(super.settings.getInputType())) {
Copy link
Collaborator

@matthias-ronge matthias-ronge Apr 6, 2023

Choose a reason for hiding this comment

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

I would like better:

Objects.nonNull(super.settings) && Objects.equals(super.settings.getInputType(), InputType.INTEGER)

Not a requirement, but I think it is better readable. And, Objects.equals() does null-checks.

Copy link
Member Author

@solth solth Aug 1, 2023

Choose a reason for hiding this comment

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

In this particular case I disagree. The additional Objects.equals is not required and unnecessarily complicates this check, since InputType.INTEGER is not null and therefor we do not need a null check here (calling the equals method on constants or enums when comparing with variables is an encouraged way for avoiding null pointer exceptions). So if you do not mind I would leave this part unchanged.

keyView instanceof SimpleMetadataViewInterface
? ((SimpleMetadataViewInterface) keyView).getInputType().toString()
: "dataTable"));
if (Objects.nonNull(metadataView)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wondering why metadataView can be null. This looks like a bug elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think you are correct. This pull request is not meant to fix the underlying problem of the metadata handling but is just intended to better deal with the resulting null pointer exception.

@solth solth added the bug label Jul 8, 2023
@solth solth force-pushed the unknown-metadata-group-npe branch from 0afcdba to 1f7b068 Compare August 1, 2023 12:50
@solth
Copy link
Member Author

solth commented Aug 1, 2023

Edit: This seem to stem from the specific functionality attached to keys named as "Person", it does not happen if if define a "Person" key in the ruleset, even with different subkeys than the one coming from K10-plus. I have not tested it yet with other datasets yet where the ATS related NPE does not happen.

@BartChris thanks for this clarification. I think "Person" is a special case and separate from the issue I tried to fix with this pull request.

Do you think the linked issue is sufficiently resolved with these proposed changes or are there other suggestions or change requests you would like to see? (e.g. as @matthias-ronge mentioned there seem to be other issues in the metadata handling which are not addressed by this pull request. It is merely meant to handle the exception that can occur when ruleset and xml transformation script do not match a little more gracefully. It does not fix the underlying problem.)

Copy link
Collaborator

@markusweigelt markusweigelt left a comment

Choose a reason for hiding this comment

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

Does not work for me because I still get an NPE.

What have I done?

  • Created a process with metadata group
  • Check if the group is available in metadata editor
  • Change the group name in meta.xml on filesystem
  • Open the metadata editor again -> NPE
java.lang.NullPointerException
	at org.kitodo.production.forms.createprocess.ProcessSimpleMetadata.isRequired(ProcessSimpleMetadata.java:96)
	at org.kitodo.production.forms.createprocess.ProcessTextMetadata.isRequired(ProcessTextMetadata.java:30)
...	

@solth
Copy link
Member Author

solth commented Aug 3, 2023

Does not work for me because I still get an NPE.

What have I done?

* Created a process with metadata group

* Check if the group is available in metadata editor

* Change the group name in `meta.xml` on filesystem

* Open the metadata editor again -> NPE
java.lang.NullPointerException
	at org.kitodo.production.forms.createprocess.ProcessSimpleMetadata.isRequired(ProcessSimpleMetadata.java:96)
	at org.kitodo.production.forms.createprocess.ProcessTextMetadata.isRequired(ProcessTextMetadata.java:30)
...	

@markusweigelt thanks for testing the changes. Yes, I think the same problem exists in the metadata editor. I just tried to handle the issue described in the linked issue, which occurs during import, though. Since the whole topic is quite complex I would suggest to handle the problem in the metadata editor separately, if you think that's ok.

@markusweigelt
Copy link
Collaborator

Ok I have read over that. I will check this only for the import again.

@markusweigelt
Copy link
Collaborator

markusweigelt commented Aug 3, 2023

Not existing top level fields working

<kitodo:metadata name="notExistingField"><xsl:text>Lorem ipsum dolor sit amet</xsl:text></kitodo:metadata>

but I am not able to import without NPE with non-existent top-level groups.

<kitodo:metadataGroup name="notExistingGroup">
     <kitodo:metadata name="notExistingField"><xsl:text>Lorem ipsum dolor sit amet</xsl:text></kitodo:metadata>
</kitodo:metadataGroup>

or

     <kitodo:metadata name="TitleDocMain"><xsl:text>Lorem ipsum dolor sit amet</xsl:text></kitodo:metadata>
</kitodo:metadataGroup>

producing following NPE

java.lang.NullPointerException
	at org.kitodo.production.forms.createprocess.ProcessTextMetadata.addLeadingZeros(ProcessTextMetadata.java:46)
	at org.kitodo.production.forms.createprocess.ProcessTextMetadata.<init>(ProcessTextMetadata.java:37)
	at org.kitodo.production.forms.createprocess.ProcessFieldedMetadata.createMetadataEntryEdit(ProcessFieldedMetadata.java:375)
	at org.kitodo.production.forms.createprocess.ProcessFieldedMetadata.createUndefinedMetadataTable(ProcessFieldedMetadata.java:258)
	at org.kitodo.production.forms.createprocess.ProcessFieldedMetadata.createMetadataEntryEdit(ProcessFieldedMetadata.java:385)
	at org.kitodo.production.forms.createprocess.ProcessFieldedMetadata.createMetadataTable(ProcessFieldedMetadata.java:233)
	at org.kitodo.production.forms.createprocess.ProcessFieldedMetadata.buildTreeNodeAndCreateMetadataTable(ProcessFieldedMetadata.java:156)
	at org.kitodo.production.forms.createprocess.ProcessFieldedMetadata.<init>(ProcessFieldedMetadata.java:134)
	at org.kitodo.production.helper.ProcessHelper.initializeProcessDetails(ProcessHelper.java:102)
	at org.kitodo.production.helper.ProcessHelper.transformToProcessDetails(ProcessHelper.java:74)
	at org.kitodo.production.helper.ProcessHelper.generateAtstslFields(ProcessHelper.java:161)
	at org.kitodo.production.services.data.ImportService.importProcessHierarchy(ImportService.java:528)
	at org.kitodo.production.forms.createprocess.CatalogImportDialog.getRecordHierarchy(CatalogImportDialog.java:181)
	at org.kitodo.production.forms.createprocess.CatalogImportDialog.getRecordById(CatalogImportDialog.java:247)
	at org.kitodo.production.forms.createprocess.CatalogImportDialog.search(CatalogImportDialog.java:108)

@markusweigelt
Copy link
Collaborator

We could also provide a general error message here, indicating that most likely the Kitodo XML generated after the XSL transformation is faulty. The errors I have noticed include, for example, empty groups when fields imported from K10Plus are empty, and there is a condition for checking them in XSL but no condition for the group.

@solth
Copy link
Member Author

solth commented Mar 4, 2024

We could also provide a general error message here, indicating that most likely the Kitodo XML generated after the XSL transformation is faulty. The errors I have noticed include, for example, empty groups when fields imported from K10Plus are empty, and there is a condition for checking them in XSL but no condition for the group.

That would be the optimal solution, in my opinion - show a warning dialog that something went wrong with the import, likeley related to the ruleset and the mapping file not being compatible, and that additional information can be found in the logs, where the exact error message (and perhaps stack trace) should be logged.

@solth solth force-pushed the unknown-metadata-group-npe branch from 1f7b068 to a56d6fc Compare March 4, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NPE during import when import mapping and ruleset do not match
4 participants