/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JWCCmdHakko
*   ソースファイル名：JWCCmdHakko.java
*   作成者          ：富士通
*   日付            ：2011年07月06日
*＜機能概要＞
*   視聴制御コマンド発行部品です。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/07/06   FJ) 富士通  新規作成
*
**********************************************************************/
package eo.common.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Properties;

import eo.common.constant.JWCStrConst;

import javax.mail.internet.InternetAddress;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.fujitsu.futurity.common.JSYLogBase;
import com.fujitsu.futurity.model.common.JCMAPLConstMgr;
import com.sun.activation.registries.MailcapParseException;


/**
 * 視聴制御コマンド発行部品です。<p>
 * <br>
 * @author 富士通
 */
public class JWCCmdHakko 
{
	
	// HTTP関連定数定義
	/**
	 * HTTPヘッダ Content-TYPE
	 */
	private static final String CONTENT_TYPE = "Content-Type";
	
	/**
	 * HTTPヘッダ Conten-TYPE（値）
	 */
	private static final String CONTENT_TYPE_VALUE = "application/soap+xml; charset=UTF-8";

	/**
	 * HTTPヘッダ リクエストメソッド定数
	 */
	private static final String HTTP_REQUEST_METHOD = "POST";
	

	//SOAP 関連定数定義
	/**
	 * SOAP1.2用Namespace
	 */
	private static final String SOAP12 = "http://www.w3.org/2003/05/soap-envelope";
	
	/**
	 * SOAPエンベロープのPrefix
	 */
	private static final String SOAP_PREFIX = "soapenv";
	
	
	// SAOP-ENV:body内部系
	/**
	 * SOAP-ENV BODY用のPrefix
	 */
	private static final String BODY_PREFIX = "ext";

	/**
	 * パッケージ用Namespace
	 */
	private static final String BODY_NAMESPACE = "http://extapi.passasp.itec.hankyu-hanshin.co.jp/";

	
	/**
	 * XSL用Prefix
	 */
	private static final String XSL_PREFIX = "xsl";
	
	/**
	 * XSL用Namespace
	 */
	private static final String XSL_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
	
	
	// CRLF定数定義
	/**
	 * CRLF定数定義
	 */
	private static final String CRLF = "\r\n";

	// SOAP ACTION定義名
	/**
	 * SOAP ACTION(仮登録（リンク）・設置)
	 */
	private static final String ACTION_SET = "Set";
	
	/**
	 * SOAP ACTION(仮登録（アンリンク）・撤去)
	 */
	private static final String ACTION_UNSET = "UnSet";

	/**
	 * SOAP ACTION(契約登録・更新)
	 */
	private static final String ACTION_CONTROL = "Control";

	/**
	 * SOAP ACTION(C-CAS交換)
	 */
	private static final String ACTION_CCASCHG = "CCASChg";

	/**
	 * SOAP ACTION(B-CAS交換)
	 */
	private static final String ACTION_BCASCHG = "BCASChg";

	/**
	 * SOAP ACTION(再登録)
	 */
	private static final String ACTION_RESET = "ReSet";

	/**
	 * SOAP ACTION(撤去後STBクリアー)
	 */
	private static final String ACTION_STBCLEAR = "STBClear";

	/**
	 * SOAP ACTION(撤去後C-CASクリアー)
	 */
	private static final String ACTION_CCASCLEAR = "CCASClear";

	/**
	 * SOAP ACTION(パスワード初期化登録)
	 */
	private static final String ACTION_PASSCLS = "PassCls";
	
	/**
	 * SOAP ACTION(メッセージ送信（個別）)
	 */
	private static final String ACTION_STBMSGSND = "StbMsgSnd";

	/**
	 * SOAP ACTION(メッセージ送信（グループ）)
	 */
	private static final String ACTION_GRPMSGSND = "GrpMsgSnd";

	/**
	 * SOAP ACTION(メッセージ送信停止)
	 */
	private static final String ACTION_MSGSNDSTOP = "MsgSndStop";

	/**
	 * SOAP ACTION(制御結果実績取得)
	 */
	private static final String ACTION_RESULT = "GetResult";

	
	// XML要素情報定数定義（結果コード取得電文）
	/**
	 * SOALFAULT（処理例外）
	 */
	private static final String SOAP_FAULT = "fault";
	
	/**
	 * SOALFAULT（エラーコード）
	 */
	private static final String SOAP_FAULT_CODE = "reason";

	/**
	 * SOALFAULT（エラー詳細）
	 */
	private static final String SOAP_FAULT_STRING = "detail";

	/**
	 * コマンド要求電文結果情報(controlResultInfo)
	 */
	private static final String CONTROL_RESULT_INFO = "ControlResultInfo";

	/**
	 * 制御要求キー
	 */
	private static final String REQUESTKEY = "RequestKey";
	/**
	 * 連番
	 */
	private static final String SEQNO = "SeqNo";
	/**
	 * 制御エリア
	 */
	private static final String CONTROLAREA = "ControlArea";
	/**
	 * 制御内容
	 */
	private static final String CONTROLTYPE = "ControlType";
	/**
	 * STB ID
	 */
	private static final String ADDRESS = "Address";
	/**
	 * 製造番号
	 */
	private static final String PRODUCTNO = "ProductNo";
	/**
	 * C-CAS ID
	 */
	private static final String CCAS = "Ccas";
	/**
	 * B-CAS ID
	 */
	private static final String BCAS = "Bcas";
	/**
	 * 制御結果
	 */
	private static final String CONTROLRESULT = "ControlResult";
	/**
	 * 制御エラー応答コード
	 */
	private static final String ERRCODE = "ErrCode";

	/**
	 * URL(デフォルト）
	 */
	private static final String DEFAULT_URL = "http://extapi.passasp.itec.hankyu-hanshin.co.jp/";

	
	// メッセージwriterから実装を移管

	/**
	 * XMLStreamWriterクラス
	 */
	private XMLStreamWriter writer = null;
	
	/**
	 * Writerクラス
	 */
	private Writer wt = null;
	
	// 文字化け対策
	/**
	 * StringWriterクラス
	 */
	private StringWriter sw = new StringWriter();

	/**
	 * 戻り電文がSoapメッセージ
	 */
	private boolean soapMessage = false;

	/**
	 * 戻り電文HTTPヘッダ
	 */
	private HashMap<Object, Object> header = new HashMap<Object, Object>();
	
	/**
	 * エラーコード（ERR9999)
	 */
	private static final String ERR9999 = "ERR9999";
	
	/**
	 * データ定義、XML要素変換テーブル
	 */
	private static HashMap<String, String> elementToMap = new HashMap<String, String>() 
	{
		{
			//局コード
			put(JWCStrConst.KYOKU_CD, "StationCode");
			//制御エリア
			put(JWCStrConst.CTRL_AREA_CD, "ControlArea");
			//制御要求キー
			put(JWCStrConst.CTRL_YOKYU_KEY, "RequestKey");
			//制御タイプ
			put(JWCStrConst.CTRL_TYPE_CD, "Type");
			//制御モード
			put(JWCStrConst.CTRL_MODE_CD, "CtlMode");
			//STB ID
			put(JWCStrConst.STB_ID, "Address");
			//製造番号
			put(JWCStrConst.STB_KIKI_SEIZO_NO, "ProductNo");
			//制御用機種コード   
			put(JWCStrConst.STB_CTRL_MODEL_CD, "EquipmentModelID");
			//C-CAS内蔵タイプSTB
			put(JWCStrConst.STB_CAS_NAIZO_FLG, "CcasFlg");
			//C-CAS ID
			put(JWCStrConst.CCAS_CARD_NO, "Ccas");
			//B-CAS ID
			put(JWCStrConst.BCAS_CARD_NO, "Bcas");
			//デジタルティア(Remux)明細
			put(JWCStrConst.EWC0011D010CBSMSG1LIST, "TierRemux");
			//デジタルティア(Remux)
			put(JWCStrConst.DGTIA_CD1, "int");
			//デジタルティア(TM)領域０明細
			put(JWCStrConst.EWC0011D010CBSMSG2LIST, "TierTM0");
			//デジタルティア(TM)領域０
			put(JWCStrConst.DGTIA_CD2, "int");
			//デジタルティア(TM)領域１明細
			put(JWCStrConst.EWC0011D010CBSMSG3LIST, "TierTM1");
			//デジタルティア(TM)領域１
			put(JWCStrConst.DGTIA_CD3, "int");
			//デジタルティア(TM)領域２明細
			put(JWCStrConst.EWC0011D010CBSMSG4LIST, "TierTM2");
			//デジタルティア(TM)領域２
			put(JWCStrConst.DGTIA_CD4, "int");
			//デジタルティア(TM)領域３明細
			put(JWCStrConst.EWC0011D010CBSMSG5LIST, "TierTM3");
			//デジタルティア(TM)領域３
			put(JWCStrConst.DGTIA_CD5, "int");
			//課金フラグ
			put(JWCStrConst.CHRG_FLG, "Payflg");
			//グループＩＤ明細
			put(JWCStrConst.EWC0011D010CBSMSG6LIST, "GroupID");
			//グループＩＤ
			put(JWCStrConst.GRP_ID, "string");
			//C-CAS初期化コマンド発行有無
			put(JWCStrConst.CCAS_SHKKA_CMD_HAK_FLG, "ClrCtlFlg");
			//再設定フラグ
			put(JWCStrConst.RE_SETTE_FLG, "Mode");
			//グループID値域コード	
			put(JWCStrConst.GRP_ID_RANGE_CD, "GroupNo");
			//STBグループID
			put(JWCStrConst.STB_GRP_ID, "GroupID");
			//HEメッセージID
			put(JWCStrConst.HE_MSG_ID, "MessageID");
			//メッセージ送出年月日時分秒
			put(JWCStrConst.MSG_SEND_DTM, "StartDateTime");
			//メッセージ停止年月日時分秒
			put(JWCStrConst.MSG_STP_DTM, "EndDateTime");
			//STBメッセージ内容
			put(JWCStrConst.STB_MSG_NAIYO, "Message");
		}
	};

	/**
	 * データ定義、XML要素変換テーブル（戻り電文）
	 */
	private static HashMap<String, String> mapToElement = new HashMap<String, String>() 
	{
		{
			//局コード
			put("StationCode", JWCStrConst.KYOKU_CD);
			//制御要求キー
			put(JWCCmdHakko.REQUESTKEY, JWCStrConst.CTRL_YOKYU_KEY);
			//連番
			put(JWCCmdHakko.SEQNO, JWCStrConst.RSLT_SEQ);
			//制御エリア
			put(JWCCmdHakko.CONTROLAREA, JWCStrConst.CTRL_AREA_CD);
			//制御内容
			put(JWCCmdHakko.CONTROLTYPE, JWCStrConst.CTRL_NAIYO);
			//STB ID
			put(JWCCmdHakko.ADDRESS, JWCStrConst.STB_ID);
			//製造番号
			put(JWCCmdHakko.PRODUCTNO, JWCStrConst.STB_KIKI_SEIZO_NO);
			//C-CAS ID
			put(JWCCmdHakko.CCAS, JWCStrConst.CCAS_CARD_NO);
			//B-CAS ID
			put(JWCCmdHakko.BCAS, JWCStrConst.BCAS_CARD_NO);
			//制御結果
			put(JWCCmdHakko.CONTROLRESULT, JWCStrConst.CTRL_RSLT_CD);
			//制御結果エラーコード
			put(JWCCmdHakko.ERRCODE, JWCStrConst.CTRL_ERR_RESPONSE_CD);
		}
	};

	/**
	 *  視聴制御コマンド発行処理を行います。
	 *  <br>
	 * @param serviceMap 視聴制御オーダ送信のインターフェイス情報
	 * @return 処理結果コード値　0：正常　1:異常
	 */
	public static HashMap<Object, Object> WctrlCmdHakko(HashMap<Object, Object>serviceMap) 
	{
		JWCCmdHakko obj = new JWCCmdHakko();
		HashMap<Object, Object>result = null;

		// 視聴制御オーダーチェック処理
		int check = obj.checkOrderCode(serviceMap, 1, 9);

		if(check != 0) {
			result = new HashMap<Object, Object>();
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
			result.put(JWCStrConst.CMD_ERROR_MESSAGE,"視聴制御オーダーコードが範囲外です。");
			return result;
		}
		
		// 電文送信処理の実行
		result = obj.execute(serviceMap, false);
		
		return result;
	}
	
	/**
	 *  STBメッセージコマンド発行処理を行います。
	 *  <br>
	 * @param serviceMap 視聴制御オーダ送信のインターフェイス情報
	 * @return 処理結果コード値　0：正常　1:異常
	 */
	public static HashMap<Object, Object> StbMsgCmdHakko(HashMap<Object, Object>serviceMap) 
	{
		JWCCmdHakko obj = new JWCCmdHakko();
		HashMap<Object, Object>result = null;

		// 視聴制御オーダーチェック処理
		int check = obj.checkOrderCode(serviceMap, 10, 12);

		if(check != 0) {
			result = new HashMap<Object, Object>();
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
			result.put(JWCStrConst.CMD_ERROR_MESSAGE,"視聴制御オーダーコードが範囲外です。");
			return result;
		}
		
		result = obj.execute(serviceMap, false);
		return result;
	}
	
	/**
	 *  コマンド結果発行処理を行います。
	 *  <br>
	 * @param serviceMap 視聴制御オーダ送信のインターフェイス情報
	 * @return 処理結果コード値　0：正常　1:異常
	 */
	public static HashMap<Object, Object> CmdRsltHakko(HashMap<Object, Object>serviceMap) 
	{
		JWCCmdHakko obj = new JWCCmdHakko();
		HashMap<Object, Object>result = null;
		
		serviceMap.put(JWCStrConst.WCTRL_ORDER_CD, "13");
		
		// 視聴制御オーダーチェック処理
		int check = obj.checkOrderCode(serviceMap, 1, 13);


		result = obj.execute(serviceMap, true);
		return result;
	}
	

