package com.jtgh.yjpt.controller; import java.io.IOException; import java.math.BigDecimal; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder.In; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRDataSource; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.rowset.SqlRowSet; import com.jtgh.yjpt.common.BusinessContext; import com.jtgh.yjpt.common.Constants; import com.jtgh.yjpt.common.GlobalData; import com.jtgh.yjpt.common.PredicateModel; import com.jtgh.yjpt.common.PredicateModel.JoinType; import com.jtgh.yjpt.common.PredicateModel.Operator; import com.jtgh.yjpt.common.ReportExportHelper; import com.jtgh.yjpt.common.SysException; import com.jtgh.yjpt.common.Utils; import com.jtgh.yjpt.entity.BaseEntity; import com.jtgh.yjpt.entity.auth.UserEntity; /** * 控制器层共通基类
*

* 所有控制器层(@controller)继承此共通基类,实现控制器层共通处理。 *

* 关于Flex调用: *

  • 需要被Flex远程调用的控制器使用@RemotingDestination注解 *
  • 需要被Flex远程调用的方法使用@RemotingInclude(可以不写,默认)注解 *
  • 不需要被Flex远程调用的方法使用@RemotingExclude注解 * * @author 袁晓冬 * */ @SuppressWarnings("unchecked") public abstract class BaseController { /** 日志记录 */ protected Logger logger = Logger.getLogger(getClass()); @Autowired protected JdbcTemplate jdbcTemplate; /** * jasper打印文件 */ public void download(Map parameters, String fileName, String reportPath, String type, JRDataSource dataSource, HttpServletResponse response, HttpServletRequest request) { try { response.setCharacterEncoding("utf-8"); if (ReportExportHelper.REPORT_EXPORT_TYPE_HTML.equals(type)) { response.setContentType("text/html"); } else { response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); } if ("FF".equals(getBrowser(request))) { // 针对火狐浏览器处理方式不一样了 //fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } else { fileName = URLEncoder.encode(fileName, "UTF-8"); } ReportExportHelper.exportFromIreport(reportPath, response, parameters, dataSource, fileName, type); } catch (IOException e) { e.printStackTrace(); } } /** * 服务器端判断客户端浏览器类型 * * @param request * @return */ private String getBrowser(HttpServletRequest request) { String UserAgent = request.getHeader("USER-AGENT").toLowerCase(); if (UserAgent != null) { if (UserAgent.indexOf("msie") >= 0) return "IE"; if (UserAgent.indexOf("firefox") >= 0) return "FF"; if (UserAgent.indexOf("safari") >= 0) return "SF"; } return null; } /** * 资源文件 */ public ResourceBundle resource = ResourceBundle.getBundle(Locale .getDefault().toString() + "/select", Locale.getDefault(), this .getClass().getClassLoader()); /** * 时间格式化 */ public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /** * 时间格式化2 */ public SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 时间格式化3 */ public SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); /** * 时间格式化4 */ public SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy/MM/dd"); /** * 时间格式化5 */ public SimpleDateFormat sdf5 = new SimpleDateFormat("HH:mm:ss"); /** * 日历 */ public Calendar now = Calendar.getInstance(); public Calendar yxq = Calendar.getInstance(); /** * 默认排序 */ public Sort sort = new Sort(Direction.DESC, "id"); // //********************日志记录******************** // private HashMap needLogMethods = new HashMap(); // // /** 登录 */ // public static final String LOG_METHOD_TYPE_LOGIN = "0"; // /** 新增 */ // public static final String LOG_METHOD_TYPE_ADD = "1"; // /** 删除 */ // public static final String LOG_METHOD_TYPE_DELETE = "2"; // /** 编辑 */ // public static final String LOG_METHOD_TYPE_EDIT = "3"; // /** 查看 */ // public static final String LOG_METHOD_TYPE_QUERY = "4"; // /** 上传 */ // public static final String LOG_METHOD_TYPE_UPLOAD = "5"; // /** 下载 */ // public static final String LOG_METHOD_TYPE_DOWNLOAD = "6"; // /** 打印 */ // public static final String LOG_METHOD_TYPE_PRINT = "7"; // /** 权力阳光办件查看 */ // public static final String LOG_METHOD_TYPE_QLYG_VIEW = "9"; // // /** // * 根据方法设置是否记录日志
    // * 返回false时不记录日志,登录方法及无需登录即可访问的方法必须不记录日志,因为无法取得登录用户信息
    // * 默认false,不记录日志 // * // * @param method // * @return // */ // public boolean needLog(String method) { // if (needLogMethods != null && needLogMethods.get(method) != null) { // return true; // } // return false; // } // // public String getMethodType(String method) { // if (needLogMethods != null && needLogMethods.get(method) != null) { // return needLogMethods.get(method); // } // return null; // } // // /** // * 添加需要记录日志的方法名 // * // * @param method // */ // protected void addNeedLogMethods(String method, String type) { // if (needLogMethods == null) // needLogMethods = new HashMap(); // if (needLogMethods.get(method) == null) // needLogMethods.put(method, type); // } // // /** // * 删除不需要记录日志的方法名 // * // * @param method // */ // protected void removeNeedLogMethods(String method) { // if (needLogMethods != null && needLogMethods.get(method) != null) // needLogMethods.remove(method); // } // // /** // * 菜单对应ID // */ // protected String menuCode = ""; // /** // * 经营人菜单对应ID // */ // protected String menuCode_jyr = ""; // // public String getMenuCode() { // if (Utils.getSession().getAttribute(GlobalData.USER_SESSION_KEY) != null) // { // UserEntity user = (UserEntity) Utils.getSession().getAttribute( // GlobalData.USER_SESSION_KEY); // if (Constants.NO.equals(user.getSfjyr())) { // return menuCode; // } else if (!StringUtils.isEmpty(menuCode_jyr)) { // return menuCode_jyr; // } // } // return menuCode; // } /** * 创建BusinessContext * * @return */ protected BusinessContext createBusinessContext() { BusinessContext bc = new BusinessContext(); bc.setAttribute("success", true); return bc; } /** * 返回提示信息给前台
    *

    * 提示信息:message *

    * 信息参数:args *

    * 是否成功:success * * @param messageId * 国际化ID * @param susscess * 是否成功 * @param args * 信息参数ID * @return */ protected BusinessContext createBusinessContext(String messageId, boolean susscess, String... args) { BusinessContext bc = new BusinessContext(); bc.setAttribute("message", messageId); bc.setAttribute("success", susscess); bc.setAttribute("args", args); return bc; } /** * 返回提示信息给前台
    *

    * 提示信息:message *

    * 包名:bundle *

    * 信息参数:args *

    * 是否成功:success * * @param messageId * @param bundle * @param susscess * @param args * @return */ protected BusinessContext createBusinessContext(String messageId, String bundle, boolean susscess, String... args) { BusinessContext bc = new BusinessContext(); bc.setAttribute("message", messageId); bc.setAttribute("bundle", bundle); bc.setAttribute("success", susscess); bc.setAttribute("args", args); return bc; } /** * 根据Entity创建BusinessContext
    *

    * entity对象:record *

    * 是否成功:success * * @param entity * @return */ protected > BusinessContext createBusinessContext( T entity) { BusinessContext bc = new BusinessContext(); bc.setAttribute("record", entity); bc.setAttribute("success", true); return bc; } /** * 根据分页对象创建BusinessContext
    * 用于分页查询
    *

    * 对象列表:records *

    * 总条数:totalCount *

    * 是否成功:success * * @param page * @return */ protected BusinessContext createBusinessContext(Page page) { BusinessContext bc = new BusinessContext(); bc.setAttribute("records", page.getContent()); bc.setAttribute("totalCount", page.getTotalElements()); bc.setAttribute("success", true); return bc; } protected BusinessContext createEmptyListBusinessContext() { BusinessContext bc = new BusinessContext(); bc.setAttribute("records", Collections.EMPTY_LIST); bc.setAttribute("totalCount", 0); bc.setAttribute("success", true); return bc; } protected BusinessContext creaBusinessContext(List list) { BusinessContext bc = new BusinessContext(); bc.setAttribute("records", list); bc.setAttribute("success", true); return bc; } /** * 判断PredicateModel的value是否为空,只添加非空数据 * * @param filterList * @param model */ protected void addNotEmptyModel(List filterList, PredicateModel model) { Object value = model.getValue(); if (null == value || "".equals(value)) { return; } if (null == filterList) { throw new SysException( "List filterList can't be null!"); } filterList.add(model); } /** * 判断PredicateModel的value是否为空,只添加非空数据 * * @param filterList * @param model */ protected void addNotEmptyModel(List filterList, String fieldName, Object value, Operator operator) { if (null == value || "".equals(value) || "null".equals(value)) { return; } if (null == filterList) { throw new SysException( "List filterList can't be null!"); } filterList.add(new PredicateModel(fieldName, value, operator)); } /** * List参数判断 * * @param filterList * @param fieldName * @param values * @param index * @param operator */ protected void addNotEmptyModel(List filterList, String fieldName, List values, int index, Operator operator) { if (values.size() >= index + 1) { addNotEmptyModel(filterList, fieldName, values.get(index), operator); } } /** * 生成唯一编号,供权力阳光使用 * * 部门编码(10位)+0000000001(流水号10位) * * @param seqName * @return */ public String generateQlygNo(String deptCode, Long entityId) { StringBuffer no = new StringBuffer(); // if (null != seqName) { no.append(GlobalData.DEPT_CODE); // String sql = "SELECT " + seqName + ".nextval FROM dual"; // try { // SqlRowSet rs = jdbcTemplate.queryForRowSet(sql); // if (rs.next()) { String seq = entityId.toString(); if (null != seq && seq.length() > 0) { if (seq.length() < 10) { for (int i = seq.length(); i < 10; i++) { no.append("0"); } no.append(seq); } else { no.append(seq.substring(seq.length() - 10, seq.length())); } } // } // } catch (Exception e) { // e.printStackTrace(); // } // } return no.toString(); } public Long generateEntityId(String seqName) { if (seqName != null) { String sql = "SELECT " + seqName + ".nextval FROM dual"; try { SqlRowSet rs = jdbcTemplate.queryForRowSet(sql); if (rs.next()) { String seq = rs.getString(1); if (null != seq && seq.length() > 0) { return Long.parseLong(seq); } } } catch (Exception e) { e.printStackTrace(); } } return 0l; } public String getCurrentRoleCode(Long functionId) { UserEntity user = Utils.getCurrentUser(); String sql = "SELECT DISTINCT ROLE.code code FROM T_YJPT_ROLE ROLE " + "JOIN T_YJPT_ROLE_USER RU ON ROLE.ID = RU.ROLEID " + "WHERE RU.USERID=? AND(" + "EXISTS(SELECT 1 FROM T_YJPT_ROLE_MENU RM " + "WHERE RM.ROLE = ROLE.ID AND RM.MENU=?) OR " + "EXISTS(SELECT 1 FROM T_YJPT_ROLE_BUTTON RB " + "WHERE RB.ROLE = ROLE.ID AND RB.BUTTON=?)) order by role.code asc"; String role = null; try { SqlRowSet rs = jdbcTemplate.queryForRowSet(sql, user.getId(), functionId, functionId); if (rs.next()) { role = rs.getString("code"); } } catch (Exception e) { e.printStackTrace(); } return role; } // /** // * 普通经营人,设置当前用户过滤条件 // * // * @param filterList // */ // protected void addRecordCodeFilter(List filterList) { // List orList = new ArrayList(); // // 经营人可以看自己及自己下级经营人的数据,非行政人员的建设方和评价机构只能看自己所在企业的数据 // if (Utils.getCurrentUser().getJyrjcxx() != null // && Utils.getSession().getAttribute(GlobalData.USER_SESSION_JYR) != null // && ((List) Utils.getSession().getAttribute( // GlobalData.USER_SESSION_JYR)).size() > 0) { // orList.add(new PredicateModel("addUser.jyrjcxx.id", Utils // .getSession().getAttribute(GlobalData.USER_SESSION_JYR), // Operator.IN)); // } // if (Utils.getCurrentUser().getJsdw() != null) { // orList.add(new PredicateModel("addUser.jsdw.id", Utils // .getCurrentUser().getJsdw().getId(), Operator.EQ)); // } // if (Utils.getCurrentUser().getAqpjjg() != null) { // orList.add(new PredicateModel("addUser.aqpjjg.id", Utils // .getCurrentUser().getAqpjjg().getId(), Operator.EQ)); // } // if (orList != null && orList.size() > 0) // filterList.add(new PredicateModel(JoinType.OR, orList)); // } /** * 根据角色设置过滤条件 * * @param functionId * @param filterList */ protected void addRecordCodeFilter(Long functionId, List filterList) { addRecordCodeFilter(functionId, filterList, null); } /** * 根据角色设置过滤条件 * * @param functionId * @param filterList * @param user * 根据某个字段过滤经营人、建设单位、评价机构的数据,默认根据addUser */ protected void addRecordCodeFilter(Long functionId, List filterList, String user) { addRecordCodeFilter(functionId, filterList, user, null); } /** * 根据角色设置过滤条件 * * @param functionId * @param filterList * @param user * 根据某个字段过滤经营人、建设单位、评价机构的数据,默认根据addUser */ protected void addRecordCodeFilter(Long functionId, List filterList, String user, String jyr) { if (user == null) user = "addUser."; if (Utils.getCurrentUser() != null && !GlobalData.adminID.equals(Utils.getCurrentUser().getId())) { List orList = new ArrayList(); String role = getCurrentRoleCode(functionId); // 行政人员可以查找所有比自己低级的数据,以及经营人、建设单位、评价机构的数据 if (Constants.NO.equals(Utils.getCurrentUser().getSfjyr())) { // 行政人员按照所在地过滤 if (Utils.getCurrentUser().getSzd() != null) filterList.add(new PredicateModel("szd.id", Utils .getCurrentUser().getSzd().getByzd2(), Operator.LIKE_R)); role = Utils.getParentRole(role); orList.add(new PredicateModel("recordCode", role, Operator.LIKE_R)); orList.add(new PredicateModel("recordCode", Constants.RECORD_CODE_JYR, Operator.LIKE_R)); orList.add(new PredicateModel("recordCode", Constants.RECORD_CODE_PJJG, Operator.LIKE_R)); orList.add(new PredicateModel("recordCode", Constants.RECORD_CODE_JSDW, Operator.LIKE_R)); orList.add(new PredicateModel("tbdyid", null, Operator.NNL)); } else { // 经营人可以看自己及自己下级经营人的数据,非行政人员的建设方和评价机构只能看自己所在企业的数据 if (Utils.getCurrentUser().getJyrjcxx() != null && Utils.getSession().getAttribute( GlobalData.USER_SESSION_JYR) != null) { if (!StringUtils.isEmpty(jyr)) { orList.add(new PredicateModel(jyr + ".id", Utils .getSession().getAttribute( GlobalData.USER_SESSION_JYR), Operator.IN)); } else { orList.add(new PredicateModel(user + "jyrjcxx.id", Utils.getSession().getAttribute( GlobalData.USER_SESSION_JYR), Operator.IN)); } } if (Utils.getCurrentUser().getJsdw() != null) { orList.add(new PredicateModel(user + "jsdw.id", Utils .getCurrentUser().getJsdw().getId(), Operator.EQ)); } if (Utils.getCurrentUser().getAqpjjg() != null) { orList.add(new PredicateModel(user + "aqpjjg.id", Utils .getCurrentUser().getAqpjjg().getId(), Operator.EQ)); } } if (orList != null && orList.size() > 0) { filterList.add(new PredicateModel(JoinType.OR, orList)); } } } /** * 根据角色设置过滤条件(流程中使用) * * @param functionId * @param filterList */ protected void addRecordCodeFilter(Long functionId, CriteriaBuilder cb, Root root, List filterList) { addRecordCodeFilter(functionId, cb, root, filterList, null, null); } /** * 根据角色设置过滤条件(流程中使用) * * @param functionId * @param filterList */ protected void addRecordCodeFilter(Long functionId, CriteriaBuilder cb, Root root, List filterList, String user, String jyr) { if (user == null) user = "addUser"; if (Utils.getCurrentUser() != null && !GlobalData.adminID.equals(Utils.getCurrentUser().getId())) {// 不是超级用户 List orList = new ArrayList(); String role = getCurrentRoleCode(functionId); // 行政人员可以查找所有比自己低级的数据,以及经营人、建设单位、评价机构的数据 if (Constants.NO.equals(Utils.getCurrentUser().getSfjyr())) {// 不是经营人 // 行政人员按照所在地过滤 if (Utils.getCurrentUser().getSzd() != null) filterList.add(cb.like( root.get("szd").get("id").as(String.class), Utils.getCurrentUser().getSzd().getByzd2() + "%")); if (role != null) role = Utils.getParentRole(role); orList.add(cb.like(root.get("recordCode").as(String.class), role + "%")); orList.add(cb.like(root.get("recordCode").as(String.class), Constants.RECORD_CODE_JYR + "%")); orList.add(cb.like(root.get("recordCode").as(String.class), Constants.RECORD_CODE_PJJG + "%")); orList.add(cb.like(root.get("recordCode").as(String.class), Constants.RECORD_CODE_JSDW + "%")); orList.add(cb.isNotNull(root.get("tbdyid").as(String.class))); } else { // 经营人可以看自己及自己下级经营人的数据,非行政人员的建设方和评价机构只能看自己所在企业的数据 if (Utils.getCurrentUser().getJyrjcxx() != null) { In in = null; if (StringUtils.isEmpty(jyr)) { in = cb.in(root.get(user).get("jyrjcxx").get("id") .as(Long.class)); } else { in = cb.in(root.get(jyr).get("id").as(Long.class)); } if (Utils.getSession().getAttribute( GlobalData.USER_SESSION_JYR) != null) { List jyrIdList = (List) Utils .getSession().getAttribute( GlobalData.USER_SESSION_JYR); if (jyrIdList != null) for (BigDecimal jyrId : jyrIdList) { in.value(jyrId.longValue()); } } orList.add(in); } if (Utils.getCurrentUser().getJsdw() != null) { orList.add(cb.equal(root.get(user).get("jsdw"), Utils .getCurrentUser().getJsdw())); } if (Utils.getCurrentUser().getAqpjjg() != null) { orList.add(cb.equal(root.get(user).get("aqpjjg"), Utils .getCurrentUser().getAqpjjg())); } } if (orList != null && orList.size() > 0) { Predicate[] p = new Predicate[orList.size()]; filterList.add(cb.or(orList.toArray(p))); } } } protected void addEmptyModel(List filterList, String fieldName, Operator nl) { filterList.add(new PredicateModel(fieldName, null, Operator.NL)); } }