Java SDK Samples - Bulk Read Operations
package samples.src.com.zoho.crm.api.bulkread;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.zoho.crm.api.bulkread.APIException;
import com.zoho.crm.api.bulkread.ActionHandler;
import com.zoho.crm.api.bulkread.ActionResponse;
import com.zoho.crm.api.bulkread.ActionWrapper;
import com.zoho.crm.api.bulkread.BulkReadOperations;
import com.zoho.crm.api.bulkread.CallBack;
import com.zoho.crm.api.bulkread.Criteria;
import com.zoho.crm.api.bulkread.FileBodyWrapper;
import com.zoho.crm.api.bulkread.JobDetail;
import com.zoho.crm.api.bulkread.Query;
import com.zoho.crm.api.bulkread.RequestWrapper;
import com.zoho.crm.api.bulkread.ResponseHandler;
import com.zoho.crm.api.bulkread.ResponseWrapper;
import com.zoho.crm.api.bulkread.Result;
import com.zoho.crm.api.bulkread.SuccessResponse;
import com.zoho.crm.api.util.Choice;
import com.zoho.crm.api.util.APIResponse;
import com.zoho.crm.api.util.Model;
import com.zoho.crm.api.util.StreamWrapper;
public class BulkRead
{
/**
* Create BulkRead Job
* This method is used to create a bulk read job to export records.
* @param moduleAPIName The API Name of the record's module
* @throws Exception
*/
public static void createBulkReadJob(String moduleAPIName) throws Exception
{
//example
//String moduleAPIName = "Leads";
//Get instance of BulkReadOperations Class
BulkReadOperations bulkReadOperations = new BulkReadOperations();
//Get instance of RequestWrapper Class that will contain the request body
RequestWrapper requestWrapper = new RequestWrapper();
//Get instance of CallBack Class
CallBack callback = new CallBack();
// To set valid callback URL.
callback.setUrl("https://www.example.com/callback");
//To set the HTTP method of the callback URL. The allowed value is post.
callback.setMethod(new Choice("post"));
//The Bulk Read Job's details is posted to this URL on successful completion / failure of job.
requestWrapper.setCallback(callback);
//Get instance of Query Class
Query query = new Query();
//Specifies the API Name of the module to be read.
query.setModule(moduleAPIName);
//Specifies the unique ID of the custom view whose records you want to export.
// query.setCvid("34770610087501");
// List of Field API Names
List fieldAPINames = new ArrayList();
fieldAPINames.add("Last_Name");
//Specifies the API Name of the fields to be fetched.
query.setFields(fieldAPINames);
//To set page value, By default value is 1.
query.setPage(1);
//Get instance of Criteria Class
Criteria criteria = new Criteria();
criteria.setGroupOperator(new Choice("or"));
List criteriaList = new ArrayList();
Criteria group11 = new Criteria();
group11.setGroupOperator(new Choice("and"));
List groupList11 = new ArrayList();
Criteria group111 = new Criteria();
group111.setAPIName("Company");
group111.setComparator(new Choice("equal"));
group111.setValue("Zoho");
groupList11.add(group111);
Criteria group112 = new Criteria();
group112.setAPIName("Owner");
group112.setComparator(new Choice("in"));
List owner = new ArrayList(Arrays.asList("34770610173021"));
group112.setValue(owner);
groupList11.add(group112);
group11.setGroup(groupList11);
criteriaList.add(group11);
Criteria group12 = new Criteria();
group12.setGroupOperator(new Choice("or"));
List groupList12 = new ArrayList();
Criteria group121 = new Criteria();
group121.setAPIName("Paid");
group121.setComparator(new Choice("equal"));
group121.setValue(true);
groupList12.add(group121);
Criteria group122 = new Criteria();
// To set API name of a field.
group122.setAPIName("Created_Time");
// To set comparator(eg: equal, greater_than.).
group122.setComparator(new Choice("between"));
List createdTime = new ArrayList(Arrays.asList("2020-06-03T17:31:48+05:30", "2020-06-03T17:31:48+05:30"));
// To set the value to be compare.
group122.setValue(createdTime);
groupList12.add(group122);
group12.setGroup(groupList12);
criteriaList.add(group12);
criteria.setGroup(criteriaList);
//To filter the records to be exported.
query.setCriteria(criteria);
//To set query JSON object.
requestWrapper.setQuery(query);
//Specify the value for this key as "ics" to export all records in the Events module as an ICS file.
//requestWrapper.setFileType(new Choice("ics"));
//Sample request body
// {"query":{"criteria":{"group_operator":"or","group":[{"group_operator":"and","group":[{"comparator":"equal","api_name":"All_day","value":false},
// {"comparator":"in","api_name":"Owner","value":["3477061001"]}]},{"group_operator":"or","group":[{"comparator":"equal","api_name":"Event_Title","value":"New Event"},
// {"comparator":"between","api_name":"Created_Time","value":["2020-06-03T17:31:48+05:30","2020-06-03T17:31:48+05:30"]}]}]},"module":"Events","page":1},
// "callback":{"method":"post","url":"https://www.example.com/callback"}}
//
//Call createBulkReadJob method that takes RequestWrapper instance as parameter
APIResponse response = bulkReadOperations.createBulkReadJob(requestWrapper);
if(response != null)
{
//Get the status code from response
System.out.println("Status Code: " + response.getStatusCode());
//Check if expected response is received
if(response.isExpected())
{
//Get object from response
ActionHandler actionHandler = response.getObject();
if(actionHandler instanceof ActionWrapper)
{
//Get the received ActionWrapper instance
ActionWrapper actionWrapper = (ActionWrapper) actionHandler;
//Get the list of obtained ActionResponse instances
List actionResponses = actionWrapper.getData();
for(ActionResponse actionResponse : actionResponses)
{
//Check if the request is successful
if(actionResponse instanceof SuccessResponse)
{
//Get the received SuccessResponse instance
SuccessResponse successResponse = (SuccessResponse)actionResponse;
//Get the Status
System.out.println("Status: " + successResponse.getStatus().getValue());
//Get the Code
System.out.println("Code: " + successResponse.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : successResponse.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + successResponse.getMessage().getValue());
}
//Check if the request returned an exception
else if(actionResponse instanceof APIException)
{
//Get the received APIException instance
APIException exception = (APIException) actionResponse;
//Get the Status
System.out.println("Status: " + exception.getStatus().getValue());
//Get the Code
System.out.println("Code: " + exception.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : exception.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + exception.getMessage().getValue());
}
}
}
//Check if the request returned an exception
else if(actionHandler instanceof APIException)
{
//Get the received APIException instance
APIException exception = (APIException) actionHandler;
//Get the Status
System.out.println("Status: " + exception.getStatus().getValue());
//Get the Code
System.out.println("Code: " + exception.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : exception.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + exception.getMessage().getValue());
}
}
else
{//If response is not as expected
//Get model object from response
Model responseObject = response.getModel();
//Get the response object's class
Class extends Model> clas = responseObject.getClass();
//Get all declared fields of the response class
Field[] fields = clas.getDeclaredFields();
for(Field field : fields)
{
//Get each value
System.out.println(field.getName() + ":" + field.get(responseObject));
}
}
}
}
}
package samples.src.com.zoho.crm.api.bulkread;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.zoho.crm.api.bulkread.APIException;
import com.zoho.crm.api.bulkread.ActionHandler;
import com.zoho.crm.api.bulkread.ActionResponse;
import com.zoho.crm.api.bulkread.ActionWrapper;
import com.zoho.crm.api.bulkread.BulkReadOperations;
import com.zoho.crm.api.bulkread.CallBack;
import com.zoho.crm.api.bulkread.Criteria;
import com.zoho.crm.api.bulkread.FileBodyWrapper;
import com.zoho.crm.api.bulkread.JobDetail;
import com.zoho.crm.api.bulkread.Query;
import com.zoho.crm.api.bulkread.RequestWrapper;
import com.zoho.crm.api.bulkread.ResponseHandler;
import com.zoho.crm.api.bulkread.ResponseWrapper;
import com.zoho.crm.api.bulkread.Result;
import com.zoho.crm.api.bulkread.SuccessResponse;
import com.zoho.crm.api.util.Choice;
import com.zoho.crm.api.util.APIResponse;
import com.zoho.crm.api.util.Model;
import com.zoho.crm.api.util.StreamWrapper;
public class BulkRead
{
/**
* Download Result
* This method is used to download the bulk read job as a CSV or an ICS file (only for the Events module).
* @param jobId The unique ID of the bulk read job.
* @param destinationFolder The absolute path where downloaded file has to be stored.
* @throws Exception
*/
public static void downloadResult(Long jobId, String destinationFolder) throws Exception
{
//example
//Long jobId = 34770615177002l;
//String destinationFolder = "/Users/user_name/Documents";
//Get instance of BulkReadOperations Class
BulkReadOperations bulkReadOperations = new BulkReadOperations();
//Call downloadResult method that takes jobId as parameters
APIResponse response = bulkReadOperations.downloadResult(jobId);
if(response != null)
{
//Get the status code from response
System.out.println("Status Code: " + response.getStatusCode());
if(Arrays.asList(204,304).contains(response.getStatusCode()))
{
System.out.println(response.getStatusCode() == 204? "No Content" : "Not Modified");
return;
}
//Check if expected response is received
if(response.isExpected())
{
//Get object from response
ResponseHandler responseHandler = response.getObject();
if(responseHandler instanceof FileBodyWrapper)
{
//Get the received FileBodyWrapper instance
FileBodyWrapper fileBodyWrapper = (FileBodyWrapper)responseHandler;
//Get StreamWrapper instance from the returned FileBodyWrapper instance
StreamWrapper streamWrapper = fileBodyWrapper.getFile();
//Create a file instance with the absolute_file_path
File file = new File(destinationFolder + File.separatorChar + streamWrapper.getName());
//Get InputStream from the response
InputStream is = streamWrapper.getStream();
//Create an OutputStream for the destination file
OutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
//read the InputStream till the end
while((bytesRead = is.read(buffer)) != -1)
{
//write data to OutputStream
os.write(buffer, 0, bytesRead);
}
//Close the InputStream
is.close();
//Flush and close the OutputStream
os.flush();
os.close();
}
//Check if the request returned an exception
else if(responseHandler instanceof APIException)
{
//Get the received APIException instance
APIException exception = (APIException) responseHandler;
//Get the Status
System.out.println("Status: " + exception.getStatus().getValue());
//Get the Code
System.out.println("Code: " + exception.getCode().getValue());
System.out.println("Details: " );
//Get the details map
for(Map.Entry entry : exception.getDetails().entrySet())
{
//Get each value in the map
System.out.println(entry.getKey() + ": " + entry.getValue());
}
//Get the Message
System.out.println("Message: " + exception.getMessage().getValue());
}
}
else
{//If response is not as expected
//Get model object from response
Model responseObject = response.getModel();
//Get the response object's class
Class extends Model> clas = responseObject.getClass();
//Get all declared fields of the response class
java.lang.reflect.Field[] fields = clas.getDeclaredFields();
for(java.lang.reflect.Field field : fields)
{
//Get each value
System.out.println(field.getName() + ":" + field.get(responseObject));
}
}
}
}
}