Official way of extracting wrapper object for all model classes
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 22 Apr 2013 16:07:00 +0200
changeset 152c85a5c0fd0d
parent 14 828eb02a1509
child 16 fc6bdb2a2b98
Official way of extracting wrapper object for all model classes
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
json/src/main/java/org/apidesign/html/json/impl/WrapperObject.java
json/src/test/java/net/java/html/json/MapModelTest.java
json/src/test/java/org/apidesign/html/json/impl/JSONListTest.java
     1.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Mon Apr 22 13:52:00 2013 +0200
     1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Mon Apr 22 16:07:00 2013 +0200
     1.3 @@ -273,14 +273,18 @@
     1.4                  w.append("  };\n");
     1.5                  writeToString(props, w);
     1.6                  writeClone(className, props, w);
     1.7 -                w.write("public " + className + " applyBindings() {\n");
     1.8 -                w.write("  ko.applyBindings();\n");
     1.9 -                w.write("  return this;\n");
    1.10 +                w.write("  public " + className + " applyBindings() {\n");
    1.11 +                w.write("    ko.applyBindings();\n");
    1.12 +                w.write("    return this;\n");
    1.13 +                w.write("  }\n");
    1.14 +                w.write("  public boolean equals(Object o) {\n");
    1.15 +                w.write("    if (o == this) return true;\n");
    1.16 +                w.write("    if (o instanceof org.apidesign.html.json.impl.WrapperObject) {\n");
    1.17 +                w.write("      ((org.apidesign.html.json.impl.WrapperObject)o).setRealObject(intKnckt().koData());\n");
    1.18 +                w.write("    }\n");
    1.19 +                w.write("    return false;\n");
    1.20 +                w.write("  }\n");
    1.21                  w.write("}\n");
    1.22 -                w.append("  public Object koData() {\n");
    1.23 -                w.append("    return intKnckt().koData();\n");
    1.24 -                w.append("  }\n");
    1.25 -                w.append("}\n");
    1.26              } finally {
    1.27                  w.close();
    1.28              }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/WrapperObject.java	Mon Apr 22 16:07:00 2013 +0200
     2.3 @@ -0,0 +1,44 @@
     2.4 +/**
     2.5 + * HTML via Java(tm) Language Bindings
     2.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details. apidesign.org
    2.16 + * designates this particular file as subject to the
    2.17 + * "Classpath" exception as provided by apidesign.org
    2.18 + * in the License file that accompanied this code.
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License
    2.21 + * along with this program. Look for COPYING file in the top folder.
    2.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    2.23 + */
    2.24 +
    2.25 +package org.apidesign.html.json.impl;
    2.26 +
    2.27 +/** A way to extract real object from a model classes.
    2.28 + *
    2.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.30 + */
    2.31 +public final class WrapperObject {
    2.32 +    private Object ko;
    2.33 +    
    2.34 +    private WrapperObject() {
    2.35 +    }
    2.36 +    
    2.37 +    public void setRealObject(Object ko) {
    2.38 +        this.ko = ko;
    2.39 +    }
    2.40 +    
    2.41 +    
    2.42 +    public static Object find(Object model) {
    2.43 +        WrapperObject ro = new WrapperObject();
    2.44 +        model.equals(ro);
    2.45 +        return ro.ko;
    2.46 +    }
    2.47 +}
     3.1 --- a/json/src/test/java/net/java/html/json/MapModelTest.java	Mon Apr 22 13:52:00 2013 +0200
     3.2 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java	Mon Apr 22 16:07:00 2013 +0200
     3.3 @@ -24,6 +24,7 @@
     3.4  import java.lang.reflect.Method;
     3.5  import java.util.HashMap;
     3.6  import java.util.Map;
     3.7 +import org.apidesign.html.json.impl.WrapperObject;
     3.8  import org.apidesign.html.json.spi.ContextBuilder;
     3.9  import org.apidesign.html.json.spi.FunctionBinding;
    3.10  import org.apidesign.html.json.spi.PropertyBinding;
    3.11 @@ -49,7 +50,7 @@
    3.12          Person p = new Person(c);
    3.13          p.setFirstName("Jarda");
    3.14          
    3.15 -        Map m = (Map)p.koData();
    3.16 +        Map m = (Map)WrapperObject.find(p);
    3.17          Object v = m.get("firstName");
    3.18          assertNotNull(v, "Value should be in the map");
    3.19          assertEquals(v.getClass(), One.class, "It is instance of One");
    3.20 @@ -69,7 +70,7 @@
    3.21          p.setFirstName("Trans");
    3.22          p.setSex(Sex.MALE);
    3.23          
    3.24 -        Map m = (Map)p.koData();
    3.25 +        Map m = (Map)WrapperObject.find(p);
    3.26          Object o = m.get("changeSex");
    3.27          assertNotNull(o, "Function registered in the model");
    3.28  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/json/src/test/java/org/apidesign/html/json/impl/JSONListTest.java	Mon Apr 22 16:07:00 2013 +0200
     4.3 @@ -0,0 +1,91 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package org.apidesign.html.json.impl;
    4.25 +
    4.26 +import net.java.html.json.Context;
    4.27 +import net.java.html.json.People;
    4.28 +import net.java.html.json.Person;
    4.29 +import net.java.html.json.Sex;
    4.30 +import org.apidesign.html.json.spi.ContextBuilder;
    4.31 +import org.apidesign.html.json.spi.FunctionBinding;
    4.32 +import org.apidesign.html.json.spi.PropertyBinding;
    4.33 +import org.apidesign.html.json.spi.Technology;
    4.34 +import static org.testng.Assert.*;
    4.35 +import org.testng.annotations.Test;
    4.36 +
    4.37 +/**
    4.38 + *
    4.39 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.40 + */
    4.41 +public class JSONListTest implements Technology<Object> {
    4.42 +    
    4.43 +    public JSONListTest() {
    4.44 +    }
    4.45 +
    4.46 +    @Test public void testConvertorOnAnObject() {
    4.47 +        Context c = ContextBuilder.create().withTechnology(this).build();
    4.48 +        
    4.49 +        Person p = new Person(c);
    4.50 +        p.setFirstName("1");
    4.51 +        p.setLastName("2");
    4.52 +        p.setSex(Sex.MALE);
    4.53 +
    4.54 +        Object real = WrapperObject.find(p);
    4.55 +        assertEquals(this, real, "I am the right model");
    4.56 +    }
    4.57 +    
    4.58 +    @Test public void testConvertorOnAnArray() {
    4.59 +        Context c = ContextBuilder.create().withTechnology(this).build();
    4.60 +        
    4.61 +        Person p = new Person(c);
    4.62 +        p.setFirstName("1");
    4.63 +        p.setLastName("2");
    4.64 +        p.setSex(Sex.MALE);
    4.65 +        
    4.66 +        People arr = new People(c);
    4.67 +        arr.getInfo().add(p);
    4.68 +
    4.69 +        Object real = WrapperObject.find(p);
    4.70 +        assertEquals(this, real, "I am the right model");
    4.71 +    }
    4.72 +
    4.73 +    @Override
    4.74 +    public Object wrapModel(Object model) {
    4.75 +        return this;
    4.76 +    }
    4.77 +
    4.78 +    @Override
    4.79 +    public void bind(PropertyBinding b, Object model, Object data) {
    4.80 +    }
    4.81 +
    4.82 +    @Override
    4.83 +    public void valueHasMutated(Object data, String propertyName) {
    4.84 +    }
    4.85 +
    4.86 +    @Override
    4.87 +    public void expose(FunctionBinding fb, Object model, Object d) {
    4.88 +    }
    4.89 +
    4.90 +    @Override
    4.91 +    public void applyBindings(Object data) {
    4.92 +    }
    4.93 +    
    4.94 +}