/** * 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; }
/** * 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(); } }
/** * 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(); }
/** * Get a given Document's attachment if any * * @param docId * @param attachName * @return Attachment */ public Attachment getAttachment(String docId, String attachName) { Document doc = database.getDocument(docId); Revision rev = doc.getCurrentRevision(); return rev.getAttachment(attachName); }