Index: build.gradle =================================================================== diff -u -r17065 -r17182 --- build.gradle (.../build.gradle) (revision 17065) +++ build.gradle (.../build.gradle) (revision 17182) @@ -216,6 +216,7 @@ dependencies { + compile group: 'com.jfinal', name: 'jfinal', version: '2.2' compile group: 'org.apache.axis', name: 'axis', version:'1.4' compile group: 'org.apache.axis', name: 'axis-ant', version:'1.4' Index: forgon-tools/src/main/java/com/forgon/entity/ExtJsEntity.java =================================================================== diff -u --- forgon-tools/src/main/java/com/forgon/entity/ExtJsEntity.java (revision 0) +++ forgon-tools/src/main/java/com/forgon/entity/ExtJsEntity.java (revision 17182) @@ -0,0 +1,114 @@ +package com.forgon.entity; + +/** + * @author zhonghaowen + * @apiNote extJs实体类, 里面有分页的属性, sql的select条件, where条件, order条件 + * @since 2017-02-04 + */ +public class ExtJsEntity { + + /** + * select的语句,不包括from + */ + private String select; + + /** + * from后面语句,包括from + */ + private String where; + + /** + * 排序语句 + */ + private String order; + + /** + * 分页的页数 + */ + private int start; + + /** + * 分页的大小 + */ + private int limit; + + /** + * 查询表的别名 + */ + private String alias; + + public ExtJsEntity() { + } + + + public ExtJsEntity(String select, String where, String order) { + this.select = select; + this.where = where; + this.order = order; + } + + public ExtJsEntity(String where) { + this.where = where; + } + + public String getSelect() { + return select; + } + + public void setSelect(String select) { + this.select = select; + } + + public String getWhere() { + return where; + } + + public void setWhere(String where) { + this.where = where; + } + + public String getOrder() { + return order; + } + + public void setOrder(String order) { + this.order = order; + } + + public int getStart() { + return start; + } + + public void setStart(int start) { + this.start = start; + } + + public int getLimit() { + return limit; + } + + public void setLimit(int limit) { + this.limit = limit; + } + + + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + @Override + public String toString() { + return "ExtJsEntity{" + + "select='" + select + '\'' + + ", where='" + where + '\'' + + ", order='" + order + '\'' + + ", start=" + start + + ", limit=" + limit + + ", alias='" + alias + '\'' + + '}'; + } +} Index: ssts-web/src/main/resources/spring/applicationContext-service.xml =================================================================== diff -u -r16279 -r17182 --- ssts-web/src/main/resources/spring/applicationContext-service.xml (.../applicationContext-service.xml) (revision 16279) +++ ssts-web/src/main/resources/spring/applicationContext-service.xml (.../applicationContext-service.xml) (revision 17182) @@ -781,6 +781,8 @@ + + Index: forgon-core/src/main/java/com/forgon/util/ExtGridUtils.java =================================================================== diff -u --- forgon-core/src/main/java/com/forgon/util/ExtGridUtils.java (revision 0) +++ forgon-core/src/main/java/com/forgon/util/ExtGridUtils.java (revision 17182) @@ -0,0 +1,440 @@ +package com.forgon.util; + +import com.forgon.entity.ExtJsEntity; +import com.forgon.tools.StrutsParamUtils; +import com.forgon.tools.hibernate.ObjectDao; +import com.forgon.tools.json.JSONUtil; +import com.forgon.tools.util.IntegerUtils; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; + +import javax.servlet.http.HttpServletRequest; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @author 袁彬 + */ +public class ExtGridUtils { + + protected Logger logger = Logger.getLogger(this.getClass()); + + private static final String GRID_MAPPARAM_FILTER_PREFIX = "grid_filter_"; + private static final String GRID_MAPPARAM_SORT = "sort"; + private static final String GRID_SEARCH_FIELDS = "fields"; + private static final String GRID_SEARCH_QUERY = "query"; + // private static String GRID_PAGE_START = "start"; + // private static String GRID_PAGE_LIMIT = "limit"; + private final static String RESULT = "rows"; + private final static String TOTAL = "total"; + + private ObjectDao objectDao; + + public void setObjectDao(ObjectDao objectDao) { + this.objectDao = objectDao; + } + + /*public static Map getPagedResult(HttpServletRequest request, String select, String sqlExceptSelect, String order, Object... paras) { + int start = StrutsParamUtils.getPraramValue(GRID_PAGE_START, 0); + int limit = StrutsParamUtils.getPraramValue(GRID_PAGE_LIMIT, 30); + int pageNum = start / limit + 1; + //搜索时,优先从搜索框开始,如果没有搜索条件就用列过滤条件 + // String searchQuery = request.getParameter(GRID_SEARCH_QUERY); + if (paras == null) { + paras = new Object[]{}; + } + //过滤条件放在所有条件之后 + List filterParamList = new ArrayList<>(); + String filterSql = parserFilterParams(request, filterParamList); + sqlExceptSelect += filterSql; + for (Object filterParam : filterParamList) { + paras = ArrayUtils.add(paras, filterParam); + } + //最后是排序 + order = getOrders(request, order); + order = StringUtils.defaultIfBlank(order, ""); + sqlExceptSelect += " " + order; + Page dataList = Db.paginate(pageNum, limit, select, sqlExceptSelect, paras); + Map rsMap = new HashMap<>(); + if (dataList != null) { + rsMap.put("total", dataList.getTotalRow()); + rsMap.put("rows", dataList.getList()); + } + else { + rsMap.put("total", 0); + rsMap.put("rows", null); + } + return rsMap; + }*/ + + + /** + * 通过原生sql来获得对应的结果 + * + * @param request {@link HttpServletRequest} + * @param extJsEntity {@link ExtJsEntity} + * @param paras sql查询的预编译参数 + * @return 查询的结果, 格式有以下两种: + *
    + *
  1. 正常查找: {rows:[],total:{}}
  2. + *
  3. 如果找不到记录或者异常: {rows:null,total:{0},tip:{}}
  4. + *
+ */ + public JSONObject getPagedResult(HttpServletRequest request, ExtJsEntity extJsEntity, Object... paras) { + try { + //搜索时,优先从搜索框开始,如果没有搜索条件就用列过滤条件 + // String searchQuery = request.getParameter(GRID_SEARCH_QUERY); + if (paras == null) { + paras = new Object[]{}; + } + //过滤条件放在所有条件之后 + List filterParamList = new ArrayList<>(); + String filterSql = this.parserFilterParams(request, filterParamList, extJsEntity); + filterSql += this.parserQueryParams(filterParamList, extJsEntity); + for (Object filterParam : filterParamList) { + paras = ArrayUtils.add(paras, filterParam); + } + String sqlExceptSelect = extJsEntity.getWhere() + filterSql; + String select = extJsEntity.getSelect(); + //这里先查询总数量,再执行排序 + Integer total = this.getTotal(select + sqlExceptSelect, paras); + if (0 == total) { + return this.buildNotFind("totalCount为0!"); + } + String sql = select + sqlExceptSelect + " " + this.getOrders(request, extJsEntity.getOrder()); + logger.debug("[sql]:" + sql); + JSONObject json = new JSONObject(); + json.put(RESULT, objectDao.findSqlByParam(sql, paras, extJsEntity.getStart(), extJsEntity.getLimit())); + json.put(TOTAL, total); + return json; + } + catch (Exception e) { + logger.error(e, e); + return this.buildNotFind(e.getMessage()); + } + } + + /** + * 通过原生sql来获得对应的结果 + * + * @param extJsEntity {@link ExtJsEntity} + * @param paras 预编译参数(可以不填或者填null) + * @return 查询结果, 返回结果可以参数参照 + * @see ExtGridUtils#getPagedResult(HttpServletRequest, ExtJsEntity, Object...) + */ + public JSONObject getPagedResult(ExtJsEntity extJsEntity, Object... paras) { + return this.getPagedResult(StrutsParamUtils.getRequest(), extJsEntity, paras); + } + + + /** + * 通过hql来获得对应的结果(RESULT是一个list map类型的json字符串) + * + * @param extJsEntity {@link ExtJsEntity} + * @param paras 预编译参数(可以不填或者填null) + * @return 查询结果, 返回结果可以参数参照 + * @see ExtGridUtils#getPagedResult(HttpServletRequest, ExtJsEntity, Object...) + */ + public JSONObject getPagedResultByHql(ExtJsEntity extJsEntity, Object... paras) { + return this.getPagedResultByHql(StrutsParamUtils.getRequest(), extJsEntity, null, paras); + } + + /** + * 通过hql来获得对应的结果(RESULT是一个list 实体类类型的json字符串) + * + * @param extJsEntity {@link ExtJsEntity} + * @param clazz 查询标识符 + * @param paras 预编译参数(可以不填或者填null) + * @return 查询结果, 返回结果可以参数参照 + * @see ExtGridUtils#getPagedResult(HttpServletRequest, ExtJsEntity, Object...) + */ + public JSONObject getPagedResultByHql(ExtJsEntity extJsEntity, Class clazz, Object... paras) { + return this.getPagedResultByHql(StrutsParamUtils.getRequest(), extJsEntity, clazz, paras); + } + + + /** + * 通过hql来获得对应的结果 + * + * @param request {@link HttpServletRequest} + * @param extJsEntity {@link ExtJsEntity} + * @param clazz 这里只是一个标识,用来判断是整个类查询还是只查询部分属性 + *
    + *
  1. 如果整个类查询,select语句可以写select po 或者可以不写
  2. + *
  3. 如果部分属性查询,select语句要用as写出别名
  4. + *
+ * @param paras 预编译参数(可以不填或者填null) + * @return 查询结果, 返回结果可以参数参照 + * @see ExtGridUtils#getPagedResult(HttpServletRequest, ExtJsEntity, Object...) + */ + public JSONObject getPagedResultByHql(HttpServletRequest request, ExtJsEntity extJsEntity, Class clazz, Object... paras) { + try { + //搜索时,优先从搜索框开始,如果没有搜索条件就用列过滤条件 + // String searchQuery = request.getParameter(GRID_SEARCH_QUERY); + if (paras == null) { + paras = new Object[]{}; + } + //过滤条件放在所有条件之后 + List filterParamList = new ArrayList<>(); + String filterSql = this.parserFilterParams(request, filterParamList, extJsEntity); + filterSql += this.parserQueryParams(filterParamList, extJsEntity); + for (Object filterParam : filterParamList) { + paras = ArrayUtils.add(paras, filterParam); + } + String sqlExceptSelect = extJsEntity.getWhere() + filterSql; + Integer total = this.getHqlTotal(sqlExceptSelect, paras); + if (0L == total) { + return this.buildNotFind("totalCount为0!"); + } + String sql = extJsEntity.getSelect() + sqlExceptSelect + " " + this.getOrders(request, extJsEntity.getOrder()); + logger.debug("[sql]:" + sql); + JSONObject json = new JSONObject(); + json.put(TOTAL, total); + List list; + if (clazz == null) { + list = objectDao.findHqlByParam(sql, paras, extJsEntity.getStart(), extJsEntity.getLimit()); + } + else { + list = objectDao.findHqlByParam(sql, paras, extJsEntity.getStart(), extJsEntity.getLimit(), clazz); + } + //这个要需要用工具类来转,以免查询全部属性时引起无限递归导致内存溢出 + String result = JSONUtil.toJSONString(list, true); + json.put(RESULT, result); + return json; + } + catch (Exception e) { + logger.error(e, e); + return this.buildNotFind(e.getMessage()); + } + } + + /** + * 构建没找到的返回值 + * + * @return 有三个属性:total(总数量),rows(记录数 null),tip(没找到的提示信息) + */ + private JSONObject buildNotFind(String msg) { + JSONObject json = new JSONObject(); + json.put(TOTAL, 0); + json.put(RESULT, null); + json.put("tip", msg); + return json; + } + + + /** + * 用sql查询总数量 + * + * @param sql 要查询的sql + * @param paras 查询的sql的参数 + * @return sql查询的总数量 + */ + private Integer getTotal(String sql, Object... paras) { + try { + String countSql = "select count(1) as total from (" + sql + ") allCount"; + List> count = objectDao.findSqlByParam(countSql, paras); + return (Integer) count.get(0).get("total"); + } + catch (Exception e) { + logger.error(e); + throw new RuntimeException(e); + } + } + + + /** + * 用hql查询总数量 + * + * @param hql 要查询的hql + * @param paras 查询的hql的参数 + * @return hql查询的总数量 + */ + private Integer getHqlTotal(String hql, Object... paras) { + try { + String countSql = "select count(*) " + hql; + List objects = objectDao.getHt().find(countSql, paras); + Long count = (Long) objects.get(0); + return count == null ? 0 : Integer.parseInt(count.toString()); + } + catch (Exception e) { + logger.error(e); + throw new RuntimeException(e); + } + } + + /** + * 解析搜索过滤的参数 + * + * @param request {@link HttpServletRequest} + * @param paras sql的条件参数 + * @param extJsEntity {@link ExtJsEntity} + * @return 根据过滤的参数拼接好的查询sql + */ + private String parserFilterParams(HttpServletRequest request, List paras, ExtJsEntity extJsEntity) { + Set filterParamPrefixSet = new HashSet<>(); + String filterSqlStr = " "; + @SuppressWarnings("rawtypes") + Enumeration paramNameEnum = request.getParameterNames(); + while (paramNameEnum.hasMoreElements()) { + String paramName = (String) paramNameEnum.nextElement(); + if (paramName.startsWith(GRID_MAPPARAM_FILTER_PREFIX)) { + paramName = paramName.substring(0, paramName.indexOf("]") + 1); + filterParamPrefixSet.add(paramName); + } + } + if (filterParamPrefixSet.size() > 0) { + for (String filterParamPrefix : filterParamPrefixSet) { + String fieldName = request.getParameter(filterParamPrefix + "[field]"); + String fieldType = request.getParameter(filterParamPrefix + "[data][type]"); + String fieldValue = request.getParameter(filterParamPrefix + "[data][value]"); + String comparison = request.getParameter(filterParamPrefix + "[data][comparison]"); + comparison = convertComparison(comparison); + String alias = extJsEntity.getAlias(); + alias = StringUtils.isBlank(alias) ? "" : alias + "."; + if ("string".equals(fieldType)) { + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " like ?", " and "); + // filterSqlStr = StringUtil.append(filterSqlStr, " " + fieldName + " like ?", " and "); + paras.add("%" + fieldValue + "%"); + } + else if ("date".equals(fieldType)) { + if ("=".equals(comparison)) { + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " >= ?", " and "); + paras.add(getDateValue(">=", fieldValue)); + + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " <= ?", " and "); + paras.add(getDateValue("<=", fieldValue)); + } + else { + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " " + comparison + "?", " and "); + paras.add(getDateValue(comparison, fieldValue)); + } + } + else if (fieldType.equals("list")) { + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " " + " in (" + fieldValue + ")", " and "); + } + else if ("numeric".equals(fieldType)) { + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " " + comparison + "?", " and "); + if (StringUtils.isNotBlank(fieldValue)) { + paras.add(Double.valueOf(fieldValue)); + } + else { + paras.add(fieldValue); + } + } + else if ("boolean".equals(fieldType)) { + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " = ?", " and "); + if (IntegerUtils.isNumeric(fieldValue)) { + paras.add(Integer.valueOf(fieldValue)); + } + else { + paras.add(Boolean.valueOf(fieldValue)); + } + } + else { + filterSqlStr = StringUtil.append(filterSqlStr, " " + alias + fieldName + " = ?", " and "); + paras.add(fieldValue); + } + } + } + return filterSqlStr; + } + + + /** + * 解析搜索栏的参数 + * + * @param paras sql的条件参数 + * @param extJsEntity {@link ExtJsEntity} + * @return 根据搜索栏的参数拼接好的查询sql + */ + private String parserQueryParams(List paras, ExtJsEntity extJsEntity) { + String[] fields = StrutsParamUtils.getPrarmArrValue(GRID_SEARCH_FIELDS); + String query = StrutsParamUtils.getPraramValue(GRID_SEARCH_QUERY, null); + String querySql = ""; + if (fields != null && fields.length > 0 && StringUtils.isNotBlank(query)) { + StringBuilder filterSqlStr = new StringBuilder(" "); + String alias = StringUtils.isBlank(extJsEntity.getAlias()) ? "" : extJsEntity.getAlias() + "."; + filterSqlStr.append(" and ("); + int len = fields.length; + for (int i = 0; i < len - 1; i++) { + filterSqlStr.append(alias).append(fields[i]).append(" like ? ").append(" or "); + paras.add("%" + query + "%"); + } + filterSqlStr.append(alias).append(fields[len - 1]).append(" like ? "); + paras.add("%" + query + "%"); + filterSqlStr.append(") "); + querySql = filterSqlStr.toString(); + logger.debug("搜索栏sql:[" + querySql + "]"); + logger.debug("搜索栏参数:[" + query + "]"); + } + return querySql; + } + + private String convertComparison(String comparison) { + if ("gt".equals(comparison)) { + return "<="; + } + else if ("lt".equals(comparison)) { + return ">="; + } + else if ("eq".equals(comparison)) { + return "="; + } + else { + return null; + } + } + + private String getDateValue(String comparison, String val) { + if (StringUtils.isBlank(val) || StringUtils.isBlank(comparison)) { + return val; + } + if (comparison.equals(">=")) { + val = val + " 00:00:00"; + } + if (comparison.equals("<=")) { + val = val + " 23:59:59"; + } + return val; + } + + private String getOrders(HttpServletRequest request, String order) { + String sort = request.getParameter(GRID_MAPPARAM_SORT); + if (StringUtils.isNotBlank(sort)) { + JSONArray array = JSONArray.fromObject(sort); + if (array != null) { + for (int i = 0; i < array.size(); i++) { + JSONObject json = array.getJSONObject(i); + order = StringUtil.append(order, " " + json.getString("property") + " " + json.getString("direction"), " , "); + } + } + } + if (StringUtils.isNotBlank(order) && !order.toLowerCase().contains("order by")) { + order = " order by " + order; + } + order = StringUtils.defaultIfBlank(order, ""); + return order; + } + + public static boolean isNumeric(String str) { + if (IntegerUtils.isNumeric(str)) { + if (str.length() > 1) { + if (str.indexOf("0") == 0 && str.indexOf("\\.") != 1) { + return false; + } + else if (str.indexOf("0") == 0) { + return false; + } + } + return true; + } + return false; + } +} Index: forgon-core/src/main/java/com/forgon/util/StringUtil.java =================================================================== diff -u --- forgon-core/src/main/java/com/forgon/util/StringUtil.java (revision 0) +++ forgon-core/src/main/java/com/forgon/util/StringUtil.java (revision 17182) @@ -0,0 +1,646 @@ +package com.forgon.util; + +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import java.util.UUID; + +import com.jfinal.kit.StrKit; +import org.apache.commons.lang.StringUtils; + + +/** + * 对字符串各种操作的工具类 + */ +public class StringUtil { + + /** + * 通过 , 拆分字符串数组 + */ + public static String[] splitString(String str) { + String[] result = str.split(",|、"); + return result; + } + + /** + * 将字符串数据转换为int + */ + public static Integer[] splitInteger(String str) { + if (!"".equals(str) && str == null) { + return null; + } + String[] result = str.split(","); + Integer[] ids = new Integer[result.length]; + for (int i = 0; i < result.length; i++) { + ids[i] = Integer.parseInt(result[i]); + } + return ids; + } + + public static HashMap getHash(Integer[] ids) { + HashMap hm = new HashMap(); + for (Integer id : ids) { + hm.put(id, id); + } + return hm; + } + + public static HashMap getHash(String[] ids) { + HashMap hm = new HashMap(); + for (String id : ids) { + hm.put(id, id); + } + return hm; + } + + /** + * 将字符串,与过来的Int对比,存在为true + */ + public static boolean isLive(String str, Integer target) { + Integer[] targets = splitInteger(str); + for (Integer son : targets) { + if (son == target) { + return true; + } + } + return false; + } + + /** + * 将字符串全部转换为Double,返回是否转换成功 + */ + public static String[] changeSuss(String[] jcjg, String exceptString) { + for (int i = 0; i < jcjg.length; i++) { + try { + if (jcjg[i] != null && !"".equals(jcjg[i])) { + if (!jcjg[i].equals(exceptString)) { + Double.parseDouble(jcjg[i]); + } + else { + //当要实现结果要--时,替换为10000 + jcjg[i] = "10000"; + } + } + } + catch (Exception e) { + return null; + } + } + return jcjg; + } + + /** + * 合并字符串,通过,分隔 + */ + public static String combineString(String[] strs) { + if (strs == null) { + return null; + } + StringBuffer result = new StringBuffer(); + for (String str : strs) { + if (org.apache.commons.lang3.StringUtils.isNotBlank(str)) { + result.append("," + str); + } + } + if (result.length() > 0) { + result = result.deleteCharAt(0); + } + return result.toString(); + + } + + /** + * 列表合并 + */ + public static String combineList(List strs) { + StringBuffer result = new StringBuffer(); + for (String str : strs) { + result.append("," + str); + } + if (strs.size() != 0) { + result.append(","); + } + return result.toString(); + } + + + /** + * 列表合并(list) + */ + public static String combineListInt(List strs) { + StringBuffer result = new StringBuffer(); + for (int i = 0; i < strs.size(); i++) { + if (i != 0) { + result.append(","); + } + result.append(strs.get(i)); + } + return result.toString(); + } + + /** + * 列表合并'result'格式 + */ + public static String combineListQuotes(List strs) { + StringBuffer result = new StringBuffer(); + for (int i = 0; i < strs.size(); i++) { + result.append(",'" + strs.get(i) + "'"); + } + result.deleteCharAt(0); + return result.toString(); + } + + /** + * 列表合并(set) + */ + public static String combineSetInt(Set strs) { + StringBuffer result = new StringBuffer(); + boolean flag = false; + for (Integer str : strs) { + if (flag) { + result.append(","); + } + result.append(str); + flag = true; + } + return result.toString(); + } + + /** + * 列表合并(set) + */ + public static String combineSetString(Set strs) { + StringBuffer result = new StringBuffer(); + for (String str : strs) { + result.append(","); + result.append(str); + } + result.deleteCharAt(0); + return result.toString(); + } + + /** + * 列表合并(set) + */ + public static Set setTester(Set tester, String names) { + if (org.apache.commons.lang3.StringUtils.isBlank(names)) { + return tester; + } + String[] name = names.split(","); + for (int i = 0; i < name.length; i++) { + tester.add(name[i]); + } + return tester; + } + + + /** + * 将字符串后两位的数字部分进行相加 augend被加数 addend加数 + */ + public static String AddOperation(String augend, int addend) { + String result = null; + String augStr = augend.substring(0, augend.length() - 2); + String augCount = augend.substring(augend.length() - 2); + int resultStr = Integer.parseInt(augCount) + addend; + if (resultStr < 10) { + result = augStr + "0" + resultStr; + } + else { + result = augStr + resultStr; + } + return result; + } + + /** + * 字符串最后两位是否为数字 + */ + public static boolean isDigit(String str) { + String augCount = str.substring(str.length() - 2); + try { + Integer.parseInt(augCount); + } + catch (Exception e) { + return false; + } + return true; + } + + /** + * 判断数据中是否有重复的值 + */ + public static boolean checkRepeat(Integer[] array) { + ArrayList temp = new ArrayList(); + for (Integer arr : array) { + //把0去掉 + if (arr != 0) { + temp.add(arr); + } + } + if (temp.size() == 0) { + //人员数组为空 + return false; + } + Set set = new HashSet(); + for (Integer user : temp) { + set.add(user); + } + if (set.size() == temp.size()) { + return false; + } + else { + return true; + } + } + + /** + * 将人员数组中 去0 + */ + public static ArrayList getUserOutZero(Integer[] array) { + ArrayList temp = new ArrayList(); + for (Integer arr : array) { + //把0去掉 + if (arr != 0) { + temp.add(arr); + } + } + return temp; + } + + /** + * 两个list里的值是否相同 不同为true 相同为false + */ + public static boolean isDifferent(ArrayList users, ArrayList oczrys) { + if (users.size() != oczrys.size()) { + return true; + } + boolean flag = false; + for (Integer user : users) { + for (Integer oczry : oczrys) { + if (user == oczry) { + flag = false; + break; + } + flag = true; + } + if (flag) { + break; + } + } + return flag; + } + + /** + * 根据当前的字符串的后两个数据和count运算,返加结果 + */ + public static String getLastbp(String lastbp) { + String result = null; + int resultStr = Integer.parseInt(lastbp); + if (resultStr < 10) { + result = "0" + resultStr; + } + else { + result = "" + resultStr; + } + return result; + } + + /** + * 根据两个样品编号,得出count数量 + */ + public static Integer getCount(String str, String lastbp) { + String augCount = str.substring(str.length() - 2); + Integer result = Integer.parseInt(lastbp) - Integer.parseInt(augCount) + 1; + return result; + } + + /** + * 根据新编号 + */ + public static String getNewNo(String str) { + String result = ""; + try { + String augNo = str.substring(0, str.length() - 3); + String augCount = str.substring(str.length() - 3); + Integer count = Integer.parseInt(augCount) + 1; + if (count < 10) { + result = augNo + "00" + count; + } + else if (count < 100) { + result = augNo + "0" + count; + } + else { + result = augNo + "" + count; + } + } + catch (Exception e) { + result = str; + } + return result; + } + + public static String xhToChar(int xh) { + String newXH; + if (xh < 10) { + newXH = "0000" + xh; + } + else if (xh < 100) { + newXH = "000" + xh; + } + else if (xh < 1000) { + newXH = "00" + xh; + } + else if (xh < 10000) { + newXH = "0" + xh; + } + else { + newXH = "" + xh; + } + return newXH; + } + + /** + * 生成新的检验编号 + */ + public static String FixXhToNewJybh(String oldJybh, int index) { + if (index == 0) { + return oldJybh; + } + String newJybh = oldJybh.substring(0, 5); + Integer oldXh = Integer.parseInt(oldJybh.substring(5)); + int newXh = oldXh + index; + String newXhStr = xhToChar(newXh); + return newJybh + newXhStr; + } + + /** + * 去掉相同的字符串 + */ + public static HashSet distinctSet(String[] strs) { + HashSet climit = new HashSet(); + // 把重复的权限去掉 + for (String c : strs) { + climit.add(c); + } + return climit; + } + + /** + * 去掉多个字符串数组里的重复项 + */ + public static String distinctStr(List strs) { + StringBuffer buffer = new StringBuffer(""); + for (String string : strs) { + String[] strings = string.split(","); + for (int i = 0; i < strings.length; i++) { + if (buffer.substring(0).indexOf(strings[i] + ",") == -1) { + buffer.append(strings[i] + ","); + } + } + } + return buffer.substring(0); + } + + /** + * [括号后面的不要 + */ + public static String indexMiddle(String str) { + int indexOf = str.indexOf("["); + if (indexOf != -1) { + str = str.substring(0, indexOf); + } + return str; + } + + /** + * 生成一个唯一标识 + */ + public static String getUUID() { + return UUID.randomUUID().toString().replace("-", ""); + } + + public static String getRandom() { + String result = ""; + for (int i = 0; i < 6; i++) { + result = result + (int) (Math.random() * 10); + } + return result; + } + + /** + * 处理填充数据 + */ + public static String fixData(Object data, String replaceStr) { + return data == null ? replaceStr : data.toString(); + } + + /** + * 处理填充数据 + */ + public static String fixData(Object data) { + return data == null ? "" : data.toString(); + } + + public static String fixDataReport(Object data) { + return data == null ? "—————" : data.toString(); + } + + /** + * 判定是否为null,如果是则返回 '' + */ + public static String isNull(String str) { + if (str == null) { + return ""; + } + return str; + } + + public static Integer getJudg(String compare, String value, String upper, String lower) throws Exception { + Integer judg = 1; + if ("0".equals(compare)) { + if (Float.parseFloat(value) > Float.parseFloat(upper)) { + judg = 0; + } + } + else if ("1".equals(compare)) { + if (Float.parseFloat(value) < Float.parseFloat(lower)) { + judg = 0; + } + } + else { + if (Float.parseFloat(value) > Float.parseFloat(upper) || Float.parseFloat(value) < Float.parseFloat(lower)) { + judg = 0; + } + } + return judg; + } + + public static String LimitValue(String upper, String lower) { + String limit = "--"; + if (StrKit.notBlank(upper, lower)) { + limit = lower + "~" + upper; + } + else if (StrKit.notBlank(lower)) { + limit = "≥" + lower; + } + else if (StrKit.notBlank(upper)) { + limit = "≤" + upper; + } + return limit; + } + + public static String isBlank(String name, String id) { + if (StrKit.isBlank(name)) { + id = null; + } + return id; + } + + public static Integer getMonth() { + Calendar cal = Calendar.getInstance(); + return cal.get(Calendar.MONTH) + 1; + } + + public static Integer getYear() { + Calendar cal = Calendar.getInstance(); + return cal.get(Calendar.YEAR); + } + + public static String getYearMonth() { + String year = StringUtil.getYear().toString(); + int month = StringUtil.getMonth(); + String tyear = year.substring(2, 4); + if (month < 10) { + return tyear + "0" + month; + } + else { + return tyear + month; + } + } + + public static String getStrMonth() { + int month = StringUtil.getMonth(); + if (month < 10) { + return "0" + month; + } + else { + return "" + month; + } + } + + public static String GetMainJsonData(String json) { + return json.substring(1, json.length() - 1); + } + + public static String sortStr(String[] keys) { + Arrays.sort(keys); + return combineString(keys); + } + + public static String append(String value, String appendString, String separator) { + if (separator == null || "".equals(separator)) { + separator = ","; + } + + if (value == null || "".equals(value)) { + value = ""; + } + + if (appendString == null || "".equals(appendString)) { + appendString = ""; + } + + if ("".equals(value)) { + value = appendString; + } + else if (!"".equals(appendString)) { + value = value + separator + appendString; + } + + return value; + } + + public static String getRandomString(int length) { //length表示生成字符串的长度 + String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < length; i++) { + int number = random.nextInt(base.length()); + sb.append(base.charAt(number)); + } + return sb.toString(); + } + + public static int parserInt(String intStr, int defaultInf) { + int parseredInt = defaultInf; + intStr = StringUtils.defaultIfEmpty(intStr, ""); + intStr = intStr.trim(); + if (StringUtils.isNumeric(intStr) && StringUtils.isNotBlank(intStr)) { + parseredInt = (new Integer(intStr)).intValue(); + } + + + return parseredInt; + } + + /** + * 生成流水号 + * + * @param lastNum 上一个编号 + * @param length 长度 + * @return + */ + public static String serialNumber(String lastNum, int length) { + String id = null; + if (StrKit.isBlank(lastNum)) { + lastNum = "0"; + } + StringBuilder sb = new StringBuilder(); + length = (length > 0) ? length : 4; + for (int i = 0; i < length; i++) { + sb.append("0"); + } + DecimalFormat df = new DecimalFormat(sb.toString()); + id = df.format(1 + Integer.parseInt(lastNum)); + return id; + } + + /** + * 日期流水号 + * + * @param lastNum 上一个编号 + * @param length 流水长度 + * @return + */ + public static String serialNumberWidthDate(String lastNum, int length) { + String sn = null; + + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); + Date date = new Date(); + if (StrKit.isBlank(lastNum)) { + StringBuilder sb = new StringBuilder(); + length = (length > 0) ? length : 4; + for (int i = 0; i < length; i++) { + sb.append("0"); + } + lastNum = sdf.format(date) + sb.toString(); + } + sn = sdf.format(date) + serialNumber(lastNum.substring(8, 8 + length), length); + return sn; + } + + public static String getRandom(int length) { + String result = ""; + for (int i = 0; i < length; i++) { + result = result + (int) (Math.random() * 10); + } + return result; + } +} Index: forgon-tools/src/main/java/com/forgon/tools/util/ExtJsUtil.java =================================================================== diff -u -r16585 -r17182 --- forgon-tools/src/main/java/com/forgon/tools/util/ExtJsUtil.java (.../ExtJsUtil.java) (revision 16585) +++ forgon-tools/src/main/java/com/forgon/tools/util/ExtJsUtil.java (.../ExtJsUtil.java) (revision 17182) @@ -1,6 +1,9 @@ package com.forgon.tools.util; -import org.apache.commons.collections.map.HashedMap; +import com.forgon.entity.ExtJsEntity; +import com.forgon.entity.PageEntity; +import org.apache.commons.collections4.map.HashedMap; +import org.apache.commons.lang3.StringUtils; import org.springframework.util.CollectionUtils; import java.util.ArrayList; @@ -14,6 +17,9 @@ */ public class ExtJsUtil { + public static final int GRID_PAGE_START = 0; + public static final int GRID_PAGE_SIZE = 30; + /** * 设置查询条件参数 * @@ -40,7 +46,7 @@ * @return */ public static Map createSelect(String valueField, String displayField) { - Map map = new HashedMap(); + Map map = new HashedMap<>(); map.put("valueField", valueField); map.put("displayField", displayField); return map; @@ -56,4 +62,137 @@ list.add(createSelect("all", "全部")); return list; } + + /** + * 构造根据where语句构造ExtJsEntity(此方法不能排序且默认查询全部属性) + *

适用于hql语句

+ * + * @param where where语句(从from开始) + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildHQLEntityDefault(String where) { + return buildHQLEntity(null, where, null); + } + + /** + * 构造根据where,order语句构造ExtJsEntity(此方法支持排序) + *

适用于hql语句

+ * + * @param where where语句(从from开始) + * @param order order语句 + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildHQLEntityDefault(String where, String order) { + return buildHQLEntity(null, where, order); + } + + /** + * 构造根据select,where语句构造ExtJsEntity(此方法不能排序) + *

适用于hql语句

+ * + * @param select select语句 + * @param where where语句(从from开始) + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildHQLEntity(String select, String where) { + return buildHQLEntity(select, where, null); + } + + /** + * 构造根据select,where,order语句构造ExtJsEntity + *

适用于hql语句

+ * + * @param select select语句 + * @param where where语句(从from开始) + * @param order order语句 + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildHQLEntity(String select, String where, String order) { + if (StringUtils.isBlank(select)) { + select = "select po "; + } + return buildEntity(select, where, order); + } + + /** + * 根据where语句构造ExtJsEntity(此方法默认全部查询且不能排序) + *

适用于原生sql语句

+ * + * @param where where语句(从from开始) + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildSQLEntityDefault(String where) { + return buildSQLEntity(null, where, null); + } + + /** + * 根据where,order语句构造ExtJsEntity(此方法默认全部查询) + *

适用于原生sql语句

+ * + * @param where where语句(从from开始) + * @param order order语句 + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildSQLEntityDefault(String where, String order) { + return buildSQLEntity(null, where, order); + } + + /** + * 构造根据select,where语句构造ExtJsEntity(此方法不排序) + *

适用于原生sql语句

+ * + * @param select select语句 + * @param where where语句(从from开始) + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildSQLEntity(String select, String where) { + return buildSQLEntity(select, where, null); + } + + /** + * 构造根据select,where,order语句构造ExtJsEntity + *

适用于原生sql语句

+ * + * @param select select语句 + * @param where where语句(从from开始) + * @param order order语句 + * @return {@link ExtJsEntity} + */ + public static ExtJsEntity buildSQLEntity(String select, String where, String order) { + if (StringUtils.isBlank(select)) { + select = "select * "; + } + return buildEntity(select, where, order); + } + + + /** + * 根据select,where,order条件构造对应的extJs实体类 + * + * @param select select的语句,不包括from + * @param where 从from之后的SQL,包括from + * @param order 排序sql + * @return {@link ExtJsEntity} + */ + private static ExtJsEntity buildEntity(String select, String where, String order) { + ExtJsEntity extJsEntity = new ExtJsEntity(select, where, order); + setPage(extJsEntity); + return extJsEntity; + } + + /** + * 设置ExtJsEntity的分页属性 + *

此方法只支持struts2,暂时不支持springmvc

+ * + * @param extJsEntity {@link ExtJsEntity} + */ + private static void setPage(ExtJsEntity extJsEntity) { + PageEntity pagePara = PageUtil.getPagePara(); + int start = pagePara.getStart(); + int limit = pagePara.getLimit(); + start = start < 0 ? ExtJsUtil.GRID_PAGE_START : start; + limit = limit < 1 ? ExtJsUtil.GRID_PAGE_SIZE : limit; + extJsEntity.setStart(start); + extJsEntity.setLimit(limit); + } }