Search in sources :

Example 1 with Name

use of com.codename1.rad.models.Property.Name 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 2 with Name

use of com.codename1.rad.models.Property.Name 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)

Example 3 with Name

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

the class ResultParserTest method dateFormatTest.

private void dateFormatTest() 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, new SimpleDateFormat("MMM d, yyyy"));
    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) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat)

Example 4 with Name

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

the class ContentType method createObjectType.

public static <V> ContentType<V> createObjectType(Class<V> representationClass) {
    if (representationClass == Entity.class) {
        return (ContentType<V>) EntityType;
    }
    if (representationClass == EntityList.class) {
        return (ContentType<V>) EntityListType;
    }
    return new ContentType<V>(new Name(representationClass.getName()), representationClass) {

        private boolean isEntity;

        private boolean isEntityList;

        {
            isEntity = Entity.class.isAssignableFrom(representationClass);
            isEntityList = EntityList.class.isAssignableFrom(representationClass);
            ;
        }

        @Override
        public boolean equals(Object obj) {
            return obj.getClass() == this.getClass() && ((ContentType) obj).getRepresentationClass() == representationClass;
        }

        @Override
        public boolean isEntity() {
            return isEntity;
        }

        @Override
        public boolean isEntityList() {
            return isEntityList;
        }

        @Override
        public int hashCode() {
            return representationClass.hashCode();
        }

        @Override
        public boolean canConvertFrom(ContentType otherType) {
            return representationClass.isAssignableFrom(otherType.representationClass);
        }

        @Override
        public <U> V from(ContentType<U> otherType, U data) {
            if (representationClass.isAssignableFrom(otherType.representationClass)) {
                return (V) data;
            }
            return super.from(otherType, data);
        }

        @Override
        public <U> U to(ContentType<U> otherType, V data) {
            if (otherType.representationClass.isAssignableFrom(representationClass)) {
                return (U) data;
            }
            return super.to(otherType, data);
        }
    };
}
Also used : Name(com.codename1.rad.models.Property.Name)

Example 5 with Name

use of com.codename1.rad.models.Property.Name in project CodenameOne by codenameone.

the class Message method sendMessageViaCloudSync.

/**
 * <p>Send an email message using the Codename One cloud to send the message, notice that this API
 * will only work for pro accounts.</p>
 * <script src="https://gist.github.com/codenameone/8229c1d4627ab3a1f17e.js"></script>
 *
 * @param sender the name of the sender, notice all email will arrive from Codename One to avoid spam issues
 * @param recipient the email for the recipient
 * @param recipientName the display name for the recipient
 * @param subject e-mail subject
 * @param plainTextBody when sending an HTML message you should also attach a plain text fallback message,
 * this is redundant if the email is a plain text message to begin with
 * @return true if sending succeeded
 */
public boolean sendMessageViaCloudSync(String sender, String recipient, String recipientName, String subject, String plainTextBody) {
    ConnectionRequest r = createMessage(sender, recipient, recipientName, subject, plainTextBody);
    r.setFailSilently(true);
    NetworkManager.getInstance().addToQueueAndWait(r);
    return r.getResposeCode() == 200;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Aggregations

IOException (java.io.IOException)36 Component (com.codename1.ui.Component)17 Hashtable (java.util.Hashtable)17 ArrayList (java.util.ArrayList)16 AnimationObject (com.codename1.ui.animations.AnimationObject)15 File (java.io.File)14 Form (com.codename1.ui.Form)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 FileInputStream (java.io.FileInputStream)13 InputStream (java.io.InputStream)12 Label (com.codename1.ui.Label)11 Button (com.codename1.ui.Button)10 Container (com.codename1.ui.Container)10 EncodedImage (com.codename1.ui.EncodedImage)10 TextArea (com.codename1.ui.TextArea)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)10 Point (java.awt.Point)10 FileOutputStream (java.io.FileOutputStream)10 Paint (android.graphics.Paint)8 BufferedInputStream (com.codename1.io.BufferedInputStream)7