Added OBDao.getOBObjectListFromString() method.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Thu, 03 Mar 2011 18:54:37 +0100
changeset 11006 b8168b32001d
parent 11005 3637e5ba01b9
child 11007 86775e3cc680
Added OBDao.getOBObjectListFromString() method.
src/org/openbravo/dal/service/OBDao.java
--- a/src/org/openbravo/dal/service/OBDao.java	Thu Mar 03 18:21:46 2011 +0100
+++ b/src/org/openbravo/dal/service/OBDao.java	Thu Mar 03 18:54:37 2011 +0100
@@ -18,9 +18,12 @@
  */
 package org.openbravo.dal.service;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.StringTokenizer;
 
+import org.apache.commons.lang.StringUtils;
 import org.hibernate.criterion.Expression;
 import org.openbravo.base.structure.BaseOBObject;
 
@@ -234,4 +237,31 @@
       }
     }
   }
+
+  /**
+   * Parses the string of comma separated id's to return a List with the BaseOBObjects of the given
+   * class. If there is an invalid id a null value is added to the List.
+   * 
+   * @param t
+   *          class of the BaseOBObject the id's belong to
+   * @param _IDs
+   *          String containing the comma separated list of id's
+   * @return a List object containing the parsed OBObjects
+   */
+  public static <T extends BaseOBObject> List<T> getOBObjectListFromString(Class<T> t, String _IDs) {
+    String strBaseOBOBjectIDs = _IDs;
+    final List<T> baseOBObjectList = new ArrayList<T>();
+    if (strBaseOBOBjectIDs.startsWith("(")) {
+      strBaseOBOBjectIDs = strBaseOBOBjectIDs.substring(1, strBaseOBOBjectIDs.length() - 1);
+    }
+    if (!strBaseOBOBjectIDs.equals("")) {
+      strBaseOBOBjectIDs = StringUtils.remove(strBaseOBOBjectIDs, "'");
+      StringTokenizer st = new StringTokenizer(strBaseOBOBjectIDs, ",", false);
+      while (st.hasMoreTokens()) {
+        String strBaseOBObjectID = st.nextToken().trim();
+        baseOBObjectList.add((T) OBDal.getInstance().get(t, strBaseOBObjectID));
+      }
+    }
+    return baseOBObjectList;
+  }
 }