/** * Finds an Object of the specified type. * @param <T> Object type. * @param classType The class of type T. * @param id The document id. * @return An object of type T. * @throws NoDocumentException If the document is not found in the database. */ public <T> T find(Class<T> classType, String id) { assertNotEmpty(classType, "Class"); assertNotEmpty(id, "id"); final URI uri = buildUri(getDBUri()).pathEncoded(id).build(); return get(uri, classType); }
/** * Checks if a document exist in the database. * @param id The document _id field. * @return true If the document is found, false otherwise. */ public boolean contains(String id) { assertNotEmpty(id, "id"); HttpResponse response = null; try { response = head(buildUri(getDBUri()).pathEncoded(id).build()); } catch (NoDocumentException e) { return false; } finally { close(response); } return true; }
/** * Removes a document from the database given both a document <code>_id</code> and <code>_rev</code> values. * @param id The document _id field. * @param rev The document _rev field. * @throws NoDocumentException If the document is not found in the database. * @return {@link Response} */ public Response remove(String id, String rev) { assertNotEmpty(id, "id"); assertNotEmpty(rev, "rev"); final URI uri = buildUri(getDBUri()).pathEncoded(id).query("rev", rev).build(); return delete(uri); }
/** * Performs a HTTP PUT request, saves or updates a document. * @return {@link Response} */ Response put(URI uri, Object object, boolean newEntity) { assertNotEmpty(object, "object"); HttpResponse response = null; try { final JsonObject json = getGson().toJsonTree(object).getAsJsonObject(); String id = getAsString(json, "_id"); String rev = getAsString(json, "_rev"); if(newEntity) { // save assertNull(rev, "rev"); id = (id == null) ? generateUUID() : id; } else { // update assertNotEmpty(id, "id"); assertNotEmpty(rev, "rev"); } final HttpPut put = new HttpPut(buildUri(uri).pathEncoded(id).build()); setEntity(put, json.toString()); response = executeRequest(put); return getResponse(response); } finally { close(response); } }
/** * Finds an Object of the specified type. * @param <T> Object type. * @param classType The class of type T. * @param id The document id. * @param params Extra parameters to append. * @return An object of type T. * @throws NoDocumentException If the document is not found in the database. */ public <T> T find(Class<T> classType, String id, Params params) { assertNotEmpty(classType, "Class"); assertNotEmpty(id, "id"); final URI uri = buildUri(getDBUri()).pathEncoded(id).query(params).build(); return get(uri, classType); }
/** * Finds an Object of the specified type. * @param <T> Object type. * @param classType The class of type T. * @param id The document _id field. * @param rev The document _rev field. * @return An object of type T. * @throws NoDocumentException If the document is not found in the database. */ public <T> T find(Class<T> classType, String id, String rev) { assertNotEmpty(classType, "Class"); assertNotEmpty(id, "id"); assertNotEmpty(id, "rev"); final URI uri = buildUri(getDBUri()).pathEncoded(id).query("rev", rev).build(); return get(uri, classType); }
/** * Saves an attachment to an existing document given both a document id * and revision, or save to a new document given only the id, and rev as {@code null}. * <p>To retrieve an attachment, see {@link #find(String)}. * @param in The {@link InputStream} holding the binary data. * @param name The attachment name. * @param contentType The attachment "Content-Type". * @param docId The document id to save the attachment under, or {@code null} to save under a new document. * @param docRev The document revision to save the attachment under, or {@code null} when saving to a new document. * @return {@link Response} */ public Response saveAttachment(InputStream in, String name, String contentType, String docId, String docRev) { assertNotEmpty(in, "in"); assertNotEmpty(name, "name"); assertNotEmpty(contentType, "ContentType"); assertNotEmpty(docId, "docId"); final URI uri = buildUri(getDBUri()).pathEncoded(docId).path("/").path(name).query("rev", docRev).build(); return put(uri, in, contentType); }