svn commit: r833964 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/conversion/Converters.java base/src/org/ofbiz/base/conversion/MiscConverters.java entity/src/org/ofbiz/entity/GenericValue.java entity/src/org/ofbiz/entity/util/Converters.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r833964 - in /ofbiz/trunk/framework: base/src/org/ofbiz/base/conversion/Converters.java base/src/org/ofbiz/base/conversion/MiscConverters.java entity/src/org/ofbiz/entity/GenericValue.java entity/src/org/ofbiz/entity/util/Converters.java

adrianc
Author: adrianc
Date: Mon Nov  9 01:48:23 2009
New Revision: 833964

URL: http://svn.apache.org/viewvc?rev=833964&view=rev
Log:
Added Java type converters for the entity engine.

This commit demonstrates how the object type converter framework can be extended by other components.

Added:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java   (with props)
Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java?rev=833964&r1=833963&r2=833964&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java Mon Nov  9 01:48:23 2009
@@ -19,6 +19,7 @@
 package org.ofbiz.base.conversion;
 
 import java.math.BigDecimal;
+import java.sql.Clob;
 import java.util.Collection;
 import java.util.List;
 import java.util.Locale;
@@ -62,6 +63,7 @@
     public static final Converter<Boolean, String> BooleanToString = new BooleanToString();
     public static final Converter<Calendar, Long> CalendarToLong = new CalendarToLong();
     public static final Converter<Calendar, String> CalendarToString = new CalendarToString();
+    public static final Converter<Clob, String> ClobToString = new ClobToString();
     public static final Converter<java.util.Date, Long> DateToLong = new DateToLong();
     public static final LocalizedConverter<java.util.Date, String> DateToString = new DateToString();
     public static final Converter<Double, BigDecimal> DoubleToBigDecimal = new DoubleToBigDecimal();

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java?rev=833964&r1=833963&r2=833964&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java Mon Nov  9 01:48:23 2009
@@ -18,6 +18,9 @@
  *******************************************************************************/
 package org.ofbiz.base.conversion;
 
+import java.io.IOException;
+import java.io.Reader;
+import java.sql.Clob;
 import java.util.Locale;
 
 import org.ofbiz.base.util.UtilMisc;
@@ -25,6 +28,8 @@
 /** Miscellaneous Converter classes. */
 public class MiscConverters {
 
+    public static final int CHAR_BUFFER_SIZE = 4096;
+
     public static class LocaleToString extends AbstractConverter<Locale, String> {
 
         public String convert(Locale obj) throws ConversionException {
@@ -62,4 +67,39 @@
 
     }
 
+    public static class ClobToString extends AbstractConverter<Clob, String> {
+
+        public String convert(Clob obj) throws ConversionException {
+            StringBuilder strBuf = new StringBuilder();
+            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
+            int charsRead = 0;
+            Reader clobReader = null;
+            try {
+                clobReader =  obj.getCharacterStream();
+                while ((charsRead = clobReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) {
+                    strBuf.append(inCharBuffer, 0, charsRead);
+                }
+            } catch (Exception e) {
+                throw new ConversionException(e);
+            }
+            finally {
+                if (clobReader != null) {
+                    try {
+                        clobReader.close();
+                    } catch (IOException e) {}
+                }
+            }
+            return strBuf.toString();
+        }
+
+        public Class<Clob> getSourceClass() {
+            return Clob.class;
+        }
+
+        public Class<String> getTargetClass() {
+            return String.class;
+        }
+
+    }
+
 }

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java?rev=833964&r1=833963&r2=833964&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/GenericValue.java Mon Nov  9 01:48:23 2009
@@ -23,11 +23,13 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javolution.context.ObjectFactory;
 import javolution.lang.Reusable;
 import javolution.util.FastMap;
 
+import org.ofbiz.base.conversion.Converter;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilValidate;
@@ -36,6 +38,7 @@
 import org.ofbiz.entity.model.ModelEntity;
 import org.ofbiz.entity.model.ModelKeyMap;
 import org.ofbiz.entity.model.ModelRelation;
+import org.ofbiz.entity.util.Converters.*;
 import org.ofbiz.entity.util.EntityUtil;
 
 
@@ -48,6 +51,10 @@
 
     public static final GenericValue NULL_VALUE = new NullGenericValue();
 
+    public static final Converter<GenericValue, List<GenericValue>> GenericValueToList = new GenericValueToList();
+    public static final Converter<GenericValue, Set<GenericValue>> GenericValueToSet = new GenericValueToSet();
+    public static final Converter<GenericValue, String> GenericValueToString = new GenericValueToString();
+
     protected static final ObjectFactory<GenericValue> genericValueFactory = new ObjectFactory<GenericValue>() {
         @Override
         protected GenericValue create() {

Added: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java?rev=833964&view=auto
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java (added)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java Mon Nov  9 01:48:23 2009
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.entity.util;
+
+import java.util.List;
+import java.util.Set;
+
+import javolution.util.FastList;
+import javolution.util.FastSet;
+
+import org.ofbiz.base.conversion.AbstractConverter;
+import org.ofbiz.base.conversion.ConversionException;
+import org.ofbiz.entity.GenericValue;
+
+/** Entity Engine <code>Converter</code> classes. */
+public class Converters {
+
+    public static class GenericValueToList extends AbstractConverter<GenericValue, List<GenericValue>> {
+
+        public List<GenericValue> convert(GenericValue obj) throws ConversionException {
+            List<GenericValue> tempList = FastList.newInstance();
+            tempList.add(obj);
+            return tempList;
+        }
+
+        public Class<GenericValue> getSourceClass() {
+            return GenericValue.class;
+        }
+
+        @SuppressWarnings("unchecked")
+        public Class<List> getTargetClass() {
+            return List.class;
+        }
+
+    }
+
+    public static class GenericValueToSet extends AbstractConverter<GenericValue, Set<GenericValue>> {
+
+        public Set<GenericValue> convert(GenericValue obj) throws ConversionException {
+            Set<GenericValue> tempSet = FastSet.newInstance();
+            tempSet.add(obj);
+            return tempSet;
+        }
+
+        public Class<GenericValue> getSourceClass() {
+            return GenericValue.class;
+        }
+
+        @SuppressWarnings("unchecked")
+        public Class<Set> getTargetClass() {
+            return Set.class;
+        }
+
+    }
+
+    public static class GenericValueToString extends AbstractConverter<GenericValue, String> {
+
+        public String convert(GenericValue obj) throws ConversionException {
+            return obj.toString();
+        }
+
+        public Class<GenericValue> getSourceClass() {
+            return GenericValue.class;
+        }
+
+        public Class<String> getTargetClass() {
+            return String.class;
+        }
+
+    }
+
+}

Propchange: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/Converters.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain