Tutorial: Using POJO

Android Using POJO

Plain Old Java Objects

You can use your own java objects with Webcom.

Your object class must meet the 3 following requirements:

  1. The class MUST NOT be an inner class,
  2. The class MUST have a default zero-arguments constructor,
  3. The class MUST define public getters/setters for the properties to serialize/deserialize.
class Contact {
    String birthday;
    String firstName;
    String lastName;
    String phoneNumber;
    String email;
    String address;

    public Contact() {
    }

    public Contact(String birthday, String email, String firstName, String lastName, @Nullable String phoneNumber, @Nullable String address) {
        this.birthday = birthday;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }
    
    ...

    public String getAddress() {
        return address;
    }

    public Contact setAddress(String address) {
        this.address = address;
        return this;
    }

    public String getBirthday() {
        return birthday;
    }

    public Contact setBirthday(String birthday) {
        this.birthday = birthday;
        return this;
    }

    public String getEmail() {
        return email;
    }

    ...
}

Writing data

The set() method will create a "contact" object into the database with the firstName, lastName, phoneNumber, email and birthday properties.

Webcom adbk = new Webcom("webcom-addressbook").child("contacts");
Contact john = new Contact("October 9, 1940", "johnandyoko@apple.com", "John", "Lennon", null, null);
adbk.child('lennon').set(john);
Contact paul = new Contact("June 18, 1942", "paulo@apple.com", "Paul", "McCartney", "020 1234 6541", null);
adbk.child('macca').set(paul);

The data is saved in the database like this:

{
  "contacts": {
    "macca": {
      "birthday": "June 18, 1942",
      "firstName": "Paul",
      "lastName": "McCartney",
      "phoneNumber": "020 1234 6541",
      "email": "paulo@apple.com"
 	},
    "lennon": {
      "birthday": "October 9, 1940",
      "firstName": "John",
      "lastName": "Lennon",
      "email": "johnandyoko@apple.com"
 	}
  }
}

POJO objects can also be used with update() and push(). However, since all properties of your POJO objects are used to serialize your object into JSON formal, update() is similar to set().

Reading data

Your POJO class can also be used when reading data. The value() method is able to use your POJO directly.

Webcom adbk = new Webcom("https://io.datasync.orange.com/base/webcom-addressbook/contacts/macca");
adbk.once(Query.Event.VALUE, new OnQuery(){
  @Override
  public void onComplete(DataSnapshot snapData, @Nullable String prevName) {
    // code to handle current data
    Contact value = snapData.value(Contact.class);
  }
  @Override
  public void onCancel(WebcomError error) {
    // event subscription was canceled because of permission reason
  }
  @Override
  public void onError(WebcomError error) {
    // an error occured
  }
});

You can also retrieve a Map and/or List of your POJO:

Webcom adbk = new Webcom("https://io.datasync.orange.com/base/webcom-addressbook/contacts/");
adbk.once(Query.Event.VALUE, new OnQuery(){
  @Override
  public void onComplete(DataSnapshot snapData, @Nullable String prevName) {
    // Get values as a Map
    Map<String, Contact> values = snapData.valueMap(Contact.class);
    Contact macca = values.get("macca");
    // Get values as a Map
    List<Contact> valueList = snapData.valueList(Contact.class);
    // If you have named keys for your data, this method is not recommended 
    // since you will lose the path information for your values and thus won't
    // be able to perform updates without an additional request
    
  }
  @Override
  public void onCancel(WebcomError error) {
    // event subscription was canceled because of permission reason
  }
  @Override
  public void onError(WebcomError error) {
    // an error occured
  }
});