package CoreFS.SA03;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import CoreFS.SA01.CoreCommunicationService;
import CoreFS.SA06.CoreICommunicationService;
import CoreFS.SA06.CoreParamObject;
import CoreFS.SA06.CoreReturnObject;

@WebService(endpointInterface = "CoreFS.SA03.CoreIWebServiceService")
public class CoreWebServiceServiceImpl implements CoreIWebServiceService {
	/**
	 * @desc 被发布给client的方法 完成方法的调用 相应客户的请求
	 * @author sunjiawang
	 * @create 091128
	 * @modify 091128
	 */
	public static final String WS_XML_TB_NODE_NAME = "ROWDATA";
	public static final String WS_XML_ROW_NODE_NAME = "ROW";
	public static final String WS_XML_SEQ_NODE_NAME = "C0";
	private CoreICommunicationService communicationService = null;
	public CoreWebServiceServiceImpl () {
		communicationService = new CoreCommunicationService();
	}
	// 查询获取的sqlRowSet转换成具有结构的xml字符串；这里rootStr,rootchild是自己定义的xml结构时取的名，可以根据需要定，比如，root可以用表名。
	public String setToXml(SqlRowSet rs, String rootStr, String rootchild) {

		String xmlStr = "";
		String[] colName = null;
		;
		//try {
//			Element root = new Element(rootStr);
//			int colLen = 0;
//			if (rs.getRow() >= 0) {
//				colName = rs.getMetaData().getColumnNames();
//				colLen = colName.length;
//				for (int i = 0; i < colLen; i++)
//					root.setAttribute(colName[i], String.valueOf(i));
//			}
//
//			while (rs.next()) {
//				Element rootchildelement = new Element(rootchild);
//
//				for (int n = 0; n < colLen; n++) {
//					Element colEle = new Element("col");
//					rootchildelement.addContent(colEle);
//					colEle.addContent(rs.getString(colName[n]));
//				}
//				root.addContent(rootchildelement);
//			}
//
//			Document doc = new Document(root);
//			Format f = Format.getPrettyFormat();
//			XMLOutputter outer = new XMLOutputter(f);
//			xmlStr += outer.outputString(doc);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	return null;
	}

	// 查询获取的List转换成具有结构的xml字符串
	public String arrayListToXml(ArrayList rs, String rootStr, String rootchild) {
		String xmlStr = "";
		Map elelist;
//		try {
//			if (rs.size() < 1)
//				return xmlStr;
//			Element root = new Element(rootStr);
//			elelist = (Map) rs.get(0);
//			int index = 0;
//			// 获取属性
//			for (Object key : elelist.keySet()) {
//				root.setAttribute(key.toString(), Integer.toString(index));
//				index += 1;
//			}
//
//			int len = rs.size();
//
//			try {
//				for (int i = 0; i < len; i++) {
//					Element rootchildelement = new Element(rootchild);
//					// 获取每行的行记录
//					elelist = (Map) rs.get(i);
//					for (Object o : elelist.keySet()) {
//						Element colEle = new Element("col");
//						rootchildelement.addContent(colEle);
//						Object obj = elelist.get(o);
//						String strTemp = "";
//						if (obj == null)
//							strTemp = "";
//						else
//							strTemp = obj.toString();
//						colEle.addContent(strTemp);
//					}
//					root.addContent(rootchildelement);
//				}
//			} catch (Exception e) {
//				System.out.println(e.toString());
//			}
//			// writeXml(doc);
//
//			Document doc = new Document(root);
//			Format f = Format.getPrettyFormat();
//			XMLOutputter outer = new XMLOutputter(f);
//			xmlStr += outer.outputString(doc);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return null;
	}

	public String listToXml(List rs, String rootStr, String rootchild) {
		String rtnStr = "";
		if (rs instanceof LinkedList)
			rtnStr = this.linkedListToXml((LinkedList) rs, rootStr, rootchild);
		if (rs instanceof ArrayList)
			rtnStr = this.arrayListToXml((ArrayList) rs, rootStr, rootchild);
		return rtnStr;
	}

	// 查询获取的List转换成具有结构的xml字符串
	public String linkedListToXml(LinkedList rs, String rootStr,
			String rootchild) {
		String xmlStr = "";
		Map elelist;
//		try {
//			if (rs.size() < 1)
//				return xmlStr;
//			Element root = new Element(rootStr);
//			elelist = (Map) rs.get(0);
//			int index = 0;
//			for (Object key : elelist.keySet()) {
//				root.setAttribute(key.toString(), Integer.toString(index));
//				index += 1;
//			}
//
//			int len = rs.size();
//
//			try {
//				for (int i = 0; i < len; i++) {
//					Element rootchildelement = new Element(rootchild);
//
//					elelist = (Map) rs.get(i);
//					for (Object o : elelist.keySet()) {
//						Element colEle = new Element("col");
//						rootchildelement.addContent(colEle);
//						Object obj = elelist.get(o);
//						String strTemp = "";
//						if (obj == null)
//							strTemp = "";
//						else
//							strTemp = obj.toString();
//						colEle.addContent(strTemp);
//					}
//					root.addContent(rootchildelement);
//				}
//			} catch (Exception e) {
//				System.out.println(e.toString());
//			}
//			// writeXml(doc);
//
//			Document doc = new Document(root);
//			Format f = Format.getPrettyFormat();
//			XMLOutputter outer = new XMLOutputter(f);
//			xmlStr += outer.outputString(doc);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return xmlStr;
	}

	public String croToXml(CoreReturnObject cro, String rootStr,
			String rootchild) {
		String rtStr = "";
		if (cro.getResult() != null) {
			if (cro.getResult() instanceof List) {
				rtStr = this.listToXml((List) cro.getResult(), rootStr,
						rootchild);
			}
			if (cro.getResult() instanceof SqlRowSet) {
				rtStr = this.setToXml((SqlRowSet) cro.getResult(), rootStr,
						rootchild);
			}
		}
		return rtStr;
	}

	public String say(String name) {
		return "hello cxf "+name;
	}

	public String runCXF(String classname, String methodname,String[]params){//, Object[] params) {

		CoreParamObject ctx = new CoreParamObject();
		ctx.setV_serviceName(classname);
		ctx.setV_method(methodname);
		ctx.setV_reqParameters(params);
		//return this.croToXml(communicationService.runActivtity(ctx),this.WS_XML_TB_NODE_NAME , this.WS_XML_ROW_NODE_NAME);
		CoreReturnObject cro=communicationService.runActivtity(ctx);
		return cro.getV_errCode().toString()+" "+cro.getV_errMsg();
	}

}