Commit record, change it via service, load it back to page

Hello again. I wonder if I could get a little help with a process I’m trying to right.

I have a record that I want to commit when the user presses a button; after committing it, a process then creates another record and attaches it to via an association. Then I want to pick up the record from the database and put it back on the screen.

Here’s how I tried to do it:

private void kickOffProcess() {

    // First of all, we commit any changes that have been made.
    // They may have changed the record and not saved before hitting
    // the start process button.
    manuscriptDs.commit();
    Manuscript manuscript = manuscriptDs.getItem();
    ProcessTemplate processTemplate = manuscript.getProcessTemplate();

    //Attach the new record to the manuscript
    processService.copyProcessTemplateToManuscript(processTemplate, manuscript);

    // Now refresh the data source from the database to pick up the changes.
    Manuscript refreshedManuscript = manuscriptService.getManuscriptById(manuscript.getId());
    // At this point the refreshed record is correct

    ManuscriptProcess manuscriptProcess = refreshedManuscript.getManuscriptProcess();

    // Now get the first stage
    Optional<ManuscriptStage> initialStage = manuscriptProcess.getManuscriptStages().stream()
            .filter(manuscriptStage -> manuscriptStage.getSequenceOrder() == 1)
            .findFirst();

    if (initialStage.isPresent()) {
        refreshedManuscript.setCurrentManuscriptStage(initialStage.get());
        manuscriptDs.setItem(refreshedManuscript); // <-- This is not right
    }

}

When I try to set the manuscriptDs data source to the new manuscript, the fields I set before committing the record seem to have been reset back to null. My guess is that the setItem function doesn’t do what I think it does, and that there is a proper way to refresh the record on the screen.

EDIT:

Okay, I’ve been playing around with it a little bit more, and the problem is that I don’t understand what happens when I commit a data source. Looking at the debugger output:

Execute manuscriptDs.getItem() --> The fields on the screen are set correctly
Execute manuscriptDs.commit()
Execute manuscriptDs.getItem() --> Now the fields are blank. Why? They have been written to the database, but they have been erased from the data source.

Okay, I think I have the answer. As far as I can tell (and I don’t know why) committing the datasource seems to put it back into the same state as it was before the record was changed on screen. But since the correct data is written to the database, all I have to do is refresh straight after the commit to read the correct info back from the database.

private void kickOffProcess() {

    commit();
    manuscriptDs.refresh();
    Manuscript manuscript = manuscriptDs.getItem();
    ProcessTemplate processTemplate = manuscript.getProcessTemplate();
    processService.copyProcessTemplateToManuscript(processTemplate, manuscript);
    manuscriptDs.refresh();
}

But no idea why the datasource is left in that state after the commit.

In fact, after calling datasource.commit(), the datasource contains another entity instance which is a result of merging changes into the record contained in the database. The returned instance is fetched according to the view specified for the datasource, so it should contain all required attributes.

I think there is now a defect in the single-value datasource implementation of commit method: it doesn’t add property listeners to the returned instance (as AbstractCollectionDatasource does by calling committed() method). Not sure it is related to your situation, but we’ll try to fix it of course.