java2

package com.tpaic.ec.util;

import java.security.Key;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.security.Base64Encoder;

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

import com.tpaic.auto.client.ec.dto.InsureTemp;
import com.tpaic.auto.client.ec.dto.TempBase;
import com.tpaic.ec.domain.NetPlanDefine;
import com.tpaic.ec.domain.Parameter;
import com.tpaic.tpfa.app.biz.dispatch.DispatchServiceException;

/**
 * 公共函数,全是static方法
 * 
 * @author xiemingmei
 * @date 2008-9-21
 */
public class CommonFunctions {

	protected static final Log logger = LogFactory.getLog(CommonFunctions.class);

	private CommonFunctions() {
	}

	/**
	 * MD5散列后base64编码 密码经过此该方法编码存进数据库
	 * 
	 * @param str
	 * @return
	 */
	public static String getMd5EncodeString(String str) {
		String encode = "666666";
		try {
			byte[] hash = MessageDigest.getInstance("MD5").digest(str.getBytes());
			encode = Base64Encoder.encode(hash);
		} catch (Exception e) {
			logger.error(e);
		}
		return encode;
	}

	/**
	 * 生成随机密码
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) {
		String str = "01234567890123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		int range = str.length();
		for (int i = 0; i < length; i++) {
			sb.append(str.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
	
	/**
	 * 把中文转换为拼音
	 * @param name
	 * @return
	 */
	public static String cnToSpell(String name){
		try {
			return Spell.converterToFirstSpell(name);
		} catch (Exception e) {
			logger.debug(e);
		}
		return name;
	}
	
