Search in sources :

Example 1 with EntityType

use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.

the class ResultParser method parseJSON.

public EntityList parseJSON(Reader json, EntityList dest, EntityType rowType) throws IOException {
    JSONParser jsonParser = new JSONParser();
    Map result = jsonParser.parseJSON(json);
    List list = (List) result.get("root");
    dest.setRowType(entityType);
    parse(list, dest);
    return dest;
}
Also used : JSONParser(com.codename1.io.JSONParser) EntityList(com.codename1.rad.models.EntityList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with EntityType

use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.

the class MultiButtonEntityView method update.

@Override
public void update() {
    EntityType type = getEntity().getEntity().getEntityType();
    boolean changed = false;
    if (line1PropDirty) {
        line1PropDirty = false;
        line1Prop = line1Prop == null ? type.findProperty(line1, Thing.name) : line1Prop;
        if (line1Prop != null) {
            String text = type.getText(line1Prop, entity.getEntity());
            if (!Objects.equals(text, getTextLine1())) {
                setTextLine1(text);
                changed = true;
            }
        }
    }
    if (line2PropDirty) {
        line2PropDirty = false;
        line2Prop = line2Prop == null ? type.findProperty(line2, Thing.description) : line2Prop;
        if (line2Prop != null) {
            String text = type.getText(line2Prop, entity.getEntity());
            if (!Objects.equals(text, getTextLine2())) {
                setTextLine2(text);
                changed = true;
            }
        }
    }
    if (line3PropDirty) {
        line3PropDirty = false;
        line3Prop = line3Prop == null ? type.findProperty(line3) : line3Prop;
        if (line3Prop != null) {
            String text = type.getText(line3Prop, entity.getEntity());
            if (!Objects.equals(text, getTextLine3())) {
                setTextLine3(text);
                changed = true;
            }
        }
    }
    if (line4PropDirty) {
        line4PropDirty = false;
        line4Prop = line4Prop == null ? type.findProperty(line4) : line4Prop;
        if (line4Prop != null) {
            String text = type.getText(line4Prop, entity.getEntity());
            if (!Objects.equals(text, getTextLine4())) {
                setTextLine4(text);
                changed = true;
            }
        }
    }
    if (iconDirty) {
        iconDirty = false;
        Property iconProp = this.iconProp != null ? this.iconProp : type.findProperty(icon);
        this.iconProp = iconProp;
        if (iconProp != null) {
            Object iconData = iconProp.getValue(getEntity().getEntity());
            if (iconData != null) {
                IconRendererAttribute iconRendererAtt = (IconRendererAttribute) viewNode.findInheritedAttribute(IconRendererAttribute.class);
                EntityImageRenderer iconRenderer = iconRendererAtt == null ? new DefaultEntityImageRenderer() : iconRendererAtt.getValue();
                iconRenderer.createImage(this, iconProp, 0, false, false).ready(im -> {
                    setIcon(im);
                    revalidateLater();
                });
            }
        }
    }
    if (changed) {
        revalidateLater();
    }
}
Also used : EntityType(com.codename1.rad.models.EntityType) EntityImageRenderer(com.codename1.rad.ui.image.EntityImageRenderer) DefaultEntityImageRenderer(com.codename1.rad.ui.image.DefaultEntityImageRenderer) IconRendererAttribute(com.codename1.rad.attributes.IconRendererAttribute) ViewProperty(com.codename1.rad.ui.ViewProperty) Property(com.codename1.rad.models.Property) DefaultEntityImageRenderer(com.codename1.rad.ui.image.DefaultEntityImageRenderer)

Example 3 with EntityType

use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.

the class PropertySelectorTest method manualExampleTest.

private void manualExampleTest() throws Exception {
    String xml = "<?xml version=\"1.0\"?>\n" + "<catalog>\n" + "   <book id=\"bk101\">\n" + "      <author>Gambardella, Matthew</author>\n" + "      <title>XML Developer's Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>44.95</price>\n" + "      <publish_date>2000-10-01</publish_date>\n" + "      <description>An in-depth look at creating applications \n" + "      with XML.</description>\n" + "   </book>\n" + "   <book id=\"bk102\">\n" + "      <author>Ralls, Kim</author>\n" + "      <title>Midnight Rain</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-12-16</publish_date>\n" + "      <description>A former architect battles corporate zombies, \n" + "      an evil sorceress, and her own childhood to become queen \n" + "      of the world.</description>\n" + "   </book>\n" + "   <book id=\"bk103\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Maeve Ascendant</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2000-11-17</publish_date>\n" + "      <description>After the collapse of a nanotechnology \n" + "      society in England, the young survivors lay the \n" + "      foundation for a new society.</description>\n" + "   </book>\n" + "   <book id=\"bk104\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>Oberon's Legacy</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-03-10</publish_date>\n" + "      <description>In post-apocalypse England, the mysterious \n" + "      agent known only as Oberon helps to create a new life \n" + "      for the inhabitants of London. Sequel to Maeve \n" + "      Ascendant.</description>\n" + "   </book>\n" + "   <book id=\"bk105\">\n" + "      <author>Corets, Eva</author>\n" + "      <title>The Sundered Grail</title>\n" + "      <genre>Fantasy</genre>\n" + "      <price>5.95</price>\n" + "      <publish_date>2001-09-10</publish_date>\n" + "      <description>The two daughters of Maeve, half-sisters, \n" + "      battle one another for control of England. Sequel to \n" + "      Oberon's Legacy.</description>\n" + "   </book>\n" + "   <book id=\"bk106\">\n" + "      <author>Randall, Cynthia</author>\n" + "      <title>Lover Birds</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-09-02</publish_date>\n" + "      <description>When Carla meets Paul at an ornithology \n" + "      conference, tempers fly as feathers get ruffled.</description>\n" + "   </book>\n" + "   <book id=\"bk107\">\n" + "      <author>Thurman, Paula</author>\n" + "      <title>Splish Splash</title>\n" + "      <genre>Romance</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>A deep sea diver finds true love twenty \n" + "      thousand leagues beneath the sea.</description>\n" + "   </book>\n" + "   <book id=\"bk108\">\n" + "      <author>Knorr, Stefan</author>\n" + "      <title>Creepy Crawlies</title>\n" + "      <genre>Horror</genre>\n" + "      <price>4.95</price>\n" + "      <publish_date>2000-12-06</publish_date>\n" + "      <description>An anthology of horror stories about roaches,\n" + "      centipedes, scorpions  and other insects.</description>\n" + "   </book>\n" + "   <book id=\"bk109\">\n" + "      <author>Kress, Peter</author>\n" + "      <title>Paradox Lost</title>\n" + "      <genre>Science Fiction</genre>\n" + "      <price>6.95</price>\n" + "      <publish_date>2000-11-02</publish_date>\n" + "      <description>After an inadvertant trip through a Heisenberg\n" + "      Uncertainty Device, James Salway discovers the problems \n" + "      of being quantum.</description>\n" + "   </book>\n" + "   <book id=\"bk110\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>Microsoft .NET: The Programming Bible</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-09</publish_date>\n" + "      <description>Microsoft's .NET initiative is explored in \n" + "      detail in this deep programmer's reference.</description>\n" + "   </book>\n" + "   <book id=\"bk111\">\n" + "      <author>O'Brien, Tim</author>\n" + "      <title>MSXML3: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>36.95</price>\n" + "      <publish_date>2000-12-01</publish_date>\n" + "      <description>The Microsoft MSXML3 parser is covered in \n" + "      detail, with attention to XML DOM interfaces, XSLT processing, \n" + "      SAX and more.</description>\n" + "   </book>\n" + "   <book id=\"bk112\">\n" + "      <author>Galos, Mike</author>\n" + "      <title>Visual Studio 7: A Comprehensive Guide</title>\n" + "      <genre>Computer</genre>\n" + "      <price>49.95</price>\n" + "      <publish_date>2001-04-16</publish_date>\n" + "      <description>Microsoft Visual Studio 7 is explored in depth,\n" + "      looking at how Visual Basic, Visual C++, C#, and ASP+ are \n" + "      integrated into a comprehensive development \n" + "      environment.</description>\n" + "   </book>\n" + "</catalog>";
    EntityType bookType = new EntityTypeBuilder().string(Thing.identifier).string(Thing.name).string(Thing.description).build();
    class Book extends BaseEntity {
    }
    EntityType.register(Book.class, bookType, cls -> {
        return new Book();
    });
    class Books extends EntityList<Book> {
    }
    EntityType.registerList(Books.class, Book.class, cls -> {
        return new Books();
    });
    Tag BOOKS = new Tag("Books");
    EntityType catalogType = new EntityTypeBuilder().list(Books.class, BOOKS).build();
    class Catalog extends BaseEntity {

        {
            setEntityType(catalogType);
        }
    }
    EntityType.register(Catalog.class, cls -> {
        return new Catalog();
    });
    ResultParser parser = new ResultParser(catalogType).property("./book", BOOKS).entityType(bookType).property("@id", Thing.identifier).property("title", Thing.name).property("description", Thing.description);
    Catalog catalog = (Catalog) parser.parseXML(xml, new Catalog());
    Books books = (Books) catalog.get(BOOKS);
    assertEqual("bk101", books.get(0).get(Thing.identifier));
    assertEqual("bk112", books.get(books.size() - 1).get(Thing.identifier));
    assertEqual("XML Developer's Guide", books.get(0).get(Thing.name));
    PropertySelector id1 = new PropertySelector(catalog, BOOKS).child(0, Thing.identifier);
    PropertySelector name1 = new PropertySelector(catalog, BOOKS).child(0, Thing.name);
    assertEqual("bk101", id1.getText(null));
    assertEqual("XML Developer's Guide", name1.getText(null));
    assertNull(propertySelector(catalog, BOOKS).child(Thing.name).getText(null));
    class Stats {

        int counter;
    }
    Stats stats = new Stats();
    name1.addPropertyChangeListener(evt -> {
        stats.counter++;
    });
    assertEqual(0, stats.counter);
    boolean res = name1.setText("Foo");
    assertTrue(res);
    assertEqual("Foo", name1.getText(null));
    assertEqual(1, stats.counter);
    Books newBooks = new Books();
    catalog.set(BOOKS, newBooks);
    assertEqual(2, stats.counter);
    assertNull(name1.getText(null));
    Book newBook = new Book();
    newBook.setText(Thing.name, "Bar");
    newBooks.add(newBook);
    assertEqual("Bar", name1.getText(null));
    assertEqual(3, stats.counter);
    newBook.setText(Thing.name, "Bar2");
    assertEqual(4, stats.counter);
    assertEqual("Bar2", name1.getText(null));
    name1.setText("Bar3");
    assertEqual("Bar3", newBook.getText(Thing.name));
    assertEqual("Bar3", name1.getText(null));
    assertEqual(5, stats.counter);
    books.get(0).set(Thing.name, "Changed again");
    assertEqual("Bar3", name1.getText(null));
    assertEqual("Changed again", books.get(0).getText(Thing.name));
    assertEqual(5, stats.counter);
    Book newBook2 = new Book();
    newBook2.setText(Thing.name, "newBook2");
    newBooks.add(newBook2);
    assertEqual(5, stats.counter);
    assertEqual("Bar3", name1.getText(null));
    newBooks.remove(newBook);
    assertEqual(6, stats.counter);
    assertEqual("newBook2", name1.getText(null));
}
Also used : ResultParser(com.codename1.rad.io.ResultParser)

Example 4 with EntityType

use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.

the class ResultParserTest method simpleJSONTest.

private void simpleJSONTest() throws Exception {
    EntityType personType = new EntityTypeBuilder().string(Person.name).string(Person.email).Date(Person.birthDate).build();
    ResultParser parser = new ResultParser(personType).property("name", Person.name).property("email", Person.email).property("dob", Person.birthDate, dateStr -> {
        if (!(dateStr instanceof String)) {
            return null;
        }
        String str = (String) dateStr;
        if (str.isEmpty()) {
            return null;
        }
        SimpleDateFormat fmt = new SimpleDateFormat("MMM d, yyyy");
        try {
            return fmt.parse(str);
        } catch (ParseException ex) {
            Log.e(ex);
            return null;
        }
    });
    String json = "{\"name\":\"Paul\", \"email\":\"paul@example.com\", \"dob\" : \"December 27, 1978\"}";
    Entity person = parser.parseRow(Result.fromContent(json, Result.JSON), personType.newInstance());
    assertEqual("Paul", person.getEntity().getText(Person.name));
    assertEqual("paul@example.com", person.getEntity().getText(Person.email));
}
Also used : Entity(com.codename1.rad.models.Entity) ResultParser(com.codename1.rad.io.ResultParser) ParseException(com.codename1.l10n.ParseException) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat)

