}
// sets the context by reading all user information
public boolean initialize(String userId, String roleId, String clientId, String orgId) {
String localClientId = clientId;
final User u = SessionHandler.getInstance().find(User.class, userId);
if (u == null) {
return false;
}
setInitialized(false);
setInAdministratorMode(true);
try {
setUser(u);
Hibernate.initialize(getUser().getClient());
Hibernate.initialize(getUser().getOrganization());
Hibernate.initialize(getUser().getDefaultOrganization());
Hibernate.initialize(getUser().getDefaultWarehouse());
Hibernate.initialize(getUser().getDefaultClient());
Hibernate.initialize(getUser().getDefaultRole());
Hibernate.initialize(getUser().getDefaultLanguage());
organizationStructureProviderByClient = new HashMap<String, OrganizationStructureProvider>();
// first take the passed role, if any
// now check if the default role is active, if not another one needs
// to be
// selected.
if (roleId != null) {
final Role r = getOne(Role.class, "select r from " + Role.class.getName() + " r where "
+ " r." + Role.PROPERTY_ID + "='" + roleId + "'");
setRole(r);
} else if (getUser().getDefaultRole() != null && getUser().getDefaultRole().isActive()) {
setRole(getUser().getDefaultRole());
} else {
final UserRoles ur = getOne(UserRoles.class, "select ur from " + UserRoles.class.getName()
+ " ur where " + " ur." + UserRoles.PROPERTY_USERCONTACT + "." + User.PROPERTY_ID
+ "='" + u.getId() + "' and ur." + UserRoles.PROPERTY_ACTIVE + "='Y' and ur."
+ UserRoles.PROPERTY_ROLE + "." + Role.PROPERTY_ACTIVE + "='Y' order by ur."
+ UserRoles.PROPERTY_ROLE + "." + Role.PROPERTY_ID + " asc", false);
if (ur == null) {
throw new OBSecurityException(
"Your user is not assigned to a Role and it is required to login into Openbravo. Ask the Security Administrator");