From 8ef060d938afdd28966de9739c20e98b75330777 Mon Sep 17 00:00:00 2001 From: Nicolas Micoud <58596990+nmicoud@users.noreply.github.com> Date: Wed, 7 Jul 2021 10:44:07 +0200 Subject: [PATCH] IDEMPIERE-3732 : MRefList.getList cannot allow to choose order by (#746) * IDEMPIERE-3732 : MRefList.getList cannot allow to choose order by * @nmicoud IDEMPIERE-3732 : MRefList.getList cannot allow to choose order by - ASP filter Co-Authored-By: Carlos Ruiz --- .../org/compiere/model/MLookupFactory.java | 5 +- .../src/org/compiere/model/MRefList.java | 93 +++++++++++-------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MLookupFactory.java b/org.adempiere.base/src/org/compiere/model/MLookupFactory.java index 39a0712b75..e147bcf748 100644 --- a/org.adempiere.base/src/org/compiere/model/MLookupFactory.java +++ b/org.adempiere.base/src/org/compiere/model/MLookupFactory.java @@ -292,8 +292,9 @@ public class MLookupFactory if (s_log.isLoggable(Level.FINEST)) s_log.finest("Cache: " + retValue); return retValue.cloneIt(); } + + boolean orderByValue = MReference.get(AD_Reference_Value_ID).isOrderByValue(); - String byValue = DB.getSQLValueString(null, "SELECT IsOrderByValue FROM AD_Reference WHERE AD_Reference_ID = ? ", AD_Reference_Value_ID); StringBuilder realSQL = new StringBuilder ("SELECT NULL, AD_Ref_List.Value,"); MClient client = MClient.get(Env.getCtx()); StringBuilder AspFilter = new StringBuilder(); @@ -322,7 +323,7 @@ public class MLookupFactory String directSql = realSQL.toString() + " AND AD_Ref_List.Value=?"; realSQL.append(AspFilter.toString()); - if ("Y".equals(byValue)) + if (orderByValue) realSQL.append(" ORDER BY 2"); else realSQL.append(" ORDER BY 3"); // sort by name/translated name - teo_sarca, [ 1672820 ] diff --git a/org.adempiere.base/src/org/compiere/model/MRefList.java b/org.adempiere.base/src/org/compiere/model/MRefList.java index 678362ca9f..2e9851e84f 100644 --- a/org.adempiere.base/src/org/compiere/model/MRefList.java +++ b/org.adempiere.base/src/org/compiere/model/MRefList.java @@ -19,7 +19,6 @@ 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; @@ -27,6 +26,7 @@ import org.compiere.util.CCache; import org.compiere.util.CLogger; import org.compiere.util.DB; import org.compiere.util.Env; +import org.compiere.util.Util; import org.compiere.util.ValueNamePair; /** @@ -44,7 +44,7 @@ public class MRefList extends X_AD_Ref_List /** * */ - private static final long serialVersionUID = -3612793187620297377L; + private static final long serialVersionUID = 2342762307330992884L; /** * Get Reference List @@ -210,45 +210,58 @@ public class MRefList extends X_AD_Ref_List */ public static ValueNamePair[] getList (Properties ctx, int AD_Reference_ID, boolean optional) { - String ad_language = Env.getAD_Language(ctx); - boolean isBaseLanguage = Env.isBaseLanguage(ad_language, "AD_Ref_List"); - String sql = isBaseLanguage ? - "SELECT Value, Name FROM AD_Ref_List WHERE AD_Reference_ID=? AND IsActive='Y' ORDER BY Name" - : - "SELECT r.Value, t.Name FROM AD_Ref_List_Trl t" - + " INNER JOIN AD_Ref_List r ON (r.AD_Ref_List_ID=t.AD_Ref_List_ID)" - + " WHERE r.AD_Reference_ID=? AND t.AD_Language=? AND r.IsActive='Y'" - + " ORDER BY t.Name" - ; - PreparedStatement pstmt = null; - ResultSet rs = null; - ArrayList list = new ArrayList(); - if (optional) - list.add(new ValueNamePair("", "")); - try - { - pstmt = DB.prepareStatement(sql, null); - pstmt.setInt(1, AD_Reference_ID); - if (!isBaseLanguage) - pstmt.setString(2, ad_language); - rs = pstmt.executeQuery(); - while (rs.next()) - list.add(new ValueNamePair(rs.getString(1), rs.getString(2))); - } - catch (SQLException e) - { - s_log.log(Level.SEVERE, sql, e); - } - finally - { - DB.close(rs, pstmt); - rs = null; pstmt = null; - } - ValueNamePair[] retValue = new ValueNamePair[list.size()]; - list.toArray(retValue); - return retValue; - } // getList + return getList(ctx, AD_Reference_ID, optional, ""); + } // getList + /** + * Get Reference List (translated) + * @param ctx context + * @param AD_Reference_ID reference + * @param optional if true add "","" + * @param orderBy N-Name, V-Value, D-Default (IsOrderByValue) + * @return List or null + */ + public static ValueNamePair[] getList (Properties ctx, int AD_Reference_ID, boolean optional, String orderBy) { + + String language = Env.getAD_Language(ctx); + boolean orderByValue = MReference.get(AD_Reference_ID).isOrderByValue(); + if (Util.isEmpty(orderBy) || "N".equals(orderBy)) + orderByValue = false; + else if ("V".equals(orderBy)) + orderByValue = true; + StringBuilder sql = new StringBuilder ("SELECT AD_Ref_List.Value,"); + MClient client = MClient.get(Env.getCtx()); + StringBuilder AspFilter = new StringBuilder(); + if ( client.isUseASP() ) { + AspFilter.append(" AND AD_Ref_List.AD_Ref_List_ID NOT IN ( ") + .append(" SELECT li.AD_Ref_List_ID") + .append(" FROM ASP_Ref_List li") + .append(" INNER JOIN ASP_Level l ON ( li.ASP_Level_ID = l.ASP_Level_ID)") + .append(" INNER JOIN ASP_ClientLevel cl on (l.ASP_Level_ID = cl.ASP_Level_ID)") + .append(" INNER JOIN AD_Client c on (cl.AD_Client_ID = c.AD_Client_ID)") + .append(" WHERE li.AD_Reference_ID=").append(AD_Reference_ID) + .append(" AND li.IsActive='Y'") + .append(" AND c.AD_Client_ID=").append(client.getAD_Client_ID()) + .append(" AND li.ASP_Status='H')"); + } + + if (Env.isBaseLanguage(language, "AD_Ref_List")) + sql.append("AD_Ref_List.Name,AD_Ref_List.IsActive FROM AD_Ref_List "); + else + sql.append("trl.Name, AD_Ref_List.IsActive ") + .append("FROM AD_Ref_List INNER JOIN AD_Ref_List_Trl trl ") + .append(" ON (AD_Ref_List.AD_Ref_List_ID=trl.AD_Ref_List_ID AND trl.AD_Language='") + .append(language).append("')"); + sql.append(" WHERE AD_Ref_List.AD_Reference_ID=").append(AD_Reference_ID); + + sql.append(AspFilter.toString()); + if (orderByValue) + sql.append(" ORDER BY 1"); + else + sql.append(" ORDER BY 2"); + + return DB.getValueNamePairs(sql.toString(), optional, null); + } // getList /** Logger */ private static CLogger s_log = CLogger.getCLogger (MRefList.class);