普元EOS简单运算逻辑的编写

package zqz;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import org.w3c.dom.*;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.primeton.tp.common.xml.XmlUtil;
import com.primeton.tp.core.api.BizContext;

/**
 * @author qzzha
 * @version 1.0
 * @date 2012-11-6
 * @class_displayName EosBizletApplication
 * 学习普元EOS简单运算逻辑的编写
 */

public class EosBizletApplication {

	/*
	 * 根据SQL查询数据库,返回LIST类型数据集合,类似于普元自带构建 BL_runFormatSql
	 * 主要学习内容:熟悉普元运算逻辑;获取属性;设置属性;怎样返回查询结果
	 * 
	 */
	public static int BL_queryFromDb(Document doc, BizContext param)
			throws Exception {
		Node listNode = (Node) param.getParaObjectAt(0);
		String sql = (String) param.getParaObjectAt(1);

		// Entity节点名称使用type属性值,没有则使用"entity"
		NamedNodeMap attrs = listNode.getAttributes();
		String entity = "";
		if (attrs.getNamedItem("type") != null) {
			entity = attrs.getNamedItem("type").getNodeValue();
		}
		if (entity == null || "".equals(entity)) {
			entity = "entity";
		}

		// entity
		Element entityNode = null;
		// filed
		Element filedNode = null;

		PreparedStatement ps = null;
		ResultSet rs = null;
		Connection conn = null;
		// 获取连接
		conn = param.getDBBroker().getConnection();
		ps = conn.prepareStatement(sql);
		rs = ps.executeQuery();
		// 获取元数据
		ResultSetMetaData rsdd = rs.getMetaData();
		int count = rsdd.getColumnCount();
		int rows = 0;
		while (rs.next()) {
			entityNode = doc.createElement(entity);
			// 设置rowNum属性值 第几条记录
			entityNode.setAttribute("rowNum", "" + rows);
			for (int i = 1; i <= count; i++) {
				// 创建以查询列名命名的元素
				filedNode = doc.createElement(rsdd.getColumnName(i));
				// 增加文本元素
				filedNode.appendChild(doc.createTextNode(rs.getString(rsdd
						.getColumnName(i))));
				entityNode.appendChild(filedNode);
			}
			listNode.appendChild(entityNode);
			rows++;
		}
		// 设置rowNum属性值 总记录条数
		((Element) listNode).setAttribute("rowNum", "" + rows);
		return 1;
	}

	/*
	 * 字符串处理应用 字符串处理之后返回
	 */
	public static int BL_GetAffixURL(Document doc, BizContext param)
			throws Exception {
		Node param0 = (Node) param.getParaObjectAt(0);
		String fileName = (String) param.getParaObjectAt(1);
		String filePath = (String) param.getParaObjectAt(2);
		// 加密,集团ITSM规定URL不能有中文
		fileName = encryptBASE64(fileName);
		filePath = encryptBASE64(filePath);
		// ip
		String url = "http://ip:9080/jt_itsm/pageFile/fileDown2.jsp?filename="
				+ fileName + "&path=" + filePath;
		XmlUtil.setNodeValue(param0, url);
		return 1;
	}

	/**
	 * * BASE64加密 * *
	 * 
	 * @param key *
	 * @return *
	 * @throws Exception
	 */
	public static String encryptBASE64(String s) throws Exception {
		byte[] key = s.getBytes();
		return (new BASE64Encoder()).encodeBuffer(key);
	}

	/**
	 * * BASE64解密 * *
	 * 
	 * @param key *
	 * @return *
	 * @throws Exception
	 */
	public static String decryptBASE64(String key) throws Exception {
		byte[] b = (new BASE64Decoder()).decodeBuffer(key);
		return new String(b);
	}
}

猜你喜欢

转载自kingzha.iteye.com/blog/1717703