250 likes | 421 Views
Java ME persistence made easy! by Thiago Rossato Thiago 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 …
E N D
Java ME persistence made easy! by Thiago Rossato Thiago 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!
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!
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!
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!
Sample using RMS publicclassPerson { private String name; private Date birthday; privatechargender; (...) } Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
Sample using RMS public Person load(int id) { Person p; try { RecordStorers = RecordStore.openRecordStore(“Person”, true); byte[] data = rs.getRecord(id); ByteArrayInputStreambais = new ByteArrayInputStream(data); DataInputStreamdis = 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!
Sample using RMS public 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!
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!
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!
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!
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!
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; (...) } The markup interface identifiesthepersistable classes. Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
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; (...) } Transientandstaticfieldaren’tpersisted. Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
Entity Phone 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 Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
Saving Person p = new Person(); p.setName(...); p.setBirthday(...); p.setGender(...); p.setPhones(...); try { int id = PersistableManager.getInstance().save(p); } 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!
Saving Person p = new Person(); p.setName(...); p.setBirthday(...); p.setGender(...); p.setPhones(...); try { int id = PersistableManager.getInstance().save(p); } catch (FloggyException e) { (...) } Anunique ID is returnedaftersavinganobject. Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
Editing and saving Person person = new Person(); try { PersistableManager.getInstance().load(person, id); person.setName(...); id = PersistableManager.getInstance().save(person); } 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!
Editing and saving Person person = new Person(); try { PersistableManager.getInstance().load(person, id); person.setName(...); id = PersistableManager.getInstance().save(person); } catch (FloggyException e) { (...) } Use theunique ID to loadanobject. Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
Listing PersistableManager 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); (...) } Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
Filtering publicclassMaleFilterimplements net.sourceforge.floggy.persistence.Filter { publicboolean matches(Persistable persistable) { Person p = (Person) persistable; returnp.getGender() == 'M'; } } (...) ObjectSetpersons = pm.find(Person.class, newMaleFilter(), null); for (int i = 0; i < persons.size(); i++) { Person p = (Person) persons.get(i); (...) } Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!
Ordering publicclassAgeComparatorimplements net.sourceforge.floggy.persistence.Comparator { publicint 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; } } ObjectSetpersons = pm.find(Person.class, null, newAgeCompator()); 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 object try { PersistableManager.getInstance().delete(person); } catch (FloggyException e) {} • Removing all objects of a specified class try { PersistableManager.getInstance().deleteAll(Person.class); } catch (FloggyException e) {} • Removing all objects try { 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!
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!
Thanks! http://floggy.org Now you don't need to write hundred of lines of persistence code anymore. Floggy will do the dirty work for you!