Skip to content

[99] Archive: Implementation Guide 2.x.x

Sven Braune edited this page Mar 24, 2021 · 1 revision

This Page is deprecated Framework version 2.x.x is no longer supported!

  1. Add it in your root build.gradle at the end of repositories:
 buildscript {
    repositories {
        maven { url 'https://jitpack.io' }
    }
  }
  ...
  allprojects {
    repositories {
	maven { url 'https://jitpack.io' }
    }
  }
  1. Add gradle dependency
  compile 'com.github.Kaufland.andcouchbaseentity:couchbase-entity-api:2.2.0'
  apt 'com.github.Kaufland.andcouchbaseentity:couchbase-entity:2.2.0'
  1. Optionally, if you use Couchbase 2.x.x (provides already implemented connector)
  compile 'com.github.Kaufland.andcouchbaseentity:couchbase-entity-connector:2.2.0@aar'

Hint: For custom connectors see

Connector

Example

  1. Configure library
  • Add the following code in your Application.class
  @Override
  public void onCreate() {
      super.onCreate();
      PersistenceConfig.configure(new Couchbase2Connector() {
          @Override
          protected Database getDatabase(String name) {
              if (DB.equals(name)) {
                  return Application.this.getDatabase();
              }
              throw new RuntimeException("wrong db name defined!!");
          }
      });
  }

Hint: For other databases implement your own connector

  • Annotate classes to generate entities (all generated classes have the suffix Entity or Wrapper (used for child entities or map wrapping)
@Entity(database = Application.DB)
@Fields(
        Field(name = "type", type = String::class, defaultValue = "product", readonly = true),
        Field(name = "name", type = String::class),
        Field("comments", type = UserComment::class, list = true),
        Field(name = "image", type = Blob::class),
        Field(name = "identifiers", type = String::class, list = true)
)
open class Product
  • Use generated classes and be happy :-)
 ProductEntity.create().setName("Beer").
                  setComments(new ArrayList<>(Arrays.asList(UserCommentEntity.create().setComment("very awesome"), 
 UserCommentEntity.create().setComment("tasty")))).
                  setImage(new Blob("image/jpeg", getResources().openRawResource(R.raw.ic_kaufland_placeholder))).
                  save();              

Hint: To modify child entities it's neccessary to invoke the setter before saving the parent entity

 ArrayList<UserCommentEntity> data = getParentEntity().getComments();
 data.remove(0);
 try {
     getParentEntity().setComments(data).save();
 } catch (CouchbaseLiteException e) {
     Log.e(TAG, "failed to save Entity", e);
 }              
Clone this wiki locally