Unable to reload editor / saving newly created object

Hi,

I want to open the editor, and when it opens have a new version of the object created if the current status is ‘approved’. In the code below (from the editor of the object) this particular sequence doesn’t work the way I expected.


public class PageEdit extends AbstractEditor<Page> {
    @Inject
    private Datasource<Page> pageDs;

    @Inject
    private Metadata metadata;

    @Inject
    private DataManager dm;

    private Page newPage;
    private boolean reloadPage = false;

    @Override
    public void init(Map<String, Object> params) {
        Page p = (Page) WindowParams.ITEM.getEntity(params);
        showNotification("Versie: " + p.getPageVersion() + " - Status: " + p.getPageStatus() + "<br>Id: " + p.getId() +
                " - Huidig? " + p.getPageCurrentVersion(), NotificationType.HUMANIZED_HTML);
        switch (p.getPageStatus()) {
            case Concept:
                // default edit mode
                break;

            case InReview:
                // return to concept
                p.setPageStatus(PageStatus.Concept);
                break;

            case Approved:
                // check if this is the latest version
                if (p.getPageCurrentVersion()) {
                    // find newer version
                    newPage = dm.load(LoadContext.create(Page.class).setQuery(
                            LoadContext.createQuery("select p from base$Page p where p.pageTag = :Tag and p.pageVersion = :Version")
                                    .setParameter("Tag", p.getPageTag())
                                    .setParameter("Version", (p.getVersion() + 1))));
                    // check if it exists
                    if (newPage == null) {
                        // no, then start a new version
                        newPage = createNewVersion(p);
                        showNotification("Nieuwe versie aangemaakt!", NotificationType.TRAY);
                    } else {
                        newPage = dm.reload(newPage, "page-view");
                    }
                    // new page editor will be reloaded later on
                    reloadPage = true;
                    //pageDs.setItem(newPage);
                }
                else {
                    showNotification("Dit is niet de laatste versie - deze mag niet gewijzigd worden!", NotificationType.ERROR);
                }
                break;

            case Disapproved:
                // show approval comments (hidden by default)
                break;

            default:
                showNotification("Pagina heeft geen status, reset naar nieuw", NotificationType.HUMANIZED);
                p.setPageStatus(PageStatus.New);
            case New:
                // initialise fields as best as we can
                // Check version to be 1 and current version to be true?
                break;

        }
    }

    @Override
    protected void postInit() {
        // Load new item on post init
        if (reloadPage) {
            pageDs.setItem(newPage);
        }
    }

    private Page createNewVersion(Page p) {
        // Make a copy and take over default fields
        Page newPage = metadata.create(Page.class);
        newPage.setPageTitle(p.getPageTitle()+ " NIEUW");
        newPage.setPageContent(p.getPageContent());
        newPage.setPageTag(p.getPageTag());
        newPage.setPageOwner(p.getPageOwner());

        // Create as concept and mark inactive
        newPage.setPageStatus(PageStatus.Concept);
        newPage.setPageCurrentVersion(false);
        // Set a version
        newPage.setPageVersion(p.getPageVersion()+1);

        // Prepare for storing the new page
        CommitContext cc = new CommitContext().addInstanceToCommit(newPage);
        // Copy all images
        for (Image img : p.getPageImages()) {
            Image newImage = metadata.create(Image.class);
            newImage.setImageFile(img.getImageFile());
            newImage.setImageTag(img.getImageTag());
            newImage.setImagePage(newPage);
            cc.addInstanceToCommit(newImage);
        }
        // And commit all in one go
        //dm.load(cc);
        dm.commit(cc);
        return newPage;
    }

What actually happens is the following:

  • when opening the editor for the ‘approved’ version and assuming there is not a newer version created already, a copy is made from the current object, set with the appropriate status information and then the editor is reloaded (using postInit()) with this new object
  • when saving changes from the editor, I get this error: "SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; SYS_PK_10337 table: BASE_PAGE"
    I am totally lost why this error appears.

As a smoking gun, it appears that if there already exists a newer (unapproved) version, the query to find that object before creating a new one doesn’t seem to return it. At all times the query does not return a page object.

Obviously, the database is ‘clean’, meaning there are no other records interfering with the scenario.

Is anyone able to explain what is happening here and why the error appears (as well as why the query doesn’t return the existing object)?
Any help appreciated!

Hmmm, I have partly rewritten the clause for Approved to this piece of code:


                // check if this is the latest version
                if (p.getPageCurrentVersion()) {
                    newPage = dm.load(LoadContext.create(Page.class).setQuery(
                            LoadContext.createQuery("select p from base$Page p where p.pageTag = '" + p.getPageTag() +
                                    "' and p.pageVersion = " + (p.getVersion() + 1))));
                    // check if it exists
                    if (newPage == null) {
                        // no, then start a new version
                        newPage = createNewVersion(p);
                        showNotification("Nieuwe versie aangemaakt!", NotificationType.TRAY);
                    }
                    newPage = dm.reload(newPage, "page-view");

                    // new page editor will be reloaded later on
                    reloadPage = true;

Now everything works as expected; it can be saved and the query succeeds in finding any concept version available.

I still think I am doing this in the wrong way but it seems to be working - and that’s progress to me :wink:

Aahhhh : just found out that getVersion() is a method available on all objects. I am using getPageVersion(), except for the query in which I am using getVersion accidentally! That explains the strange behaviour I was having.

My previous change that seemed to work was more or less being lucky; it went wrong after going through a number of scenario’s.

Now everything works fine. Sorry for mentioning this issue on the forum.