IDEMPIERE-2409 Improve Context Help performance using Cache

This commit is contained in:
Carlos Ruiz 2015-01-17 10:51:23 -05:00
parent f2cda463d1
commit 3caf738eae
3 changed files with 264 additions and 148 deletions

View File

@ -0,0 +1,62 @@
/***********************************************************************
* This file is part of iDempiere ERP Open Source *
* http://www.idempiere.org *
* *
* Copyright (C) Contributors *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* 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., 51 Franklin Street, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
* Contributors: *
* - Carlos Ruiz - globalqss *
**********************************************************************/
package org.compiere.model;
import java.sql.ResultSet;
import java.util.Properties;
/**
* Context Help Model
*
* @author Carlos Ruiz
*/
public class MCtxHelp extends X_AD_CtxHelp {
/**
*
*/
private static final long serialVersionUID = 2824732566905179805L;
/**
* Standard Constructor
* @param ctx context
* @param AD_CtxHelp_ID
* @param trxName transaction
*/
public MCtxHelp (Properties ctx, int AD_CtxHelp_ID, String trxName) {
super (ctx, AD_CtxHelp_ID, trxName);
} // MCtxHelp
/**
* Load Constructor
* @param ctx context
* @param rs result set
* @param trxName transaction
*/
public MCtxHelp (Properties ctx, ResultSet rs, String trxName) {
super(ctx, rs, trxName);
} // MCtxHelp
} // MCtxHelp

View File

