details:   /erp/devel/pi/rev/0fbf88fbd3e9
changeset: 8548:0fbf88fbd3e9
user:      Martin Taal <martin.taal <at> openbravo.com>
date:      Fri Oct 15 15:44:15 2010 +0200
summary:   Fixes issue 14859: Add feature for REST delete request to select 
entities via HQL in URI instead of xml-document in request body

diffstat:

 src-test/org/openbravo/test/AllWebserviceTests.java      |   1 -
 src-test/org/openbravo/test/webservice/WSUpdateTest.java |  20 ++++++++-
 src/org/openbravo/dal/core/DalUtil.java                  |  20 +++++++++
 src/org/openbravo/dal/service/OBQuery.java               |  22 ++++++++++
 src/org/openbravo/service/rest/DalWebService.java        |  34 ++++++++++++++++
 5 files changed, 95 insertions(+), 2 deletions(-)

diffs (161 lines):

diff -r 9b99b3f27361 -r 0fbf88fbd3e9 
src-test/org/openbravo/test/AllWebserviceTests.java
--- a/src-test/org/openbravo/test/AllWebserviceTests.java       Fri Oct 15 
15:31:54 2010 +0200
+++ b/src-test/org/openbravo/test/AllWebserviceTests.java       Fri Oct 15 
15:44:15 2010 +0200
@@ -22,7 +22,6 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
-import org.openbravo.test.webservice.BaseWSTest;
 import org.openbravo.test.webservice.PerformanceTest;
 import org.openbravo.test.webservice.WSReadTest;
 import org.openbravo.test.webservice.WSUpdateTest;
