⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 .#common.java.1.3

📁 管理公司合同
💻 3
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 2006-7-12 14:48:24
 *
 * By SinoBest
 * Copyright hnisi.com.cn, 2005-2006, All rights reserved.
 */

package cn.com.juneng.system.common;

import java.io.File;
import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;

import cn.com.juneng.system.common.exception.SystemRuleException;
import cn.com.juneng.system.common.util.OgnlUtil;

/**
 * @author yehailong
 * 
 */

public class COMMON {
	public static void main(String[] args) {
		String[] a = new String[] { "1", "2" };
		System.out.println(arrayIndexOf(a, "1"));
	}

	/**
	 * 附件存储位置,临时使用,需要在配置文件中定义
	 */
	private static final String filePath = "c:\\attachment";

	/**
	 * 流程定义XML存储路径
	 */
	public static final String flowXmlPath = "wf" + File.separator + "WebFlow"
			+ File.separator + "flows" + File.separator;

	/**
	 * 日期格式:yyyy-MM-dd
	 */
	public static final String DATA_FORMAT = "yyyy-MM-dd";

	/**
	 * 时间格式:yyyy-MM-dd HH:mm:ss
	 */
	public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static final String DB_DIALECT = "SQLSERVER";

	private static String uid = "0";

	public static String TABLE_STYLE = "class=table_frame cellSpacing=1 cellPadding=1";
   
	/**
	 * 是否最新信息:newflag:最新;oldflag:旧信息;
	 * */
	public static String NEWFLAG="1";
	public static String OLDFLAG="2";
	
	/**
	 * 获取系统唯一ID
	 * 
	 * @return
	 */
	public static String getUUID() {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String tempId = sf.format(new Date());
		if (Long.parseLong(uid) >= Long.parseLong(tempId))
			uid = (Long.parseLong(uid) + 1) + "";
		else
			uid = tempId;

		return uid;
	}

	/**
	 * 判断某个对象是否为空 集合类、数组做特殊处理
	 * 
	 * @param obj
	 * @return 如为空,返回true,否则false
	 * @author yehailong
	 */
	public static boolean isEmpty(Object obj) {
		if (obj == null)
			return true;

		// 如果不为null,需要处理几种特殊对象类型
		if (obj instanceof String) {
			return obj.equals("");
		} else if (obj instanceof Collection) {
			// 对象为集合
			Collection coll = (Collection) obj;
			return coll.size() == 0;
		} else if (obj instanceof Map) {
			// 对象为Map
			Map map = (Map) obj;
			return map.size() == 0;
		} else if (obj.getClass().isArray()) {
			// 对象为数组
			return Array.getLength(obj) == 0;
		} else {
			// 其他类型,只要不为null,即不为empty
			return false;
		}
	}

	/**
	 * 字符串是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDigitalString(String str) {
		if (isEmpty(str)) {
			return false;
		} else {
			return (!str.startsWith("0")) && str.matches("[0-9]*");
		}
	}

	/**
	 * 系统附件存储位置
	 * 
	 * @return
	 */
	public static String getFilePath() {
		File file = new File(filePath);
		if (file == null || !file.exists()) {
			file.mkdirs();
		}
		return filePath;
	}

	/**
	 * 只显示特定长度字符串,如果超过len,使用title来显示全部内容
	 * 
	 * @param value
	 *            字符串值
	 * @param len
	 *            显示长度
	 * @return
	 */
	public static String showValue(String value, int len) {
		if (COMMON.isEmpty(value)) {
			return " ";
		}
		if (value.length() <= len) {
			return value;
		}
		StringBuffer buf = new StringBuffer("<span title='" + value + "'>");
		buf.append(value.substring(0, len));
		buf.append("...</span>");

		return buf.toString();
	}

