package com.eactive.eai.adapter.jtc;

import tmax.common.BufferAccess;
import tmax.jtc.io.TuxCarrayBuffer;
import tmax.jtc.io.TuxFieldBuffer;
import tmax.jtc.io.TuxStringBuffer;
import tmax.jtmax.JtmaxService;
import tmax.webt.WebtException;
import tmax.webt.WebtSystem;

// TUXEDO -> JEUS
//1.2.2. Bean 서비스 호출 설정
// JTC 인바운드 통신을 통해 Bean 서비스를 호출하려면 다음과 같은 설정이 필요하다.
//
// webt.properties 설정 ($JEUS_HOME/lib/application)
//JEUS SIDE
//webt.properties
//tux.beans.service.list = GSVC02
//tux.beans.GSVC02.class = test.JtcBeanSample
// Tuxedo 환경설정
//TUX Side
//*SERVICE
//GSVC02
// Tuxedo 도메인 환경설정
//*DM_REMOTE_SERVICES
//GSVC01
//         RNAME = "GSVC01"
//         RDOM = "TUXGW1"
//
//*DM_TDOMAIN
//TUXGW1  NWADDR = "//192.168.37.100:9521" 
// JEUS와 Tuxedo 기동

public class JtcBeanSample implements JtmaxService {

	public void destroy() throws Exception {

	}

	public void initialize() throws Exception {

	}

	public BufferAccess service(BufferAccess tux) throws WebtException {
		String bufferType = tux.getBufferTypeString();
		if ("STRING".equals(bufferType)) {
			TuxStringBuffer tuxbuf = (TuxStringBuffer) tux;
			// System.out.println("received string value : " + tuxbuf.getString());
			TuxStringBuffer ret = new TuxStringBuffer(WebtSystem.getDefaultCharset());
			ret.setString(tuxbuf.getString().toUpperCase());
			return ret;
		} else if ("CARRAY".equals(bufferType)) {
			TuxCarrayBuffer tuxbuf = (TuxCarrayBuffer) tux;
			String rcvstr = null;
			try {
				rcvstr = new String( tuxbuf.getBytes(), WebtSystem.getDefaultCharset());
			}
			catch(Exception ex) {
				throw new WebtException(ex.getMessage());
			}
			
			String rtnstr = rcvstr.toUpperCase();
			// System.out.println("received string value : " + rcvstr);
			// System.out.println("return string value : " + rtnstr);
			TuxCarrayBuffer ret = new TuxCarrayBuffer(WebtSystem.getDefaultCharset());
			ret.setBytes(rtnstr.getBytes());;
			return ret;
		} else {
			TuxFieldBuffer tuxbuf = (TuxFieldBuffer) tux;
			// sample table = new sample();
			// String rcvstr =
			// tuxbuf.getField(table.fml_sec100).get().stringValue();

			String rcvstr = tuxbuf.getField("SEC100").get().stringValue();
			String rtnstr = rcvstr.toUpperCase();
			// System.out.println("received string value : " + rcvstr);
			// System.out.println("return string value : " + rtnstr);
			TuxFieldBuffer ret = new TuxFieldBuffer(false); // FML 16임으로 false로
			// ret.createField(sample.fml_sec2).add(rtnstr);
			ret.createField("SEC2").add(rtnstr);
			return ret;
		}
	}
}