Poetry


Source link: https://github.com/elastique/poetry

Poetry

Poetry is an Android persistence library that allows you to persist a JSON object tree directly into an SQLite database through OrmLite. Poetry enables you to write less code and persist data much faster.

Consider this JSON:

{
  "id" : 1,  "name" : "John Doe" 
}

And this Java model:

@DatabaseTable class User {
  @DatabaseField(id = true)  public int id;
@DatabaseField)  public String name; 
}

They can be stored into the database like this:

JSONObject json_object = ...; // processed JSON tree DatabaseHelper helper = ...; // OrmLite databasehelper; JsonPersister persister = new JsonPersister(helper.getWritableDatabase());
 persister.persistObject(User.class, json_object);

Features

  • Annotation-based model configuration
  • Advanced DatabaseHelper with easy DatabaseConfiguration
  • Support for relationships:
    • One-to-one
    • Many-to-one
    • One-to-many
    • Many-to-many
  • Persist objects and arrays of objects
  • Persist objects with arrays of base types (e.g. JSON String array persisted to separate table)

Requirements

  • Android 2.3.3 (API level 10) or higher

Usage

build.gradle

repositories {

  mavenCentral()
  maven {

url "https://dl.bintray.com/elastique/poetry"
  
}
 
}
dependencies {

  compile (

[group: 'nl.elastique.poetry', name: 'poetry', version: '3.0.1']
  ) 
}

Demo

A Demo application is available on GitHub

Behaviors

  • Models that are imported more than once within a single JSON tree are updated every time they are processed.
  • Attributes that are not specified in JSON are not updated
  • Attributes that are imported with null value will have a null value in the database. Make sure your model allows this.
  • When you use JsonPersister's persistArray() method, it will import the array and delete all objects from the database that do not correspond to any of the imported IDs.

Tutorial

Creating a DatabaseHelper

The Poetry DatabaseHelper allows you to easily configure your database. In the example below, you can see a custom DatabaseHelper with a DatabaseConfiguration that holds the model version and model classes.

MyDatabaseHelper.java

import android.database.sqlite.SQLiteDatabase; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.support.ConnectionSource; import nl.elastique.poetry.database.DatabaseConfiguration;  public class MyDatabaseHelper extends nl.elastique.poetry.database.DatabaseHelper {

  public final static DatabaseConfiguration sConfiguration = new DatabaseConfiguration(1, new Class<?>[]
  {

User.class,

Group.class,

UserTag.class,

UserGroup.class
  
}
);

public DatabaseHelper(Context context)
  {

super(context, sConfiguration);

  
}

public static DatabaseHelper getHelper(Context context)
  {

return OpenHelperManager.getHelper(context, DatabaseHelper.class);

  
}
 
}

Mapping custom JSON properties

By default, the name of the attribute is used to map from JSON. Your naming conventions might now allow this. You can specify the json key name with the @MapFrom annotation.

User.java

@DatabaseTable class User {
  @DatabaseField(id = true, columnName = "id")  @MapFrom("id")
  private int mId;
@DatabaseField(columnName = "name")  @MapFrom("name")
  private String mName; 
}

One-to-many relationships

In this example, a Game object holds a list of Player objects.

game.json

{

  "id": 1,
  "players" : [

{

 "id": 1,

 "name": "John"

}
,

{

 "id": 2,

 "name": "Jane"

}

  ] 
}

Game.java

@DatabaseTable public class Game {

  @DatabaseField(id = true)
  public int id;

 @ForeignCollectionField(eager = true)  public ForeignCollection<Player> players; 
}

Player.java

@DatabaseTable public class Player {

  @DatabaseField(id = true)
  public int id;

@DatabaseField
  public String name; 
}

Many-to-many relationships

In this example, a User can have 0 or more Groups, and a Group can have 0 or more Users.

users.json

[  {

"id" : 1,

"name" : "John",

"groups" : [

 {

  "id" : 1,

  "name" : "Group 1"

 
}
,

 {

  "id" : 2,

  "name" : "Group 2"

 
}

]  
}
,  {

"id" : 2,

"name" : "Jane",

"groups" : [

 {

  "id" : 1,

  "name" : "Group 1"

 
}

]  
}
 ]

User.java

@DatabaseTable public class User {

  @DatabaseField(id = true)
  public int id;

@DatabaseField
  public String name;

 /** 
  * Many-to-many relationships. 
  * 
  * OrmLite requires a ForeignCollectionField with the helper-type UserGroup to assist in the database relational mapping. 
  * JSON-to-SQLite persistence also requires the additional annotation "ManyToManyField" 
  */
  @ForeignCollectionField(eager = true)
  @ManyToManyField(targetType = Group.class)  public ForeignCollection<UserGroup> groups; 
}

UserGroup.java

@DatabaseTable public class UserGroup {

  @DatabaseField(generatedId = true)
  public int id;

@DatabaseField(foreign = true)  public User user;

@DatabaseField(foreign = true)
  public Group group; 
}

Group.java

@DatabaseTable public class Group {

  @DatabaseField(id = true)
  public int id;

@DatabaseField
  public String name; 
}

One-to-one relationships

In this example, a User can have 0 or 1 Friend user.

users.json

[  {

"id": 1,

"name" : "John",

"friend" : {
 "id" : 2 
}
  
}
,  {

"id": 2,

"name" : "Jane",

"friend" : {
 "id" : 1 
}
  
}
 ]

The following alternative JSON is also valid:

users.json

[  {

"id": 1,

"name" : "John",

"friend" : 2  
}
,  {

"id": 2,

"name" : "Jane",

"friend" : 1  
}
 ]

User.java

@DatabaseTable public class User {

  @DatabaseField(id = true)
  public int id;

@DatabaseField
  public String name;

 @DatabaseField(foreign = true)
  public User friend; 
}

Arrays of base types

Arrays of base types work the same as one-to-many relationships. The only difference is that you have to define a model that holds the base types and use the @ForeignCollectionFieldSingleTarget annotation to specify the database column name that holds the value.

user.json

{

  "id": 1,
  "tags" : [

"tag1",

"tag2"
  ] 
}

User.java

@DatabaseTable public class User {

  @DatabaseField(id = true)
  public int id;

 // The targetField refers to the table's column name  @ForeignCollectionField(eager = true)  @ForeignCollectionFieldSingleTarget(targetField = "value")  @MapFrom("tags")  public ForeignCollection<UserTag> userTags; 
}

UserTag.java

@DatabaseTable public class UserTag {

  @DatabaseField(generatedId = true)
  public int id;

@DatabaseField(foreign = true)
  public User user;

@DatabaseField  public String value; 
}

Resources

Inspiration comes from UC Browser. It's a beautiful and funny ball to show pull to refresh and go back.It's based on Bessel curve.

This library is to take picture using camera from background without displaying preview.

A sample application that can be used as a template for implementing Mobile Two Factor Auth

The major drawback of authentication performed including something that the user possesses is that the physical token (the USB stick, the bank card, the key or similar) must be carried around by the user, practically at all times. Loss and theft are a risk. There are also costs involved in procuring and subsequently replacing tokens of this kind. In addition, there are inherent conflicts and unavoidable trade-offs between usability and security.

It is a library with several commonly used Input Dialogs implementations.

A Mac App, for Apk multi-channel package.

A playground for MVVM style architecture on Android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes