floggy-m3dd-2009-01-21

Post on 21-Jan-2015

738 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation used on the M3DD conference in Santa Clara, CA

TRANSCRIPT

Java ME persistence made easy!by

Thiago RossatoThiago Moreira

About Us• Thiago Moreira

– Works with …• Java since 2001• Java ME since 2002

– SCJP, SCMAD, SCDJWS and SCDBC• Thiago Rossato

– Works with …• Java since 1999• Java ME since 2002

– SCJP and SCMAD• Priscila Lugon

– Works with …• Java ME since 2004

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 2

About Floggy

• What is Floggy?– Floggy is a 100% object-oriented open source

persistence framework for Java ME / MIDP applications.

– Floggy allows developers to work with high-level persistence commands.

• What Floggy is not?– A database for Java ME applications.

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 3

RMS and JSR-75

• How to store data when developing MIDP applications?– RMS (Record Management System)• Implementation is mandatory: in most cases is the only

option.

– JSR 75 adds file system support• Implementation is optional: not all devices support this

JSR.

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 4

RMS

• Supported by all MIDP devices• Simple and functional API• Modeled after a simple record-oriented

database• Data is manipulated as byte arrays

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 5

Sample using RMS

public class Person { private String name;private Date birthday;private char gender;

(...)}

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 6

Sample using RMSpublic Person load(int id) {

Person p;

try {RecordStore rs = RecordStore.openRecordStore(“Person”, true);byte[] data = rs.getRecord(id);ByteArrayInputStream bais = new ByteArrayInputStream(data);DataInputStream dis = new DataInputStream(bais);

try {p = new Person();p.setName(dis.readUTF());p.setBirthday(new Date(dis.readLong()));p.setGender(dis.readChar());dis.close();

} catch (IOException e) {}

rs.closeRecordStore();} catch (RecordStoreException e) {}

return p;}

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 7

Sample using RMSpublic void save(Person p) {

byte[] data = null;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DataOutputStream dos = new DataOutputStream(baos);

try {

dos.writeUTF(p.getName());

dos.writeLong(p.getBirthday().getTime());

dos.writeChar(p.getGender());

data = baos.toByteArray();

dos.close();

} catch (IOException e) {}

RecordStore rs = null;

try {

rs = RecordStore.openRecordStore(“Person”, true);

int id = rs.addRecord(data, 0, data.length);

rs.closeRecordStore();

} catch (RecordStoreException e) {}

}

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 8

How Floggy works?

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 9

Structure• Package net.sourceforge.floggy.persistence• Persistence– Persistable– PersistableManager

• Search– ObjsetSet– Filter– Comparator

• Exception– FloggyException

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 10

IDEs

• Eclipse– Floggy Eclipse Plugin

• Netbeans• Ant• Maven• Command Line

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 11

import net.sourceforge.floggy.persistence.Persistable;

public class Person implements Persistable {

private String name;

private Date birthday;

private char gender;

private Phone[] phones;

private transient int age;

public static int SOME_STATIC_FIELD = 1;

(...)

}

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 12

Entity Person

import net.sourceforge.floggy.persistence.Persistable;

public class Person implements Persistable {

private String name;

private Date birthday;

private char gender;

private Phone[] phones;

private transient int age;

public static int SOME_STATIC_FIELD = 1;

(...)

}

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 13

Entity Person

The markup interface identifies the

persistable classes.

import net.sourceforge.floggy.persistence.Persistable;

public class Person implements Persistable {

private String name;

private Date birthday;

private char gender;

private Phone[] phones;

private transient int age;

public static int SOME_STATIC_FIELD = 1;

(...)

}

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 14

Entity Person

Transient and static field aren’t persisted.

import net.sourceforge.floggy.persistence.Persistable;

public class Phone implements Persistable {

private String number;

private String extension;

private int type; // Mobile, Home, Work, etc

(...)

}

15

Entity Phone

15 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

SavingPerson p = new Person();p.setName(...);p.setBirthday(...);p.setGender(...);p.setPhones(...);

try {

int id = PersistableManager.getInstance().save(p);

} catch (FloggyException e) {(...)

}

16 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

SavingPerson p = new Person();p.setName(...);p.setBirthday(...);p.setGender(...);p.setPhones(...);

try {

int id = PersistableManager.getInstance().save(p);

} catch (FloggyException e) {(...)

}

An unique ID is returned after saving

an object.

17 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

Editing and savingPerson person = new Person();

try {

PersistableManager.getInstance().load(person, id);

person.setName(...);

id = PersistableManager.getInstance().save(person);

} catch (FloggyException e) {(...)

}

18 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

Editing and savingPerson person = new Person();

try {

PersistableManager.getInstance().load(person, id);

person.setName(...);

id = PersistableManager.getInstance().save(person);

} catch (FloggyException e) {(...)

}

Use the unique ID to load an object.

19 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

ListingPersistableManager pm = PersistableManager.getInstance();ObjectSet persons = pm.find(Person.class, null, null);for (int i = 0; i < persons.size(); i++) { Person p = (Person) persons.get(i); (...)}

An optimized way ...

Person person = new Person();PersistableManager pm = PersistableManager.getInstance();ObjectSet persons = pm.find(Person.class, null, null);for (int i = 0; i < persons.size(); i++) { persons.get(i, person); (...)}

20 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

Filteringpublic class MaleFilter implements net.sourceforge.floggy.persistence.Filter {

public boolean matches(Persistable persistable) { Person p = (Person) persistable; return p.getGender() == 'M'; }}

(...)

ObjectSet persons = pm.find(Person.class, new MaleFilter(), null);

for (int i = 0; i < persons.size(); i++) { Person p = (Person) persons.get(i); (...)}

21 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

Orderingpublic class AgeComparator implements net.sourceforge.floggy.persistence.Comparator {

public int compare(Persistable o1, Persistable o2) { Person p1 = (Person) o1; Person p2 = (Person) o2; if (p1.getBirthday().getTime() < p2.getBirthday().getTime()) {

return PRECEDES;}

if (p1.getBirthday().getTime() > p2.getBirthday().getTime()) {

return FOLLOWS; }

return EQUIVALENT;}

}

ObjectSet persons = pm.find(Person.class, null, new AgeCompator());

22 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!

Removing operations

• Removing a single objecttry {

PersistableManager.getInstance().delete(person);} catch (FloggyException e) {}

• Removing all objects of a specified classtry {

PersistableManager.getInstance().deleteAll(Person.class);} catch (FloggyException e) {}

• Removing all objectstry {

PersistableManager.getInstance().deleteAll();

} catch (FloggyException e) {}

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 23

Information

• Site– Documentation– Roadmap– FAQ

• Mailing lists at SF.net– floggy-users@lists.sourceforge.net

• Tracker at SF.net– Bugs– Feature requests

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 24

Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you! 25

Thanks!

http://floggy.org

top related