	/**
	 * SOAP電文送信処理を実行する。
	 * 
	 * @param serviceMap 視聴制御オーダ送信のインターフェイス情報
	 * @param resultFlg コマンド結果発行処理電文有無 true:コマンド結果発行処理 false：視聴制御コマンド実行処理
	 * @return 処理結果コード値　0：正常　1:異常
	 */
	private HashMap<Object, Object>  execute(Map<Object, Object>serviceMap,boolean resultFlg) 
	{
		HashMap<Object, Object> result = new HashMap<Object, Object>();
		HttpURLConnection connection = null;
		InputStream is = null;
		InetAddress inetaddress = null;
		String responseContentType = null;
		OutputStream os = null;
		try
		{
			URL url = null;
			
			
			String urlString = JWCCmdHakko.DEFAULT_URL;

			// URLをプロパティファイルから取得
			if(serviceMap.containsKey(JWCStrConst.CMD_URL)) {
				urlString = (String)serviceMap.get(JWCStrConst.CMD_URL);
			}
			
			if(resultFlg) 
			{
				urlString = urlString + "commonControl.asmx";
			}
			else 
			{
				urlString = urlString + "PanaCasControl.asmx";
			}
			url = new URL(urlString);
			// 電文を送信
			connection = (HttpURLConnection)url.openConnection();
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			// PKGとのSOAP通信におけるタイムアウト値
			Integer pkg_Timeout =(Integer.parseInt(JCMAPLConstMgr.getString(JWCStrConst.WC_PKG_TIMEOUT) ));
			connection.setReadTimeout(pkg_Timeout);
			
			
			connection.setRequestMethod(JWCCmdHakko.HTTP_REQUEST_METHOD);
			connection.setRequestProperty(JWCCmdHakko.CONTENT_TYPE, JWCCmdHakko.CONTENT_TYPE_VALUE);
			
			os = connection.getOutputStream();
			
			this.write(serviceMap, os);
			
			connection.connect();
			
			inetaddress =  InetAddress.getLocalHost();

			// 電文の戻りを取得
			is = connection.getInputStream();

			responseContentType = connection.getHeaderField("Content-Type");
				
			result = this.read(inetaddress.getHostName(), inetaddress.getHostAddress(), responseContentType, is);
			
			return result;
		}
		catch(XMLStreamException e) 
		{
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
			result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + e.getMessage());	
			return result;
		}
		catch(MalformedURLException e) 
		{
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
			result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + e.getMessage());	
			return result;
		}
		catch (IOException e) 
		{
			
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			if(connection != null && inetaddress != null)
			{
				is = connection.getErrorStream();
				responseContentType = connection.getHeaderField("Content-Type");
				try 
				{
					if(is != null)
					{
						String msg = connection.getResponseMessage();
						result = this.read(inetaddress.getHostName(), inetaddress.getHostAddress(), responseContentType, is);
					}
					else 
					{
						result.put(JWCStrConst.CMD_RESULT_CD,"1");
						result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
						result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + e.getMessage());
					}
				}
				catch(Exception ex) 
				{
					result.put(JWCStrConst.CMD_RESULT_CD,"1");
					result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
					result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + ex.getMessage());	
					return result;
				}
			}
			if(result != null && result.get(JWCStrConst.CMD_ERROR_MESSAGE) != null )
			{
				result.put(JWCStrConst.CMD_RESULT_CD,"1");
			}
			else 
			{
				result.put(JWCStrConst.CMD_RESULT_CD,"1");
				result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
				result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + e.getMessage());	
			}
			return result;
		}
		catch (ParserConfigurationException e) 
		{
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
			result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + e.getMessage());	
			return result;
		}
		catch (SAXException e) 
		{
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
			result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + e.getMessage());	
			return result;
		}
		catch(Exception e) 
		{
			result.put(JWCStrConst.CMD_RESULT_CD,"1");
			result.put(JWCStrConst.CMD_ERROR_CD, ERR9999);
			result.put(JWCStrConst.CMD_ERROR_MESSAGE,"XML電文処理でエラーが発生しました。" + e.getMessage());	
			return result;
			
		}
		finally
		{
			if(os != null)
			{
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is != null)
			{
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	
	}

	
	/**
	 * 電文送信初期処理
	 * @param wt Writerオブジェクト
	 */
	private void init(Writer wt) 
	throws FactoryConfigurationError,XMLStreamException
	{
		this.wt = wt;
		
		XMLOutputFactory outfactory = XMLOutputFactory.newInstance();
		this.writer = outfactory.createXMLStreamWriter(this.sw);
		this.writer.setPrefix(SOAP_PREFIX, SOAP12);
			
	}
	/**
	 * クローズ処理
	 */
	private void close()
	throws XMLStreamException,IOException
	{
		
		writer.flush();
		writer.close();

		// 最後に全部出す。
		this.wt.write(sw.toString());
	}
	
	
	/**
	 * XML属性書き込み処理
	 * 
	 * @param name 属性名
	 * @param value　値
	 * @throws XMLStreamException
	 */
	private void writeAttribute(String name, String value) 
	throws XMLStreamException
	{
		this.writer.writeAttribute(name, value);
	}

	
	/**
	 * XML名前空間書き込み処理
	 * 
	 * @param prefix Prefix
	 * @param uri 名前空間URL
	 * @throws XMLStreamException
	 */
	private void writeNamespace(String prefix, String uri) 
	throws XMLStreamException
	{
		this.writer.writeNamespace(prefix, uri);
	}

	/**
	 * XMLドキュメント開始タグ出力処理
	 * 
	 * @param encoding
	 * @throws XMLStreamException
	 */
	private void writeStartDocument(String encoding) 
	throws XMLStreamException
	{
		this.writer.writeStartDocument(encoding, "1.0");
	}
	
	/**
	 * XMLドキュメント終了タグ出力処理
	 * @throws XMLStreamException
	 * 
	 */
	private void writeEndDocument() 
	throws XMLStreamException
	{
		this.writer.writeEndDocument();
	}

	/**
	 * XML要素開始タグ出力処理
	 * 
	 * @param name
	 * @throws XMLStreamException
	 */
	private void writeStartElement(String name) 
	throws XMLStreamException
	{
		this.writer.writeStartElement(JWCCmdHakko.BODY_NAMESPACE , name);
	}

	/**
	 * XMLSoap要素開始タグ出力処理
	 * 
	 * @param name
	 * @throws XMLStreamException
	 */
	private void writeStartSOAPElement(String name) 
	throws XMLStreamException
	{
		this.writer.writeStartElement(SOAP_PREFIX, name, SOAP12);
	}
	

	/**
	 * XMLBody要素開始タグ出力処理
	 * 
	 * @param prefix
	 * @param name
	 * @param namespace
	 * @throws XMLStreamException
	 */
	private void writeStartBodyElement(String prefix, String name, String namespace) 
	throws XMLStreamException
	{
		this.writer.writeStartElement(prefix, name, namespace);
	}
	
	/**
	 * XML要素終了タグ出力処理
	 * 
	 * @param name
	 * @throws XMLStreamException
	 */
	private void writeEndElement(String name) 
	throws XMLStreamException
	{
		this.writer.writeEndElement();
	}

	/**
	 * XML要素終了タグ出力処理
	 * 
	 * @param name
	 * @throws XMLStreamException
	 */
	private void writeEndSOAPElement(String name) 
	throws XMLStreamException
	{
		this.writer.writeEndElement();
	}
	
	/**
	 * XMLデータ出力処理
	 * 
	 * @param str
	 * @throws XMLStreamException
	 */
	private void writeCharacters(String str) 
	throws XMLStreamException
	{
		if(str == null)
		{
			this.writer.writeCharacters("");
		}
		else
		{
			this.writer.writeCharacters(str);
		}
	}
	
	
	/**
	 * XML nullタグ出力処理
	 * 
	 * @param prefix
	 * @param name
	 * @param namespace
	 * @throws XMLStreamException
	 */
	private void writeEmptyElement(String prefix, String name, String namespace) 
	throws XMLStreamException
	{
		this.writer.writeEmptyElement(prefix, name, namespace);
	}

	/**
	 * XML出力処理
	 * 
	 * @param userData
	 * @param osw
	 * @throws XMLStreamException
	 */
	private int writeXML(Map<Object, Object> userData,OutputStreamWriter osw) 
	throws XMLStreamException,IOException
	{
		
		int result = 0;
		this.init(osw);

		this.writeStartDocument("UTF-8");
		this.writeStartSOAPElement("Envelope");
		this.writeNamespace(SOAP_PREFIX, SOAP12);

		result = this.writeUserData(userData);

		this.writeEndSOAPElement("Envelope");
		this.writeEndDocument();

		this.close();
		
		return result;
	}
	
	
	/**
	 * 書き込み
	 * 
	 * @param userData
	 * @param os
	 * @throws IOException
	 * @throws XMLStreamException
	 */
	public int write(Map<Object, Object> userData,OutputStream os) 
	throws IOException,XMLStreamException
	{
		OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
		try 
		{
				
			// 本文
			int result = this.writeXML(userData,osw);
			osw.write(JWCCmdHakko.CRLF);
			
			return result;
		}
		finally 
		{
			if(osw != null) {
				osw.flush();
				osw.close();
			}
			if(os != null) {
				os.flush();
				os.close();
			}

		}
			
	}
	
	
	/**
	 * ユーザデータ部分のメッセージ生成
	 * @param writer
	 * @param userData
	 * @throws XMLStreamException
	 */
	private int writeUserData(Map<Object, Object> userData) 
	throws XMLStreamException
	{
		int result = 0;
		
		this.writeStartSOAPElement("Body");
		
		result = this.trace(userData);
		
		this.writeEndSOAPElement("Body");
		
		return result;
		
	}
	
	/**
	 * UserData内をトレースする。
	 * @param writer
	 * @param map
	 * @param serviceIfFlag
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private int trace(Map<Object, Object> map) 
	throws XMLStreamException
	{

		
		if(map.containsKey(JWCStrConst.WCTRL_ORDER_CD)) {
			String strWctrlOrderCode = (String)map.get(JWCStrConst.WCTRL_ORDER_CD);
			
			// 視聴制御オーダーコードの内容により、電文作成処理を振り分け。
			if("01".equals(strWctrlOrderCode)) 
			{
				this.traceSet(map);
			}
			else if("02".equals(strWctrlOrderCode)) 
			{
				this.traceUnSet(map);
			}
			else if("03".equals(strWctrlOrderCode)) 
			{
				this.traceControl(map);
			}
			else if("04".equals(strWctrlOrderCode)) 
			{
				this.traceCCASChg(map);
			}
			else if("05".equals(strWctrlOrderCode)) 
			{
				this.traceBCASChg(map);
			}
			else if("06".equals(strWctrlOrderCode)) 
			{
				this.traceReSet(map);
			}
			else if("07".equals(strWctrlOrderCode)) 
			{
				this.traceSTBClear(map);
			}
			else if("08".equals(strWctrlOrderCode)) 
			{
				this.traceCCASClear(map);
				
			}
			else if("09".equals(strWctrlOrderCode)) 
			{
				this.tracePassCls(map);
				
			}
			else if("10".equals(strWctrlOrderCode)) 
			{
				this.traceStbMsgSnd(map);
				
			}
			else if("11".equals(strWctrlOrderCode)) 
			{
				this.traceGrpMsgSnd(map);
				
			}
			else if("12".equals(strWctrlOrderCode)) 
			{
				this.traceMsgSndStop(map);
				
			}
			else if("13".equals(strWctrlOrderCode)) 
			{
				this.traceResult(map);
			}
			else 
			{
				// エラーログ出力
				return -1;
			}
			return 0;
		}
		else
		{
			// エラーログ出力
			return -1;
		}
	}

	/**
	 *  オブジェクトを文字列に変換します。
	 *  
	 * @param obj 変換するオブジェクト
	 * @return 変換された文字列
	 */
	private String convString(Object obj)
	{
		if (obj == null) 
		{
			return "";
		}
		else if (obj instanceof Integer) 
		{
			return String.valueOf((Integer)obj);
		}
		else if (obj instanceof Long) 
		{
			return String.valueOf((Long)obj);
		}
		else if (obj instanceof Short) 
		{
			return String.valueOf((Short)obj);
		}
		else if (obj instanceof Double) 
		{
			return String.valueOf((Double)obj);
		}
		else if (obj instanceof Float) 
		{
			return String.valueOf((Float)obj);
		}
		else if (obj instanceof Byte) 
		{
			return String.valueOf((Byte)obj);
		}
		else if (obj instanceof Boolean) 
		{
			return String.valueOf((Boolean)obj);
		}
		else if (obj instanceof String) 
		{
			return (String)obj;
		}
		return obj.toString();
	}

	/**
	 * 項目名からSoapElement名を取得します。
	 * 
	 * @param obj 項目名
	 * @return XMLElement名
	 */
	private String getElementName(String obj) 
	{
		return elementToMap.get(obj);
	}
	
	/**
	 * Soap項目名から項目名を取得します。
	 * 
	 * @param obj　Soap項目名
	 * @return 項目名
	 */
	private String getMapName(String obj) 
	{
		return mapToElement.get(obj);
	}
	

	/**
	 * 仮登録(リンク)・設置の電文を組み立てます。
	 * （視聴制御オーダーコード：01）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceSet(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{

		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_SET, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);
		this.writer.setPrefix(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);
		
		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writer.setPrefix(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//制御モード
		if(serviceMap.containsKey(JWCStrConst.CTRL_MODE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_MODE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_MODE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_MODE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
	
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));

			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}
		//制御用機種コード   
		if(serviceMap.containsKey(JWCStrConst.STB_CTRL_MODEL_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_CTRL_MODEL_CD));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_CTRL_MODEL_CD)));
			this.writeEndElement(getElementName(JWCStrConst.STB_CTRL_MODEL_CD));
		}
		//C-CAS内蔵タイプSTB
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
		}
		
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			String stb_cas_flag = (String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG);
			
			if("1".equals(stb_cas_flag))
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(" ");
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			else
			{
				//C-CAS ID
				if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
				{
					this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
					this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
					this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				}
				
			}
		}
		else
		{
			//C-CAS ID
			if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			
		}
		
		//B-CAS ID
		if(serviceMap.containsKey(JWCStrConst.BCAS_CARD_NO)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.BCAS_CARD_NO)));
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		else 
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(" ");
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		//デジタルティア(Remux)
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG1LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG1LIST), JWCStrConst.DGTIA_CD1, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG1LIST));
		}
		
		//デジタルティア(TM)領域０
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG2LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG2LIST), JWCStrConst.DGTIA_CD2, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG2LIST));
		}
		//デジタルティア(TM)領域１
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG3LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG3LIST), JWCStrConst.DGTIA_CD3, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG3LIST));
		}
		//デジタルティア(TM)領域２
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG4LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG4LIST), JWCStrConst.DGTIA_CD4, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG4LIST));
		}
		//デジタルティア(TM)領域３
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG5LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG5LIST), JWCStrConst.DGTIA_CD5, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG5LIST));
		}
		
		//課金フラグ
		if(serviceMap.containsKey(JWCStrConst.CHRG_FLG)) {
			this.writeStartElement(getElementName(JWCStrConst.CHRG_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CHRG_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.CHRG_FLG));
		}
		//グループＩＤ
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG6LIST)) {
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG6LIST));
		}
		else 
		{
			ArrayList<Map<Object, Object>>groupid = new ArrayList<Map<Object,Object>>();
			for(int i = 0 ; i < 6;i++) {
				HashMap<Object, Object>map = new HashMap<Object, Object>();
				map.put(JWCStrConst.GRP_ID,"        ");
				groupid.add(map);
			}
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID,groupid);
		}
		
		//C-CAS初期化コマンド発行有無
		if(serviceMap.containsKey(JWCStrConst.CCAS_SHKKA_CMD_HAK_FLG)) {
			this.writeStartElement(getElementName(JWCStrConst.CCAS_SHKKA_CMD_HAK_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CCAS_SHKKA_CMD_HAK_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.CCAS_SHKKA_CMD_HAK_FLG));
		}
		this.writeEndElement(JWCCmdHakko.ACTION_SET);

	}

	/**
	 * 仮登録(アンリンク)・撤去の電文を組み立てます。
	 * （視聴制御オーダーコード：02）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceUnSet(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{

		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_UNSET, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);
		
		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));
			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}
		//制御モード
		if(serviceMap.containsKey(JWCStrConst.CTRL_MODE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_MODE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_MODE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_MODE_CD));
		}

		this.writeEndElement(JWCCmdHakko.ACTION_UNSET);

	}
	
	/**
	 * 契約登録・更新の電文を組み立てます。
	 * （視聴制御オーダーコード：03）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceControl(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{

		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_CONTROL, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);
		
		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));
			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}
		//C-CAS内蔵タイプSTB
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
		}
		//C-CAS ID
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			String stb_cas_flag = (String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG);
			
			if("1".equals(stb_cas_flag))
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(" ");
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			else
			{
				//C-CAS ID
				if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
				{
					this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
					this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
					this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				}
				
			}
		}
		else
		{
			//C-CAS ID
			if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			
		}
		
		//B-CAS ID
		if(serviceMap.containsKey(JWCStrConst.BCAS_CARD_NO)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.BCAS_CARD_NO)));
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		else 
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(" ");
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		//デジタルティア(Remux)
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG1LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG1LIST), JWCStrConst.DGTIA_CD1, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG1LIST));
		}
		
		//デジタルティア(TM)領域０
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG2LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG2LIST), JWCStrConst.DGTIA_CD2, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG2LIST));
		}
		//デジタルティア(TM)領域１
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG3LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG3LIST), JWCStrConst.DGTIA_CD3, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG3LIST));
		}
		//デジタルティア(TM)領域２
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG4LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG4LIST), JWCStrConst.DGTIA_CD4, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG4LIST));
		}
		//デジタルティア(TM)領域３
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG5LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG5LIST), JWCStrConst.DGTIA_CD5, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG5LIST));
		}
		//課金フラグ
		if(serviceMap.containsKey(JWCStrConst.CHRG_FLG)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CHRG_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CHRG_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.CHRG_FLG));
		}
		//グループＩＤ
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG6LIST)) 
		{
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG6LIST));
		}
		else 
		{
			ArrayList<Map<Object, Object>>groupid = new ArrayList<Map<Object,Object>>();
			for(int i = 0 ; i < 6;i++) {
				HashMap<Object, Object>map = new HashMap<Object, Object>();
				map.put(JWCStrConst.GRP_ID,"        ");
				groupid.add(map);
			}
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID,groupid);
		}

		// 再設定フラグ
		if(serviceMap.containsKey(JWCStrConst.RE_SETTE_FLG)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.RE_SETTE_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.RE_SETTE_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.RE_SETTE_FLG));
		}
		
		//制御モード
		if(serviceMap.containsKey(JWCStrConst.CTRL_MODE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_MODE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_MODE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_MODE_CD));
		}
		this.writeEndElement(JWCCmdHakko.ACTION_CONTROL);
				
	}
	/**
	 * C-CAS交換の電文を組み立てます。
	 * （視聴制御オーダーコード：04）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceCCASChg(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_CCASCHG, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));
			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}

		//制御用機種コード   
		if(serviceMap.containsKey(JWCStrConst.STB_CTRL_MODEL_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_CTRL_MODEL_CD));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_CTRL_MODEL_CD)));
			this.writeEndElement(getElementName(JWCStrConst.STB_CTRL_MODEL_CD));
		}

		//C-CAS ID
		if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) {
			this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
			this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
		}
		//B-CAS ID
		if(serviceMap.containsKey(JWCStrConst.BCAS_CARD_NO)) {
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.BCAS_CARD_NO)));
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		else 
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters("                    ");
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		//デジタルティア(Remux)
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG1LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG1LIST), JWCStrConst.DGTIA_CD1, (List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG1LIST));
		}
		
		//デジタルティア(TM)領域０
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG2LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG2LIST), JWCStrConst.DGTIA_CD2, (List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG2LIST));
		}
		//デジタルティア(TM)領域１
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG3LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG3LIST), JWCStrConst.DGTIA_CD3, (List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG3LIST));
		}
		//デジタルティア(TM)領域２
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG4LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG4LIST), JWCStrConst.DGTIA_CD4, (List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG4LIST));
		}
		//デジタルティア(TM)領域３
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG5LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG5LIST), JWCStrConst.DGTIA_CD5, (List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG5LIST));
		}
		//課金フラグ
		if(serviceMap.containsKey(JWCStrConst.CHRG_FLG)) {
			this.writeStartElement(getElementName(JWCStrConst.CHRG_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CHRG_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.CHRG_FLG));
		}

		//グループＩＤ
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG6LIST)) 
		{
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG6LIST));
		}
		else 
		{
			ArrayList<Map<Object, Object>>groupid = new ArrayList<Map<Object,Object>>();
			for(int i = 0 ; i < 6;i++) {
				HashMap<Object, Object>map = new HashMap<Object, Object>();
				map.put(JWCStrConst.GRP_ID,"        ");
				groupid.add(map);
			}
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID,groupid);
		}

		this.writeEndElement(JWCCmdHakko.ACTION_CCASCHG);
	}
	
	/**
	 * B-CAS交換の電文を組み立てます。
	 * （視聴制御オーダーコード：05）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceBCASChg(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_BCASCHG, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}

		//B-CAS ID
		if(serviceMap.containsKey(JWCStrConst.BCAS_CARD_NO)) {
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.BCAS_CARD_NO)));
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(" ");
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		
		//デジタルティア(TM)領域０
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG2LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG2LIST), JWCStrConst.DGTIA_CD2, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG2LIST));
		}
		//デジタルティア(TM)領域１
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG3LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG3LIST), JWCStrConst.DGTIA_CD3, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG3LIST));
		}
		//デジタルティア(TM)領域２
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG4LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG4LIST), JWCStrConst.DGTIA_CD4, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG4LIST));
		}
		//デジタルティア(TM)領域３
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG5LIST)) {
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG5LIST), JWCStrConst.DGTIA_CD5, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG5LIST));
		}		
		this.writeEndElement(JWCCmdHakko.ACTION_BCASCHG);

	}

	/**
	 * 再登録の電文を組み立てます。
	 * （視聴制御オーダーコード：06）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceReSet(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_RESET, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));
			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}
		//制御用機種コード   
		if(serviceMap.containsKey(JWCStrConst.STB_CTRL_MODEL_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_CTRL_MODEL_CD));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_CTRL_MODEL_CD)));
			this.writeEndElement(getElementName(JWCStrConst.STB_CTRL_MODEL_CD));
		}
		//C-CAS内蔵タイプSTB
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
		}
		//C-CAS ID
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			String stb_cas_flag = (String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG);
			
			if("1".equals(stb_cas_flag))
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(" ");
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			else
			{
				//C-CAS ID
				if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
				{
					this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
					this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
					this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				}
				
			}
		}
		else
		{
			//C-CAS ID
			if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			
		}
		//B-CAS ID
		if(serviceMap.containsKey(JWCStrConst.BCAS_CARD_NO)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.BCAS_CARD_NO)));
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		else
		{
			this.writeStartElement(getElementName(JWCStrConst.BCAS_CARD_NO));
			this.writeCharacters(" ");
			this.writeEndElement(getElementName(JWCStrConst.BCAS_CARD_NO));
		}
		
		//デジタルティア(Remux)
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG1LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG1LIST), JWCStrConst.DGTIA_CD1, (List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG1LIST));
		}
		
		//デジタルティア(TM)領域０
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG2LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG2LIST), JWCStrConst.DGTIA_CD2, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG2LIST));
		}
		//デジタルティア(TM)領域１
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG3LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG3LIST), JWCStrConst.DGTIA_CD3, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG3LIST));
		}
		//デジタルティア(TM)領域２
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG4LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG4LIST), JWCStrConst.DGTIA_CD4, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG4LIST));
		}
		//デジタルティア(TM)領域３
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG5LIST)) 
		{
			this.traceArrayOfInt(getElementName(JWCStrConst.EWC0011D010CBSMSG5LIST), JWCStrConst.DGTIA_CD5, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG5LIST));
		}
		//課金フラグ
		if(serviceMap.containsKey(JWCStrConst.CHRG_FLG)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CHRG_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CHRG_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.CHRG_FLG));
		}
		//グループＩＤ
		if(serviceMap.containsKey(JWCStrConst.EWC0011D010CBSMSG6LIST)) 
		{
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID, 
					(List<Map<Object, Object>>)serviceMap.get(JWCStrConst.EWC0011D010CBSMSG6LIST));
		}
		else 
		{
			ArrayList<Map<Object, Object>>groupid = new ArrayList<Map<Object,Object>>();
			for(int i = 0 ; i < 6;i++) {
				HashMap<Object, Object>map = new HashMap<Object, Object>();
				map.put(JWCStrConst.GRP_ID,"        ");
				groupid.add(map);
			}
			this.traceArrayOfString(getElementName(JWCStrConst.EWC0011D010CBSMSG6LIST), JWCStrConst.GRP_ID,groupid);
		}

		this.writeEndElement(JWCCmdHakko.ACTION_RESET);
	}
	
	/**
	 * 撤去後STBクリアーの電文を組み立てます。
	 * （視聴制御オーダーコード：07）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceSTBClear(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_STBCLEAR, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));
			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}
		this.writeEndElement(JWCCmdHakko.ACTION_STBCLEAR);
	}
	
	/**
	 * 撤去後C-CASクリアーの電文を組み立てます。
	 * （視聴制御オーダーコード：08）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void traceCCASClear(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_CCASCLEAR, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		
		//C-CAS ID
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			String stb_cas_flag = (String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG);
			
			if("1".equals(stb_cas_flag))
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(" ");
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			else
			{
				//C-CAS ID
				if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
				{
					this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
					this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
					this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				}
				
			}
		}
		else
		{
			//C-CAS ID
			if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			
		}
		this.writeEndElement(JWCCmdHakko.ACTION_CCASCLEAR);
	}
	
	/**
	 * パスワード初期化登録の電文を組み立てます。
	 * （視聴制御オーダーコード：09）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	@SuppressWarnings("unchecked")
	private void tracePassCls(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_PASSCLS, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));
			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}
		//C-CAS内蔵タイプSTB
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG))));
			this.writeEndElement(getElementName(JWCStrConst.STB_CAS_NAIZO_FLG));
		}
		//C-CAS ID
		if(serviceMap.containsKey(JWCStrConst.STB_CAS_NAIZO_FLG)) 
		{
			String stb_cas_flag = (String)serviceMap.get(JWCStrConst.STB_CAS_NAIZO_FLG);
			
			if("1".equals(stb_cas_flag))
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(" ");
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			else
			{
				//C-CAS ID
				if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
				{
					this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
					this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
					this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				}
				
			}
		}
		else
		{
			//C-CAS ID
			if(serviceMap.containsKey(JWCStrConst.CCAS_CARD_NO)) 
			{
				this.writeStartElement(getElementName(JWCStrConst.CCAS_CARD_NO));
				this.writeCharacters(convString(serviceMap.get(JWCStrConst.CCAS_CARD_NO)));
				this.writeEndElement(getElementName(JWCStrConst.CCAS_CARD_NO));
			}
			
		}
		this.writeEndElement(JWCCmdHakko.ACTION_PASSCLS);
	}
	
	
	/**
	 * メッセージ送信(個別)の電文を組み立てます。
	 * （視聴制御オーダーコード：10）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	private void traceStbMsgSnd(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_STBMSGSND, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		//STB ID
		if(serviceMap.containsKey(JWCStrConst.STB_ID)) 
		{
			String stb_id = convString(serviceMap.get(JWCStrConst.STB_ID));
			if(stb_id != null && !"".equals(stb_id) && stb_id.length() > 12) {
				stb_id = stb_id.substring(stb_id.length() - 12,stb_id.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_ID));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_ID)));
			this.writeCharacters(stb_id);
			this.writeEndElement(getElementName(JWCStrConst.STB_ID));
		}
		//製造番号
		if(serviceMap.containsKey(JWCStrConst.STB_KIKI_SEIZO_NO)) 
		{
			String stb_kiki_no = convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO));
			if(stb_kiki_no != null && !"".equals(stb_kiki_no) && stb_kiki_no.length() >15)
			{
				stb_kiki_no = stb_kiki_no.substring(stb_kiki_no.length() - 15 ,stb_kiki_no.length());
			}
			
			this.writeStartElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_KIKI_SEIZO_NO)));
			this.writeCharacters(stb_kiki_no);
			this.writeEndElement(getElementName(JWCStrConst.STB_KIKI_SEIZO_NO));
		}
		//メッセージID
		if(serviceMap.containsKey(JWCStrConst.HE_MSG_ID)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.HE_MSG_ID));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.HE_MSG_ID)));
			this.writeEndElement(getElementName(JWCStrConst.HE_MSG_ID));
		}
		//送出開始日時分
		if(serviceMap.containsKey(JWCStrConst.MSG_SEND_DTM)) 
		{
			String msg_send_dtm = (String)serviceMap.get(JWCStrConst.MSG_SEND_DTM);

			if(msg_send_dtm != null && !"".equals(msg_send_dtm))
			{
				msg_send_dtm = msg_send_dtm.substring(0,12);
			}

			this.writeStartElement(getElementName(JWCStrConst.MSG_SEND_DTM));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.MSG_SEND_DTM)));
			this.writeCharacters(convString(msg_send_dtm));
			this.writeEndElement(getElementName(JWCStrConst.MSG_SEND_DTM));
		}
		//送出停止日時分
		if(serviceMap.containsKey(JWCStrConst.MSG_STP_DTM)) 
		{
			String msg_stp_dtm = (String)serviceMap.get(JWCStrConst.MSG_STP_DTM);

			if(msg_stp_dtm != null && !"".equals(msg_stp_dtm))
			{
				msg_stp_dtm = msg_stp_dtm.substring(0,12);
			}
			
			this.writeStartElement(getElementName(JWCStrConst.MSG_STP_DTM));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.MSG_STP_DTM)));
			this.writeCharacters(convString(msg_stp_dtm));
			this.writeEndElement(getElementName(JWCStrConst.MSG_STP_DTM));
		}
		//ﾒｯｾｰｼﾞ情報
		if(serviceMap.containsKey(JWCStrConst.STB_MSG_NAIYO)) 
		{
			this.writeStartElement(getElementName(JWCStrConst.STB_MSG_NAIYO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_MSG_NAIYO)));
			this.writeEndElement(getElementName(JWCStrConst.STB_MSG_NAIYO));
		}

		
		this.writeEndElement(JWCCmdHakko.ACTION_STBMSGSND);

	}
	
	/**
	 * メッセージ送信(グループ)の電文を組み立てます。
	 * （視聴制御オーダーコード：11）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	private void traceGrpMsgSnd(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_GRPMSGSND, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		
		//グループ番号
		if(serviceMap.containsKey(JWCStrConst.GRP_ID_RANGE_CD)) {
			// 20111012 change start
			this.writeStartElement(getElementName(JWCStrConst.GRP_ID_RANGE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.GRP_ID_RANGE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.GRP_ID_RANGE_CD));
			// 20111012 change end
		}
		
		//グループＩＤ
		if(serviceMap.containsKey(JWCStrConst.STB_GRP_ID)) {
			// 20111012 change start
			this.writeStartElement(getElementName(JWCStrConst.STB_GRP_ID));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_GRP_ID)));
			this.writeEndElement(getElementName(JWCStrConst.STB_GRP_ID));
			// 20111012 change end
		}

		//メッセージID
		if(serviceMap.containsKey(JWCStrConst.HE_MSG_ID)) {
			this.writeStartElement(getElementName(JWCStrConst.HE_MSG_ID));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.HE_MSG_ID)));
			this.writeEndElement(getElementName(JWCStrConst.HE_MSG_ID));
		}
		//送出開始日時分
		if(serviceMap.containsKey(JWCStrConst.MSG_SEND_DTM)) {
			String msg_send_dtm = (String)serviceMap.get(JWCStrConst.MSG_SEND_DTM);
	
			if(msg_send_dtm != null && !"".equals(msg_send_dtm))
			{
				msg_send_dtm = msg_send_dtm.substring(0,12);
			}
			
			this.writeStartElement(getElementName(JWCStrConst.MSG_SEND_DTM));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.MSG_SEND_DTM)));
			this.writeCharacters(convString(msg_send_dtm));
			this.writeEndElement(getElementName(JWCStrConst.MSG_SEND_DTM));
		}
		//送出停止日時分
		if(serviceMap.containsKey(JWCStrConst.MSG_STP_DTM)) {
			String msg_stp_dtm = (String)serviceMap.get(JWCStrConst.MSG_STP_DTM);
			
			if(msg_stp_dtm != null && !"".equals(msg_stp_dtm))
			{
				msg_stp_dtm = msg_stp_dtm.substring(0,12);
			}
			
			this.writeStartElement(getElementName(JWCStrConst.MSG_STP_DTM));
//			this.writeCharacters(convString(serviceMap.get(JWCStrConst.MSG_STP_DTM)));
			this.writeCharacters(convString(msg_stp_dtm));
			this.writeEndElement(getElementName(JWCStrConst.MSG_STP_DTM));
		}
		//ﾒｯｾｰｼﾞ情報
		if(serviceMap.containsKey(JWCStrConst.STB_MSG_NAIYO)) {
			this.writeStartElement(getElementName(JWCStrConst.STB_MSG_NAIYO));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.STB_MSG_NAIYO)));
			this.writeEndElement(getElementName(JWCStrConst.STB_MSG_NAIYO));
		}		
		this.writeEndElement(JWCCmdHakko.ACTION_GRPMSGSND);
	
	}
	
	/**
	 * メッセージ送信停止の電文を組み立てます。
	 * （視聴制御オーダーコード：12）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	private void traceMsgSndStop(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_MSGSNDSTOP, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}
		//制御エリア
		if(serviceMap.containsKey(JWCStrConst.CTRL_AREA_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_AREA_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_AREA_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_AREA_CD));
		}
		//制御要求キー
		if(serviceMap.containsKey(JWCStrConst.CTRL_YOKYU_KEY)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.CTRL_YOKYU_KEY)));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_YOKYU_KEY));
		}
		//制御タイプ
		if(serviceMap.containsKey(JWCStrConst.CTRL_TYPE_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.CTRL_TYPE_CD))));
			this.writeEndElement(getElementName(JWCStrConst.CTRL_TYPE_CD));
		}
		
		//メッセージID
		if(serviceMap.containsKey(JWCStrConst.HE_MSG_ID)) {
			this.writeStartElement(getElementName(JWCStrConst.HE_MSG_ID));
			this.writeCharacters(convString(serviceMap.get(JWCStrConst.HE_MSG_ID)));
			this.writeEndElement(getElementName(JWCStrConst.HE_MSG_ID));
		}
		
		this.writeEndElement(JWCCmdHakko.ACTION_MSGSNDSTOP);
	
	}
	
	
	/**
	 * 制御結果の電文を組み立てます。
	 * （視聴制御オーダーコード：13）
	 * 
	 * @param serviceMap
	 * @throws XMLStreamException
	 */
	private void traceResult(Map<Object, Object>serviceMap) 
	throws XMLStreamException
	{
		this.writeStartBodyElement(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.ACTION_RESULT, JWCCmdHakko.BODY_NAMESPACE);
		this.writeNamespace(JWCCmdHakko.BODY_PREFIX, JWCCmdHakko.BODY_NAMESPACE);

		//局コード
		if(serviceMap.containsKey(JWCStrConst.KYOKU_CD)) {
			this.writeStartElement(getElementName(JWCStrConst.KYOKU_CD));
			this.writeCharacters(convString(Integer.parseInt((String)serviceMap.get(JWCStrConst.KYOKU_CD))));
			this.writeEndElement(getElementName(JWCStrConst.KYOKU_CD));
		}

		this.writeEndElement(JWCCmdHakko.ACTION_RESULT);
		
	}
	
	/**
	 * Int型配列の要素を設定する。
	 * 
	 * @param parent 親要素名
	 * @param key 配列要素名
	 * @param list XML子リスト
	 * @throws XMLStreamException
	 */
	private void traceArrayOfInt(String parent, String key, List<Map<Object, Object>> list)
	throws XMLStreamException
	{
		this.writeStartElement(parent);
		for(Map<Object, Object>map : list){
			if(map.containsKey(key)) {
				this.writeStartElement(getElementName(key));
				if(map.get(key) instanceof String) 
				{
					this.writeCharacters(convString(Integer.parseInt((String)map.get(key))));
				}
				else 
				{
					this.writeCharacters(convString(map.get(key)));
				}
				this.writeEndElement(getElementName(key));
			}
		}
		this.writeEndElement(getElementName(parent));
	}
	
	/**
	 * String型配列の要素を設定する。
	 * 
	 * @param parent 親要素名
	 * @param key 配列要素名
	 * @param list XML子リスト
	 * @throws XMLStreamException
	 */
	private void traceArrayOfString(String parent, String key, List<Map<Object, Object>> list)
	throws XMLStreamException
	{
		this.writeStartElement(parent);
		for(Map<Object, Object>map : list){
			if(map.containsKey(key)) {
				this.writeStartElement(getElementName(key));
				String grpid = (String)map.get(key);
				if(grpid == null || "".equals(grpid.trim()))
				{
					this.writeCharacters(convString("        "));
				}
				else
				{
					this.writeCharacters(convString(grpid));
				}
				this.writeEndElement(getElementName(key));
			}
		}
		this.writeEndElement(getElementName(parent));
	}

	/**
	 * 配列null時の要素を設定する。
	 * 
	 * @param parent 親要素名
	 * @throws XMLStreamException
	 */
	private void traceArrayOfNull(String parent) 
	throws XMLStreamException
	{
		this.writeEmptyElement(JWCCmdHakko.XSL_PREFIX, parent, JWCCmdHakko.XSL_PREFIX);
		this.writeAttribute("xsl:nil", "true");
		this.writeNamespace(JWCCmdHakko.XSL_PREFIX, JWCCmdHakko.XSL_NAMESPACE);
	}
	
	/**
	 * 電文読み込み処理
	 * 
	 * @param host
	 * @param ip
	 * @param contentType
	 * @param is
	 * @return XML電文解析結果
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	private HashMap<Object, Object> read(String host, String ip, String contentType, InputStream is) 
	throws IOException,ParserConfigurationException,SAXException
	{
		String charset = this.getEncoding(contentType);
		StringReader sr = readSingleResource(charset, is);
		 HashMap<Object, Object>result = null;
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		DocumentBuilder db = dbf.newDocumentBuilder();
		
		Document doc = db.parse(new InputSource(sr));
		
		Element elem = doc.getDocumentElement();
		
		// XMLの実行結果を取得する。
		result = analyzeXml(elem,true);
		
		return result;
	}

	/**
	 * 電文読み取り処理（添付ファイルなし）
	 * 
	 * @param charSet
	 * @param is
	 * @return
	 * @throws IOException
	 */
	private StringReader readSingleResource(String charSet, InputStream is) 
	throws IOException
	{

		this.soapMessage = true;

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		byte[] buf = new byte[1024*8];

		int length = readLine(is, buf, 0, buf.length);

		while(length != -1){
			// ボディ部
			baos.write(buf, 0, length);
			length = readLine(is, buf, 0, buf.length);
		}

		byte[] contents = baos.toByteArray();
		
		
		// コンテントタイプから、encoding指定を得る。
		StringReader reader = new StringReader(new String(contents, charSet));
		
		return reader;
	}
	
	/**
	 * 行データ取得処理
	 * 
	 * @param is InputStream 
	 * @param abyte0 abyte0[]
	 * @param i i
	 * @param j j
	 * @return int 実行結果
	 * @throws IOException
	 */
	private int readLine(InputStream is, byte abyte0[], int i, int j) 
	throws IOException 
	{
		if (j <= 0) 
		{
			return 0;
		}
		int k = 0;
		int l;
		do 
		{
			if((l = is.read()) == -1) 
			{
				break;
			}
			abyte0[i++] = (byte)l;
			k++;
		}
		while(l != 10 && k != j);
		
		return k <= 0 ? -1 : k;
	}

	/**
	 * エンコーディングデータ取得処理
	 * 
	 * @param contentType HTTPContentType文字列
	 * @return エンコーディング
	 */
	private String getEncoding(String contentType)
	{

		if(contentType != null)
		{

			// 半角空白で切って、
			String[] contentTypes = contentType.split(" ");

			// 開始がcharsetで始まるものを探して、
			for(String ct : contentTypes){
				if(ct.startsWith("charset=")){
					ct = ct.trim();
					return ct.substring("charset=".length());
				}
			}
		}

		// 見つけられなかった場合
		return "UTF-8";

	}
	
	/**
	 * コマンド送信戻り電文解析処理
	 * 
	 * @param elem コマンド解析電文rootXML要素
	 * @return XML解析結果
	 */
	private HashMap<Object, Object> analyzeXml(Node elem,boolean rootflag) 
	{
		Node node = elem.getFirstChild();
		HashMap<Object, Object> resultMap = null;
		
		ArrayList<Map<Object, Object>>nodelist = new ArrayList<Map<Object, Object>>();
		
		//電文の要素をすべて解析する。
		while(node != null)
		{
			if(node.getNodeType() == Node.ELEMENT_NODE) 
			{
				
				String nodename = node.getLocalName();
				String test = node.getLocalName();
				
				
				// SOAP例外の場合
				if(JWCCmdHakko.SOAP_FAULT.equals(nodename.toLowerCase()))
				{
					resultMap = new HashMap<Object, Object>();
					StringBuffer errCode = new StringBuffer();
					String faultinfo = analyzeFaultXml(node, errCode);
					resultMap.put(JWCStrConst.CMD_RESULT_CD,"1");
					resultMap.put(JWCStrConst.CMD_ERROR_CD, errCode.toString());
					resultMap.put(JWCStrConst.CMD_ERROR_MESSAGE,faultinfo);	
					return resultMap;
				}
				// 受信結果受付の場合
				else if(JWCCmdHakko.CONTROL_RESULT_INFO.equals(nodename)) 
				{
					if(resultMap == null) {
						resultMap = new HashMap<Object, Object>();
					}
					Map<Object, Object>resultinfo = analyzeResultXml(node);
					nodelist.add(resultinfo);
					resultMap.put(JWCStrConst.CMD_RESULT_CD,"0");
					resultMap.put(JWCStrConst.CMD_RESULT_DETAIL,nodelist);
					
				}
				// 子供のノードがある場合、さらに子供の要素をチェックする。
				if(node.hasChildNodes()) 
				{
					HashMap<Object, Object> child = analyzeXml(node,false);
					if(child != null) {
						return child;
					}
				}
			}
				node = node.getNextSibling();
		}
		
		if(rootflag && resultMap == null)
		{
			resultMap = new HashMap<Object, Object>();
			resultMap.put(JWCStrConst.CMD_RESULT_CD,"0");
			return resultMap;
		}
		
		return resultMap;
	}
	
	/**
	 * XMLFAULT電文解析処理
	 * 
	 * @param elem XMLFAULT電文
	 * @return ArrayListエラー情報
	 */
	private String analyzeFaultXml(Node elem,StringBuffer errcode) 
	{
		Node node = elem.getFirstChild();
//		ArrayList<Object> list = new ArrayList<Object>();
		String errmsg = "";
		
		while(node != null) {
			
			if(node.getNodeType() == Node.ELEMENT_NODE) 
			{
				String nodename = node.getLocalName();

				if(JWCCmdHakko.SOAP_FAULT_CODE.equals(nodename.toLowerCase())) {
					
//					list.add(node.getTextContent());
					Node child = node.getFirstChild();
					if(child != null)
					{
						errmsg = errmsg + "エラーコード：" + child.getTextContent();
						errcode.append(child.getTextContent());
					}
				}
				if(JWCCmdHakko.SOAP_FAULT_STRING.equals(nodename.toLowerCase())) {
					
//					list.add(node.getTextContent());
					errmsg = errmsg + "エラー詳細：" + node.getTextContent();
				}
			}
			
			node = node.getNextSibling();
		}
		
		return errmsg;
	}

	/**
	 * コマンド結果発行戻り電文解析処理
	 * 
	 * @param elem XMLコマンド発行電文戻りNode
	 * @return コマンド結果発行戻値
	 */
	private Map<Object, Object>analyzeResultXml(Node elem) 
	{
		
		Node node = elem.getFirstChild();
		HashMap<Object, Object>result = new HashMap<Object, Object>();
		
		while(node != null) {

			if(node.getNodeType() == Node.ELEMENT_NODE) 
			{
				String nodename = node.getLocalName();
				String elementString = this.getMapName(nodename);
				
				if(elementString != null) {
					result.put(elementString,node.getTextContent());
				}
			}
			
			node = node.getNextSibling();
		}
		return result;
	}
	
	/**
	 * 視聴制御オーダーコードチェック処理
	 * 
	 * @parma map 視聴制御オーダオマップ
	 * @param start 視聴制御オーダコード範囲（開始）
	 * @param end 視聴制御オーダコード範囲（終了）
	 */
	private int checkOrderCode(HashMap<Object, Object> map,int start , int end) {
		int result = 0;
		
		String  ordercode = (String)map.get(JWCStrConst.WCTRL_ORDER_CD);
		if(ordercode == null)
		{
			return -1;
		}
		int iordercode = Integer.parseInt(ordercode);
		
		if(iordercode < start || iordercode > end) {
			return -1;
		}
		
		return result;
	}
}