	/**
	 * 类似如String.indexOf函数,获取对象在数组中的位置,如无,返回-1
	 * 
	 * @param array
	 * @param value
	 * @return
	 */
	public static int arrayIndexOf(Object array, Object value) {
		if (array == null || value == null) {
			return -1;
		}
		int len = Array.getLength(array);
		for (int i = 0; i < len; i++) {
			if (Array.get(array, i).equals(value)) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 对象属性复制,props以","分隔
	 * 
	 * @param target
	 *            目标对象 普通javabean对象或者Map对象
	 * @param src
	 *            源对象 普通javabean对象或者Map对象
	 * @param targetProps
	 *            目标对象属性,以","分隔
	 * @param srcProps
	 *            源对象属性,以","分隔
	 */
	public static void copyProperty(Object target, Object src,
			String targetProps, String srcProps) throws SystemRuleException {
		if (COMMON.isEmpty(targetProps) || COMMON.isEmpty(srcProps)) {
			throw new SystemRuleException("复制的属性名不能为空!");
		}
		String[] targetPropArr = targetProps.split(",");
		String[] srcPropArr = srcProps.split(",");
		if (targetPropArr.length != srcPropArr.length) {
			throw new SystemRuleException("目标属性和源属性个数不一致!");
		}
		for (int i = 0; i < targetPropArr.length; i++) {
			Object value = null;
			if (src instanceof Map) {
				value = ((Map) src).get(srcPropArr[i]);
			} else {
				try {
					value = PropertyUtils.getProperty(src, srcPropArr[i]);
				} catch (Exception e) {
					e.printStackTrace();
					throw new SystemRuleException("程序出错:" + e.getMessage());
				}
			}
			if (target instanceof Map) {
				((Map) target).put(targetPropArr[i], value);
			} else {
				try {
					PropertyUtils.setProperty(target, targetPropArr[i], value);
				} catch (Exception e) {
					e.printStackTrace();
					throw new SystemRuleException("程序出错:" + e.getMessage());
				}
			}
		}
	}

	/**
	 * @see #copyProperty(Object, Object, String, String)
	 * @param target
	 * @param src
	 * @param targetProps
	 * @throws SystemRuleException
	 */
	public static void copyProperty(Object target, Object src,
			String targetProps) throws SystemRuleException {
		copyProperty(target, src, targetProps, targetProps);
	}

	/**
	 * 集合转换成MAP
	 * 
	 * @param cl
	 *            集合
	 * @param keyProp
	 *            KEY属性名
	 * @return
	 */
	public static Map collection2map(Collection cl, String keyProp) {
		if (isEmpty(cl)) {
			return null;
		}
		Iterator it = cl.iterator();
		Map map = new HashMap();
		while (it.hasNext()) {
			Object value = it.next();
			if (value instanceof Map) {
				Map temp = (Map) value;
				try {
					map.put(temp.get(keyProp), value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				try {
					map.put(PropertyUtils.getProperty(value, keyProp), value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return map;
	}

	/**
	 * 通过传递的BaseForm构造,分页SQL语句
	 * 
	 * @param sql
	 * @param form
	 * @return
	 * 
	 * @author yehailong
	 */
	public static String getPageSql(String sql, BaseForm form) {
		return getPageSql(sql, form.getStartPos(), form.getEndPos());
	}

	public static String getPageSql(String sql, int startPos, int endPos) {
		if (DB_DIALECT.equals("ORACLE")) {
			// 先按查询条件查询出从0到页未的记录.然后再取出从页开始到页未的记录
			StringBuffer sqlBuffer = new StringBuffer();
			sqlBuffer
					.append("select * from ( select row_.*, rownum rownum_ from ( ");
			sqlBuffer.append(sql);
			sqlBuffer.append(" ) row_ where rownum <= " + endPos
					+ ") where rownum_ >= " + startPos);

			return sqlBuffer.toString();
		} else {
			// TODO 其他数据库的分页算法
			return sql;
		}
	}

	/**
	 * <p>
	 * 根据request传入的参数构造查询条件语句
	 * 
	 * <li>查询参数名必须包含$Q_的特殊标志
	 * <li>$Q_后面的字符串与数据库字段对应
	 * <li>$Q_前面的字符串位查询匹配方式(如=,like,>=)
	 * <li>如不指定,时间默认匹配为=,其他默认为like
	 * <li>当传入的参数值匹配DATA_FORMAT/TIME_FORMAT,则当时间处理
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return 查询条件sql语句
	 * 
	 * @author yehailong
	 */
	public static String getQuerySql(HttpServletRequest request) {
		StringBuffer buf = new StringBuffer();
		Enumeration params = request.getParameterNames();
		String param = null;
		String value = null;
		int i = 0;
		HashMap qMap = new HashMap();
		while (params.hasMoreElements()) {
			param = (String) params.nextElement();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -