Create a view window

Hi,

Instead of a standard Edit screen I want to create a view screen.

What I did so far is I created my entity and a standard entity browser UI screen. This is good so far.
After that I created an Entity Frame, so an empty frame where I will add my components as I needed, etc.
I edited the entity browser screen’s table list’s edit action and renamed it to View, and also added a OnView to the invoke. So I thought I go into the controller and add the code for opening.

And this is where I am at the moment. Ideally I would like to pass the selected entity from the table to my opened Entity Frame so I can freely display some data from the selected entity.

May I ask how to solve this? I tried the openWindow command but no luck to properly call this method.

Hi,

In order to open a frame, you need to use the openFrame method.

Also, I would recommend taking a look at the Entity combined screen (aka Master-Detail screen) which may fit your needs. Below an example of such a screen:

image
image

Regards,
Gleb

Hi,

Thanks, I have solved this a bit different way and now need one more help.

So my main window is an AbstractWindow. In this when I double click on the GroupTable it opens up my custom window to view some data, like this:

Before the open I also update the item:

MyEntity me = (MyEntity) myEntityTable.getSingleSelected();
me.setHandled(2);
CommitContext commitContext = new CommitContext(me);
dataManager.commit(commitContext);
  
openWindow("testapp$ViewFrame", WindowManager.OpenType.DIALOG,ParamsMap.of("myentity", me));

After that my window opens and displays all the data I needed. It displays also a checkbox. What I want is when the checkbox value changed, I need to update the entity in the DB.

I do it like this in my new testapp$ViewFrame window’s init:

checkBoxHandle.addValueChangeListener(e -> {
	if (Boolean.TRUE.equals(e.getValue())) {
		myentity.setHandled(3);
	} else {
		myentity.setHandled(2);
	}
	CommitContext commitContext = new CommitContext(myentity);
	dataManager.commit(commitContext);
});

But the problem is after the commit it says “Object MyEntity was modified in another transaction”.

So I believe I will have to reload it somehow back to the original list window.

May I ask how to do that?

Thanks

Hi,

In general situation, you can reload an entity using the DataManager.reload() method, but it seems that you don’t need it. According to the exception you get, I assume that you try to commit a nested entity and if I right, you simply need to update your entity attribute values and replace it in a datasource using the CollectionDatasource.updateItem() method.

Regards,
Gleb

Hi Gleb,

I tried to change my code a little bit:

In my popup AbstractWindow’s init I have changed the checkbox listener a bit:

checkBoxHandle.addValueChangeListener(e -> {
	if (Boolean.TRUE.equals(e.getValue())) {
		myentity.setHandled(3);
	} else {
		myentity.setHandled(2);
	}
	CommitContext commitContext = new CommitContext(myentity);
	dataManager.commit(commitContext);
    dataManager.reload(myentity, "testapp$OriginalParentWindow.browse");
});

So I am not sure if I got this right. The error is still there and here is the exception from the logs:

10:32:00.363 ERROR c.h.cuba.core.sys.ServiceInterceptor    - Exception:
javax.persistence.OptimisticLockException: Exception [EclipseLink-5010] (Eclipse Persistence Services - 2.6.2.cuba24): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [com.myapp.testapp.entity.MyEntity-b256f7ab-0532-44ef-b894-2d6ad1f54739 [detached]] cannot be merged because it has changed or been deleted since it was last read.
Class> com.myapp.testapp.entity.MyEntity
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:555) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:530) ~[eclipselink-2.6.2.cuba24.jar:2.6.2.cuba24]
        at com.haulmont.cuba.core.sys.EntityManagerImpl.internalMerge(EntityManagerImpl.java:434) ~[cuba-core-6.10.6.jar:6.10.6]

So basically what I want here is to be able to click on this checkbox in the popup window and change it’s value in the DB. As I mentioned this entity is coming from the parent window’s grouptable.

Due to case complexity, we will be able to help you if you send us a small sample project along with reproduction scenario that demonstrates the issue.

Regards,
Gleb

Here is a sample project I created:
https://drive.google.com/open?id=1DrdWqEquZkb2YpXkjLgV4SFTwaHGeQpq

When you run it it will add one record with the init db.

So basically just double click the sample record in the Application/My Entities screen.

A view window will appear. And just try to click the Active checkbox. Every time you click it should update the active field in the DB. Also when you close the window the list should be updated. But usually I got that error message that I mentioned.

Hi,

Thank you for the demo project. I can suggest the foolowing changes:

  1. dataManager.commit returns committed entity and you have to use that entity
  2. Update an entity in a datasource after a manual commit
  3. Pass the committed entity to the screen
  4. Refresh a datasource after the screen is closed
@Inject
private DataManager dataManager;

@Inject
private GroupTable<?> myEntitiesTable;

@Inject
private GroupDatasource<MyEntity, UUID> myEntitiesDs;

public void onView() {
    MyEntity me = (MyEntity) myEntitiesTable.getSingleSelected();
    me.setActive(true);
    // Update the entity reference after commit
    MyEntity committed = dataManager.commit(me);
    myEntitiesDs.updateItem(committed);

    AbstractWindow abstractWindow = openWindow("viewscreen", WindowManager.OpenType.DIALOG, ParamsMap.of("myentity", committed));
    abstractWindow.addCloseListener(actionId -> {
        myEntitiesDs.refresh();
    });
}

And the same for the Viewscreen controller

activecheckbox.addValueChangeListener(e -> {
    if (Boolean.TRUE.equals(e.getValue())) {
        myentity.setActive(true);
    } else {
        myentity.setActive(false);
    }
    // Update the entity reference after commit
    myentity = dataManager.commit(myentity);
    
});

Regards,
Gleb

Many thanks, it seems it is working!

Hi, sorry to dig up this old thread! I am facing the same conceptual problem, but using platform 7.1.2.

I looked at the documentation and messed around, but could not find the equivalent of CollectionDatasource.updateItem().

(What I want to do is just refresh the master table in a master-detail screen after editing the detail item).
Thanks!