@ -0,0 +1,196 @@
/***********************************************************************
* This file is part of iDempiere ERP Open Source *
* http://www.idempiere.org *
* *
* Copyright (C) Contributors *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* 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., 51 Franklin Street, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
* Contributors: *
* - Carlos Ruiz - globalqss *
**********************************************************************/
package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.CCache;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
/**
* Context Help Message Model
*
* @author Carlos Ruiz
*/
public class MCtxHelpMsg extends X_AD_CtxHelpMsg {
/**
*
*/
private static final long serialVersionUID = -7892179461720143685L;
/** Logging */
private static CLogger s_log = CLogger.getCLogger(MCtxHelpMsg.class);
/** Context Help Message Cache */
private static CCache<String, String> s_cache = new CCache<String, String>(Table_Name, 10);
/**
* Standard Constructor
* @param ctx context
* @param AD_CtxHelpMsg_ID
* @param trxName transaction
*/
public MCtxHelpMsg (Properties ctx, int AD_CtxHelpMsg_ID, String trxName) {
super (ctx, AD_CtxHelpMsg_ID, trxName);
} // MCtxHelpMsg
/**
* Load Constructor
* @param ctx context
* @param rs result set
* @param trxName transaction
*/
public MCtxHelpMsg (Properties ctx, ResultSet rs, String trxName) {
super(ctx, rs, trxName);
} // MCtxHelpMsg
/**
* Get the context help message defined for the type, recordid, language, client, org
* @param ctx
* @param ctxtype
* @param recordId
* @return the message translated
*/
public static String get(Properties ctx, String ctxType, int recordId) {
StringBuilder key = new StringBuilder()
.append(ctxType).append("|")
.append(recordId).append("|")
.append(Env.getAD_Client_ID(ctx)).append("|")
.append(Env.getAD_Org_ID(ctx)).append("|")
.append(Env.getAD_Language(Env.getCtx()));
String retValue = null;
if (s_cache.containsKey(key.toString())) {
retValue = s_cache.get(key.toString());
if (s_log.isLoggable(Level.FINEST)) s_log.finest("Cache: " + retValue);
return retValue;
}
int AD_CtxHelp_ID = getCtxHelpID(ctxType, recordId);
if (AD_CtxHelp_ID > 0) {
StringBuilder sql = new StringBuilder();
if (Env.isBaseLanguage(Env.getCtx(), X_AD_CtxHelpMsg.Table_Name)) {
sql.append("SELECT MsgText ");
sql.append("FROM AD_CtxHelpMsg ");
sql.append("WHERE IsActive = 'Y' ");
sql.append("AND AD_Client_ID IN (0, ?) ");
sql.append("AND AD_Org_ID IN (0, ?) ");
sql.append("AND AD_CtxHelp_ID = ? ");
sql.append("ORDER BY AD_Client_ID DESC, AD_Org_ID DESC, AD_CtxHelpMsg_ID DESC");
} else {
sql.append("SELECT mt.MsgText ");
sql.append("FROM AD_CtxHelpMsg m ");
sql.append("LEFT JOIN AD_CtxHelpMsg_Trl mt ON (mt.AD_CtxHelpMsg_ID = m.AD_CtxHelpMsg_ID AND mt.AD_Language = ?) ");
sql.append("WHERE mt.IsActive = 'Y' ");
sql.append("AND m.AD_Client_ID IN (0, ?) ");
sql.append("AND m.AD_Org_ID IN (0, ?) ");
sql.append("AND m.AD_CtxHelp_ID = ? ");
sql.append("ORDER BY m.AD_Client_ID DESC, m.AD_Org_ID DESC, m.AD_CtxHelpMsg_ID DESC");
}
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), null);
int count = 1;
if (!Env.isBaseLanguage(Env.getCtx(), X_AD_CtxHelpMsg.Table_Name))
pstmt.setString(count++, Env.getAD_Language(Env.getCtx()));
pstmt.setInt(count++, Env.getAD_Client_ID(ctx));
pstmt.setInt(count++, Env.getAD_Org_ID(ctx));
pstmt.setInt(count++, AD_CtxHelp_ID);
rs = pstmt.executeQuery();
if (rs.next())
retValue = rs.getString(X_AD_CtxHelpMsg.COLUMNNAME_MsgText);
} catch (Exception e) {
throw new AdempiereException(e);
} finally {
DB.close(rs, pstmt);
rs=null; pstmt=null;
}
}
if (retValue == null)
retValue = "";
s_cache.put(key.toString(), retValue);
return retValue;
}
private static int getCtxHelpID(String ctxType, int recordId) {
Properties ctx = Env.getCtx();
String column;
String table;
if (ctxType == X_AD_CtxHelp.CTXTYPE_Tab) {
column = X_AD_Tab.COLUMNNAME_AD_Tab_ID;
table = X_AD_Tab.Table_Name;
} else if (ctxType == X_AD_CtxHelp.CTXTYPE_Process) {
column = X_AD_Process.COLUMNNAME_AD_Process_ID;
table = X_AD_Process.Table_Name;
} else if (ctxType == X_AD_CtxHelp.CTXTYPE_Form) {
column = X_AD_Form.COLUMNNAME_AD_Form_ID;
table = X_AD_Form.Table_Name;
} else if (ctxType == X_AD_CtxHelp.CTXTYPE_Info) {
column = X_AD_InfoWindow.COLUMNNAME_AD_InfoWindow_ID;
table = X_AD_InfoWindow.Table_Name;
} else if (ctxType == X_AD_CtxHelp.CTXTYPE_Workflow) {
column = X_AD_Workflow.COLUMNNAME_AD_Workflow_ID;
table = X_AD_Workflow.Table_Name;
} else if (ctxType == X_AD_CtxHelp.CTXTYPE_Task) {
column = X_AD_Task.COLUMNNAME_AD_Task_ID;
table = X_AD_Task.Table_Name;
} else if (ctxType == X_AD_CtxHelp.CTXTYPE_Node) {
column = X_AD_WF_Node.COLUMNNAME_AD_WF_Node_ID;
table = X_AD_WF_Node.Table_Name;
} else {
column = "";
table = X_AD_CtxHelp.Table_Name;
}
StringBuilder sql = new StringBuilder();
sql.append("SELECT t.AD_CtxHelp_ID FROM ");
sql.append(table);
sql.append(" t, AD_CtxHelp h WHERE t.AD_CtxHelp_ID=h.AD_CtxHelp_ID AND t.IsActive='Y' AND h.IsActive='Y' AND h.AD_Client_ID IN (0, ?) AND h.AD_Org_ID IN (0, ?) ");
if (ctxType == X_AD_CtxHelp.CTXTYPE_Home) {
sql.append("AND h.CtxType");
} else if (ctxType == X_AD_CtxHelp.CTXTYPE_Tab || ctxType == X_AD_CtxHelp.CTXTYPE_Process ||
ctxType == X_AD_CtxHelp.CTXTYPE_Form || ctxType == X_AD_CtxHelp.CTXTYPE_Info ||
ctxType == X_AD_CtxHelp.CTXTYPE_Workflow || ctxType == X_AD_CtxHelp.CTXTYPE_Task ||
ctxType == X_AD_CtxHelp.CTXTYPE_Node) {
sql.append("AND t.");
sql.append(column);
} else {
sql.append("AND 1"); // must not happen
}
sql.append("=? ");
sql.append("ORDER BY h.AD_Client_ID DESC, h.AD_Org_ID DESC, h.AD_CtxHelp_ID DESC");
return DB.getSQLValue(null, sql.toString(), Env.getAD_Client_ID(ctx), Env.getAD_Org_ID(ctx), ctxType == X_AD_CtxHelp.CTXTYPE_Home ? ctxType : recordId);
}
} // MCtxHelpMsg

