You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by nm...@apache.org on 2016/07/05 19:06:52 UTC

svn commit: r1751530 - in /ofbiz/trunk/framework: entity/entitydef/entitymodel_test.xml service/servicedef/services_test_se.xml service/src/org/ofbiz/service/engine/EntityAutoEngine.java service/src/org/ofbiz/service/test/ServiceEntityAutoTests.java

Author: nmalin
Date: Tue Jul  5 19:06:52 2016
New Revision: 1751530

URL: http://svn.apache.org/viewvc?rev=1751530&view=rev
Log:
 Add new process on entity-auto for create invocation to populate automatically the field changeUserLoginId and statusDate for EntityStatus. The purpose is track userlogin for status change, and apply on all entity that cover the EntityStatus concept. The origin issue has been raise by Nameet Jain on OFBIZ-7611
This improvement add a control to not update or delete an EntityStatus by entity-auto (for help developer) and complete unit test

Modified:
    ofbiz/trunk/framework/entity/entitydef/entitymodel_test.xml
    ofbiz/trunk/framework/service/servicedef/services_test_se.xml
    ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/EntityAutoEngine.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/test/ServiceEntityAutoTests.java

Modified: ofbiz/trunk/framework/entity/entitydef/entitymodel_test.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/entitydef/entitymodel_test.xml?rev=1751530&r1=1751529&r2=1751530&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/entitydef/entitymodel_test.xml (original)
+++ ofbiz/trunk/framework/entity/entitydef/entitymodel_test.xml Tue Jul  5 19:06:52 2016
@@ -91,6 +91,26 @@ under the License.
             <key-map field-name="testingTypeId" />
         </view-link>
     </view-entity>
+    <!-- =========================================================
+      Used for testing EntityStatus concept on entity-auto
+    ========================================================= -->
+    <entity entity-name="TestingStatus"
+            package-name="org.ofbiz.entity.test"
+            title="Entity for testing EntityStatus concept">
+        <description>An entity for testing EntityStatus concept</description>
+        <field name="testingStatusId" type="id-ne"/>
+        <field name="testingId" type="id-ne" />
+        <field name="statusId" type="id-ne"/>
+        <field name="statusDate" type="date-time"/>
+        <field name="changeByUserLoginId" type="id-vlong"/>
+        <prim-key field="testingStatusId"/>
+        <relation type="one" fk-name="TEST_STA_STSITM" rel-entity-name="StatusItem">
+          <key-map field-name="statusId"/>
+        </relation>
+        <relation type="one" fk-name="TEST_STA_USRLGN" title="ChangeBy" rel-entity-name="UserLogin">
+           <key-map field-name="changeByUserLoginId" rel-field-name="userLoginId"/>
+        </relation>
+    </entity>
   <!-- =========================================================
      An entity for testing the BLOB data type
    ========================================================== -->

Modified: ofbiz/trunk/framework/service/servicedef/services_test_se.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/servicedef/services_test_se.xml?rev=1751530&r1=1751529&r2=1751530&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/servicedef/services_test_se.xml (original)
+++ ofbiz/trunk/framework/service/servicedef/services_test_se.xml Tue Jul  5 19:06:52 2016
@@ -100,6 +100,21 @@ under the License.
         <auto-attributes include="pk" mode="IN" optional="false"/>
         <attribute name="dateTimeField" mode="IN" type="Timestamp" optional="true"/>
     </service>
+    <service name="testEntityAutoCreateTestingStatus" auth="false"
+             engine="entity-auto" default-entity-name="TestingStatus" invoke="create">
+        <attribute name="testingId" type="String" mode="IN"/>
+        <attribute name="statusId" type="String" mode="IN"/>
+        <attribute name="testingStatusId" type="String" mode="OUT"/>
+    </service>
+    <service name="testEntityAutoUpdateTestingStatus" auth="true"
+             engine="entity-auto" default-entity-name="TestingStatus" invoke="update">
+        <auto-attributes include="pk" mode="IN" optional="false"/>
+        <attribute name="statusId" type="String" mode="IN"/>
+    </service>
+    <service name="testEntityAutoDeleteTestingStatus" auth="true"
+             engine="entity-auto" default-entity-name="TestingStatus" invoke="delete">
+        <auto-attributes include="pk" mode="IN" optional="false"/>
+    </service>
 
     <!-- lock wait timeout retry testing services - a scenario that we can't do automatically with the single service because the parent owns the tx we have to have end before it will succeed -->
     <service name="testServiceLockWaitTimeoutRetryCantRecover" engine="java" auth="false" transaction-timeout="2"

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/EntityAutoEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/EntityAutoEngine.java?rev=1751530&r1=1751529&r2=1751530&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/EntityAutoEngine.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/EntityAutoEngine.java Tue Jul  5 19:06:52 2016
@@ -321,6 +321,20 @@ public final class EntityAutoEngine exte
                 newEntity.set("changedDate", UtilDateTime.nowTimestamp());
             }
         }
+        if (modelEntity.getField("changeByUserLoginId") != null) {
+            GenericValue userLogin = (GenericValue) parameters.get("userLogin");
+            if (userLogin != null) {
+                newEntity.set("changeByUserLoginId", userLogin.get("userLoginId"));
+            } else {
+                throw new GenericServiceException("You call a creation on entity that require the userLogin to track the activity, please controle that your service definition has auth='true'");
+            }
+            //Oh changeByUserLoginId detected, check if an EntityStatus concept
+            if (modelEntity.getEntityName().endsWith("Status")) {
+                if (modelEntity.getField("statusDate") != null && parameters.get("statusDate") == null) {
+                    newEntity.set("statusDate", UtilDateTime.nowTimestamp());
+                }
+            }
+        }
         newEntity.create();
         result.put("crudValue", newEntity);
         return result;