diff -r 9b99b3f27361 -r 0fbf88fbd3e9 
src-test/org/openbravo/test/webservice/WSUpdateTest.java
--- a/src-test/org/openbravo/test/webservice/WSUpdateTest.java  Fri Oct 15 
15:31:54 2010 +0200
+++ b/src-test/org/openbravo/test/webservice/WSUpdateTest.java  Fri Oct 15 
15:44:15 2010 +0200
@@ -117,6 +117,20 @@
    * @throws Exception
    */
   public void testReadAddDeleteCity() throws Exception {
+    if (cityId == null) {
+      testACreateCity();
+    }
+    doTestReadAddDeleteCity(false);
+  }
+
+  public void testReadAddDeleteQueryCity() throws Exception {
+    if (cityId == null) {
+      testACreateCity();
+    }
+    doTestReadAddDeleteCity(true);
+  }
+
+  private void doTestReadAddDeleteCity(boolean doDeleteQuery) throws Exception 
{
     final String city = doTestGetRequest("/ws/dal/City/" + cityId, null, 200);
     String newCity = city.replaceAll("</name>", (System.currentTimeMillis() + 
"").substring(6)
         + "</name>");
@@ -165,7 +179,11 @@
     assertTrue(indexCity2 != -1);
 
     // delete it
-    doDirectDeleteRequest("/ws/dal/City/" + id, 200);
+    if (doDeleteQuery) {
+      doDirectDeleteRequest("/ws/dal/City?where=name='" + newName + "'", 200);
+    } else {
+      doDirectDeleteRequest("/ws/dal/City/" + id, 200);
+    }
 
     // sleep 1 seconds, so that the city is deleted
     Thread.sleep(1000);
diff -r 9b99b3f27361 -r 0fbf88fbd3e9 src/org/openbravo/dal/core/DalUtil.java
--- a/src/org/openbravo/dal/core/DalUtil.java   Fri Oct 15 15:31:54 2010 +0200
+++ b/src/org/openbravo/dal/core/DalUtil.java   Fri Oct 15 15:44:15 2010 +0200
@@ -31,6 +31,7 @@
 import org.hibernate.proxy.HibernateProxy;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.model.Property;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.base.structure.BaseOBObject;
@@ -106,6 +107,25 @@
   }
 
   /**
+   * Finds a property using the db column name and table name.
+   * 
+   * @param tableName
+   *          the table name, is used to find the {...@link Entity}
+   * @param dbColumnName
+   *          is used to find the {...@link Property}
+   * @return a Property or null if no property found
+   */
+  public static Property getProperty(String tableName, String dbColumnName) {
+    final Entity entity = 
ModelProvider.getInstance().getEntityByTableName(tableName);
+    for (Property property : entity.getProperties()) {
+      if (property.getColumnName().equalsIgnoreCase(dbColumnName)) {
+        return property;
+      }
+    }
+    return null;
+  }
+
+  /**
    * Translates a so-called property path to a property. The passed entity is 
the starting entity.
    * For example the property: organization.name and entity: Product will 
result in the
    * Organization.name property to be returned.
diff -r 9b99b3f27361 -r 0fbf88fbd3e9 src/org/openbravo/dal/service/OBQuery.java
--- a/src/org/openbravo/dal/service/OBQuery.java        Fri Oct 15 15:31:54 
2010 +0200
+++ b/src/org/openbravo/dal/service/OBQuery.java        Fri Oct 15 15:44:15 
2010 +0200
@@ -158,6 +158,28 @@
     return qryStr;
   }
 
+  public Query deleteQuery() {
+    final String qryStr = createQueryString();
+    String whereClause;
+    final int whereIndex = qryStr.toLowerCase().indexOf(WHERE);
+
+    if (whereIndex != -1) {
+      whereClause = qryStr.substring(whereIndex);
+    } else {
+      throw new OBException("Exception when creating delete query " + qryStr);
+    }
+
+    try {
+      final Query qry = getSession().createQuery(
+          "DELETE FROM " + getEntity().getName() + " " + whereClause);
+      setParameters(qry);
+      return qry;
+    } catch (final Exception e) {
+      throw new OBException("Exception when creating delete query " + "DELETE 
FROM "
+          + getEntity().getName() + " " + whereClause, e);
+    }
+  }
+
   /**
    * Creates a Hibernate Query object using the whereclause and extra filters 
(for readable
    * organizations etc.).
diff -r 9b99b3f27361 -r 0fbf88fbd3e9 
src/org/openbravo/service/rest/DalWebService.java
--- a/src/org/openbravo/service/rest/DalWebService.java Fri Oct 15 15:31:54 
2010 +0200
+++ b/src/org/openbravo/service/rest/DalWebService.java Fri Oct 15 15:44:15 
2010 +0200
@@ -290,6 +290,40 @@
       return;
     }
 
+    if (segments.length == 1) {
+      final String entityName = segments[0];
+
+      try {
+        ModelProvider.getInstance().getEntity(entityName);
+      } catch (final CheckException ce) {
+        throw new ResourceNotFoundException("Resource " + entityName + " not 
found", ce);
+      }
+
+      final String where = request.getParameter(PARAMETER_WHERE);
+      String whereOrderByClause = "";
+      if (where != null) {
+        whereOrderByClause += where;
+      }
+
+      try {
+        final OBQuery<BaseOBObject> obq = 
OBDal.getInstance().createQuery(entityName,
+            whereOrderByClause);
+
+        Object o = obq.deleteQuery().executeUpdate();
+
+        final String resultXml = 
WebServiceUtil.getInstance().createResultXMLWithLogWarning(
+            "Action performed successfully", "Removed business objects " + o, 
null);
+        response.setContentType("text/xml;charset=UTF-8");
+        final Writer w = response.getWriter();
+        w.write(resultXml);
+        w.close();
+      } catch (final Exception e) {
+        throw new OBException(e);
+      }
+
+      return;
+    }
+
     // use the content of the request
     doChangeAction(path, request, response, ChangeAction.DELETE);
   }

------------------------------------------------------------------------------
Download new Adobe(R) Flash(R) Builder(TM) 4
The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly 
Flex(R) Builder(TM)) enable the development of rich applications that run
across multiple browsers and platforms. Download your free trials today!
http://p.sf.net/sfu/adobe-dev2dev
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to