Opening screen to edit User entity

I’m sure this is simple, but I am having trouble opening and populating a screen to edit the User entity. What is the proper way to have this screen open from the main menu and populate and then save the User object? I have a screen I called Profile Edit that looks like this right now:

ProfileEdit.java:

@UiController("ws_Profile.edit")
@UiDescriptor("profile-edit.xml")
public class ProfileEdit extends Screen {

    @Inject
    private InstanceLoader<User> profileDl;

    @Inject
    private UserSession userSession;

    @Subscribe
    public void onAfterInit(AfterInitEvent event) {
        User user = userSession.getUser();
        System.out.println("User = " + user.getName());


    }
}

profile-edit.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"
        caption="msg://caption"
        messagesPack="com.thrivalist.workscape.web.screens.profile">
    <data>
        <instance id="profileDc"
                  class="com.haulmont.cuba.security.entity.User"
                  view="user.edit">
            <loader id = "profileDl"/>
        </instance>
    </data>
    <layout>
        <grid align="MIDDLE_CENTER"
              spacing="true">
            <columns count="2"/>
            <rows>
                <row>
                    <label value="First Name"/>
                    <textField id="firstName"
                               required="true"/>
                </row>
                <row>
                    <label value="Middle Name"/>
                    <textField id="middleName"/>
                </row>
                <row>
                    <label value="Last Name"/>
                    <textField id="lastName"
                               required="true"/>
                </row>
                <row>
                    <label value="Email"/>
                    <textField id="email"
                               required="true"/>
                </row>
                <row>
                    <hbox align="MIDDLE_CENTER"
                          colspan="2"
                          spacing="true">
                        <button id="okBtn"
                                align="TOP_RIGHT"
                                caption="OK"/>
                        <button id="cancelBtn"
                                caption="Cancel"/>
                    </hbox>
                </row>
            </rows>
        </grid>
    </layout>
</window>

Hello @clint.dalton,

you can use a component in the web module. Here an example:

@Component("ksm_LdapConfigurationMenu")
public class LdapConfigurationMenu {

    @Inject
    private LdapService ldapService;

    @Inject
    private ScreenBuilders screenBuilders;


    public void showEntityEditor() {
        final Screen frameOwner = AppUI.getCurrent().getTopLevelWindowNN().getFrameOwner();
        screenBuilders.editor(LdapConfiguration.class, frameOwner)
                .editEntity(ldapService.loadConfiguration())
                .withOpenMode(OpenMode.NEW_TAB)
                .show();
    }

}

In the web-menu.xml you add a item to call the method showEntityEditor in the component:

<menu-config xmlns="http://schemas.haulmont.com/cuba/menu.xsd">
    ...
    <item bean="ksm_LdapConfigurationMenu" beanMethod="showEntityEditor"/>
    ...
</menu-config>

Greetings
Andreas

As I understand the problem, it could be solved by a simple screen as follows:

<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"
        caption="msg://caption"
        messagesPack="com.company.demo.web.screens">
    <data>
        <instance id="userDc" class="com.haulmont.cuba.security.entity.User" view="_local">
        </instance>
    </data>
    <layout>
        <textField caption="User Name" dataContainer="userDc" property="name"/>
        <button id="saveBtn" caption="Save"/>
    </layout>
</window>
@UiController("demo_UserProfileScreen")
@UiDescriptor("user-profile.xml")
public class UserProfileScreen extends Screen {

    @Inject
    private UserSession userSession;

    @Inject
    private InstanceContainer<User> userDc;
    @Inject
    private DataContext dataContext;

    @Subscribe
    public void onBeforeShow(BeforeShowEvent event) {
        User trackedUser = dataContext.merge(userSession.getCurrentOrSubstitutedUser());
        userDc.setItem(trackedUser);
    }

    @Subscribe("saveBtn")
    public void onSaveBtnClick(Button.ClickEvent event) {
        dataContext.commit();
        closeWithDefaultAction();
    }
}

But the changes made to the user will be reflected in the current user session only after re-login. If you need them immediately, update individual attributes of the user obtained with userSession.getCurrentOrSubstitutedUser().