Skip to product menu
Skip to main content

Java SDK Samples - Bulk Read Operations

Create a Bulk Read Job
Get Bulk Read Job Details
              
              
package com.zoho.crm.sample.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
{
	/**
	 * Get BulkRead Job Details
	 * This method is used to get the details of a bulk read job performed previously.
	 * @param jobId The unique ID of the bulk read job.
	 * @throws Exception
	 */
	public static void getBulkReadJobDetails(Long jobId) throws Exception
	{
		//example
		//Long jobId = 3477061000005177002L;
		
		//Get instance of BulkReadOperations Class
		BulkReadOperations bulkReadOperations = new BulkReadOperations();
		
		//Call getBulkReadJobDetails method that takes jobId as parameter
		APIResponse<ResponseHandler> response = bulkReadOperations.getBulkReadJobDetails(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 ResponseWrapper)
				{
					//Get the received ResponseWrapper instance
					ResponseWrapper responseWrapper = (ResponseWrapper) responseHandler;
					
					//Get the list of obtained jobDetail instances
					List<JobDetail> jobDetails = responseWrapper.getData();
				
					for(JobDetail jobDetail : jobDetails)
					{
						//Get the Job ID of each jobDetail
						System.out.println("Bulk read Job ID: " + jobDetail.getId());
						
						//Get the Operation of each jobDetail
						System.out.println("Bulk read Operation: " + jobDetail.getOperation());
						
						//Get the Operation of each jobDetail
						System.out.println("Bulk read State: " + jobDetail.getState().getValue());
						
						//Get the Result instance of each jobDetail
						Result result = jobDetail.getResult();
						
						//Check if Result is not null
						if(result != null)
						{
							//Get the Page of the Result
							System.out.println("Bulkread Result Page: " + result.getPage().toString());
							
							//Get the Count of the Result
							System.out.println("Bulkread Result Count: "+ result.getCount().toString());
							
							//Get the Download URL of the Result
							System.out.println("Bulkread Result Download URL: "+ result.getDownloadUrl());
							
							//Get the Per_Page of the Result
							System.out.println("Bulkread Result Per_Page: "+ result.getPerPage().toString());
							
							//Get the MoreRecords of the Result
							System.out.println("Bulkread Result MoreRecords: "+ result.getMoreRecords().toString());
							
						}
						
						// Get the Query instance of each jobDetail
						Query query = jobDetail.getQuery();
						
						if(query != null)
						{
							//Get the Module Name of the Query
							System.out.println("Bulk read Query Module: " + query.getModule());
							
							//Get the Page of the Query
							System.out.println("Bulk read Query Page: " + query.getPage().toString());
							
							//Get the cvid of the Query
							System.out.println("Bulk read Query cvid: " + query.getCvid());
							
							//Get the fields List of each Query
							List<String> fields = query.getFields();
							
							//Check if fields is not null
							if(fields != null)
							{
								for(Object fieldName : fields)
								{
									//Get the Field Name of the Query
									System.out.println("Bulk read Query Fields: " + fieldName);
								}
							}
							
							// Get the Criteria instance of each Query
							Criteria criteria = query.getCriteria();
							
							//Check if criteria is not null
							if(criteria != null)
							{
								printCriteria(criteria);
							}
						}
						
						//Get the CreatedBy User instance of each jobDetail
						com.zoho.crm.api.users.User createdBy = jobDetail.getCreatedBy();
						
						//Check if createdBy is not null
						if(createdBy != null)
						{
							//Get the ID of the CreatedBy User
							System.out.println("Bulkread Created By User-ID: " + createdBy.getId());
							
							//Get the Name of the CreatedBy User
							System.out.println("Bulkread Created By user-Name: " + createdBy.getName());
						}
						
						//Get the CreatedTime of each jobDetail
						System.out.println("Bulkread CreatedTime: " + jobDetail.getCreatedTime());
						
						//Get the ID of each jobDetail
						System.out.println("Bulkread File Type: " + jobDetail.getFileType());
					}
				}
				//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<String, Object> 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));
				}
			}
		}
	}
	
	private static void printCriteria(Criteria criteria)
    {
		//Get the APIName of the Criteria
		System.out.println("Bulk read Query Criteria APIName: " + criteria.getAPIName());
		
		if(criteria.getComparator() != null)
		{
			//Get the Comparator of the Criteria
			System.out.println("Bulk read Query Criteria Comparator: " + criteria.getComparator().getValue());
		}
		
		if(criteria.getValue() != null)
		{
			//Get the Value of the criteria
			System.out.println("Bulk read Query Criteria Value: " + criteria.getValue().toString());
		}
		
		//Get the List of Criteria instance of each Criteria
		List<Criteria> criteriaGroup = criteria.getGroup();
		
		if(criteriaGroup != null)
		{
			for(Criteria criteria1 : criteriaGroup)
			{
				printCriteria(criteria1);
			}
		}
		
		if(criteria.getGroupOperator() != null)
		{
			//Get the Group Operator of the Criteria
			System.out.println("Bulk read Query Criteria Group Operator: " + criteria.getGroupOperator().getValue());	
		}
    }
}
 
Download Result
              
              
package com.zoho.crm.sample.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 = 3477061000005177002L;
		//String destinationFolder = "/Users/user_name/Documents";
		
		//Get instance of BulkReadOperations Class
		BulkReadOperations bulkReadOperations = new BulkReadOperations();
		
		//Call downloadResult method that takes jobId as parameters
		APIResponse<ResponseHandler> 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<String, Object> 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));
				}
			}	
		}
	}
}