[ 2507390 ] FR - Save language and other login data in cookie

This commit is contained in:
Heng Sin Low 2009-02-06 21:48:42 +00:00
parent b5ecd19629
commit 4706ca2473
5 changed files with 363 additions and 9 deletions

View File

@ -24,6 +24,7 @@ import javax.servlet.http.HttpSession;
import org.adempiere.webui.desktop.DefaultDesktop;
import org.adempiere.webui.desktop.IDesktop;
import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.util.UserPreference;
import org.compiere.model.MSession;
import org.compiere.model.MSysConfig;
import org.compiere.util.CLogger;
@ -66,12 +67,16 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
private String langSession;
private UserPreference userPreference;
private static final CLogger logger = CLogger.getCLogger(AdempiereWebUI.class);
public AdempiereWebUI()
{
this.addEventListener(Events.ON_CLIENT_INFO, this);
this.setVisible(false);
userPreference = new UserPreference();
}
public void onCreate()
@ -219,6 +224,9 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
Executions.sendRedirect("index.zul");
}
/**
* @return IDesktop
*/
public IDesktop getAppDeskop()
{
return appDesktop;
@ -239,4 +247,20 @@ public class AdempiereWebUI extends Window implements EventListener, IWebClient
}
}
/**
* @param userId
* @return UserPreference
*/
public UserPreference loadUserPreference(int userId) {
userPreference.loadPreference(userId);
return userPreference;
}
/**
* @return UserPrerence
*/
public UserPreference getUserPreference() {
return userPreference;
}
}

View File

@ -1,13 +1,54 @@
/******************************************************************************
* Copyright (C) 2008 Low Heng Sin *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.webui;
import org.adempiere.webui.desktop.IDesktop;
import org.adempiere.webui.util.UserPreference;
/**
*
* @author hengsin
*
*/
public interface IWebClient {
/**
* login completed
*/
public void loginCompleted();
/**
* logout
*/
public void logout();
/**
*
* @return IDesktop
*/
public IDesktop getAppDeskop();
/**
*
* @param userId
* @return UserPreference
*/
public UserPreference loadUserPreference(int userId);
/**
*
* @return UserPreference
*/
public UserPreference getUserPreference();
}

View File

