Session level editable variable

What is the best option to use the last selected option of the user during a session?
For example, a company has 5 departments and user A has access to department 1 and department 2. Therefore, when the user is logging in, s/he will have option to select either department 1 or 2 to make it default. Suppose the user selected department 2 before starting a CRUD transaction. When the user will do CRUD transactions, s/he will be able to do the operation for the selected department 2. My question is, what is the recommended best practice to store and retrieve the value of selected department (2 in this case) to be used in data manipulation? Note that user may change the selected default department anytime. Is this something can be stored in user table?

1 Like

Probably the user session attributes are what you need.

Thanks. What will be your suggestions to track which department have the user selected last time?

Can you share a little more thoughts how this could be achieved using session attributes? Let me explain. User will select a department when logged in but will have option to change the department name. As and when the department name is changed, it will be available to persist to any other fields.

Can you share a small project where the user selects a department and then uses it? Let it be hardcoded, I’ll try to change your code to demonstrate the usage of session attributes.

Hi
Please find attached the demo application as requested for your guidance.

Step 1: Add few departments e.g. Accounting, Supply Chain, Marketing, IS.
Step 2: Select a department to set it default from the menu “Select Default Department”. This default setting can be changed anytime the user like and user may close the screen but the system should keep this somewhere somehow to use it.
Step3: When the user will open the Order browser, the application should show only those orders which are related to this selected default department. Similarly, when saving a new Order, the system will store this default Department.
Step 4: The user will have option to change the default department selection. When the user will change this default setting, the precondition would be, there will ave no windows open except this default selection UI in order to ensure the is no conflict currently showing data and new default department.

Thanks for help…

demo-session-variable.zip (356.5K)

Please look at the attached project.

Selectdefaultdepartment.java controller allows a user to select a department and store it as a session attribute:


public class Selectdefaultdepartment extends AbstractWindow {
    @Inject
    private OptionsList deptList;
    @Inject
    private UserSessionSource userSessionSource;

    public void onOkBtnClick(Component source) {
        Department value = deptList.getValue();
        userSessionSource.getUserSession().setAttribute("defaultDepartment", value);
        showNotification("Default department set to " + value.getName(), NotificationType.HUMANIZED);
        close(COMMIT_ACTION_ID);
    }

    public void onCancelBtnClick(Component source) {
        close(CLOSE_ACTION_ID);
    }
}

In the OrderBrowse controller, we read the session parameter and modify the datasource query if needed:


public class OrderBrowse extends AbstractLookup {
    @Inject
    private UserSessionSource userSessionSource;
    @Inject
    private CollectionDatasource<Order, UUID> ordersDs;

    @Override
    public void init(Map<String, Object> params) {
        Department defaultDepartment = userSessionSource.getUserSession().getAttribute("defaultDepartment");
        if (defaultDepartment != null) {
            showNotification("Default department is " + defaultDepartment.getName(), NotificationType.HUMANIZED);
            ordersDs.setQuery("select e from demosessionvariable$Order e where e.department.id = :session$defaultDepartment");
        } else {
            showNotification("Default department is not set", NotificationType.HUMANIZED);
        }
    }
}

In the OrderEdit controller we assign a default value to the department attribute of a new Order:


public class OrderEdit extends AbstractEditor<Order> {

    @Inject
    private UserSessionSource userSessionSource;

    @Override
    protected void initNewItem(Order item) {
        Department department = userSessionSource.getUserSession().getAttribute("defaultDepartment");
        item.setDepartment(department);
    }
}

demo-session-variable.zip (37.4K)

Thank you so much Konstantin. The demo is setting the default department when I am going to edit Orders. It is also showing orders which is related to default department which is very good.

However, I couldn’t save the created order even I already had selected the default department. I am getting the following error message. A snap-shot is also attached.


com.haulmont.cuba.core.global.RemoteException:
---
java.lang.RuntimeException: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.cuba7): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
Error Code: -458
---
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.cuba7): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
Error Code: -458
---
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.cuba7): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
Error Code: -458
---
org.eclipse.persistence.exceptions.DatabaseException: 
Internal Exception: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
Error Code: -458
---
java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
---
org.hsqldb.HsqlException: java.lang.NullPointerException java.lang.NullPointerException

Snip20160521_1

You’ve got exception from HSQLDB. Try to run Create database, maybe the one in my project is not valid for some reason.

It’s working perfectly. Thank you so much.

Very nice example - Just what I needed. Thanks.