[MGNLUI-2509] JcrNewNodeAdapter should be reusable after first call to applyChanges() Created: 09/Dec/13  Updated: 13/Aug/19  Resolved: 13/Aug/19

Status: Closed
Project: Magnolia UI
Component/s: None
Affects Version/s: 5.2
Fix Version/s: 5.2.x

Type: Bug Priority: Major
Reporter: Federico Grilli Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: integration, maintenance, vaadin
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
causality
is causing MGNLGROOVY-102 When a compilation error occurs while... Closed
relation
is related to MGNLUI-2488 Security App: Cannot create role with... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled

 Description   

Suppose you're creating a new node and doing some additional validation in an action, besides that done by the validator proper.
If such additional validation fails for some reason, you are presented again with the form to fix the data you entered before. Now data are correct, you hit the submit button and no validation error will occur.
However, the action will fail again, this time with NPE. What happened is that each time you call JcrNewNodeAdapter.applyChanges() to get an actual jcr Node in synch with the latest status of the adapter. The first time that jcr Node is created on the fly and is a transient one as the session it is contained in hasn't been saved yet (it will be saved once all validation passes). The second time the jcr node will be no longer created on the fly, rather fetched from the current JCR session as if it were no longer a new node rather an existing one, but since the session wasn't saved due to the validation errors, subsequent calls to applyChanges() will return null.
We should take into account that a new node adapter isn't always saved on first attempt, therefore we must be able to reuse the adapter until it is either valid (hence saved to a jcr repository) or discarded. What we could do in info.magnolia.ui.vaadin.integration.jcr.JcrNewNodeAdapter.applyChanges() is keeping on creating the node on the fly as long as needed

 // If changes were already applied, behave like a JcrNodeAdapter
        if (appliedChanges) {
            Node node = super.applyChanges();
            if (node != null) {
                isNew = false;
                return node;
            }
            // else validation errors probably occurred, so we need to recreate the new node on the fly
        }

Generated at Mon Feb 12 08:57:21 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.