import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpSession;
import com.zte.ssb.zorm.Query;
import com.zte.ssb.zorm.Session;
public class CMSTools
{
public static final String WITHOUT_TIME_PATTERN = "yyyy-MM-dd";
public static final String WITH_TIME_PATTERN = "yyyy-MM-dd hh:mm:ss";
public static boolean isCollectionEmpty(Collection o)
{
if (o == null || o.size() == 0)
return true;
return false;
}
public static boolean isEmpty(String val)
{
if (val == null || "".equals(val))
return true;
return false;
}
public static Date toDate(String val)
{
return toDate(val, WITHOUT_TIME_PATTERN);
}
public static Date toDateWithTime(String val)
{
return toDate(val, WITH_TIME_PATTERN);
}
public static String dateToString(Date date)
{
return dateToString(date, WITHOUT_TIME_PATTERN);
}
public static String dateToStringWithTime(Date date)
{
return dateToString(date, WITH_TIME_PATTERN);
}
private static Date toDate(String val, String format)
{
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
if (val == null)
{
return null;
}
if ("".equals(val))
{
return null;
}
try
{
date = sdf.parse(val);
}
catch (ParseException e)
{
e.printStackTrace();
}
return date;
}
private static String dateToString(Date date, String format)
{
if (date == null)
return "";
SimpleDateFormat sdf = new SimpleDateFormat(WITHOUT_TIME_PATTERN);
return sdf.format(date);
}
public static String dateToStringWithPattern(Date date, String format)
{
if (date == null)
return "";
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 得到本月的第一天
* @return
*/
public static String getMonthFirstDay()
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return dateToString(calendar.getTime(), "yyyy-MM-dd");
}
/**
* 得到本月的最后一天
*
* @return
*/
public static String getMonthLastDay()
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateToString(calendar.getTime(), "yyyy-MM-dd");
}
/**
* 根据Session设置对应连接的语言
* @param
* @return Connection 设置了数据库语言环境连接
* @throws SQLException
*/
public static void setSessionLang(Session session)
{
try {
HttpSession curSession = ThreadShareObject.getInstance().getSession();
String sql = "ALTER SESSION SET NLS_LANGUAGE = 'SIMPLIFIED CHINESE'";
if (curSession != null)
{
String userLanguage = null;
userLanguage = (String)curSession.getAttribute(CmsConsts.USER_LANGUAGE);
if (userLanguage != null && userLanguage.equals(CmsConsts.CMS_LANGUAGE_ENGLISH))
{
sql = "ALTER SESSION SET NLS_LANGUAGE = 'AMERICAN'";
}
}
Query q = session.createSQLQuery(sql);
q.executeUpdate();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
/**
* 填充查询参数
* @param query
* @param params
*/
public static void fillQueryParams(Query query, List
{
if (!CMSTools.isCollectionEmpty(params))
{
Iterator itr = params.iterator();
while (itr.hasNext())
{
SQLParamEntity param = (SQLParamEntity)itr.next();
if ("S".equals(param.getParamType()))
{
query.setParameter(param.getParamName(), param.getParamValue().toString());
}
else if ("L".equals(param.getParamType()))
{
query.setParameterList(param.getParamName(), (String[])param.getParamValue());
}
}
}
}
/**
* 由于oracle in 里面只能放1000个属性值 编写此方法进行字符串转换变成(id in (1,2...) or id in(1000,1001...))
* 2014-7-29
* @author yang.daofang5 6092001817
* @param ids id集合
* @param fieldStr 属性名称
* @return 截取之后的string
*/
public static String whereInLengthSubString(List
if(ids==null||ids.size()==0){
//没有销售处则返回空值
return fieldStr+" in (0) ";
}
StringBuffer str=new StringBuffer();
if(ids.size()>=1000){
str.append("(");
}
str.append(fieldStr+" in (");
for(int j=0;j
str.delete(str.length()-1, str.length());
str.append(") or "+fieldStr+" in ("+ids.get(j)+",");
}else{
str.append(ids.get(j)+",");
}
}
str.delete(str.length()-1,str.length()).append(")");
if(ids.size()>999){
str.append(")");
}
return str.toString();
}
/**
* 由于oracle in 里面只能放1000个属性值 编写此方法进行字符串转换变成(id in (1,2...) or id in(1000,1001...))
* 2015-03-04
* @author yang.daofang5 6092001817
* @param ids id集合
* @param fieldStr 属性名称
* @return 截取之后的string
*/
public static String whereInLengthSubString(Long userId,String fieldStr,Session session){
if(userId==null){
//没有销售处则返回空值
return fieldStr+" in (0) ";
}
StringBuffer sql=new StringBuffer();
sql.append("SELECT C.NODE_ID ");
sql.append(" FROM CDM_HIERARCHY A, ");
sql.append(" CDM_HIERARCHY_NODE B, ");
sql.append(" CDM_ROLE_HRC_NODE C, ");
sql.append(" CDM_ROLES D, ");
sql.append(" CDM_ROLE_HOLDERS E, ");
sql.append(" BAS_USER F ");
sql.append("WHERE F.EMPLOYEE_ID = E.PERSON_ID ");
sql.append(" AND E.ROLE_ID = D.ROLE_ID ");
sql.append(" AND D.ROLE_ID = C.ROLE_ID ");
sql.append(" AND C.NODE_ID = B.NODE_ID ");
sql.append(" AND B.HIERARCHY_ID = A.HIERARCHY_ID ");
sql.append(" AND A.ENABLED_FLAG = 'Y' ");
sql.append(" AND B.ENABLED_FLAG = 'Y' ");
sql.append(" AND C.ENABLED_FLAG = 'Y' ");
sql.append(" AND D.ENABLED_FLAG = 'Y' ");
sql.append(" AND E.ENABLED_FLAG = 'Y' ");
sql.append(" AND F.USER_ID = ?");
Query query=session.createSQLQuery(sql.toString());
query.setParameter(0, userId);
List
if(ids==null){
return fieldStr+" in (0) ";
}
StringBuffer str=new StringBuffer();
if(ids.size()>=1000){
str.append("(");
}
str.append(fieldStr+" in (");
for(int j=0;j
str.delete(str.length()-1, str.length());
str.append(") or "+fieldStr+" in ("+ids.get(j)+",");
}else{
str.append(ids.get(j)+",");
}
}
str.delete(str.length()-1,str.length()).append(")");
if(ids.size()>999){
str.append(")");
}
return str.toString();
}
public static void main(String args[])
{
List
String sql = whereStringLengthSubString(ids,"tou");
System.out.print(sql);
}
/**
* (fieldStr in ('1','2'...) )
* 2014-7-29
* @author yang.daofang5 6092001817
* @param ids id集合
* @param fieldStr 属性名称
* @return 截取之后的string
*/
public static String whereStringLengthSubString(List
if(ids==null||ids.size()==0){
//没有销售处则返回空值
return fieldStr+" in ('') ";
}
StringBuffer str=new StringBuffer();
if(ids.size()>=1000){
str.append("(");
}
str.append(fieldStr+" in (");
for(int j=0;j
}
str.delete(str.length()-1,str.length()).append(")");
if(ids.size()>999){
str.append(")");
}
return str.toString();
}
/**
* 转换成double,为空或者null的话,返回0
* @param obj
* @return
*/
public static double objectToDouble(Object obj)
{
double res = Double.parseDouble("0");
if(obj!=null && !CMSTools.isEmpty(obj.toString()) )
{
res = Double.parseDouble(obj.toString());
}
return res;
}
/**
* 转换成Long,为空或者null的话,返回0
* @param obj
* @return
*/
public static Long objectToLong(Object obj)
{
Long res = 0L;
if(obj!=null && !CMSTools.isEmpty(obj.toString()) )
{
res = Long.parseLong(obj.toString());
}
return res;
}
/**
* 转换成String,为空或者null的话,返回""
* @param obj
* @return
*/
public static String objectToString(Object obj)
{
String str = (obj==null?"":obj.toString());
return str;
}
/**
* 转换成String,为空或者null的话,返回"0"
* @param obj
* @return
*/
public static String objectToNumber(Object obj)
{
String str = (obj==null||obj.toString().equals("")? "0" : obj.toString());
return str;
}
/**
* 比较2个时间大小,如果date1
* @param date2
* @return true
*/
public static boolean checkDate(String date1,String date2){
java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Calendar c1=java.util.Calendar.getInstance();
java.util.Calendar c2=java.util.Calendar.getInstance();
try{
c1.setTime(df.parse(date1));
c2.setTime(df.parse(date2));
}catch(java.text.ParseException e){
System.err.println("格式不正确");
e.getStackTrace();
}
int result=c1.compareTo(c2);
if(result<0){
//date1
}else{
return false;
}
}
}