Hello CUBA team,
I have extended the User entity and edit screen in a CUBA project to add a few instance fields, which has been working as expected. One thing I now need to do is detect when a UserRole collection has changed within the extended legacy User edit screen so I can take some further action. The problem I’m running into is that the dataSource for the UserRole collection doesn’t seem to be available to my extended screen. This screen was extended some time ago, so maybe it wasn’t done correctly.
ext-user-edit.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
xmlns:ext="http://schemas.haulmont.com/cuba/window-ext.xsd"
class="com.coral.registry.web.screens.user.ExtUserEditor"
extends="com/haulmont/cuba/gui/app/security/user/edit/user-edit.xml"
messagesPack="com.coral.registry.web.screens.user">
<dsContext>
<datasource id="userDs" view="extUser-view"/>
<collectionDatasource id="organizationsDs" class="com.coral.registry.entity.Organization" view="_base">
<query>
<![CDATA[select e from registry_Organization e order by e.name]]>
</query>
</collectionDatasource>
</dsContext>
<layout>
<groupBox>
<grid>
<columns>
<column id="fieldGroupLeftColumn"/>
<column id="fieldGroupRightColumn" flex="1"/>
<column id="fieldGroupExtColumn" flex="3"/>
</columns>
<rows>
<row>
<fieldGroup id="fieldGroupExt" datasource="userDs" width="AUTO">
<column width="250px">
<field id="requestedRole" property="requestedRole" editable="false"/>
<field id="phoneNumber" property="phoneNumber"/>
<field id="organization">
<lookupPickerField id="organizationField" datasource="userDs"
property="organization" optionsDatasource="organizationsDs"
required="true"/>
</field>
<field id="reportedOrgField" property="reportedOrg" caption="Reported Org"
editable="false"/>
<field id="addField" width="100%">
<button id="addBtn" caption="Add Organization" width="AUTO"
align="MIDDLE_RIGHT"/>
</field>
</column>
</fieldGroup>
</row>
</rows>
</grid>
</groupBox>
</layout>
</window>
ExtUserEditor.java
@Entity(name = "registry_ExtUser")
public class ExtUserEditor extends UserEditor {
@Inject
private ScreenBuilders screenBuilders;
@Inject
private OrganizationService organizationService;
@Inject
private Button addBtn;
@Subscribe
public void onBeforeShow(BeforeShowEvent event) {
ExtUser editedEntity = (ExtUser) getEditedEntity();
if (editedEntity.getOrganization() != null) {
addBtn.setVisible(false);
}
}
@Subscribe("organizationField")
public void onOrganizationFieldValueChange(HasValue.ValueChangeEvent<Organization> event) {
if (event.isUserOriginated()) {
addBtn.setVisible(event.getValue() == null);
}
}
@Subscribe("addBtn")
public void onAddBtnClick(Button.ClickEvent event) {
ExtUser editedEntity = (ExtUser) getEditedEntity();
String reportedOrg = editedEntity.getReportedOrg();
String email = editedEntity.getEmail();
MapScreenOptions options = new MapScreenOptions(ParamsMap.of("emailDomain",
email != null ? organizationService.getDomainFromEmail(email) : ""));
screenBuilders.editor(Organization.class, this)
.withScreenClass(OrganizationEdit.class)
.newEntity()
.withInitializer(organization -> {
if (reportedOrg != null) {
organization.setName(reportedOrg);
}
})
.withOptions(options)
.withAfterCloseListener(organizationEditAfterScreenCloseEvent -> {
if (organizationEditAfterScreenCloseEvent.closedWith(StandardOutcome.COMMIT)) {
Organization organization = organizationEditAfterScreenCloseEvent.getSource().getEditedEntity();
((ExtUser) getEditedEntity()).setOrganization(organization);
}
})
.build()
.show();
}
}
So even though the screen (descriptor and controller) are extended and function as expected, I can’t seem to find a way to inject the DataSource from the parent screen.
Do I maybe need to recreate the descriptor/controller in full and replace the “parent” screen instead of extending it? Or is there another way to detect changes directly in the UserRole collection?
Thanks!
Adam