Search in sources :

Example 26 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.

the class Cloudant method deleteDesignDocument.

/**
 * Delete a design document.
 *
 * Marks the specified design document as deleted by adding a `_deleted` field with the value `true`. Documents with
 * this field are not returned with requests but stay in the database. You must supply the current (latest) revision,
 * either by using the `rev` parameter or by using the `If-Match` header to specify the revision.
 *
 * @param deleteDesignDocumentOptions the {@link DeleteDesignDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentResult}
 */
public ServiceCall<DocumentResult> deleteDesignDocument(DeleteDesignDocumentOptions deleteDesignDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(deleteDesignDocumentOptions, "deleteDesignDocumentOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", deleteDesignDocumentOptions.db());
    pathParamsMap.put("ddoc", deleteDesignDocumentOptions.ddoc());
    RequestBuilder builder = RequestBuilder.delete(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_design/{ddoc}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "deleteDesignDocument");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (deleteDesignDocumentOptions.ifMatch() != null) {
        builder.header("If-Match", deleteDesignDocumentOptions.ifMatch());
    }
    if (deleteDesignDocumentOptions.batch() != null) {
        builder.query("batch", String.valueOf(deleteDesignDocumentOptions.batch()));
    }
    if (deleteDesignDocumentOptions.rev() != null) {
        builder.query("rev", String.valueOf(deleteDesignDocumentOptions.rev()));
    }
    ResponseConverter<DocumentResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentResult>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult)

Example 27 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.

the class GetInfoFromExistingDatabase method main.

public static void main(String[] args) {
    // 1. Create a Cloudant client with "EXAMPLES" service name ===========
    Cloudant client = Cloudant.newInstance("EXAMPLES");
    // 2. Get server information ==========================================
    ServerInformation serverInformation = client.getServerInformation().execute().getResult();
    System.out.println("Server Version: " + serverInformation.getVersion());
    // 3. Get database information for "animaldb" =========================
    String dbName = "animaldb";
    GetDatabaseInformationOptions dbInformationOptions = new GetDatabaseInformationOptions.Builder(dbName).build();
    DatabaseInformation dbInformationResponse = client.getDatabaseInformation(dbInformationOptions).execute().getResult();
    // 4. Show document count in database =================================
    Long documentCount = dbInformationResponse.getDocCount();
    System.out.println("Document count in \"" + dbInformationResponse.getDbName() + "\" database is " + documentCount + ".");
    // 5. Get zebra document out of the database by document id ===========
    GetDocumentOptions getDocOptions = new GetDocumentOptions.Builder().db(dbName).docId("zebra").build();
    Document documentAboutZebra = client.getDocument(getDocOptions).execute().getResult();
    System.out.println("Document retrieved from database:\n" + documentAboutZebra);
}
Also used : DatabaseInformation(com.ibm.cloud.cloudant.v1.model.DatabaseInformation) ServerInformation(com.ibm.cloud.cloudant.v1.model.ServerInformation) GetDatabaseInformationOptions(com.ibm.cloud.cloudant.v1.model.GetDatabaseInformationOptions) GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Document(com.ibm.cloud.cloudant.v1.model.Document)

Example 28 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.

the class UpdateDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Update the document =============================================
    // Set the options to get the document out of the database if it exists
    String exampleDbName = "orders";
    GetDocumentOptions documentInfoOptions = new GetDocumentOptions.Builder().db(exampleDbName).docId("example").build();
    try {
        // Try to get the document if it previously existed in the database
        Document document = client.getDocument(documentInfoOptions).execute().getResult();
        // Note: for response byte stream use:
        /*
            InputStream documentAsByteStream =
                client.getDocumentAsStream(documentInfoOptions)
                    .execute()
                    .getResult();
            */
        // Add Bob Smith's address to the document
        document.put("address", "19 Front Street, Darlington, DL5 1TY");
        // Remove the joined property from document object
        document.removeProperty("joined");
        // Update the document in the database
        PostDocumentOptions updateDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(document).build();
        // ================================================================
        // Note: for request byte stream use:
        /*
            PostDocumentOptions updateDocumentOptions =
                new PostDocumentOptions.Builder()
                    .db(exampleDbName)
                    .contentType("application/json")
                    .body(documentAsByteStream)
                    .build();
            */
        // ================================================================
        DocumentResult updateDocumentResponse = client.postDocument(updateDocumentOptions).execute().getResult();
        // ====================================================================
        // Note: updating the document can also be done with the "putDocument"
        // method. docId and rev are required for an UPDATE operation,
        // but rev can be provided in the document object too:
        /*
            PutDocumentOptions updateDocumentOptions =
                new PutDocumentOptions.Builder()
                    .db(exampleDbName)
                    .docId(document.getId()) // docId is a required parameter
                    .rev(document.getRev())
                    .document(document) // rev in the document object CAN replace above `rev` parameter
                    .build();
            DocumentResult updateDocumentResponse = client
                .putDocument(updateDocumentOptions)
                .execute()
                .getResult();
            */
        // ====================================================================
        // Keeping track of the latest revision number of the document object
        // is necessary for further UPDATE/DELETE operations:
        document.setRev(updateDocumentResponse.getRev());
        System.out.println("You have updated the document:\n" + document);
    } catch (NotFoundException nfe) {
        System.out.println("Cannot update document because " + "either \"orders\" database or the \"example\" " + "document was not found.");
    }
}
Also used : DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Document(com.ibm.cloud.cloudant.v1.model.Document) PostDocumentOptions(com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)

Example 29 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.

the class SecurityTest method testSecurity.

@Test
public void testSecurity() throws Throwable {
    SecurityObject securityObjectModel = new SecurityObject.Builder().names(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).roles(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
    assertEquals(securityObjectModel.names(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(securityObjectModel.roles(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    Security securityModel = new Security.Builder().admins(securityObjectModel).members(securityObjectModel).cloudant(new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    }).couchdbAuthOnly(true).build();
    assertEquals(securityModel.admins(), securityObjectModel);
    assertEquals(securityModel.members(), securityObjectModel);
    assertEquals(securityModel.cloudant(), new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    });
    assertEquals(securityModel.couchdbAuthOnly(), Boolean.valueOf(true));
    String json = TestUtilities.serialize(securityModel);
    Security securityModelNew = TestUtilities.deserialize(json, Security.class);
    assertTrue(securityModelNew instanceof Security);
    assertEquals(securityModelNew.admins().toString(), securityObjectModel.toString());
    assertEquals(securityModelNew.members().toString(), securityObjectModel.toString());
    assertEquals(securityModelNew.couchdbAuthOnly(), Boolean.valueOf(true));
}
Also used : SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Security(com.ibm.cloud.cloudant.v1.model.Security) Test(org.testng.annotations.Test)

Example 30 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.

the class Cloudant method getDocumentShardsInfo.

/**
 * Retrieve shard information for a specific document.
 *
 * Retrieves information about a specific shard where a particular document is stored, along with information about
 * the nodes where that shard has a replica.
 *
 * @param getDocumentShardsInfoOptions the {@link GetDocumentShardsInfoOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DocumentShardInfo}
 */
public ServiceCall<DocumentShardInfo> getDocumentShardsInfo(GetDocumentShardsInfoOptions getDocumentShardsInfoOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getDocumentShardsInfoOptions, "getDocumentShardsInfoOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", getDocumentShardsInfoOptions.db());
    pathParamsMap.put("doc_id", getDocumentShardsInfoOptions.docId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_shards/{doc_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getDocumentShardsInfo");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    ResponseConverter<DocumentShardInfo> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentShardInfo>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) DocumentShardInfo(com.ibm.cloud.cloudant.v1.model.DocumentShardInfo)

Aggregations

RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)70 HashMap (java.util.HashMap)51 JsonObject (com.google.gson.JsonObject)23 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)17 Ok (com.ibm.cloud.cloudant.v1.model.Ok)11 Document (com.ibm.cloud.cloudant.v1.model.Document)9 List (java.util.List)8 AllDocsResult (com.ibm.cloud.cloudant.v1.model.AllDocsResult)6 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)6 ArrayList (java.util.ArrayList)6 Cloudant (com.ibm.cloud.cloudant.v1.Cloudant)5 SecurityObject (com.ibm.cloud.cloudant.v1.model.SecurityObject)5 Test (org.testng.annotations.Test)5 DesignDocument (com.ibm.cloud.cloudant.v1.model.DesignDocument)3 GetDocumentOptions (com.ibm.cloud.cloudant.v1.model.GetDocumentOptions)3 PostAllDocsOptions (com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions)3 PostDocumentOptions (com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)3 ReplicationDocument (com.ibm.cloud.cloudant.v1.model.ReplicationDocument)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3