@ -27,7 +27,10 @@ import org.adempiere.webui.component.Row;
import org.adempiere.webui.component.Rows;
import org.adempiere.webui.component.Textbox;
import org.adempiere.webui.component.Window;
import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.util.UserPreference;
import org.adempiere.webui.window.LoginWindow;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Language;
@ -41,6 +44,7 @@ import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Image;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
/**
*
@ -141,6 +145,7 @@ public class LoginPanel extends Window implements EventListener
txtUserId.setCols(25);
txtUserId.setMaxlength(40);
txtUserId.setWidth("220px");
txtUserId.addEventListener(Events.ON_CHANGE, this); // Elaine 2009/02/06
txtPassword = new Textbox();
txtPassword.setId("txtPassword");
@ -187,6 +192,31 @@ public class LoginPanel extends Window implements EventListener
lblLanguage.setValue(res.getString("Language"));
}
}
// Elaine 2009/02/06 - initial language
if (event.getName().equals(Events.ON_CHANGE))
{
if(eventComp.getId().equals(txtUserId.getId()))
{
String userId = txtUserId.getValue();
if(userId != null && userId.length() > 0)
{
int AD_User_ID = DB.getSQLValue(null, "SELECT AD_User_ID FROM AD_User WHERE Name = ?", userId);
if(AD_User_ID > 0)
{
// Elaine 2009/02/06 Load preference from AD_Preference
UserPreference userPreference = SessionManager.getSessionApplication().loadUserPreference(AD_User_ID);
String initDefault = userPreference.getProperty(UserPreference.P_LANGUAGE);
for(int i = 0; i < lstLanguage.getItemCount(); i++)
{
Listitem li = lstLanguage.getItemAtIndex(i);
if(li.getLabel().equals(initDefault))
lstLanguage.setSelectedItem(li);
}
}
}
}
}
//
}
/**
* validates user name and password when logging in
@ -211,7 +241,9 @@ public class LoginPanel extends Window implements EventListener
Env.setContext(ctx, Env.LANGUAGE, langName);
Language language = Language.getLanguage(langName);
Env.verifyLanguage(ctx, language);
wndLogin.loginOk(userId, userPassword);
wndLogin.loginOk(userId, userPassword);
Env.setContext(ctx, "Language", lstLanguage.getSelectedItem().getLabel()); // Elaine 2009/02/06
}
}
}

View File

@ -24,6 +24,8 @@ import org.adempiere.webui.component.ConfirmPanel;
import org.adempiere.webui.component.Label;
import org.adempiere.webui.component.Window;
import org.adempiere.webui.exception.ApplicationException;
import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.util.UserPreference;
import org.adempiere.webui.window.LoginWindow;
import org.compiere.db.CConnection;
import org.compiere.model.MRole;
@ -202,9 +204,18 @@ public class RolePanel extends Window implements EventListener
btnCancel.setLabel("Cancel");
btnCancel.addEventListener("onClick", this);
// initial role - Elaine 2009/02/06
UserPreference userPreference = SessionManager.getSessionApplication().getUserPreference();
String initDefault = userPreference.getProperty(UserPreference.P_ROLE);
for(int i = 0; i < rolesKNPairs.length; i++)
lstRole.appendItem(rolesKNPairs[i].getName(), rolesKNPairs[i].getID());
lstRole.setSelectedIndex(0);
{
Listitem li = lstRole.appendItem(rolesKNPairs[i].getName(), rolesKNPairs[i].getID());
if(rolesKNPairs[i].getID().equals(initDefault))
lstRole.setSelectedItem(li);
}
if (lstRole.getSelectedIndex() == -1)
lstRole.setSelectedIndex(0);
//
updateClientList();
}
@ -214,14 +225,23 @@ public class RolePanel extends Window implements EventListener
Listitem lstItemRole = lstRole.getSelectedItem();
if(lstItemRole != null)
{
// initial client - Elaine 2009/02/06
UserPreference userPreference = SessionManager.getSessionApplication().getUserPreference();
String initDefault = userPreference.getProperty(UserPreference.P_CLIENT);
KeyNamePair roleKNPair = new KeyNamePair(new Integer((String)lstItemRole.getValue()), lstItemRole.getLabel());
KeyNamePair clientKNPairs[] = login.getClients(roleKNPair);
if(clientKNPairs != null && clientKNPairs.length > 0)
{
for(int i = 0; i < clientKNPairs.length; i++)
lstClient.appendItem(clientKNPairs[i].getName(), clientKNPairs[i].getID());
lstClient.setSelectedIndex(0);
{
Listitem li = lstClient.appendItem(clientKNPairs[i].getName(), clientKNPairs[i].getID());
if(clientKNPairs[i].getID().equals(initDefault))
lstClient.setSelectedItem(li);
}
if (lstClient.getSelectedIndex() == -1)
lstClient.setSelectedIndex(0);
}
//
//force reload of default role
MRole.getDefault(Env.getCtx(), true);
@ -235,14 +255,23 @@ public class RolePanel extends Window implements EventListener
Listitem lstItemClient = lstClient.getSelectedItem();
if(lstItemClient != null)
{
// initial organisation - Elaine 2009/02/06
UserPreference userPreference = SessionManager.getSessionApplication().getUserPreference();
String initDefault = userPreference.getProperty(UserPreference.P_ORG);
KeyNamePair clientKNPair = new KeyNamePair(new Integer((String)lstItemClient.getValue()), lstItemClient.getLabel());
KeyNamePair orgKNPairs[] = login.getOrgs(clientKNPair);
if(orgKNPairs != null && orgKNPairs.length > 0)
{
for(int i = 0; i < orgKNPairs.length; i++)
lstOrganisation.appendItem(orgKNPairs[i].getName(), orgKNPairs[i].getID());
lstOrganisation.setSelectedIndex(0);
{
Listitem li = lstOrganisation.appendItem(orgKNPairs[i].getName(), orgKNPairs[i].getID());
if(orgKNPairs[i].getID().equals(initDefault))
lstOrganisation.setSelectedItem(li);
}
if (lstOrganisation.getSelectedIndex() == -1)
lstOrganisation.setSelectedIndex(0);
}
//
}
updateWarehouseList();
}
@ -253,14 +282,23 @@ public class RolePanel extends Window implements EventListener
Listitem lstItemOrganisation = lstOrganisation.getSelectedItem();
if(lstItemOrganisation != null)
{
// initial warehouse - Elaine 2009/02/06
UserPreference userPreference = SessionManager.getSessionApplication().getUserPreference();
String initDefault = userPreference.getProperty(UserPreference.P_WAREHOUSE);
KeyNamePair organisationKNPair = new KeyNamePair(new Integer((String)lstItemOrganisation.getValue()), lstItemOrganisation.getLabel());
KeyNamePair warehouseKNPairs[] = login.getWarehouses(organisationKNPair);
if(warehouseKNPairs != null && warehouseKNPairs.length > 0)
{
for(int i = 0; i < warehouseKNPairs.length; i++)
lstWarehouse.appendItem(warehouseKNPairs[i].getName(), warehouseKNPairs[i].getID());
lstWarehouse.setSelectedIndex(0);
{
Listitem li = lstWarehouse.appendItem(warehouseKNPairs[i].getName(), warehouseKNPairs[i].getID());
if(warehouseKNPairs[i].getID().equals(initDefault))
lstWarehouse.setSelectedItem(li);
}
if (lstWarehouse.getSelectedIndex() == -1)
lstWarehouse.setSelectedIndex(0);
}
//
}
}
@ -337,5 +375,15 @@ public class RolePanel extends Window implements EventListener
return ;
}
wndLogin.loginCompleted();
// Elaine 2009/02/06 save preference to AD_Preference
UserPreference userPreference = SessionManager.getSessionApplication().getUserPreference();
userPreference.setProperty(UserPreference.P_LANGUAGE, Env.getContext(Env.getCtx(), "Language"));
userPreference.setProperty(UserPreference.P_ROLE, Integer.parseInt((String) lstItemRole.getValue()));
userPreference.setProperty(UserPreference.P_CLIENT, Integer.parseInt((String) lstItemClient.getValue()));
userPreference.setProperty(UserPreference.P_ORG, Integer.parseInt((String) lstItemOrg.getValue()));
userPreference.setProperty(UserPreference.P_WAREHOUSE, Integer.parseInt((String) lstItemWarehouse.getValue()));
userPreference.savePreference();
//
}
}

View File

@ -0,0 +1,209 @@
/******************************************************************************
* Copyright (C) 2008 Low Heng Sin *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.webui.util;
import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Logger;
import org.compiere.model.I_AD_Preference;
import org.compiere.model.MPreference;
import org.compiere.model.Query;
import org.compiere.util.Env;
import org.compiere.util.Language;
/**
*
* @author hengsin
*
*/
public final class UserPreference implements Serializable {
private static final long serialVersionUID = 1L;
/** Language */
public static final String P_LANGUAGE = "Language";
private static final String DEFAULT_LANGUAGE = Language.getName
(System.getProperty("user.language") + "_" + System.getProperty("user.country"));
/** Role */
public static final String P_ROLE = "Role";
private static final String DEFAULT_ROLE = "";
/** Client Name */
public static final String P_CLIENT = "Client";
private static final String DEFAULT_CLIENT = "";
/** Org Name */
public static final String P_ORG = "Organization";
private static final String DEFAULT_ORG = "";
/** Warehouse Name */
public static final String P_WAREHOUSE = "Warehouse";
private static final String DEFAULT_WAREHOUSE = "";
/** Ini Properties */
private static final String[] PROPERTIES = new String[] {
P_LANGUAGE,
P_ROLE,
P_CLIENT,
P_ORG,
P_WAREHOUSE };
/** Ini Property Values */
private static final String[] VALUES = new String[] {
DEFAULT_LANGUAGE,
DEFAULT_ROLE,
DEFAULT_CLIENT,
DEFAULT_ORG,
DEFAULT_WAREHOUSE };
/** Container for Properties */
private Properties props = new Properties();
private int m_AD_User_ID;
/** Logger */
@SuppressWarnings("unused")
private static Logger log = Logger.getLogger(UserPreference.class.getName());
/**
* save user preference
*/
public void savePreference() {
if (m_AD_User_ID > 0) {
Query query = new Query(Env.getCtx(), I_AD_Preference.Table_Name, "AD_User_ID = ? AND Attribute = ? AND AD_Window_ID Is NULL", null);
for (int i = 0; i < PROPERTIES.length; i++) {
String attribute = PROPERTIES[i];
String value = props.getProperty(attribute);
MPreference preference = query.setParameters(new Object[]{m_AD_User_ID, attribute}).first();
if (preference == null) {
preference = new MPreference(Env.getCtx(), 0, null);
preference.setAD_User_ID(m_AD_User_ID);
preference.setAttribute(attribute);
}
preference.setValue(value);
preference.saveEx();
}
}
}
/**
* load user preference
* @param AD_User_ID
*/
public void loadPreference(int AD_User_ID) {
if (AD_User_ID > 0) {
m_AD_User_ID = AD_User_ID;
props = new Properties();
Query query = new Query(Env.getCtx(), I_AD_Preference.Table_Name, "AD_User_ID = ? AND Attribute = ? AND AD_Window_ID Is NULL", null);
for (int i = 0; i < PROPERTIES.length; i++) {
String attribute = PROPERTIES[i];
String value = VALUES[i];
MPreference preference = query.setParameters(new Object[]{m_AD_User_ID, attribute}).first();
if (preference != null) {
value = preference.getValue();
}
props.setProperty(attribute, value);
}
}
}
/**
* delete all user preference
*/
public void deletePreference() {
if (m_AD_User_ID > 0) {
props = new Properties();
Query query = new Query(Env.getCtx(), I_AD_Preference.Table_Name, "AD_User_ID = ? AND Attribute = ? AND AD_Window_ID Is NULL", null);
for (int i = 0; i < PROPERTIES.length; i++) {
String attribute = PROPERTIES[i];
MPreference preference = query.setParameters(new Object[]{m_AD_User_ID, attribute}).first();
if (preference != null) {
preference.deleteEx(true);
}
}
}
}
/***************************************************************************
* Set Property
*
* @param key
* Key
* @param value
* Value
*/
public void setProperty(String key, String value) {
if (props == null)
props = new Properties();
if (value == null)
props.setProperty(key, "");
else
props.setProperty(key, value);
} // setProperty
/**
* Set Property
*
* @param key
* Key
* @param value
* Value
*/
public void setProperty(String key, boolean value) {
setProperty(key, value ? "Y" : "N");
} // setProperty
/**
* Set Property
*
* @param key
* Key
* @param value
* Value
*/
public void setProperty(String key, int value) {
setProperty(key, String.valueOf(value));
} // setProperty
/**
* Get Propery
*
* @param key
* Key
* @return Value
*/
public String getProperty(String key) {
if (key == null)
return "";
String value = props.getProperty(key, "");
if (value == null || value.length() == 0)
return "";
return value;
} // getProperty
/**
* Get Propery as Boolean
*
* @param key
* Key
* @return Value
*/
public boolean isPropertyBool(String key) {
return getProperty(key).equals("Y");
} // getProperty
}