Example 5 with EntityType

use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.

the class ResultParserTest method dateFormatXMLTest.

private void dateFormatXMLTest() throws Exception {
    EntityType personType = new EntityTypeBuilder().string(Person.name).string(Person.email).Date(Person.birthDate).build();
    ResultParser parser = new ResultParser(personType).property("/person/name", Person.name).property("/person/email", Person.email).property("/person/dob", Person.birthDate, new SimpleDateFormat("MMM d, yyyy"));
    String json = "<person><name>Paul</name> <email>paul@example.com</email> <dob>December 27, 1978</dob></person>";
    XMLParser xparser = new XMLParser();
    Element root = xparser.parse(new StringReader("<?xml version='1.0'?>\n" + json));
    Entity person = parser.parseRow(Result.fromContent(root), personType.newInstance());
    assertEqual("Paul", person.getEntity().getText(Person.name));
    assertEqual("paul@example.com", person.getEntity().getText(Person.email));
}
Also used : Entity(com.codename1.rad.models.Entity) Element(com.codename1.xml.Element) StringReader(java.io.StringReader) ResultParser(com.codename1.rad.io.ResultParser) XMLParser(com.codename1.xml.XMLParser) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat)

Aggregations

ResultParser (com.codename1.rad.io.ResultParser)9 Entity (com.codename1.rad.models.Entity)9 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)7 Element (com.codename1.xml.Element)6 Property (com.codename1.rad.models.Property)5 XMLParser (com.codename1.xml.XMLParser)5 StringReader (java.io.StringReader)5 List (java.util.List)5 Map (java.util.Map)5 ParseException (com.codename1.l10n.ParseException)4 Log (com.codename1.io.Log)3 ParsingService (com.codename1.rad.io.ParsingService)3 PropertyParserCallback (com.codename1.rad.io.ResultParser.PropertyParserCallback)3 ResultParser.resultParser (com.codename1.rad.io.ResultParser.resultParser)3 com.codename1.rad.models (com.codename1.rad.models)3 BaseEntity.entityTypeBuilder (com.codename1.rad.models.BaseEntity.entityTypeBuilder)3 Result (com.codename1.rad.processing.Result)3 Person (com.codename1.rad.schemas.Person)3 Product (com.codename1.rad.schemas.Product)3 Thing (com.codename1.rad.schemas.Thing)3