package com.digertech.sdk.samples;

import java.io.File;

import com.digertech.sdk.imgvalidate.*;
import com.digertech.sdk.reader.*;
import com.digertech.sdk.records.*;
import com.digertech.sdk.records.x937.*;

/**
* This sample implements ProcessingInterface loads the records 
* from Image Cash Letter file, validate the image data in type
* 52 record and print field value of each record and image 
* validation results when it processes records.
* 
* To run this sample, two jar files in the lib folder should 
* be included in the classpath:
*   FileProcessingSDK.jar
*   ImageValidationSDK.jar
*   
* Execute the commmand to run the sample : 
*    java com.digertech.sdk.samples.SampleX937FileProcessor2 [ICL file] [license key 1] [license key 2]
*           [ICL file]      - ICL file full path name.
*           [license key 1] - File processing SDK license key.
*           [license key 2] - TIFF image validation SDK license key.
*/
public class SampleICLFileReader2 implements ReaderInterface {
	
	private int             itemCount    = 0;
	
	public SampleICLFileReader2() {
		super();
	}
	
	public void readRecord(int recordType, X9Record record) throws FileIOException {
		// skip if this record is null
		if (record == null) return;
		
		// count the record.
		if (recordType == X9Record.TYPE_25 || recordType == X9Record.TYPE_31) {
			itemCount++;
		}

		if (skipRecord(recordType)) return;
		if (recordType == X9Record.TYPE_25 || recordType == X9Record.TYPE_31) {
			System.out.println("                            ");
			System.out.println("  Item Index = " + itemCount);
		} else {
			System.out.println("                            ");
		}
		
		System.out.println("============================");
		System.out.println("|| Process type " + recordType + " record ||");
		System.out.println("============================");
		
		switch(recordType) {
		case X9Record.TYPE_01 :
			processType01Record(record);
			break;
		case X9Record.TYPE_10 :
			processType10Record(record);
			break;
		case X9Record.TYPE_20 :
			processType20Record(record);
			break;
		case X9Record.TYPE_25 :
			processType25Record(record);
			break;
		case X9Record.TYPE_26 :
			processType26Record(record);
			break;
		case X9Record.TYPE_27 :
			processType27Record(record);
			break;
		case X9Record.TYPE_28 :
			processType28Record(record);
			break;
		case X9Record.TYPE_31 :
			processType31Record(record);
			break;
		case X9Record.TYPE_32 :
			processType32Record(record);
			break;
		case X9Record.TYPE_33 :
			processType33Record(record);
			break;
		case X9Record.TYPE_34 :
			processType34Record(record);
			break;
		case X9Record.TYPE_35 :
			processType35Record(record);
			break;
		case X9Record.TYPE_40 :
			processType40Record(record);
			break;
		case X9Record.TYPE_41 :
			processType41Record(record);
			break;
		case X9Record.TYPE_50 :
			processType50Record(record);
			break;
		case X9Record.TYPE_52 :
			processType52Record(record);
			break;
		case X9Record.TYPE_54 :
			processType54Record(record);
			break;
		case X9Record.TYPE_70 :
			processType70Record(record);
			break;
		case X9Record.TYPE_75 :
			processType75Record(record);
			break;
		case X9Record.TYPE_85 :
			processType85Record(record);
			break;
		case X9Record.TYPE_90 :
			processType90Record(record);
			break;
		case X9Record.TYPE_99 :
			processType99Record(record);
			break;
		default :
			processRecord(record);
			break;
		}
	}
	
	private void processType01Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type01Record t01 = (X937Type01Record)record;
			
			System.out.println("Record Type               = " + t01.getRecordType());
			System.out.println("Standard Level            = " + t01.getStandardLevel());
			System.out.println("Test File Ind             = " + t01.getTestFileInd());
			System.out.println("Dest Routing Num          = " + t01.getDestRoutingNum());
			System.out.println("Origin  RoutingNum        = " + t01.getOriginRoutingNum());
			System.out.println("File Create Date          = " + t01.getFileCreateDate());
			System.out.println("File Create Time          = " + t01.getFileCreateTime());
			System.out.println("Resend Ind                = " + t01.getResendInd());
			System.out.println("Dest Name                 = " + t01.getDestName());
			System.out.println("Origin Name               = " + t01.getOriginName());
			System.out.println("File Id Modifier          = " + t01.getFileIdModifier());
			System.out.println("Country Code              = " + t01.getCountryCode());
			System.out.println("User Fie ld               = " + t01.getUserField());
			System.out.println("Reserved                  = " + t01.getReserved());
		}
	}
	
	private void processType10Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type10Record t10 = (X937Type10Record)record;
			
			System.out.println("Record Type               = " + t10.getRecordType());
			System.out.println("Collection Type Ind       = " + t10.getCollectionTypeInd());
			System.out.println("DestRouting Num           = " + t10.getDestRoutingNum());
			System.out.println("Institution Routing Num   = " + t10.getInstitutionRoutingNum());
			System.out.println("Business Date             = " + t10.getBusinessDate());
			System.out.println("Create Date               = " + t10.getCreateDate());
			System.out.println("Create Time               = " + t10.getCreateTime());
			System.out.println("Record Type Ind           = " + t10.getRecordTypeInd());
			System.out.println("Doc Type Ind              = " + t10.getDocTypeInd());
			System.out.println("Cashletter ID             = " + t10.getCashletterID());
			System.out.println("Orig Contact Name         = " + t10.getOrigContactName());
			System.out.println("Orig Contact Phone        = " + t10.getOrigContactPhone());
			System.out.println("Fed Work Type             = " + t10.getFedWorkType());
			System.out.println("User Field                = " + t10.getUserField());
			System.out.println("Reserved                  = " + t10.getReserved());
		}
	}

	private void processType20Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type20Record t20 = (X937Type20Record)record;
			
			System.out.println("Record Type               = " + t20.getRecordType());
			System.out.println("Collection Type Ind       = " + t20.getCollectionTypeInd());
			System.out.println("Dest Routing Num          = " + t20.getDestRoutingNum());
			System.out.println("Institution Routing Num   = " + t20.getInstitutionRoutingNum());
			System.out.println("Business Date             = " + t20.getBusinessDate());
			System.out.println("Create Date               = " + t20.getCreateDate());
			System.out.println("Bundle ID                 = " + t20.getBundleID());
			System.out.println("Bundle Seq Num            = " + t20.getBundleSeqNum());
			System.out.println("Cycle Num                 = " + t20.getCycleNum());
			System.out.println("Return Routing Num        = " + t20.getReturnRoutingNum());
			System.out.println("User Field                = " + t20.getUserField());
			System.out.println("Reserved                  = " + t20.getReserved());
		}
 	}

	private void processType25Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type25Record t25 = (X937Type25Record)record;
			
			System.out.println("Record Type               = " + t25.getRecordType());
			System.out.println("Aux OnUs                  = " + t25.getAuxOnUs());
			System.out.println("Ext Proc Code             = " + t25.getExtProcCode());
			System.out.println("Routing Num               = " + t25.getRoutingNum());
			System.out.println("Routing Num Check Digit   = " + t25.getRoutingNumCheckDigit());
			System.out.println("OnUs Field                = " + t25.getOnUsField());
			System.out.println("Amount                    = " + t25.getAmount());
			System.out.println("Item Seq Num              = " + t25.getItemSeqNum());
			System.out.println("Doc Type Ind              = " + t25.getDocTypeInd());
			System.out.println("Ret Accept Ind            = " + t25.getRetAcceptInd());
			System.out.println("Micr Valid Ind            = " + t25.getMicrValidInd());
			System.out.println("Bofd Ind                  = " + t25.getBofdInd());
			System.out.println("Addendum Count            = " + t25.getAddendumCount());
			System.out.println("Correction Ind            = " + t25.getCorrectionInd());
			System.out.println("Archive Type Ind          = " + t25.getArchiveTypeInd());
		}
	}

	private void processType26Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type26Record t26 = (X937Type26Record)record;
			
			System.out.println("Record Type               = " + t26.getRecordType());
			System.out.println("Collection Type Ind       = " + t26.getAddendumNum());
			System.out.println("Dest Routing Num          = " + t26.getBofdRoutingNum());
			System.out.println("Institution Routing Num   = " + t26.getBofdBusinessDate());
			System.out.println("Business Date             = " + t26.getBofdItemSeqNum());
			System.out.println("Create Date               = " + t26.getBofdDepositAccountNum());
			System.out.println("Create Time               = " + t26.getBofdDepositBranch());
			System.out.println("Record Type Ind           = " + t26.getPayeeName());
			System.out.println("Doc Type Ind              = " + t26.getTruncationInd());
			System.out.println("Cashletter ID             = " + t26.getBofdConversionInd());
			System.out.println("Cashletter ID             = " + t26.getBofdCorrectionInd());
			System.out.println("User Field                = " + t26.getUserField());
			System.out.println("Reserved                  = " + t26.getReserved());
		}
	}

	private void processType27Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type27Record t27 = (X937Type27Record)record;
			
			System.out.println("Record Type               = " + t27.getRecordType());
			System.out.println("Collection Type Ind       = " + t27.getVarRecordInd());
			System.out.println("Dest Routing Num          = " + t27.getMicroArchiveSeqNum());
			System.out.println("Institution Routing Num   = " + t27.getImgArchiveLocatorLen());
			System.out.println("Business Date             = " + t27.getImgArchiveLocatorLen());
			System.out.println("Create Date               = " + t27.getDescription());
			System.out.println("User Field                = " + t27.getUserField());
			System.out.println("Reserved                  = " + t27.getReserved());
		}
	}

	private void processType28Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type28Record t28 = (X937Type28Record)record;
			
			System.out.println("Record Type               = " + t28.getRecordType());
			System.out.println("Addendum Record Num       = " + t28.getAddendumRecordNum());
			System.out.println("Routing Num               = " + t28.getRoutingNum());
			System.out.println("Endorsing Date            = " + t28.getEndorsingDate());
			System.out.println("Item Seq Num              = " + t28.getItemSeqNum());
			System.out.println("Truncation Ind            = " + t28.getTruncationInd());
			System.out.println("Conversion Ind            = " + t28.getConversionInd());
			System.out.println("Correction Ind            = " + t28.getCorrectionInd());
			System.out.println("Return Reason             = " + t28.getReturnReason());
			System.out.println("User Field                = " + t28.getUserField());
			System.out.println("Reserved                  = " + t28.getReserved());
		}
	}

	private void processType31Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type31Record t31 = (X937Type31Record)record;
			
			System.out.println("Record Type               = " + t31.getRecordType());
			System.out.println("Routing Num               = " + t31.getRoutingNum());
			System.out.println("Routing Num Check Digit   = " + t31.getRoutingNumCheckDigit());
			System.out.println("OnUs                      = " + t31.getOnUs());
			System.out.println("Amount                    = " + t31.getAmount());
			System.out.println("Return Reason             = " + t31.getReturnReason());
			System.out.println("Addendum Count            = " + t31.getAddendumCount());
			System.out.println("Doc Type Ind              = " + t31.getDocTypeInd());
			System.out.println("Forward Bundle Date       = " + t31.getForwardBundleDate());
			System.out.println("Item Seq Num              = " + t31.getItemSeqNum());
			System.out.println("Ext Proc Code             = " + t31.getExtProcCode());
			System.out.println("Notification Ind          = " + t31.getNotificationInd());
			System.out.println("Archive Type Ind          = " + t31.getArchiveTypeInd());
			System.out.println("Reserved                  = " + t31.getReserved());
		}
	}

	private void processType32Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type32Record t32 = (X937Type32Record)record;
			
			System.out.println("Record Type               = " + t32.getRecordType());
			System.out.println("Collection Type Ind       = " + t32.getAddendumNum());
			System.out.println("Dest Routing Num          = " + t32.getBofdRoutingNum());
			System.out.println("Institution Routing Num   = " + t32.getBofdBusinessDate());
			System.out.println("Business Date             = " + t32.getBofdItemSeqNum());
			System.out.println("Create Date               = " + t32.getBofdDepositAccountNum());
			System.out.println("Create Time               = " + t32.getBofdDepositBranch());
			System.out.println("Record Type Ind           = " + t32.getPayeeName());
			System.out.println("Doc Type Ind              = " + t32.getTruncationInd());
			System.out.println("Cashletter ID             = " + t32.getBofdConversionInd());
			System.out.println("Cashletter ID             = " + t32.getBofdCorrectionInd());
			System.out.println("User Field                = " + t32.getUserField());
			System.out.println("Reserved                  = " + t32.getReserved());
		}
	}

	private void processType33Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type33Record t33 = (X937Type33Record)record;
			
			System.out.println("Record Type              = " + t33.getRecordType());
			System.out.println("Payor Bank Name          = " + t33.getPayorBankName());
			System.out.println("Aux OnUs                 = " + t33.getAuxOnUs());
			System.out.println("Pay Bank Item Seq Num    = " + t33.getPayBankItemSeqNum());
			System.out.println("Pay Bank Business Date   = " + t33.getPayBankBusinessDate());
			System.out.println("Pay Account Name         = " + t33.getPayAccountName());
		}
	}

	private void processType34Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type34Record t34 = (X937Type34Record)record;
			
			System.out.println("Record Type             = " + t34.getRecordType());
			System.out.println("Var Record Ind          = " + t34.getVarRecordInd());
			System.out.println("Micro Archive Seq Num   = " + t34.getMicroArchiveSeqNum());
			System.out.println("Img Archive LocatorLen  = " + t34.getImgArchiveLocatorLen());
			System.out.println("Img Archive Locator Len = " + t34.getImgArchiveLocatorLen());
			System.out.println("Description             = " + t34.getDescription());
			System.out.println("User Field              = " + t34.getUserField());
			System.out.println("Reserved                = " + t34.getReserved());
		}
	}

	private void processType35Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type35Record t35 = (X937Type35Record)record;
			
			System.out.println("Record Type             = " + t35.getRecordType());
			System.out.println("Addendum Record Num     = " + t35.getAddendumRecordNum());
			System.out.println("Routing Num             = " + t35.getRoutingNum());
			System.out.println("Endorsing Date          = " + t35.getEndorsingDate());
			System.out.println("Item Seq Num            = " + t35.getItemSeqNum());
			System.out.println("Truncation Ind          = " + t35.getTruncationInd());
			System.out.println("Conversion Ind          = " + t35.getConversionInd());
			System.out.println("Correction Ind          = " + t35.getCorrectionInd());
			System.out.println("Return Reason           = " + t35.getReturnReason());
			System.out.println("User Field              = " + t35.getUserField());
			System.out.println("Reserved                = " + t35.getReserved());
		}
	}

	private void processType40Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type40Record t40 = (X937Type40Record)record;
			
			System.out.println("Record Type             = " + t40.getRecordType());
			System.out.println("Dest Routing Num        = " + t40.getDestRoutingNum());
			System.out.println("Low Account             = " + t40.getLowAccount());
			System.out.println("High Account            = " + t40.getHighAccount());
			System.out.println("Total Item Count        = " + t40.getTotalItemCount());
			System.out.println("Total Item Amount       = " + t40.getTotalItemAmount());
			System.out.println("User Field              = " + t40.getUserField());
			System.out.println("Reserved                = " + t40.getReserved());
		}
	}

	private void processType41Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type41Record t41 = (X937Type41Record)record;
			
			System.out.println("Record Type            = " + t41.getRecordType());
			System.out.println("Dest Routing Num       = " + t41.getDestRoutingNum());
			System.out.println("Non-Hit Ind            = " + t41.getNonHitInd());
			System.out.println("Total Item Count       = " + t41.getTotalItemCount());
			System.out.println("Total Item Amount      = " + t41.getTotalItemAmount());
			System.out.println("User Field             = " + t41.getUserField());
			System.out.println("Reserved               = " + t41.getReserved());
		}
	}

	private void processType50Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type50Record t50 = (X937Type50Record)record;
			
			System.out.println("Record Type            = " + t50.getRecordType());
			System.out.println("Image Ind              = " + t50.getImageInd());
			System.out.println("Routing Num            = " + t50.getRoutingNum());
			System.out.println("Create Date            = " + t50.getCreateDate());
			System.out.println("View Format Ind        = " + t50.getViewFormatInd());
			System.out.println("View Compression       = " + t50.getViewCompression());
			System.out.println("View Data Size         = " + t50.getViewDataSize());
			System.out.println("View Side Ind          = " + t50.getViewSideInd());
			System.out.println("View Descriptor        = " + t50.getViewDescriptor());
			System.out.println("Dig Sign Ind           = " + t50.getDigSignInd());
			System.out.println("Dig Sign Method        = " + t50.getDigSignMethod());
			System.out.println("Security Key Size      = " + t50.getSecurityKeySize());
			System.out.println("Protected Data Start   = " + t50.getProtectedDataStart());
			System.out.println("Protected Datat Len    = " + t50.getProtectedDatatLen());
			System.out.println("Img Recreate Ind       = " + t50.getImgRecreateInd());
			System.out.println("User Field             = " + t50.getUserField());
			System.out.println("Reserved               = " + t50.getReserved());
		}
	}

	private void processType52Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type52Record t52 = (X937Type52Record)record;
			
			System.out.println("Record Type            = " + t52.getRecordType());
			System.out.println("Routing Num            = " + t52.getRoutingNum());
			System.out.println("Business Date          = " + t52.getBusinessDate());
			System.out.println("Cycle Num              = " + t52.getCycleNum());
			System.out.println("Item Seq Num           = " + t52.getItemSeqNum());
			System.out.println("Security Orig Name     = " + t52.getSecurityOrigName());
			System.out.println("Security Auth Name     = " + t52.getSecurityAuthName());
			System.out.println("Security Key Name      = " + t52.getSecurityKeyName());
			System.out.println("Clipping Origin        = " + t52.getClippingOrigin());
			System.out.println("Clipping Coordinate H1 = " + t52.getClippingCoordinateH1());
			System.out.println("Clipping Coordinate H2 = " + t52.getClippingCoordinateH2());
			System.out.println("Clipping Coordinate V1 = " + t52.getClippingCoordinateV1());
			System.out.println("Clipping Coordinate V2 = " + t52.getClippingCoordinateV2());
			System.out.println("Image Ref Key Len      = " + t52.getImageRefKeyLen());
			System.out.println("Image Ref Key          = " + t52.getImageRefKey());
			System.out.println("Dig Sig Len            = " + t52.getDigSigLen());
