package com.wolf.pub;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2007-11-29
* Time: 22:40:06
* To change this template use File | Settings | File Templates.
*/
import javax.servlet.jsp.JspWriter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.ResultSet;
public class CommonTools {
protected static DictPool dictPool = DictPool.getInstance();
public static InitDataSource dataSource = new InitDataSource();
protected CommonTools()
{
}
public static void getDictValue(String dictName, JspWriter out, String selectValue, boolean withID, String filter)
{
DictDto dict = dictPool.getDictDto(dictName);
Pattern p = null;
Matcher m = null;
if(dict == null)
return;
Map hashmap = dict.getDictValues();
if(hashmap == null)
return;
String dictKey = "";
String dictValue = "";
if(filter.length() > 0)
p = Pattern.compile(filter);
try
{
Iterator it = hashmap.keySet().iterator();
do
{
if(!it.hasNext())
break;
dictKey = it.next().toString();
dictValue = hashmap.get(dictKey).toString();
dictValue = dictValue.split("[|]")[0];
if(filter.length() <= 0 || p.matcher(dictKey).find())
if(withID)
{
if(dictKey.equals(selectValue))
out.println("<option value=" + dictKey + " selected>" + dictKey + ":" + dictValue);
else
out.println("<option value=" + dictKey + ">" + dictKey + ":" + dictValue);
} else
if(dictKey.equals(selectValue))
out.println("<option value=" + dictKey + " selected>" + dictValue);
else
out.println("<option value=" + dictKey + ">" + dictValue);
} while(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static ResultSet getQueryResult(String sql)
{
ResultSet rs = null;
try
{
rs = dataSource.executeQuery(sql);
//dataSource.closeStmt();
}
catch(Exception e)
{
e.printStackTrace();
rs = null;
}
return rs;
}
public static String getSystemDate(String dataFormat)
{
String sql = " select to_char(sysdate,'" + dataFormat + "') ";
String retStr = "";
ResultSet rs = null;
try
{
rs = dataSource.executeQuery(sql);
if(rs.next()){
retStr = rs.getString(1);
}
}
catch(Exception e)
{
e.printStackTrace();
}
rs = null;
return retStr;
}
public static String getDictValueMC(String dictName, String dictValue)
{
String retStr;
label0:
{
DictDto dict = dictPool.getDictDto(dictName);
if(dict == null)
return "";
Map hashmap = dict.getDictValues();
if(hashmap == null)
return "";
String dictKey = "";
retStr = "";
try
{
Iterator it = hashmap.keySet().iterator();
do
{
if(!it.hasNext())
break label0;
dictKey = it.next().toString();
} while(!dictKey.equals(dictValue));
retStr = hashmap.get(dictKey).toString();
retStr = retStr.split("[|]")[0];
}
catch(Exception e)
{
e.printStackTrace();
}
}
return retStr;
}
//根据查询结果界面的字段类型返回显示的字符串 ,目前只处理字典项的数据
public static String getQueryJgValue(String fieldtype, String fieldvalue)
{
String retStr;
String ft = fieldtype.substring(0,1);
String dictName = "";
if(ft.equals("N")){
dictName = fieldtype.substring(2,fieldtype.length()-1);
retStr = getDictValueMC(dictName,fieldvalue);
}else{
retStr = fieldvalue;
}
return retStr;
}
/*
public static ArrayList getNews(){
ArrayList list = new ArrayList();
try{
String sql = "select dwmc,zgdw from t_ask_newspaperpublish";
ResultSet rs = dataSource.executeQuery(sql);
while(rs.next()){
Sbkjyxkz_Form sb=new Sbkjyxkz_Form();
sb.setDwmc(rs.getString(1));
sb.setZgdw(rs.getString(2));
list.add(sb);
}
dataSource.close();
return list;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
*/
/*********************************
* wolf
*
*/
public static String toStrOption(String key,String value,boolean selected)
{
String htmlStr=null;
if(!selected)
{
if((key!=null&&key.length()>0)&&(value!=null&&value.length()>0))
{
htmlStr="<option value="+key+">"+value+"</option>";
return htmlStr;
}
else
{
return null;
}
}
else
{
if((key!=null&&key.length()>0)&&(value!=null&&value.length()>0))
{
htmlStr="<option value="+key+" selected=/"selected/">"+value+"</option>";
return htmlStr;
}
else
{
return null;
}
}
}
/********************************
* 用于将hashmap转换成select中的option元素
* @param hs
* @param title
* @param sKey
* @return retstr
*/
public static String toOptions(HashMap hs,String title,String sKey)
{
String retstr=null;
retstr="<option value="+"0"+">"+title+"</option>";
if(!hs.isEmpty())
{
Set st=hs.entrySet();
Iterator it=st.iterator();
while(it.hasNext())
{
Map.Entry me=(Map.Entry)it.next();
if(!"".equals(sKey)&&me.getKey().equals(sKey))
{
retstr+=toStrOption((String)me.getKey(),(String)me.getValue(),true);
}
else
{
retstr+=toStrOption((String)me.getKey(),(String)me.getValue(),false);
}
}
}
return retstr;
}
}