/*
 * Created on Nov 30, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.orc.ibatis;

import java.io.FileOutputStream;
import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**
 * @author Owen Cline
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class TestIbatis {

//	Init Log4J if I want to log messages from my class
//	static Logger logger = Logger.getLogger(TestIbatis.class); 
	
	
	private static SqlMapClient sqlMap = null;
	public static final String IBATIS_XML_CONFIG_FILE = "sql-map-config.xml";

	static {
		try {
			// Log message via Log4J
//			logger.info( "Executing:"); 			
			String resource = IBATIS_XML_CONFIG_FILE;
			Reader reader = Resources.getResourceAsReader(resource);
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static SqlMapClient getSqlMapInstance() {
		return sqlMap;
	}

	public static void main(String[] args) {
		try {
		
			sqlMap.startTransaction();
			String s="a test string This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored." +
					"Subclasses of OutputStream must provide an implementation for this method." +
					"sWrites b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length)." +
					"Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation." +
					"The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation." +
					"If b is null, a NullPointerException is thrown." +
					"If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.  ";
			byte[] blobs=s.getBytes("iso-8859-1");
			//byte[] blobs=s.getBytes("IDENTITY-H");
			//byte[]blobs={ 15, -45, -124, 59 };
			Employee emp=new Employee();
			emp.setEmpno(45);
			emp.setName("abc");
			emp.setSalary(30000);
			emp.setDesc(blobs);
			sqlMap.insert("insertingBlob", emp);
			sqlMap.commitTransaction();
			sqlMap.endTransaction();
			System.out.println("done with insert");
			
			Employee emp1 = (Employee) sqlMap.queryForObject("getEmployee", "45");
			System.out.println(
				"Single Lookup - Employee Number " + emp1.getEmpno() +
					"    Name: "
					+ emp1.getName()
					+ "  Salary: "
					+ emp1.getSalary()
					);
			byte[] desc=emp1.getDesc();
			
		
			
			String filename="abc.pdf";
			FileOutputStream fos=new FileOutputStream(filename);
			
			fos.write(desc);
			fos.close();
			/*String filename1="abc.pdf";
			File file=new File(filename1);
			FileWriter fw=new FileWriter(file);
			fw.write(text);
			fw.close();*/
			System.out.println("done");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