	public static String getRandomPassword(int length) {
		String str = "0123456789";
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		int range = str.length();
		for (int i = 0; i < length; i++) {
			sb.append(str.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
	public static void main(String[] a) {
		System.out.println(getRandomString(6));
	}
	/**
	 * 返回SHA-1的散列值
	 * @param string
	 * @return
	 */
	public static String getSHA1String(String string){
		try {
			MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
			return Base64Encoder.encode(sha1.digest(string.getBytes()));
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}

	/**
	 * 生成随机数字(短信、邮件校验码)
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomNo(int length) {
		String str = "0123456789";
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		int range = str.length();
		for (int i = 0; i < length; i++) {
			sb.append(str.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}

	/**
	 * DES加密
	 * 
	 * @param data-明文
	 * @return 密文
	 * @throws Exception
	 */
	public static String encodeDES(String data) throws Exception {

		KeyGenerator _generator = KeyGenerator.getInstance("DES");
		_generator.init(new SecureRandom("tpaic-ec".getBytes()));
		Key key = _generator.generateKey();
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] endata = cipher.doFinal(data.getBytes());
		String endata2 = new BASE64Encoder().encode(endata);
		return endata2;
	}

	/**
	 * DES解密
	 * 
	 * @param endata-密文
	 * @return 明文
	 * @throws Exception
	 */
	public static String decodeDES(String endata) throws Exception {
		KeyGenerator _generator = KeyGenerator.getInstance("DES");
		_generator.init(new SecureRandom("tpaic-ec".getBytes()));
		Key key = _generator.generateKey();
		Cipher cipher = Cipher.getInstance("DES");

		// 用密钥初始化Cipher对象
		cipher.init(Cipher.DECRYPT_MODE, key);
		// 执行解密操作
		byte[] bytes = new BASE64Decoder().decodeBuffer(endata);
		byte decryptedData[] = cipher.doFinal(bytes);
		// 然后将解密后的数据转化成原来的类文件。
		return new String(decryptedData);
	}

	/**
	 * 验证手机号码
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean validatePhonenumber(String phone) {
		return Pattern.matches("^[1]{1}[358]{1}[0-9]{9}$", phone);
	}

	/**
	 * 验证邮箱地址
	 * 
	 * @param email
	 * @return
	 */
	public static boolean validateEmail(String email) {
		return Pattern.matches("^[_a-z0-9-]+([.][_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-9-]+)*$", email);
	}

	/**
	 * 根据code获取基础参数设置
	 * 
	 * @param email
	 * @return Parameter域对象
	 * @throws DispatchServiceException
	 */
	public static String getParameterByCode(String paraCode, Map paraMap) throws DispatchServiceException {
		Parameter parameter = (Parameter) paraMap.get(paraCode);
		return parameter.getParaValue();
	}

	/**
	 * 获取投保单号
	 * 
	 * @param insureTemp
	 * @return
	 */
	public static String getApplyPolicyNo(InsureTemp insureTemp) {
		String applyPolicyNo = "";
		for (int i = 0; i < insureTemp.getTempBaseList().size(); i++) {
			if (((TempBase) insureTemp.getTempBaseList().get(i)).getApplyPolicyNo() != null)
				applyPolicyNo += ((TempBase) insureTemp.getTempBaseList().get(i)).getApplyPolicyNo() + "  ";
		}

		return applyPolicyNo;
	}

	/**
	 * 获取保险产品名称
	 * 
	 * @param insureTemp
	 * @return
	 */
	public static String getProductName(List tempBaseList, Collection planList) {
		String productName = "";
		String planCode;
		for (int i = 0; i < tempBaseList.size(); i++) {
			if (((TempBase) tempBaseList.get(i)) != null) {
				TempBase tempBase= ((TempBase) tempBaseList.get(i));
				planCode = tempBase.getPlanCode();
				String planDefineId=tempBase.getPlanDefineId();
				if("".equals(productName)){
				    productName += getProductNameByCode(planDefineId,planCode, planList);
				}else{
				    productName +=","+getProductNameByCode(planDefineId,planCode, planList);
				}
			}
		}

		return productName;
	}

	/**
	 * 获取网上产品名称
	 * 
	 * @param planCode
	 * @return
	 */
	public static String getProductNameByCode(String planDefineId,String planCode, Collection planList) {
		String planName = planCode;
		if (StringUtil.isEmptyString(planName)) {
			return "";
		}

		Iterator it = planList.iterator();
		NetPlanDefine planDefine;
		while (it.hasNext()) {
			planDefine = (NetPlanDefine) it.next();
			if (planDefineId.equals(planDefine.getPlanDefineId())) {
				planName = planDefine.getPlanNetName();
				break;
			}
		}

		return planName;
	}

}



<%@ page contentType="text/html; charset=GBK" %>
<%@ include file="/WEB-INF/jsp/common/tag_include.jsp" %>
<%
response.reset(); 
response.setContentType("application/x-download; charset=GBK"); 
response.setHeader("Content-disposition","attachment; filename="+new String("电子商务日报表".getBytes("GB2312"), "ISO_8859_1") +".xls");
//以上这行设定传送到前端浏览器时的档名为*.xls
%>
<table id="resultList" width="100%" border="1" cellpadding="0" cellspacing="0">
	<tr>
		<th rowspan="2">IP浏览城市</th>
	    <th rowspan="2">IP浏览页面数</th>
	    <th colspan="2">车险</th>
	    <th colspan="2">非车险</th>
	    <th colspan="3">其他</th>
	</tr>
	<tr>
	    <th>报价量</th>
	    <th>签单保费</th>
	    <th>签单数</th>
	    <th>签单保费</th>
	    <th>保单验真次数</th>
	    <th>赔案查询次数</th>
	    <th>投保预约次数</th>
	  </tr>
</table>




package web.basic.util;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import web.basic.dto.MyTablesButtonDto;
import web.basic.pojo.Auditing;
import web.basic.pojo.Button;
import web.basic.pojo.DefaultValue;
import web.basic.pojo.FilterOfTextType;
import web.basic.pojo.MyTable;
import web.basic.pojo.Process;
import web.basic.pojo.ReportForms;
import web.basic.pojo.SortTable;
import web.basic.pojo.TableColumn;
import web.basic.pojo.TableColumnConstraint;
import web.basic.pojo.TextColumnType;
import web.basic.pojo.ValueEpeat;
import web.basic.pojo.Warn;


/**
 * 缓存管理器
 * @author ex_yixb
 *
 */
/**
 * @author ex_yixb
 *
 */
public class CacheManage {
	private static CacheManage cacheManage=new CacheManage();
	private  int date=60*60*1000;  //缓存时间
	private Map<String,Date>  cacheDate=Collections.synchronizedMap(new HashMap<String,Date>());  //缓存的时间Map
	private Map<String,Object>  cacheData = Collections.synchronizedMap(new HashMap<String,Object>()); //缓存的数据
	
	/**
	 * 刷新所有缓存
	 */
	public void flushAll(){
		cacheDate.clear();
	}
	
	/**
	 * 刷新一个缓存
	 */
	public void flushKey(String key){
		cacheDate.remove(key);
	}
	
	/**
	 * 清空缓存
	 */
	public void clear(){
		cacheDate.clear();
		cacheData.clear();
	}
	
	/**
	 * 判断缓存是否到期
	 * @return
	 */
	private boolean ifCacheDateMature(String key){
		Date date = cacheDate.get(key);
		if(date==null){
			return true;
		}
		long time =date.getTime()+this.date;
		Date curr = new Date();
		long currTime = curr.getTime();
		if(time<currTime){
			return true;
		}
		return false;
	}

	/**
	 * 得到缓存对象
	 * @return
	 */
	public static CacheManage getCacheManage() {
		return cacheManage;
	}
	
	
	
	/**
	 * 得到当前表中所有的列
	 * @return
	 * @throws SQLException 
	 */
	public List<TableColumn> getCurrTableAllColumns(Integer myTableId) throws SQLException{
		boolean bool=ifCacheDateMature(Constants.CURR_TABLE_ALL_COLUMN);
		if(!bool){ // 未到期
			Object object = cacheData.get(Constants.CURR_TABLE_ALL_COLUMN);
		     if(object!=null){
		    	 return (List) object;
		     }
		}
		//到期
		List<TableColumn> list = IbatisSmartDao.queryForList("ec.tableColumn.queryTableColumn",myTableId);
		if(myTableId!=null){
			cacheData.put(Constants.CURR_TABLE_ALL_COLUMN,list);
			cacheDate.put(Constants.CURR_TABLE_ALL_COLUMN,new Date());
		}
		return list;
	}
	
	/**
	 * 得到当前表
	 * @return
	 * @throws SQLException 
	 */
	public MyTable getCurrTable(Integer myTableId) throws SQLException{
		boolean bool=ifCacheDateMature(Constants.CURR_TABLE);
		if(!bool){ // 未到期
			MyTable object = (MyTable) cacheData.get(Constants.CURR_TABLE);
		     if(object!=null && object.getId()==myTableId && myTableId>0){
		    	 return  object;
		     }
		}
		//到期
		Object obj = IbatisSmartDao.queryForObject("ec.myTable.queryObj.id",myTableId);
		if(myTableId!=null && myTableId>0){
			cacheData.put(Constants.CURR_TABLE,obj);
			cacheDate.put(Constants.CURR_TABLE,new Date());
		}
		return (MyTable) obj;
	}
	
	/**
	 * 得到当前表所有的Text列
	 * @return
	 * @throws SQLException 
	 */
	public TextColumnType getCurrTableAllTextColumn(Integer columnId) throws SQLException{
		boolean bool=ifCacheDateMature(Constants.CURR_TABLE_ALL_TEXT_COLUMN);
		if(!bool){ // 未到期
			Map<Integer,TextColumnType> map= (Map<Integer, TextColumnType>) cacheData.get(Constants.CURR_TABLE_ALL_TEXT_COLUMN);
		     if(map!=null){
		    	 TextColumnType text=(TextColumnType) map.get(columnId);
		    	 if(text==null){
		    		  text=(TextColumnType) IbatisSmartDao.queryForObject("ec.textColumnType.queryTextColumnObj",columnId);
		    		  text.setFilterOfTextType((FilterOfTextType) IbatisSmartDao.queryForObject("ec.filterOfTextType.queryFilterObj.columnTypeId",text.getId()));
		    		  text.setValueEpeat((ValueEpeat) IbatisSmartDao.queryForObject("ec.valueEpeat.queryValueEpeat.textColumnTypeId",text.getId()));
	        		   text.setWarn((Warn) IbatisSmartDao.queryForObject("ec.warn.queryObj.columnId",columnId));
	    		       text.setDefaultValue((DefaultValue) IbatisSmartDao.queryForObject("ec.defaultValue.queryObj.columnId",columnId));
        			  text.setAuditing((Auditing) IbatisSmartDao.queryForObject("ec.auditing.queryObj.tableColumnId",columnId));
		    		text.setTableColumnConstraint((TableColumnConstraint) IbatisSmartDao.queryForObject("ec.tableColumnConstraint.queryObj.columnId",columnId));
		    		text.setProcess((Process) IbatisSmartDao.queryForObject("ec.process.queryProcNo",text.getId()));
	    		  map.put(columnId, text);
		    	 }
		    	 return text;
		     }
		}
		//到期
		HashMap<Integer,TextColumnType> map = new HashMap<Integer,TextColumnType>();
		TextColumnType text=(TextColumnType) IbatisSmartDao.queryForObject("ec.textColumnType.queryTextColumnObj",columnId);
		text.setFilterOfTextType((FilterOfTextType) IbatisSmartDao.queryForObject("ec.filterOfTextType.queryFilterObj.columnTypeId",text.getId()));
		text.setValueEpeat((ValueEpeat) IbatisSmartDao.queryForObject("ec.valueEpeat.queryValueEpeat.textColumnTypeId",text.getId()));
		   text.setWarn((Warn) IbatisSmartDao.queryForObject("ec.warn.queryObj.columnId",columnId));
	       text.setDefaultValue((DefaultValue) IbatisSmartDao.queryForObject("ec.defaultValue.queryObj.columnId",columnId));
		  text.setAuditing((Auditing) IbatisSmartDao.queryForObject("ec.auditing.queryObj.tableColumnId",columnId));
		text.setTableColumnConstraint((TableColumnConstraint) IbatisSmartDao.queryForObject("ec.tableColumnConstraint.queryObj.columnId",columnId));
		text.setProcess((Process) IbatisSmartDao.queryForObject("ec.process.queryProcNo",text.getId()));
		map.put(columnId, text);
		if(columnId!=null){
			cacheData.put(Constants.CURR_TABLE_ALL_TEXT_COLUMN,map);
			cacheDate.put(Constants.CURR_TABLE_ALL_TEXT_COLUMN,new Date());
		}
		return text;
	}
	
	/**
	 * 清空当前表所有的Text列
	 */
	public void clearCurrTableAllTextColumn(Integer columnId) throws SQLException{
		Map<Integer,TextColumnType> map= (Map<Integer, TextColumnType>) cacheData.get(Constants.CURR_TABLE_ALL_TEXT_COLUMN);
		if(map!=null){
			map.remove(columnId);
		}
	}
	
	/**
	 *得到类别表 
	 */
	public SortTable getCacheSortTable(Integer sortTableId) throws SQLException{
		boolean bool=ifCacheDateMature(Constants.SORT_TABLE_ALL);
		if(!bool){ // 未到期
			Map<Integer,SortTable> map= (Map<Integer, SortTable>) cacheData.get(Constants.SORT_TABLE_ALL);
		     if(map!=null){
		    	 SortTable sortTable=(SortTable) map.get(sortTableId);
		    	 if(sortTable==null){
		    		  sortTable=(SortTable) IbatisSmartDao.queryForObject("ec.sortTable.querySortTableNo",sortTableId);
		    		  map.put(sortTableId, sortTable);
		    	 }
		    	 return sortTable;
		     }
		}
		//到期
		HashMap<Integer,SortTable> map = new HashMap<Integer,SortTable>();
		SortTable sortTable=(SortTable) IbatisSmartDao.queryForObject("ec.sortTable.querySortTableNo",sortTableId);
		map.put(sortTableId, sortTable);
		if(sortTableId!=null){
			cacheData.put(Constants.SORT_TABLE_ALL,map);
			cacheDate.put(Constants.SORT_TABLE_ALL,new Date());
		}
		return sortTable;
	}
	
	/**
	 * 清空当前类别表
	 */
	public void clearCacheSortTable(Integer sortTableId) throws SQLException{
		Map<Integer,TextColumnType> map= (Map<Integer, TextColumnType>) cacheData.get(Constants.SORT_TABLE_ALL);
		if(map!=null){
			map.remove(sortTableId);
		}
	}
	
	
	/**
	 * 得到所有的表
	 * @throws SQLException 
	 */
	public List<MyTable>  getAllTable() throws SQLException{
		boolean bool=ifCacheDateMature(Constants.ALL_TABLE);
		if(!bool){ // 未到期
			Object object = cacheData.get(Constants.ALL_TABLE);
		     if(object!=null){
		    	 return (List) object;
		     }
		}
		//到期
		List<MyTable> list = IbatisSmartDao.queryForList("ec.myTable.queryAll");
		cacheData.put(Constants.ALL_TABLE,list);
		cacheDate.put(Constants.ALL_TABLE,new Date());
		return list;
	}
	
	/**
	 *得到所有的 Button 
	 * @throws SQLException 
	 */
	public List<Button>  getAllButton() throws SQLException{
		boolean bool=ifCacheDateMature(Constants.ALL_BUTTON);
		if(!bool){ // 未到期
			Object object = cacheData.get(Constants.ALL_BUTTON);
		     if(object!=null){
		    	 return (List) object;
		     }
		}
		//到期
		List<Button> list = IbatisSmartDao.queryForList("ec.button.queryAllButton");
		cacheData.put(Constants.ALL_BUTTON,list);
		cacheDate.put(Constants.ALL_BUTTON,new Date());
		return list;
	}
	
	/**
	 * 得到某一个表所有的Button
	 * @throws SQLException 
	 */
	public List<Button>  getCurrTableAllButton(int myTableId) throws SQLException{
		boolean bool=ifCacheDateMature(Constants.CURR_TABLE_ALL_BUTTON);
		if(!bool){ // 未到期
			Object object = cacheData.get(Constants.CURR_TABLE_ALL_BUTTON);
		     if(object!=null){
		    	 return (List) object;
		     }
		}
		//到期
		List<Button> list = new Vector<Button>();
		List<Button> allButton  = getAllButton();
		List<MyTablesButtonDto>  dtos =  IbatisSmartDao.queryForList("ec.button.queryTableAllButton",myTableId);
		for (Iterator iterator = dtos.iterator(); iterator.hasNext();) {
			MyTablesButtonDto myTablesButtonDto = (MyTablesButtonDto) iterator
					.next();
			Button button =null;
			for (Iterator iterator2 = allButton.iterator(); iterator2.hasNext();) {
				Button button2 = (Button) iterator2.next();
				if(button2.getId()==myTablesButtonDto.getButtonId()){
					button2.setIfshow(myTablesButtonDto.getIfshow());
					button=button2;
					break;
				}
			}
			if(button!=null){
				list.add(button);
			}
		}
		cacheData.put(Constants.CURR_TABLE_ALL_BUTTON,list);
		cacheDate.put(Constants.CURR_TABLE_ALL_BUTTON,new Date());
		return list;
	}
	
	/**
	 *得到所有的 报表 
	 * @throws SQLException 
	 */
	public List<ReportForms>  getAllReportForms() throws SQLException{
		boolean bool=ifCacheDateMature(Constants.ALL_REPORT_FORMS);
		if(!bool){ // 未到期
			Object object = cacheData.get(Constants.ALL_REPORT_FORMS);
		     if(object!=null){
		    	 return (List) object;
		     }
		}
		//到期
		List<ReportForms> list = IbatisSmartDao.queryForList("ec.reportForms.queryAllReportForms");
		cacheData.put(Constants.ALL_REPORT_FORMS,list);
		cacheDate.put(Constants.ALL_REPORT_FORMS,new Date());
		return list;
	}
	
	
	
	
	
	
	
	
	
	
	
	

	
	
}

你可能感兴趣的:(apache,jboss,Security,sun)