You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2014/10/29 06:31:42 UTC
svn commit: r1635049 - in
/ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity:
test/EntityTestSuite.java util/Converters.java
Author: adrianc
Date: Wed Oct 29 05:31:42 2014
New Revision: 1635049
URL: http://svn.apache.org/r1635049
Log:
Added GenericValue<->JSON convertes. Entity values can be serialized to JSON.
Modified:
ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java
ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/util/Converters.java
Modified: ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java?rev=1635049&r1=1635048&r2=1635049&view=diff
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java (original)
+++ ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/test/EntityTestSuite.java Wed Oct 29 05:31:42 2014
@@ -37,7 +37,9 @@ import java.util.concurrent.atomic.Atomi
import javax.sql.rowset.serial.SerialBlob;
import org.ofbiz.base.concurrent.ExecutionPool;
+import org.ofbiz.base.lang.JSON;
import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.ObjectType;
import org.ofbiz.base.util.Observable;
import org.ofbiz.base.util.Observer;
import org.ofbiz.base.util.UtilDateTime;
@@ -1209,6 +1211,23 @@ public class EntityTestSuite extends Ent
assertTrue("One big transaction was not faster than several small ones", totalTimeOneTransaction < totalTimeSeveralSmallTransactions);
}
+ public void testConverters() throws Exception {
+ // Must use the default delegator because the deserialized GenericValue can't
+ // find the randomized one.
+ Delegator localDelegator = DelegatorFactory.getDelegator("default");
+ GenericValue testValue = localDelegator.create("Testing", "testingId", "JSON_TEST", "testingTypeId", "TEST-UPDATE-1",
+ "description", "Testing JSON Converters", "testingSize", (long) 123, "testingDate",
+ new Timestamp(System.currentTimeMillis()));
+ assertNotNull("Created GenericValue not null", testValue);
+ JSON json = (JSON) ObjectType.simpleTypeConvert(testValue, "org.ofbiz.base.lang.JSON", null, null);
+ assertNotNull("JSON instance not null", json);
+ GenericValue convertedValue = (GenericValue) ObjectType.simpleTypeConvert(json, "org.ofbiz.entity.GenericValue", null,
+ null);
+ assertNotNull("GenericValue converted from JSON not null", convertedValue);
+ assertEquals("GenericValue converted from JSON equals original value", testValue, convertedValue);
+ testValue.remove();
+ }
+
private final class TestObserver implements Observer {
private Observable observable;
private Object arg;
Modified: ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/util/Converters.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/util/Converters.java?rev=1635049&r1=1635048&r2=1635049&view=diff
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/util/Converters.java (original)
+++ ofbiz/branches/json-integration-refactoring/framework/entity/src/org/ofbiz/entity/util/Converters.java Wed Oct 29 05:31:42 2014
@@ -18,19 +18,79 @@
*******************************************************************************/
package org.ofbiz.entity.util;
+import java.io.IOException;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import org.ofbiz.base.conversion.AbstractConverter;
import org.ofbiz.base.conversion.ConversionException;
import org.ofbiz.base.conversion.ConverterLoader;
+import org.ofbiz.base.lang.JSON;
+import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.UtilGenerics;
+import org.ofbiz.entity.Delegator;
+import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.entity.GenericValue;
+import org.ofbiz.entity.model.ModelField;
+import org.ofbiz.entity.model.ModelFieldType;
/** Entity Engine <code>Converter</code> classes. */
public class Converters implements ConverterLoader {
+
+ public static class JSONToGenericValue extends AbstractConverter<JSON, GenericValue> {
+ public JSONToGenericValue() {
+ super(JSON.class, GenericValue.class);
+ }
+
+ public GenericValue convert(JSON obj) throws ConversionException {
+ Map<String, Object> fieldMap;
+ try {
+ fieldMap = UtilGenerics.<Map<String, Object>>cast(obj.toObject(Map.class));
+ String delegatorName = (String) fieldMap.remove("_DELEGATOR_NAME_");
+ String entityName = (String) fieldMap.remove("_ENTITY_NAME_");
+ if (delegatorName == null || entityName == null) {
+ throw new ConversionException("Invalid JSON object");
+ }
+ Delegator delegator = DelegatorFactory.getDelegator(delegatorName);
+ GenericValue value = delegator.makeValue(entityName);
+ for (Map.Entry<String, Object> entry : fieldMap.entrySet()) {
+ String fieldName = entry.getKey();
+ Object fieldValue = entry.getValue();
+ ModelField field = value.getModelEntity().getField(fieldName);
+ ModelFieldType type = delegator.getEntityFieldType(value.getModelEntity(), field.getType());
+ value.set(fieldName, ObjectType.simpleTypeConvert(fieldValue, type.getJavaType(), null, null));
+ }
+ return value;
+ } catch (ConversionException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new ConversionException(e);
+ }
+ }
+ }
+
+ public static class GenericValueToJSON extends AbstractConverter<GenericValue, JSON> {
+ public GenericValueToJSON() {
+ super(GenericValue.class, JSON.class);
+ }
+
+ public JSON convert(GenericValue obj) throws ConversionException {
+ Map<String, Object> fieldMap = new HashMap<String, Object>(obj);
+ fieldMap.put("_DELEGATOR_NAME_", obj.getDelegator().getDelegatorName());
+ fieldMap.put("_ENTITY_NAME_", obj.getEntityName());
+ try {
+ return JSON.from(fieldMap);
+ } catch (IOException e) {
+ throw new ConversionException(e);
+ }
+ }
+ }
+
public static class GenericValueToList extends AbstractConverter<GenericValue, List<GenericValue>> {
public GenericValueToList() {
super(GenericValue.class, List.class);