From a515c81c2db91920d1fc694aee53f5410cbcc9f5 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Wed, 4 Apr 2012 23:39:52 -0500 Subject: [PATCH] IDEMPIERE-129 Restrictions on Toolbar and Menu - peer review and apply best practices http://jira.idempiere.com/browse/IDEMPIERE-129 --- .../model/MToolBarButtonRestrict.java | 44 +++++++------------ .../src/org/compiere/apps/APanel.java | 15 +++---- 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MToolBarButtonRestrict.java b/org.adempiere.base/src/org/compiere/model/MToolBarButtonRestrict.java index 299e7b1f4b..d61ba84cbb 100644 --- a/org.adempiere.base/src/org/compiere/model/MToolBarButtonRestrict.java +++ b/org.adempiere.base/src/org/compiere/model/MToolBarButtonRestrict.java @@ -16,12 +16,8 @@ *****************************************************************************/ package org.compiere.model; -import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.Properties; -import java.util.logging.Level; import org.compiere.util.CLogger; import org.compiere.util.DB; @@ -34,7 +30,11 @@ import org.compiere.util.Env; */ public class MToolBarButtonRestrict extends X_AD_ToolBarButtonRestrict { - private static final long serialVersionUID = -422120961441035731L; + /** + * + */ + private static final long serialVersionUID = 751989571891306735L; + private static CLogger s_log = CLogger.getCLogger(MToolBarButtonRestrict.class); /** @@ -59,34 +59,20 @@ public class MToolBarButtonRestrict extends X_AD_ToolBarButtonRestrict super(ctx, rs, trxName); } // MToolBarButtonRestrict - /** Renvoie une liste des restrictions à appliquer en fonction du rôle, de la fenêtre, du formulaire, ... **/ - public static ArrayList getOf (Properties ctx, int AD_Role_ID, String Action, int Action_ID, String trxName) + /** Returns a list of restrictions to be applied according to the role, the window of the form ... **/ + public static int[] getOf (Properties ctx, int AD_Role_ID, String Action, int Action_ID, String className, String trxName) { // Action : R-Report, W-Window, X-form - ArrayList list = new ArrayList(); - String sql = "SELECT AD_ToolBarButton_ID FROM AD_ToolBarButtonRestrict WHERE IsActive = 'Y'" - + " AND AD_Client_ID IN (0, " + Env.getAD_Client_ID(ctx) + ")" - + " AND (AD_Role_ID IS NULL OR AD_Role_ID = " + AD_Role_ID + ")" - + " AND (Action IS NULL " - + " OR Action='" + Action + "' AND (AD_Window_ID IS NULL OR AD_Window_ID = " + Action_ID + "))"; - s_log.warning("sql="+sql); + + " AND AD_Client_ID IN (0, ?)" + + " AND (AD_Role_ID IS NULL OR AD_Role_ID = ?)" + + " AND (Action IS NULL OR Action=? AND (AD_Window_ID IS NULL OR (Action='W' AND AD_Window_ID=?)))" + + " AND AD_ToolBarButton_ID IN (SELECT AD_ToolBarButton_ID FROM AD_ToolBarButton WHERE IsActive='Y' AND Classname=?)"; + s_log.info("sql="+sql); + + int[] ids = DB.getIDsEx(trxName, sql, Env.getAD_Client_ID(ctx), AD_Role_ID, Action, Action_ID, className); - PreparedStatement pstmt = DB.prepareStatement(sql, trxName); - ResultSet rs; - try { - rs = pstmt.executeQuery(); - - while (rs.next()) - { - if (!list.contains(rs.getInt(1))) - list.add (rs.getInt(1)); - } - } catch (SQLException e) { - s_log.log(Level.SEVERE, sql, e); - } - - return list; + return ids; } // getOf /** diff --git a/org.adempiere.ui.swing/src/org/compiere/apps/APanel.java b/org.adempiere.ui.swing/src/org/compiere/apps/APanel.java index d3c5f2abe5..2dec7cb525 100644 --- a/org.adempiere.ui.swing/src/org/compiere/apps/APanel.java +++ b/org.adempiere.ui.swing/src/org/compiere/apps/APanel.java @@ -146,9 +146,9 @@ public final class APanel extends CPanel implements DataStatusListener, ChangeListener, ActionListener, IProcessMonitor { /** - * + * */ - private static final long serialVersionUID = 6066778919781303581L; + private static final long serialVersionUID = -253840959387736456L; private boolean isNested = false; private boolean ToolBarMenuRestictionLoaded = false; @@ -2918,17 +2918,16 @@ public final class APanel extends CPanel private void updateToolBarAndMenuWithRestriction() { - ArrayList restrictionList = new ArrayList(); int ToolBarButton_ID = 0; - restrictionList = MToolBarButtonRestrict.getOf(m_ctx, MRole.getDefault().getAD_Role_ID(), "W", m_window.getAD_Window_ID(), null); - log.warning("restrictionList="+restrictionList.toString()); + int[] restrictionList = MToolBarButtonRestrict.getOf(m_ctx, MRole.getDefault().getAD_Role_ID(), "W", m_window.getAD_Window_ID(), this.getClass().getName(), null); + log.info("restrictionList="+restrictionList.toString()); - for (int i = 0; i < restrictionList.size(); i++) + for (int i = 0; i < restrictionList.length; i++) { - ToolBarButton_ID= restrictionList.get(i); + ToolBarButton_ID= restrictionList[i]; - X_AD_ToolBarButton tbt = new X_AD_ToolBarButton(Env.getCtx(), ToolBarButton_ID, null); + X_AD_ToolBarButton tbt = new X_AD_ToolBarButton(m_ctx, ToolBarButton_ID, null); String restrictName = tbt.getComponentName(); log.config("tbt="+tbt.getAD_ToolBarButton_ID() + " / " + restrictName); boolean found=false;