Scala SDK Samples - Field Attachment Operations

Get Field Attachments
              
              
package com.zoho.crm.sample.fieldattachments

import java.util
import java.io.{File, FileOutputStream}
import com.zoho.crm.api.fieldattachments.FieldAttachmentsOperations
import com.zoho.crm.api.fieldattachments.APIException
import com.zoho.crm.api.fieldattachments.FileBodyWrapper

object FieldAttachments {
    /*
	 *   Get FieldAttachments 
	 * This method is used to get Field Attachments
	 * @param moduleAPIname The module name of the Module
	 * @param recordId The record Id of the record
	 * @param attachmentID The attachment ID of the attachment
	 * @throws Exception
	 */
    @throws[Exception]
	def getFieldAttachments(destinationFolder: String, moduleAPIname: String, recordId: Long, attachmentID: Long): Unit = {
		//Get instance of FileOperations Class
		val fieldAttachmentsOperations = new FieldAttachmentsOperations(moduleAPIname, recordId, Option(attachmentID))
	
		//Call getFile method that takes paramInstance as parameters
		val responseOption = fieldAttachmentsOperations.getFieldAttachments()
		
		if (responseOption.isDefined) { //check response
			var response = responseOption.get
			println("Status Code: " + response.getStatusCode)
			if (util.Arrays.asList(204, 304).contains(response.getStatusCode)) {
				println(if (response.getStatusCode == 204) "No Content"
				else "Not Modified")
				return
			}
			
			//Check if expected response is received
			if (response.isExpected) {
        val responseHandler = response.getObject
        responseHandler match {
          case fileBodyWrapper: FileBodyWrapper =>
            //Get StreamWrapper instance from the returned FileBodyWrapper instance
            val streamWrapper = fileBodyWrapper.getFile.get
            //Create a file instance with the absolute_file_path
            val file = new File(destinationFolder + java.io.File.separatorChar + streamWrapper.getName.get)
            //Get InputStream from the response
            val is = streamWrapper.getStream.get
            //Create an OutputStream for the destination file
            val os = new FileOutputStream(file)
            val buffer = new Array[Byte](1024)
            var bytesRead: Integer = 0
            //read the InputStream till the end
            while ( {
              bytesRead = is.read(buffer)
              bytesRead != -1
            }) { //write data to OutputStream
              os.write(buffer, 0, bytesRead)
            }
            //Close the InputStream
            is.close()
            //Flush and close the OutputStream
            os.flush()
            os.close()
          case exception: APIException =>
            println("Status: " + exception.getStatus.getValue)
            println("Code: " + exception.getCode.getValue)
            println("Details: ")

            exception.getDetails.foreach(entry => {
              println(entry._1 + ": " + entry._2)
            })
            println("Message: " + exception.getMessage.getValue)
          case _ =>
        }
      }
      else if (response.getStatusCode != 204) {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}