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!