fixed issue 40591: deprecate OBDal methods to compose sql in clauses
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 11 Apr 2019 10:28:24 +0200
changeset 35687 17fff2db996a
parent 35686 b45635db7349
child 35688 c51687516a3d
fixed issue 40591: deprecate OBDal methods to compose sql in clauses

Bind parameters should be used instead.
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ADTreeDatasourceService.java
src-test/src/org/openbravo/test/dal/DalComplexQueryTestOrderLine.java
src-test/src/org/openbravo/test/dal/DalPerformanceExampleTest.java
src/org/openbravo/dal/service/OBDal.java
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ADTreeDatasourceService.java	Thu Apr 11 08:40:44 2019 +0200
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/ADTreeDatasourceService.java	Thu Apr 11 10:28:24 2019 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2013-2018 Openbravo SLU
+ * All portions are Copyright (C) 2013-2019 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -269,6 +269,7 @@
     return responseData;
   }
 
+  @SuppressWarnings("deprecation")
   private OBQuery<BaseOBObject> getNodeChildrenQuery(Map<String, String> parameters,
       String parentId, String hqlWhereClause, String hqlWhereClauseRootNodes, Tab tab, Tree tree,
       Entity entity) throws JSONException {
--- a/src-test/src/org/openbravo/test/dal/DalComplexQueryTestOrderLine.java	Thu Apr 11 08:40:44 2019 +0200
+++ b/src-test/src/org/openbravo/test/dal/DalComplexQueryTestOrderLine.java	Thu Apr 11 10:28:24 2019 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2018 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -28,6 +28,7 @@
 import org.hibernate.Session;
 import org.hibernate.query.Query;
 import org.junit.Test;
+import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
 import org.openbravo.model.common.order.OrderLine;
@@ -197,9 +198,8 @@
     whereClause.append(" and ol.salesOrder.documentStatus='CO' ");
 
     // Add the readable organization and client clauses
-    whereClause
-        .append(" and ol.organization " + OBDal.getInstance().getReadableOrganizationsInClause());
-    whereClause.append(" and ol.client " + OBDal.getInstance().getReadableClientsInClause());
+    whereClause.append(" and ol.organization in :orgs");
+    whereClause.append(" and ol.client in :clients");
 
     // append active
     whereClause.append(" and ol.active=true");
@@ -220,10 +220,12 @@
 
     log.debug(hql);
 
-    // final Session session = OBDal.getInstance().getSession();
-    // session.createQuery(hql.toString());
-    final Query<Object[]> query = OBDal.getInstance().getSession().createQuery(hql, Object[].class);
-    query.setParameter("bpId", "1000017");
+    final Query<Object[]> query = OBDal.getInstance()
+        .getSession()
+        .createQuery(hql, Object[].class)
+        .setParameter("bpId", "1000017")
+        .setParameterList("orgs", OBContext.getOBContext().getReadableOrganizations())
+        .setParameterList("clients", OBContext.getOBContext().getReadableClients());
 
     for (Object[] os : query.list()) {
       for (Object result : os) {
--- a/src-test/src/org/openbravo/test/dal/DalPerformanceExampleTest.java	Thu Apr 11 08:40:44 2019 +0200
+++ b/src-test/src/org/openbravo/test/dal/DalPerformanceExampleTest.java	Thu Apr 11 10:28:24 2019 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2012-2018 Openbravo SLU 
+ * All portions are Copyright (C) 2012-2019 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -32,6 +32,7 @@
 import org.junit.Test;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.dal.core.DalUtil;
+import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
@@ -250,13 +251,13 @@
       int i = 0;
       // for joining referenced objects we can't use OBQuery, but have to
       // use a direct Hibernate query object
-      final String queryStr = "from BusinessPartner as bp left join bp.businessPartnerCategory where bp.organization.id "
-          + OBDal.getInstance().getReadableOrganizationsInClause();
+      final String queryStr = "from BusinessPartner as bp left join bp.businessPartnerCategory where bp.organization.id in :orgs";
 
       final Query<Object[]> qry = OBDal.getInstance()
           .getSession()
-          .createQuery(queryStr, Object[].class);
-      qry.setMaxResults(1000);
+          .createQuery(queryStr, Object[].class)
+          .setParameterList("orgs", OBContext.getOBContext().getReadableOrganizations())
+          .setMaxResults(1000);
       final ScrollableResults scroller = qry.scroll(ScrollMode.FORWARD_ONLY);
       while (scroller.next()) {
         final BusinessPartner bp = (BusinessPartner) scroller.get()[0];
--- a/src/org/openbravo/dal/service/OBDal.java	Thu Apr 11 08:40:44 2019 +0200
+++ b/src/org/openbravo/dal/service/OBDal.java	Thu Apr 11 10:28:24 2019 +0200
@@ -690,7 +690,9 @@
    * 
    * @return an in-clause which can be directly used inside of a HQL clause
    * @see OBContext#getReadableOrganizations()
+   * @deprecated Use bind statement parameter instead of this method
    */
+  @Deprecated
   public String getReadableOrganizationsInClause() {
     return createInClause(OBContext.getOBContext().getReadableOrganizations());
   }
@@ -702,7 +704,9 @@
    * 
    * @return an in-clause which can be directly used inside of a HQL clause
    * @see OBContext#getReadableClients()
+   * @deprecated Use bind statement parameter instead of this method
    */
+  @Deprecated
   public String getReadableClientsInClause() {
     return createInClause(OBContext.getOBContext().getReadableClients());
   }