Prevent NPE when creating unknown metadata group and metadata
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.
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.
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.)
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 -> NPEjava.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.
Ok I have read over that. I will check this only for the import again.
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)
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.
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.