m.coli
(Manuel Coli)
May 11, 2016, 5:00pm
#1
Hi,
In my application one employee can have more than one user id.
I’ve extended entity User with the refer to the entity “Employees”.
1 - How can create a DS that can get only the employee of that logged user?
2 - How can i extend Session Parameters like :session$userId with something like :session$employeeId
Thanks
Manuel
Manuel
1 Like
krivopustov
(Konstantin Krivopustov)
May 12, 2016, 6:53am
#2
Hi Manuel,
1 - How can create a DS that can get only the employee of that logged user?
Could you elaborate? Do you want to allow a user to edit properties of his own Employee entity?
2 - How can i extend Session Parameters like :session$userId with something like :session$employeeId
You can set your own session attribute after a user is logged in. Override the connectionStateChanged() method in your App class:
public class App extends DefaultApp {
@Override
public void connectionStateChanged(Connection connection) throws LoginException {
super.connectionStateChanged(connection);
if (connection.getSession() != null) {
DataManager dataManager = AppBeans.get(DataManager.class);
Employee employee = dataManager.load(LoadContext.create(Employee.class).setQuery(
LoadContext.createQuery("select u.employee from demo$ExtUser u where u.id = :userId")
.setParameter("userId", connection.getSession().getUser().getId())));
connection.getSession().setAttribute("employeeId", employee);
}
}
}
After that, you can use :session$employeeId parameter in datasource queries.
2 Likes
m.coli
(Manuel Coli)
May 12, 2016, 9:01am
#3
Hi Konstantin,
Could you elaborate? Do you want to allow a user to edit properties of his own Employee entity?
Exactly I want that all employee is allowed to edit his own properties .
Thanks for the response!!
krivopustov
(Konstantin Krivopustov)
May 13, 2016, 6:19am
#4
Assuming you already have an Employee edit screen, you should open it with the particular Employee instance which is related to the current user.
You can do it if you create a “screen launcher” class and use it in menu instead of the editor screen. The launcher class may look as follows:
package com.company.demo.web.employee;
import com.company.demo.entity.Employee;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.WindowManagerProvider;
import com.haulmont.cuba.gui.config.WindowConfig;
import com.haulmont.cuba.gui.config.WindowInfo;
import java.util.concurrent.Callable;
public class SelfEmployee implements Callable {
@Override
public Object call() throws Exception {
DataManager dataManager = AppBeans.get(DataManager.class);
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
Employee employee = dataManager.load(LoadContext.create(Employee.class).setQuery(
LoadContext.createQuery("select u.employee from demo$ExtUser u where u.id = :userId")
.setParameter("userId", uss.getUserSession().getUser().getId())));
WindowManagerProvider windowManagerProvider = AppBeans.get(WindowManagerProvider.class);
WindowConfig windowConfig = AppBeans.get(WindowConfig.class);
WindowInfo windowInfo = windowConfig.getWindowInfo("demo$Employee.edit");
return windowManagerProvider.get().openEditor(windowInfo, employee, WindowManager.OpenType.NEW_TAB);
}
}
Register it in web-screens.xml :
<screen id="selfEmployee"
class="com.company.demo.web.employee.SelfEmployee"/>
Now you can open the “selfEmployee” screen programmatically or register it in web-menu.xml :
<menu-config xmlns="http://schemas.haulmont.com/cuba/menu.xsd">
<menu id="application"
insertBefore="administration">
<item id="selfEmployee"/>
</menu>
</menu-config>
m.coli
(Manuel Coli)
May 13, 2016, 10:09am
#5
Hi Konstantin,
Thanks a lot!!!
Manuel
mario
(Mario David)
January 11, 2017, 9:09am
#6
Hi Konstantin,
can you elaborate why in the example code you showed, you fetched the employee of Type “Employee” but added the employee instance with the name “employeeId” (instead of just “employee”) into the session? Additionally is it possible to get the employee instance out of the session? In case in my program i don’t want to have the id but instead the instance so that i can pass it to a referencing entity in the setter e.g.
Thanks & Bye
Mario
krivopustov
(Konstantin Krivopustov)
January 11, 2017, 11:26am
#7
Hi Mario,
I believe it’s just a mistake or a typo. You should be able to save any serializable objects in session, including detached entities.
rulesprog
(Rulesprog)
August 20, 2018, 12:10pm
#8
Why am I getting error after I did the same:
rulesprog
(Rulesprog)
August 21, 2018, 9:18am
#9
Fixed by changing
<bean id="cuba_App" class="com.dtc.callbook.web.myApp" scope="prototype"/>
to
<bean id="cuba_App" class="com.dtc.callbook.web.myApp" scope="vaadin"/>