CommonTools.java

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;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值