# HG changeset patch
# User Asier Lostalé <asier.lostale@openbravo.com>
# Date 1567609017 -7200
#      Wed Sep 04 16:56:57 2019 +0200
# Node ID f0d1349a8c179456875df9c89550bf50bb52115d
# Parent  50ffd29b565678f890272fa340cfe07b7218b3e3
fixes xxxx

  Small code improvements in CheckAppovals:
    1. Store approvals as a Set instead of composing a String: it is intended to
       check if the set of approvals contains a concrete one. It is more efficient
       to lookup in a Set instead of in a String.
    2. Create approvals object only if needed
    3. Using primitive int type instead of Integer type prevents boxing + unboxing.
    4. There is no need to create a temporary preferenceQuery object to immediatly
       execute the query.

diff --git a/src/org/openbravo/retail/posterminal/utility/CheckApproval.java b/src/org/openbravo/retail/posterminal/utility/CheckApproval.java
--- a/src/org/openbravo/retail/posterminal/utility/CheckApproval.java
+++ b/src/org/openbravo/retail/posterminal/utility/CheckApproval.java
@@ -11,6 +11,7 @@ package org.openbravo.retail.posterminal
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Optional;
@@ -30,7 +31,6 @@ import org.codehaus.jettison.json.JSONAr
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.hibernate.criterion.Restrictions;
-import org.hibernate.query.Query;
 import org.openbravo.base.secureApp.AllowedCrossDomainsHandler;
 import org.openbravo.base.secureApp.PasswordHash;
 import org.openbravo.dal.core.OBContext;
@@ -91,10 +91,6 @@ public class CheckApproval extends HttpS
         result.put("error", jsonError);
       } else {
         String supervisorId = supervisor.map(User::getId).get();
-        String approvals = "'" + approvalType.getString(0) + "'";
-        for (int i = 1; i < approvalType.length(); i++) {
-          approvals = approvals + ",'" + approvalType.getString(i) + "'";
-        }
 
         Set<String> naturalTreeOrgList = OBContext.getOBContext()
             .getOrganizationStructureProvider(client)
@@ -111,14 +107,14 @@ public class CheckApproval extends HttpS
             + "   or p.visibleAtOrganization.id in (:orgList) "
             + "   or p.visibleAtOrganization is null) group by p.property";
 
-        Query<String> preferenceQuery = OBDal.getInstance()
+        List<String> preferenceList = OBDal.getInstance()
             .getSession()
-            .createQuery(hqlQuery, String.class);
-        preferenceQuery.setParameter("user", supervisorId);
-        preferenceQuery.setParameter("org", organization);
-        preferenceQuery.setParameterList("orgList", naturalTreeOrgList);
+            .createQuery(hqlQuery, String.class)
+            .setParameter("user", supervisorId)
+            .setParameter("org", organization)
+            .setParameterList("orgList", naturalTreeOrgList)
+            .list();
 
-        List<String> preferenceList = preferenceQuery.list();
         if (preferenceList.isEmpty()) {
           result.put("status", 1);
           JSONObject jsonError = new JSONObject();
@@ -129,7 +125,13 @@ public class CheckApproval extends HttpS
           result.put("status", 0);
           JSONObject jsonData = new JSONObject();
           JSONObject jsonPreference = new JSONObject();
-          Integer c = 0;
+
+          Set<String> approvals = new HashSet<>(approvalType.length());
+          for (int i = 0; i < approvalType.length(); i++) {
+            approvals.add(approvalType.getString(i));
+          }
+
+          int c = 0;
           for (String preference : preferenceList) {
             jsonPreference.put(preference, preference);
             if (approvals.contains(preference)) {