View File

@ -14,29 +14,18 @@
package org.adempiere.webui.panel;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.webui.desktop.IDesktop;
import org.compiere.model.GridField;
import org.compiere.model.GridTab;
import org.compiere.model.MCtxHelpMsg;
import org.compiere.model.MForm;
import org.compiere.model.MInfoWindow;
import org.compiere.model.MProcess;
import org.compiere.model.MTab;
import org.compiere.model.MTask;
import org.compiere.model.X_AD_CtxHelp;
import org.compiere.model.X_AD_CtxHelpMsg;
import org.compiere.model.X_AD_Form;
import org.compiere.model.X_AD_InfoWindow;
import org.compiere.model.X_AD_Process;
import org.compiere.model.X_AD_Tab;
import org.compiere.model.X_AD_Task;
import org.compiere.model.X_AD_WF_Node;
import org.compiere.model.X_AD_Workflow;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.wf.MWFNode;
@ -501,142 +490,11 @@ public class HelpController
return htmlString;
}
private int getCtxHelpID(String ctxType, int recordId)
{
Properties ctx = Env.getCtx();
StringBuilder sql = new StringBuilder();
sql.append("SELECT t.");
if (ctxType == X_AD_CtxHelp.CTXTYPE_Tab)
sql.append(X_AD_Tab.COLUMNNAME_AD_CtxHelp_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Process)
sql.append(X_AD_Process.COLUMNNAME_AD_CtxHelp_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Form)
sql.append(X_AD_Form.COLUMNNAME_AD_CtxHelp_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Info)
sql.append(X_AD_InfoWindow.COLUMNNAME_AD_CtxHelp_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Workflow)
sql.append(X_AD_Workflow.COLUMNNAME_AD_CtxHelp_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Task)
sql.append(X_AD_Task.COLUMNNAME_AD_CtxHelp_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Node)
sql.append(X_AD_WF_Node.COLUMNNAME_AD_CtxHelp_ID);
else
sql.append(X_AD_CtxHelp.COLUMNNAME_AD_CtxHelp_ID);
sql.append(" FROM ");
if (ctxType == X_AD_CtxHelp.CTXTYPE_Tab)
sql.append(X_AD_Tab.Table_Name);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Process)
sql.append(X_AD_Process.Table_Name);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Form)
sql.append(X_AD_Form.Table_Name);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Info)
sql.append(X_AD_InfoWindow.Table_Name);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Workflow)
sql.append(X_AD_Workflow.Table_Name);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Task)
sql.append(X_AD_Task.Table_Name);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Node)
sql.append(X_AD_WF_Node.Table_Name);
else
sql.append(X_AD_CtxHelp.Table_Name);
sql.append(" t, AD_CtxHelp h ");
sql.append("WHERE t.AD_CtxHelp_ID = h.AD_CtxHelp_ID ");
sql.append("AND t.IsActive = 'Y' ");
sql.append("AND h.IsActive = 'Y' ");
sql.append("AND h.AD_Client_ID IN (0, ?) ");
sql.append("AND h.AD_Org_ID IN (0, ?) ");
if (ctxType == X_AD_CtxHelp.CTXTYPE_Home)
sql.append("AND h." + X_AD_CtxHelp.COLUMNNAME_CtxType);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Tab || ctxType == X_AD_CtxHelp.CTXTYPE_Process ||
ctxType == X_AD_CtxHelp.CTXTYPE_Form || ctxType == X_AD_CtxHelp.CTXTYPE_Info ||
ctxType == X_AD_CtxHelp.CTXTYPE_Workflow || ctxType == X_AD_CtxHelp.CTXTYPE_Task ||
ctxType == X_AD_CtxHelp.CTXTYPE_Node)
{
sql.append("AND t.");
if (ctxType == X_AD_CtxHelp.CTXTYPE_Tab)
sql.append(X_AD_Tab.COLUMNNAME_AD_Tab_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Process)
sql.append(X_AD_Process.COLUMNNAME_AD_Process_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Form)
sql.append(X_AD_Form.COLUMNNAME_AD_Form_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Info)
sql.append(X_AD_InfoWindow.COLUMNNAME_AD_InfoWindow_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Workflow)
sql.append(X_AD_Workflow.COLUMNNAME_AD_Workflow_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Task)
sql.append(X_AD_Task.COLUMNNAME_AD_Task_ID);
else if (ctxType == X_AD_CtxHelp.CTXTYPE_Node)
sql.append(X_AD_WF_Node.COLUMNNAME_AD_WF_Node_ID);
}
else
sql.append("1");
sql.append(" = ? ");
sql.append("ORDER BY h.AD_Client_ID DESC, h.AD_Org_ID DESC, h.AD_CtxHelp_ID DESC");
return DB.getSQLValue(null, sql.toString(), Env.getAD_Client_ID(ctx), Env.getAD_Org_ID(ctx), ctxType == X_AD_CtxHelp.CTXTYPE_Home ? ctxType : recordId);
}
private String getCtxHelpMsgList(String ctxType, int recordId)
{
Properties ctx = Env.getCtx();
int AD_CtxHelp_ID = getCtxHelpID(ctxType, recordId);
if (AD_CtxHelp_ID > 0)
{
StringBuilder sql = new StringBuilder();
if (Env.isBaseLanguage(Env.getCtx(), X_AD_CtxHelpMsg.Table_Name))
{
sql.append("SELECT MsgText ");
sql.append("FROM AD_CtxHelpMsg ");
sql.append("WHERE IsActive = 'Y' ");
sql.append("AND AD_Client_ID IN (0, ?) ");
sql.append("AND AD_Org_ID IN (0, ?) ");
sql.append("AND AD_CtxHelp_ID = ? ");
sql.append("ORDER BY AD_Client_ID DESC, AD_Org_ID DESC, AD_CtxHelpMsg_ID DESC");
}
else
{
sql.append("SELECT mt.MsgText ");
sql.append("FROM AD_CtxHelpMsg m ");
sql.append("LEFT JOIN AD_CtxHelpMsg_Trl mt ON (mt.AD_CtxHelpMsg_ID = m.AD_CtxHelpMsg_ID AND mt.AD_Language = ?) ");
sql.append("WHERE mt.IsActive = 'Y' ");
sql.append("AND m.AD_Client_ID IN (0, ?) ");
sql.append("AND m.AD_Org_ID IN (0, ?) ");
sql.append("AND m.AD_CtxHelp_ID = ? ");
sql.append("ORDER BY m.AD_Client_ID DESC, m.AD_Org_ID DESC, m.AD_CtxHelpMsg_ID DESC");
}
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql.toString(), null);
int count = 1;
if (!Env.isBaseLanguage(Env.getCtx(), X_AD_CtxHelpMsg.Table_Name))
pstmt.setString(count++, Env.getAD_Language(Env.getCtx()));
pstmt.setInt(count++, Env.getAD_Client_ID(ctx));
pstmt.setInt(count++, Env.getAD_Org_ID(ctx));
pstmt.setInt(count++, AD_CtxHelp_ID);
rs = pstmt.executeQuery();
if (rs.next())
return rs.getString(X_AD_CtxHelpMsg.COLUMNNAME_MsgText);
}
catch (Exception e)
{
throw new AdempiereException(e);
}
finally
{
DB.close(rs, pstmt);
rs=null; pstmt=null;
}
}
return "";
String retValue = MCtxHelpMsg.get(ctx, ctxType, recordId);
return retValue;
}
/**