@@ -418,6 +432,20 @@ public final class EntityAutoEngine exte
                 }
             }
         }
+
+        if (modelEntity.getField("changeByUserLoginId") != null ) {
+            if (modelEntity.getEntityName().endsWith("Status")) {
+                //Oh update on EntityStatus concept detected ... not possible, return invalid request
+                throw new GenericServiceException("You call a updating operation on entity that track the activity, sorry I can't do that, please amazing developer check your service definition ;)");
+            }
+            GenericValue userLogin = (GenericValue) parameters.get("userLogin");
+            if (userLogin != null) {
+                lookedUpValue.set("changeByUserLoginId", userLogin.get("userLoginId"));
+            } else {
+                throw new GenericServiceException("You call a updating operation on entity that track the activity, sorry I can't do that, please amazing developer check your service definition ;)");
+            }
+        }
+
         lookedUpValue.store();
         result.put("crudValue", lookedUpValue);
         return result;
@@ -438,6 +466,13 @@ public final class EntityAutoEngine exte
             throw new GenericServiceException("In Service [" + modelService.name + "] which uses the entity-auto engine with the delete invoke option not all pk fields have the mode IN");
         }
 
+        if (modelEntity.getField("changeByUserLoginId") != null ) {
+            if (modelEntity.getEntityName().endsWith("Status")) {
+                //Oh update on EntityStatus concept detected ... not possible, return invalid request
+                throw new GenericServiceException("You call a deleting operation on entity that track the activity, sorry I can't do that, please amazing developer check your service definition ;)");
+            }
+        }
+
         GenericValue lookedUpValue = PrimaryKeyFinder.runFind(modelEntity, parameters, dctx.getDelegator(), false, true, null, null);
         if (lookedUpValue == null) {
             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ServiceValueNotFoundForRemove", locale));

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/test/ServiceEntityAutoTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/test/ServiceEntityAutoTests.java?rev=1751530&r1=1751529&r2=1751530&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/test/ServiceEntityAutoTests.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/test/ServiceEntityAutoTests.java Tue Jul  5 19:06:52 2016
@@ -28,6 +28,7 @@ import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.entity.GenericValue;
 import org.ofbiz.entity.util.EntityQuery;
+import org.ofbiz.service.GenericServiceException;
 import org.ofbiz.service.ServiceUtil;
 import org.ofbiz.service.testtools.OFBizTestCase;
 
@@ -193,4 +194,47 @@ public class ServiceEntityAutoTests exte
         assertTrue(now.compareTo(testFieldType.getTimestamp("dateTimeField")) == 0);
     }
 
+
+    public void testEntityAutoEntityStatusConcept() throws Exception {
+        delegator.create("Testing", "testingId", "TESTING_7");
+        delegator.create("StatusType", "statusTypeId", "TESTINGSTATUS");
+        delegator.create("StatusItem", "statusId", "TESTING_CREATE", "statusTypeId", "TESTINGSTATUS");
+        delegator.create("StatusItem", "statusId", "TESTING_UPDATE", "statusTypeId", "TESTINGSTATUS");
+        GenericValue userLogin = delegator.findOne("UserLogin", true, "userLoginId", "system");
+
+        //test create testingStatus with userlogin
+        Map<String, Object> testingStatusCreateMap = UtilMisc.toMap("testingId", "TESTING_7", "statusId", "TESTING_CREATE", "userLogin", userLogin);
+        Map<String, Object> results = dispatcher.runSync("testEntityAutoCreateTestingStatus", testingStatusCreateMap);
+        assertTrue(ServiceUtil.isSuccess(results));
+        GenericValue testing = EntityQuery.use(delegator).from("TestingStatus").where("testingId", "TESTING_7").queryFirst();
+        assertNotNull(testing.getTimestamp("statusDate"));
+        assertEquals("system", testing.getString("changeByUserLoginId"));
+
+        //test create testingStatus without userLogin
+        try {
+            testingStatusCreateMap = UtilMisc.toMap("testingId", "TESTING_7", "statusId", "TESTING_CREATE");
+            results = dispatcher.runSync("testEntityAutoCreateTestingStatus", testingStatusCreateMap, 10, true);
+            assertTrue(ServiceUtil.isError(results));
+        } catch (GenericServiceException e) {
+            assertEquals(e.toString(),"You call a creation on entity that require the userLogin to track the activity, please controle that your service definition has auth='true'");
+        }
+
+        //test update testingStatus
+        try {
+            Map<String, Object> testingStatusUpdateMap = UtilMisc.toMap("testingStatusId", testing.get("testingStatusId"), "statusId", "TESTING_UPDATE", "userLogin", userLogin);
+            results = dispatcher.runSync("testEntityAutoUpdateTestingStatus", testingStatusUpdateMap, 10, true);
+            assertTrue(ServiceUtil.isError(results));
+        } catch (GenericServiceException e) {
+            assertEquals(e.toString(), "You call a updating operation on entity that track the activity, sorry I can't do that, please amazing developer check your service definition ;)");
+        }
+
+        //test delete testingStatus
+        try {
+            Map<String, Object> testingStatusDeleteMap = UtilMisc.toMap("testingStatusId", testing.get("testingStatusId"), "userLogin", userLogin);
+            results = dispatcher.runSync("testEntityAutoDeleteTestingStatus", testingStatusDeleteMap, 10, true);
+            assertTrue(ServiceUtil.isError(results));
+        } catch (GenericServiceException e) {
+            assertEquals(e.toString(), "You call a deleting operation on entity that track the activity, sorry I can't do that, please amazing developer check your service definition ;)");
+        }
+    }
 }