Uploaded image for project: 'Magnolia UI'
  1. Magnolia UI
  2. MGNLUI-2509

JcrNewNodeAdapter should be reusable after first call to applyChanges()

XMLWordPrintable

      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
              }
      

        Acceptance criteria

              Unassigned Unassigned
              fgrilli Federico Grilli
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: