Related to issue 33989: Code review improvements
authorAlvaro Ferraz <alvaro.ferraz@openbravo.com>
Mon, 19 Sep 2016 11:14:46 +0200
changeset 30552 018192ddfcb3
parent 30551 dcaf5195237a
child 30553 fa17d173e3ce
child 30556 17da7ad0ad62
Related to issue 33989: Code review improvements

- Avoid setting admin mode in getOrgLedgerRecursive() and getClientLedger() methods as it is already done in getOrgLedger() method in OBLedgerUtils class.
- Retrieve first, general ledger of login organization in LoginUtils class. If no result is retrieved, we will retrieve general ledger of rest of context organizations.
src/org/openbravo/base/secureApp/LoginUtils.java
src/org/openbravo/erpCommon/utility/OBLedgerUtils.java
--- a/src/org/openbravo/base/secureApp/LoginUtils.java	Wed Sep 14 13:39:12 2016 +0200
+++ b/src/org/openbravo/base/secureApp/LoginUtils.java	Mon Sep 19 11:14:46 2016 +0200
@@ -276,15 +276,28 @@
       vars.setSessionValue("#Client_SMTP", data[0].smtphost);
       data = null;
 
+      // Get General Ledger of login organization
       AttributeData[] attr = null;
-      String[] orgList = Utility.getContext(conn, vars, "#User_Org", "LoginHandler").split(",");
-      for (String orgId : orgList) {
-        String acctSchemaId = OBLedgerUtils.getOrgLedger(orgId.replace("'", ""));
-        if (StringUtils.isNotEmpty(acctSchemaId)) {
-          attr = AttributeData.selectAcctSchema(conn, acctSchemaId,
-              Utility.getContext(conn, vars, "#User_Client", "LoginHandler"));
-          if (ArrayUtils.isNotEmpty(attr)) {
-            break;
+      String acctSchemaId = OBLedgerUtils.getOrgLedger(strOrg);
+      if (StringUtils.isNotEmpty(acctSchemaId)) {
+        attr = AttributeData.selectAcctSchema(conn, acctSchemaId,
+            Utility.getContext(conn, vars, "#User_Client", "LoginHandler"));
+      }
+
+      // Get General Ledger of context organizations
+      if (ArrayUtils.isEmpty(attr)) {
+        String[] orgList = Utility.getContext(conn, vars, "#User_Org", "LoginHandler")
+            .replace("'", "").split(",");
+        for (String orgId : orgList) {
+          if (!StringUtils.equals(orgId, strOrg)) {
+            acctSchemaId = OBLedgerUtils.getOrgLedger(orgId);
+            if (StringUtils.isNotEmpty(acctSchemaId)) {
+              attr = AttributeData.selectAcctSchema(conn, acctSchemaId,
+                  Utility.getContext(conn, vars, "#User_Client", "LoginHandler"));
+              if (ArrayUtils.isNotEmpty(attr)) {
+                break;
+              }
+            }
           }
         }
       }
--- a/src/org/openbravo/erpCommon/utility/OBLedgerUtils.java	Wed Sep 14 13:39:12 2016 +0200
+++ b/src/org/openbravo/erpCommon/utility/OBLedgerUtils.java	Mon Sep 19 11:14:46 2016 +0200
@@ -54,28 +54,30 @@
         // No organization
         return null;
       }
+
       final Organization org = OBDal.getInstance().get(Organization.class, orgId);
       if (org == null) {
         // No organization
         return null;
       }
+
       String acctSchemaId = getOrgLedgerRecursive(org);
-      if (!StringUtils.isEmpty(acctSchemaId)) {
-        // Get ledger of organization tree
+      if (StringUtils.isNotEmpty(acctSchemaId)) {
+        // Get general ledger of organization tree
         return acctSchemaId;
       }
+
       String clientId = StringUtils.equals(orgId, "0") ? OBContext.getOBContext()
           .getCurrentClient().getId() : org.getClient().getId();
-      // Get client base Ledger
+      // Get client base general ledger
       return getClientLedger(clientId);
 
     } catch (Exception e) {
       log4j.error("Impossible to get ledger for organization id " + orgId, e);
+      return null;
     } finally {
       OBContext.restorePreviousMode();
     }
-
-    return null;
   }
 
   /**
@@ -89,43 +91,40 @@
    *         has a General Ledger defined.
    */
   private static String getOrgLedgerRecursive(Organization org) {
-    try {
-      OBContext.setAdminMode(true);
-      if (org.getGeneralLedger() != null) {
-        return org.getGeneralLedger().getId();
+
+    if (org.getGeneralLedger() != null) {
+      // Get general ledger of organization
+      return org.getGeneralLedger().getId();
+    }
+
+    if (StringUtils.equals(org.getId(), "0")) {
+      // * organization doesn't have parents
+      return null;
+    }
+
+    // Loop through parent organization list
+    OrganizationStructureProvider osp = OBContext.getOBContext().getOrganizationStructureProvider(
+        org.getClient().getId());
+    List<String> parentOrgIds = osp.getParentList(org.getId(), false);
+    for (String orgId : parentOrgIds) {
+      Organization parentOrg = OBDal.getInstance().get(Organization.class, orgId);
+      if (parentOrg.getGeneralLedger() != null) {
+        return parentOrg.getGeneralLedger().getId();
       }
-      if (org.getId().equals("0")) {
-        return null;
-      }
-      OrganizationStructureProvider osp = OBContext.getOBContext()
-          .getOrganizationStructureProvider(org.getClient().getId());
-      List<String> parentOrgIds = osp.getParentList(org.getId(), false);
-      for (String orgId : parentOrgIds) {
-        Organization parentOrg = OBDal.getInstance().get(Organization.class, orgId);
-        if (parentOrg.getGeneralLedger() != null) {
-          return parentOrg.getGeneralLedger().getId();
-        }
-      }
-      return null;
-    } finally {
-      OBContext.restorePreviousMode();
     }
+
+    return null;
   }
 
   private static String getClientLedger(String clientId) {
-    try {
-      OBContext.setAdminMode(true);
-      StringBuffer where = new StringBuffer();
-      where.append(" select " + AcctSchema.PROPERTY_ID);
-      where.append(" from " + AcctSchema.ENTITY_NAME);
-      where.append(" where " + AcctSchema.PROPERTY_CLIENT + ".id = :clientId");
-      where.append(" order by " + AcctSchema.PROPERTY_NAME);
-      Query qry = OBDal.getInstance().getSession().createQuery(where.toString());
-      qry.setParameter("clientId", clientId);
-      qry.setMaxResults(1);
-      return (String) qry.uniqueResult();
-    } finally {
-      OBContext.restorePreviousMode();
-    }
+    StringBuffer where = new StringBuffer();
+    where.append(" select " + AcctSchema.PROPERTY_ID);
+    where.append(" from " + AcctSchema.ENTITY_NAME);
+    where.append(" where " + AcctSchema.PROPERTY_CLIENT + ".id = :clientId");
+    where.append(" order by " + AcctSchema.PROPERTY_NAME);
+    Query qry = OBDal.getInstance().getSession().createQuery(where.toString());
+    qry.setParameter("clientId", clientId);
+    qry.setMaxResults(1);
+    return (String) qry.uniqueResult();
   }
 }