ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brandon Goodin" <brandon.goo...@gmail.com>
Subject Re: Mapping xmlbeans arrays using Ibatis
Date Mon, 17 Sep 2007 14:08:01 GMT
Wasn't this posted three days ago?

Brandon

On 9/17/07, Fábio Pisaruk <pisaruk@gmail.com> wrote:
>
> Hello
>
> For those one you that wish Ibatis to be capable of mapping xmlbeans
> arrays
> i´ve changed its code to do so and, as far as i´m concerned it´s working
> fine.
>
> There are two scenarios concerning xmlbean arrays:
>
> 1-) Map xmlbeans array property using nested resultMap.
>
> <resultMap id="resultMap-Person" class="Person">
>     <result property="name" column="nome">
>     <result property="addressArray" resultMap="resultMap-address">
> </resultMap>
>
> <select id="get-person" resultMap="resultMap-Person">
>        select * from person p ,address a where p.person_id=a.person_id and
> p.person_id=#value#
> </select>
>
> 2-) Map xmlbeans array property using nested select
> <resultMap id="resultMap-Person" class="Person">
>     <result property="name" column="nome">
>     <result property="addressArray" select="resultMap-address"
> column="person_id">
> </resultMap>
>
> <select id="get-person" resultMap="resultMap-Person">
>        select * from person p where p.person_id=a.person_id
> </select>
>
> <select id="get-addresses" resultMap="resultMap-Address">
>        select * from address where a.person_id=#value#
> </select>
>
>
> Both issues have been correct and now xmlbeans and ibatis have a smooth
> integration.
> To make it work you have to change the following methods of the class
> com.ibatis.common.beans.ComplexBeanProbe:
> - public void setObject(Object object, String name, Object value)
> - protected void setProperty(Object object, String name, Object value)
>
> Original Code:
>
>   public void setObject(Object object, String name, Object value) {
>     if (name.indexOf('.') > -1) {
>       StringTokenizer parser = new StringTokenizer(name, ".");
>       String property = parser.nextToken();
>       Object child = object;
>       while (parser.hasMoreTokens()) {
>         Class type = getPropertyTypeForSetter(child, property);
>         Object parent = child;
>         child = getProperty(parent, property);
>         if (child == null) {
>           if (value == null) {
>             return; // don't instantiate child path if value is null
>           } else {
>             try {
>               child = ResultObjectFactoryUtil.createObjectThroughFactory
> (type);
>               setObject(parent, property, child);
>             } catch (Exception e) {
>               throw new ProbeException("Cannot set value of property '" +
> name + "' because '" + property + "' is null and cannot be instantiated on
> instance of " + type.getName () + ". Cause:" + e.toString(), e);
>             }
>           }
>         }
>         property = parser.nextToken();
>       }
>       setProperty(child, property, value);
>     } else {
>       setProperty(object, name, value);
>     }
>   }
>
> protected void setProperty(Object object, String name, Object value) {
>     ClassInfo classCache = ClassInfo.getInstance(object.getClass());
>     try {
>       if (name.indexOf('[') > -1) {
>         setIndexedProperty(object, name, value);
>       } else {
>         if (object instanceof Map) {
>           ((Map) object).put(name, value);
>         } else {
>           Method method = classCache.getSetter (name);
>           if (method == null) {
>             throw new NoSuchMethodException("No SET method for property "
> + name + " on instance of " + object.getClass().getName());
>           }
>           Object[] params = new Object[1];
>           params[0] = value;
>           try {
>             method.invoke(object, params);
>           } catch (Throwable t) {
>             throw ClassInfo.unwrapThrowable(t);
>           }
>         }
>       }
>     } catch (ProbeException e) {
>       throw e;
>     } catch (Throwable t) {
>       if (object == null) {
>         throw new ProbeException("Could not set property '" + name + "'
> for null reference.  Cause: " + t.toString(), t);
>       } else {
>         throw new ProbeException("Could not set property '" + name + "'
> for " + object.getClass().getName() + ".  Cause: " + t.toString(), t);
>       }
>     }
>   }
>
> New code claiming to have correct the two issues exposed above:
>
> public void setObject(Object object, String name, Object value) {
>         if (name.indexOf('.') > -1) {
>             StringTokenizer parser = new StringTokenizer(name, ".");
>             String property = parser.nextToken();
>             Object child = object;
>             while (parser.hasMoreTokens()) {
>                 Class type = getPropertyTypeForSetter(child, property);
>                 Object parent = child;
>                 child = getProperty(parent, property);
>                 if (child == null)
>                     if (value == null)
>                         return; // don't instantiate child path if value
> is null
>                     else
>                         try {
>                             setObject(parent, property,
> ResultObjectFactoryUtil
>                                     .createObjectThroughFactory(type));
>                             child = getProperty(parent, property);
>                         } catch (Exception e) {
>                             throw new ProbeException(
>                                     "Cannot set value of property '"
>                                             + name
>                                             + "' because '"
>                                             + property
>                                             + "' is null and cannot be
> instantiated on instance of "
>                                             + type.getName() + ". Cause:"
>                                             + e.toString(), e);
>                         }
>                 property = parser.nextToken ();
>             }
>             setProperty(child, property, value);
>         } else
>             setProperty(object, name, value);
>     }
>
> protected void setProperty(Object object, String name, Object value) {
>         ClassInfo classCache = ClassInfo.getInstance(object.getClass());
>         try {
>             if (name.indexOf('[') > -1) {
>                 setIndexedProperty(object, name, value);
>             } else {
>                 if (object instanceof Map) {
>                     ((Map) object).put(name, value);
>                 } else {
>                     if (classCache.getSetter(name) == null) {
>                         throw new NoSuchMethodException(
>                                 "No SET method for property " + name
>                                         + " on instance of "
>                                         + object.getClass().getName());
>                     }
>                     if ((XmlObject[].class.isAssignableFrom(classCache
>                             .getSetter(name).getParameterTypes()[0]))
>                             && !value.getClass().isArray()) {
>                         String propertyName = name.substring(0, 1)
>                                 .toUpperCase().concat(name.substring(1));
>                         Method addNewMethod = object.getClass().getMethod(
>                                 "addNew"
>                                         + propertyName.substring (0,
>                                                 propertyName.length()
>                                                         -
> "Array".length()),
>                                 null);
>                         Method setIndexedMethod = object.getClass().getMethod(
>
>                                 "set" + propertyName,
>                                 new Class[] { int.class,
>                                         addNewMethod.getReturnType() });
>                         Method sizeOfMethod = object.getClass().getMethod(
>                                 "sizeOf" + propertyName, null);
>                         addNewMethod.invoke(object, null);
>                         setIndexedMethod
>                                 .invoke(object, new Object[] {
>                                         new Integer(((Integer)
> sizeOfMethod
>                                                 .invoke(object, null))
>                                                 .intValue() - 1), value
> });
>                     } else {
>                         try {
>                             classCache.getSetter(name).invoke(object,
>                                     new Object[] { value });
>                         } catch (Throwable t) {
>                             throw ClassInfo.unwrapThrowable(t);
>                         }
>                     }
>                 }
>             }
>         } catch (ProbeException e) {
>             throw e;
>         } catch (Throwable t) {
>             if (object == null) {
>                 throw new ProbeException("Could not set property '" + name
>                         + "' for null reference.  Cause: " + t.toString(),
> t);
>             } else {
>                 throw new ProbeException("Could not set property '" + name
>                         + "' for " + object.getClass().getName() + ".
> Cause: "
>                         + t.toString(), t);
>             }
>         }
>     }
>
> The most important changes were highlighted.
> For those of you interested i can give a detailed explanation about each
> change.
>
> That´s all.
>
> --
> Visto como se não executa logo a sentença sobre a má obra, o coração dos
> filhos dos homens está inteiramente disposto a praticar o mal.
>
>
> --Nerd´s sign
>
> If you have four classes, Everybody, Somebody, Anybody, and Nobody, if
> Somebody has a bug, it could be Anybody 's fault but Nobody really knows,
> while Everybody shares responsibility.
>
> "Programming today is a race between software engineers striving to build
> bigger and better idiot-proof programs, and the universe trying to build
> bigger and better idiots. So far, the universe is winning." - Rick Cook
>
> --
> Visto como se não executa logo a sentença sobre a má obra, o coração dos
> filhos dos homens está inteiramente disposto a praticar o mal.
>
>
> --Nerd´s sign
>
> If you have four classes, Everybody, Somebody, Anybody, and Nobody, if
> Somebody has a bug, it could be Anybody 's fault but Nobody really knows,
> while Everybody shares responsibility.
>
> "Programming today is a race between software engineers striving to build
> bigger and better idiot-proof programs, and the universe trying to build
> bigger and better idiots. So far, the universe is winning." - Rick Cook

Mime
View raw message