//			System.out.println("Dig Sig                = " + t52.getDigSig());
			System.out.println("Image Data Len         = " + t52.getImageDataLen());
//			System.out.println("Image Data             = " + t52.getImageData());
			validateImage(t52.getImageData());
		}
	}

	private void validateImage(byte[] imageData) {
		ImageValidator  imgValidator = new ImageValidator();

		System.out.println("                                ");
		System.out.println("====== Validate Image Data =====");

		try {
			if (imgValidator.isImageValid(imageData)) {
				System.out.println(" Image is valid. ");
			} else {
				System.out.println(" Image is invalid. ");
			}	

			// return all tags in the image.
			int tags[] = imgValidator.validateImage(imageData);
			
			System.out.println("EOFB            = " + imgValidator.isEOFB());
			System.out.println("LittleEndian    = " + imgValidator.isLittleEndian());
			System.out.println("SinglePage      = " + imgValidator.isSinglePage());
			System.out.println("TagInOrder      = " + imgValidator.isTagInOrder());
			
			for (int i=0;i<tags.length;i++) {
				ImageTagValue tagValue = imgValidator.getTagValue(tags[i]);
				System.out.println("tag            = " + tagValue.getTagNum());
				System.out.println("type           = " + tagValue.getType());
				System.out.println("offset         = " + tagValue.getOffset());
				System.out.println("count          = " + tagValue.getCount());
				System.out.println("value          = " + tagValue.getValue());
				System.out.println("valid code     = " + tagValue.getValidCode());
				System.out.println("valid          = " + tagValue.isValid());
				
				if (!tagValue.isValid()) {
					System.out.println("   Fail On Byte Order   = " + tagValue.isFailOnByteOrder());
					System.out.println("   Fail On Byte Size    = " + tagValue.isFailOnByteSize());
					System.out.println("   Fail On Count        = " + tagValue.isFailOnCount());
					System.out.println("   Fail On Tag Order    = " + tagValue.isFailOnTagOrder());
					System.out.println("   Fail On Type         = " + tagValue.isFailOnType());
					System.out.println("   Fail On Value        = " + tagValue.isFailOnValue());
					System.out.println("   Fail On Value Offset = " + tagValue.isFailOnValueOffset());
				}
				System.out.println("-----------------------");
			}
		} catch (ImageException e) {
			System.out.println("Error validate image. " + e.toString());
		}
		System.out.println("                                ");
	}
	
	private void processType54Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type54Record t54 = (X937Type54Record)record;
			
			System.out.println("Record Type            = " + t54.getRecordType());
			System.out.println("Global Image Quality   = " + t54.getGlobalImageQuality());
			System.out.println("Global Image Usability = " + t54.getGlobalImageUsability());
			System.out.println("Image Bank Specific Test = " + t54.getImageBankSpecificTest());
			System.out.println("Partial Image          = " + t54.getPartialImage());
			System.out.println("Excess Image Skew      = " + t54.getExcessImageSkew());
			System.out.println("Piggyback Image        = " + t54.getPiggybackImage());
			System.out.println("Too Light Or Dark      = " + t54.getTooLightOrDark());
			System.out.println("Streak Bands           = " + t54.getStreakBands());
			System.out.println("Below Min Image Size   = " + t54.getBelowMinImageSize());
			System.out.println("Exceed Max Image Size  = " + t54.getExceedMaxImageSize());
			System.out.println("Reserved 12_24         = " + t54.getReserved12_24());

			System.out.println("Image Enabled Pod      = " + t54.getImageEnabledPod());
			System.out.println("Source Doc Bad         = " + t54.getSourceDocBad());
			System.out.println("Date Usability         = " + t54.getDateUsability());
			System.out.println("Payee Usability        = " + t54.getPayeeUsability());
			System.out.println("Convenient Amount Usability = " + t54.getConvenientAmountUsability());
			System.out.println("Amount Words Usability = " + t54.getAmountWordsUsability());
			System.out.println("Signature Usability    = " + t54.getSignatureUsability());
			System.out.println("Payor Name Address Usability = " + t54.getPayorNameAddressUsability());
			System.out.println("Micr Line Usability    = " + t54.getMicrLineUsability());
			System.out.println("Memo Line Usability    = " + t54.getMemoLineUsability());
			System.out.println("Payor Bank Name Address Usability = " + t54.getPayorBankNameAddressUsability());
			System.out.println("Payee Endorsement Usability = " + t54.getPayeeEndorsementUsability());
			System.out.println("Bofd Endorsement Usability = " + t54.getBofdEndorsementUsability());
			System.out.println("Transit Endorsement Usability = " + t54.getTransitEndorsementUsability());
			System.out.println("Reserved 39_44         = " + t54.getReserved39_44());

			System.out.println("User Field             = " + t54.getUserField());
			System.out.println("Reserved               = " + t54.getReserved());
		}
	}

	private void processType70Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type70Record t70 = (X937Type70Record)record;
			
			System.out.println("Record Type            = " + t70.getRecordType());
			System.out.println("Item Count             = " + t70.getItemCount());
			System.out.println("Total Amount           = " + t70.getTotalAmount());
			System.out.println("Micr Total Amount      = " + t70.getMicrTotalAmount());
			System.out.println("Image Count            = " + t70.getImageCount());
			System.out.println("User Field             = " + t70.getUserField());
			System.out.println("Reserved               = " + t70.getReserved());
		}
	}

	private void processType75Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type75Record t75 = (X937Type75Record)record;
			
			System.out.println("Record Type            = " + t75.getRecordType());
			System.out.println("Dest Routing Num       = " + t75.getDestRoutingNum());
			System.out.println("Seq Num                = " + t75.getSeqNum());
			System.out.println("Bundle Count           = " + t75.getBundleCount());
			System.out.println("Number Id              = " + t75.getNumberId());
			System.out.println("Total Amount           = " + t75.getTotalAmount());
			System.out.println("Reserved               = " + t75.getReserved());
		}
	}

	private void processType85Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type85Record t85 = (X937Type85Record)record;
			
			System.out.println("Record Type            = " + t85.getRecordType());
			System.out.println("Routing Num            = " + t85.getRoutingNum());
			System.out.println("Total Amount           = " + t85.getTotalAmount());
			System.out.println("Item Count             = " + t85.getItemCount());
			System.out.println("UserField              = " + t85.getUserField());
			System.out.println("Reserved               = " + t85.getReserved());
		}
	}

	private void processType90Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type90Record t90 = (X937Type90Record)record;
			
			System.out.println("Record Type           = " + t90.getRecordType());
			System.out.println("Bundle Count          = " + t90.getBundleCount());
			System.out.println("Item Count            = " + t90.getItemCount());
			System.out.println("Total Amount          = " + t90.getTotalAmount());
			System.out.println("Image Count           = " + t90.getImageCount());
			System.out.println("Institution Name      = " + t90.getInstitutionName());
			System.out.println("Settlement Date       = " + t90.getSettlementDate());
			System.out.println("Reserved              = " + t90.getReserved());
		}
	}

	private void processType99Record(X9Record record) {
		if (record.getFormat() == X9Record.X937_2003) {
			X937Type99Record t99 = (X937Type99Record)record;
			
			System.out.println("Record Type          = " + t99.getRecordType());
			System.out.println("Cashletter Count     = " + t99.getCashletterCount());
			System.out.println("Total Record Count   = " + t99.getTotalRecordCount());
			System.out.println("Total Item Count     = " + t99.getTotalItemCount());
			System.out.println("Total Amount         = " + t99.getTotalAmount());
			System.out.println("Contact Name         = " + t99.getContactName());
			System.out.println("Contact Phone        = " + t99.getContactPhone());
			System.out.println("Reserved             = " + t99.getReserved());
		}
	}
	
	private void processRecord(X9Record record) {
		// do something with this record...
	}
	
	private boolean skipRecord(int recordType) {
		if (itemCount <= 10) {
			// print all records if item index less than 10
			return false;
		} else if (itemCount <= 500) {
			if (itemCount % 50 == 0) {
				// print records for every 50 item if item index less than 500
				return false;
			}
		} else if (itemCount <= 10000) {
			if (itemCount % 500 == 0) {
				// print records for every 500 item if item index less than 10000
				return false;
			}
		} else {
			if (itemCount % 1000 == 0) {
				// print records for every 1000 item
				return false;
			}
		}
		
		if (recordType == X9Record.TYPE_10 ||
			recordType == X9Record.TYPE_20 ||
			recordType == X9Record.TYPE_70 ||
			recordType == X9Record.TYPE_75 ||
			recordType == X9Record.TYPE_85 ||
			recordType == X9Record.TYPE_90 ||
			recordType == X9Record.TYPE_99) {
			
			return false;
		} else {
			return true;
		}
	}
	
	public static void setLicenseKey() {
		// The license key contains 25 chars. You will recieve the license key after you
		// purchase the SDK product.
		String processLicenseKey  = "XXXXXXXXXXXXXXXXXXXXXXXXX";
		String validateLicenseKey = "YYYYYYYYYYYYYYYYYYYYYYYYY";

		setLicenseKey(processLicenseKey, validateLicenseKey);
	}
	public static void setLicenseKey(String processLicenseKey, String validateLicenseKey) {
		System.out.println("************************************************************************");
		System.out.println("*  DigerTech Inc. ICL File Processing and Image Validation SDK Sample  *");
		System.out.println("*                                                                      *");
		System.out.println("*                         Version : " + FPLicenseInfo.VERSION + "                              *");
		System.out.println("************************************************************************");
		System.out.println("                                                                        ");

		// Make sure the license key is set to the SDK before it starts to process files
		// or validate the images. Set the license key only one time when the JVM starts.
		
		// If the license key is valid, SDK will return the valid product id of the license.
		// othwise the productId is free version
		int productId = DTFileReader.setLicenseKey(processLicenseKey);
		
		if (productId == DTFileReader.SDK_FILE_READER_DEVELOPER) {
			System.out.println("This is a Developer version SDK license. SDK can process file less than 500 items.");
		} else if (productId == DTFileReader.SDK_FILE_READER_ENTERPRISE_I) {
			System.out.println("This is an Enterprise I version SDK license. SDK can process file less than 10000 items.");
		} else if (productId == DTFileReader.SDK_FILE_READER_ENTERPRISE_II) {
			System.out.println("This is an Enterprise II version SDK license. SDK can process file unlimited items.");
 		} else {
			System.out.println("This is a free version SDK license. SDK can process file less than 10 items.");
		}
		FPLicenseInfo fpLicenseInfo = DTFileReader.getLicenseInfo();
		System.out.println("------------------------- ");
		System.out.println("Computer Id             = " + fpLicenseInfo.getComputerId());
		System.out.println("Product Id              = " + fpLicenseInfo.getProductId());
		System.out.println("Version                 = " + fpLicenseInfo.getVersion());
		System.out.println("Permanent               = " + fpLicenseInfo.isPermanent());
		System.out.println("Expired                 = " + fpLicenseInfo.isExpired());
		System.out.println("Maintiance Expired Date = " + fpLicenseInfo.getMaintainanceDate());
		System.out.println("------------------------- ");
		
		productId = ImageValidator.setLicenseKey(validateLicenseKey);
		
		if (productId == ImageValidator.SDK_IMAGE_VALIDATE_DEVELOPER) {
			System.out.println("This is a Developer version SDK license. SDK can check or validate less than 1000 images.");
		} else if (productId == ImageValidator.SDK_IMAGE_VALIDATE_ENTERPRISE_I) {
			System.out.println("This is an Enterprise I version SDK license. SDK can only check unlimited images.");
		} else if (productId == ImageValidator.SDK_IMAGE_VALIDATE_ENTERPRISE_II) {
			System.out.println("This is an Enterprise II version SDK license. SDK can check or validate unlimited images.");
 		} else {
			System.out.println("This is a free version SDK license. SDK can check or validate less than 20 images.");
		}

		IVLicenseInfo ivLicenseInfo = ImageValidator.getLicenseInfo();
		System.out.println("------------------------- ");
		System.out.println("Computer Id             = " + ivLicenseInfo.getComputerId());
		System.out.println("Product Id              = " + ivLicenseInfo.getProductId());
		System.out.println("Version                 = " + ivLicenseInfo.getVersion());
		System.out.println("Permanent               = " + ivLicenseInfo.isPermanent());
		System.out.println("Expired                 = " + ivLicenseInfo.isExpired());
		System.out.println("Maintiance Expired Date = " + ivLicenseInfo.getMaintainanceDate());
		System.out.println("------------------------- ");
	}

	
	public static void main(String args[]) {
		if (args == null || args.length < 1) {
			System.out.println(new StringBuffer().append("Usage:  \n") 
				  .append("java com.digertech.sdk.samples.SampleX937FileProcessor2 [ICL file] [license key 1] [license key 2]\n")
				  .append("           [ICL file]      - ICL file with full path name.\n")
				  .append("           [license key 1] - File processing SDK license key.\n")
				  .append("           [license key 2] - TIFF image validation SDK license key.\n")
				  .toString());
		} else {
			// Set the SDK license key before we process any files.
			// If it is better to set SDK license key when the JVM starts.
			if (args.length == 3) {
				setLicenseKey(args[1], args[2]);
			} else {
				setLicenseKey();
			}
			
			File file                         = new File(args[0]);
			DTFileReader reader               = null;
			SampleICLFileReader2 processor = null;
			
			try {
				// Initialize the DTFileReader which will parse the records in
				// the ICL file.
				reader    = new DTFileReader(file);
				
				// Initialize this sample file which implements the ProcessingInterface
				processor = new SampleICLFileReader2();
				
				// Call the readRecords method in the DTFileReader which read all 
				// records and call the processRecord method in this sample class
				// to print each field value of all records in the ICL file.
				reader.readRecords(processor);
				
			} catch (FileIOException e) {
				System.out.println("Error reading ICL file. " + e.toString());
			} finally {
				if (reader != null) {
					// ensure to close the ICL file after the file is processed.
					try {
						reader.close();
					} catch (FileIOException e) {
						System.out.println("Error closing ICL file. " + e.toString());
					}
				}
			}
			
		}
	}

}
