/**
   * cr-U-d
   *
   * @param key
   * @param value
   * @param docId
   * @return success or failure
   */
  public boolean update(final String key, final Object value, String docId) {

    if (database == null) {
      Log.e("DatabaseWrapper", "database is null : DatabaseWrapper.update");
      return false;
    }
    // update the document
    try {
      Document doc = database.getDocument(docId);

      // this alternative way is better for handling write conflicts
      doc.update(
          new Document.DocumentUpdater() {
            @Override
            public boolean update(UnsavedRevision newRevision) {
              Map<String, Object> properties = newRevision.getUserProperties();
              properties.put(key, value);
              newRevision.setUserProperties(properties);
              return true;
            }
          });

      /*	Map<String, Object> docContent = doc.getProperties();
      //Working on a copy
      Map<String, Object> updatedContent = new HashMap<String, Object>();
      updatedContent.putAll(docContent);
      updatedContent.put(key, value);
      doc.putProperties(updatedContent);*/
    } catch (CouchbaseLiteException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }
  public DatabaseWrapper(String dbname, Context context) {

    this.context = context;
    /* Manages access to databases */
    try {
      manager = new Manager(new AndroidContext(context), Manager.DEFAULT_OPTIONS);
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }

    // create a name for the database and make sure the name is legal
    // Only the following characters are valid:
    // abcdefghijklmnopqrstuvwxyz0123456789_$()+-/
    if (!Manager.isValidDatabaseName(dbname)) {
      Log.e("DatabaseWrapper", "Invalid database name: '" + dbname + "'");
      return;
    }
    // get existing db with that name
    // or create a new one if it doesn't exist
    try {
      database = manager.getDatabase(dbname);

    } catch (CouchbaseLiteException e) {
      e.printStackTrace();
      return;
    }
  }
  /**
   * Write an Attachment for a given Document
   *
   * @param docId
   * @param attachName
   * @param mimeType e.g. "image/jpeg"
   * @param in
   */
  public void writeAttachment(String docId, String attachName, String mimeType, InputStream in) {

    try {
      Document doc = database.getDocument(docId);
      UnsavedRevision newRev = doc.getCurrentRevision().createRevision();
      newRev.setAttachment(attachName, mimeType, in);
      newRev.save();
    } catch (CouchbaseLiteException e) {
      e.printStackTrace();
    }
  }
 @Kroll.method
 public SavedRevisionProxy save(@Kroll.argument(optional = true) boolean allowConflict) {
   lastError = null;
   try {
     SavedRevision saved = ((UnsavedRevision) revision).save(allowConflict);
     return new SavedRevisionProxy(documentProxy, saved);
   } catch (CouchbaseLiteException e) {
     lastError = TitouchdbModule.convertStatusToErrorDict(e.getCBLStatus());
   }
   return null;
 }
 public FeedAdapter(Context context, final LiveQuery query) {
   this.context = context;
   this.query = query;
   try {
     enumerator = query.run();
   } catch (CouchbaseLiteException e) {
     e.printStackTrace();
   }
   query
       .getDatabase()
       .addChangeListener(
           new Database.ChangeListener() {
             @Override
             public void changed(final Database.ChangeEvent event) {
               ((Activity) FeedAdapter.this.context)
                   .runOnUiThread(
                       new Runnable() {
                         @Override
                         public void run() {
                           if (event.isExternal() == true) {
                             try {
                               enumerator = query.run();
                             } catch (CouchbaseLiteException e) {
                               e.printStackTrace();
                             }
                             for (DocumentChange change : event.getChanges()) {
                               int i = 0;
                               while (enumerator.hasNext()) {
                                 QueryRow row = enumerator.next();
                                 if (row.getDocumentId().equals(change.getDocumentId())) {
                                   notifyItemChanged(i);
                                 }
                                 i++;
                               }
                             }
                           }
                         }
                       });
             }
           });
   //        query.addChangeListener(new LiveQuery.ChangeListener() {
   //            @Override
   //            public void changed(final LiveQuery.ChangeEvent event) {
   //                ((Activity) FeedAdapter.this.context).runOnUiThread(new Runnable() {
   //                    @Override
   //                    public void run() {
   //                        if (event)
   //                        enumerator = event.getRows();
   //                        notifyDataSetChanged();
   //                    }
   //                });
   //            }
   //        });
 }
  /**
   * Remove an Attachment from a Document
   *
   * @param docId
   * @param attachName
   */
  public void deleteAttachment(String docId, String attachName) {

    try {
      Document doc = database.getDocument(docId);
      UnsavedRevision newRev = doc.getCurrentRevision().createRevision();
      newRev.removeAttachment(attachName);
      // (You could also update newRev.properties while you're here)
      newRev.save();
    } catch (CouchbaseLiteException e) {
      e.printStackTrace();
    }
  }
  /**
   * C-rud
   *
   * @param docContent
   * @return docId
   */
  public String create(Map<String, Object> docContent, String docId) {

    if (database == null) {
      Log.e("DatabaseWrapper", "database is null : DatabaseWrapper.create(" + ")");
      return "";
    }
    // create an empty document
    Document doc = database.getDocument(docId);
    // .createDocument();

    // add content to document and write the document to the database
    try {
      doc.putProperties(docContent);
    } catch (CouchbaseLiteException e) {
      e.printStackTrace();
      return "";
    }
    return doc.getId();
  }
  /**
   * cru-D
   *
   * @param docId
   * @return
   */
  public boolean delete(String docId) {

    if (database == null) {
      Log.e("DatabaseWrapper", "database is null : DatabaseWrapper.delete(" + docId + ")");
      return false;
    }
    Document doc = null;
    // delete the document
    try {
      if (!database.isOpen()) {
        database.open();
      }
      doc = database.getExistingDocument(docId);
      if (doc != null) {
        doc.delete();
      }
    } catch (CouchbaseLiteException e) {
      e.printStackTrace();
    }
    if (doc != null) {
      return doc.isDeleted();
    }
    return false;
  }