/*
  * (non-Javadoc)
  *
  * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
  */
 public void contextDestroyed(ServletContextEvent event) {
   // 记录系统日志
   LogsSystem systemLog = new LogsSystem();
   systemLog.setOperator(LogsSystem.BACKGROUND);
   systemLog.setLogDate(new Date());
   systemLog.setEventType("系统被关闭");
   systemLog.setContent(
       "系统被关闭于[" + DateUtils.getToday(DateUtils.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS) + "]!");
   systemLog.setPriority(LogsApplication.IMPORTANT);
   this.systemLogManager.saveOrUpdateSystemLog(systemLog);
 }
 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
  */
 public void contextInitialized(ServletContextEvent servletContextEvent) {
   initGlobal(servletContextEvent);
   initRoleRightsMap();
   initOther(servletContextEvent);
   // 记录系统日志
   LogsSystem systemLog = new LogsSystem();
   systemLog.setOperator(LogsSystem.BACKGROUND);
   systemLog.setLogDate(new Date());
   systemLog.setEventType("系统已启动");
   systemLog.setContent(
       "系统已启动于[" + DateUtils.getToday(DateUtils.DATE_FORMAT_YYYY_MM_DD_HH_MM_SS) + "]!");
   systemLog.setPriority(LogsApplication.IMPORTANT);
   this.systemLogManager.saveOrUpdateSystemLog(systemLog);
 }
  /*
   *
   * (non-Javadoc)
   *
   * @see com.opensymphony.xwork.ActionSupport#execute()
   */
  public String execute() {
    List loginLogList = null;
    LogsLoginSearchCondition logsSearch = null;
    String searchOperator = "";
    String searchEventType = "";
    String searchStartLogDate = "";
    String searchEndLogDate = "";
    try {

      /** 验证请求的属性是否完整,防止知道URL非法请求----开始 */
      String page = this.getRequest().getParameter("page");
      String operator = this.getRequest().getParameter("operator");
      String eventType = this.getRequest().getParameter("eventType");
      String startLogDate = this.getRequest().getParameter("startLogDate");
      String endLogDate = this.getRequest().getParameter("endLogDate");
      String isNewSearch = this.getRequest().getParameter("isNew");
      if (page != null) {
        if (page.trim().length() > 0) {
          try {
            NumberUtils.parseNumber(page, Integer.class);
          } catch (Exception e) {
            /** 记录日志 */
            LogsApplication appLog = new LogsApplication();
            appLog.setOperator(this.getUserSessionInfo().getName());
            appLog.setLogDate(new Date());
            appLog.setEventType("搜索登陆日志异常");
            appLog.setContent(
                "操作员["
                    + this.getUserSessionInfo().getName()
                    + "]"
                    + "搜索登陆日志出现异常,异常信息:["
                    + e.getMessage()
                    + "]!");
            appLog.setPriority(LogsApplication.EXCEPTION);
            appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
            this.appLogManager.saveOrUpdateAppLog(appLog);

            /** 输出错误信息 */
            logger.error(e.getMessage());
            e.printStackTrace();
          }
        }
      }

      /** 构造搜索对象 */
      logsSearch = new LogsLoginSearchCondition();
      if (StringUtils.hasText(operator)) {
        searchOperator = operator;
        logsSearch.setOperator(StringUtils.trimWhitespace(operator));
      }
      if (StringUtils.hasText(eventType)) {
        searchEventType = eventType;
        logsSearch.setEventType(StringUtils.trimWhitespace(eventType));
      }
      if (StringUtils.hasText(startLogDate)) {
        logsSearch.setStartLogDate(java.sql.Date.valueOf(startLogDate));
        searchStartLogDate =
            DateUtils.formatDateToString(
                logsSearch.getStartLogDate(), DateUtils.DATE_FORMAT_YYYY_MM_DD);
      }
      if (StringUtils.hasText(endLogDate)) {
        logsSearch.setEndLogDate(java.sql.Date.valueOf(endLogDate));
        searchEndLogDate =
            DateUtils.formatDateToString(
                logsSearch.getEndLogDate(), DateUtils.DATE_FORMAT_YYYY_MM_DD);
      }

      /** 验证请求的属性是否完整----结束 */

      /** 构造Page对象 */
      Page pageInfo = this.getLoginLogManager().searchLoginLogs(logsSearch, this.getUserPageSize());

      /** 设置取的页数 */
      if (page != null) {
        if (page.trim().length() > 0) {
          try {
            pageInfo.setPage(
                (Integer) NumberUtils.parseNumber(StringUtils.trimWhitespace(page), Integer.class));
          } catch (Exception e) {
            /** 记录日志 */
            LogsApplication appLog = new LogsApplication();
            appLog.setOperator(this.getUserSessionInfo().getName());
            appLog.setLogDate(new Date());
            appLog.setEventType("搜索登陆日志异常");
            appLog.setContent(
                "操作员["
                    + this.getUserSessionInfo().getName()
                    + "]"
                    + "搜索登陆日志出现异常,异常信息:["
                    + e.getMessage()
                    + "]!");
            appLog.setPriority(LogsApplication.EXCEPTION);
            appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
            this.appLogManager.saveOrUpdateAppLog(appLog);

            /** 输出错误信息 */
            logger.error(e.getMessage());
            e.printStackTrace();
          }
        }
      }

      /** 如果是新搜索 */
      if (isNewSearch != null) {
        if (isNewSearch.trim().length() > 0) {
          if (isNewSearch.equalsIgnoreCase("true")) {
            pageInfo.setPage(1);
          }
        }
      }
      /** 获得分页集合 */
      loginLogList = this.getLoginLogManager().searchLoginLogs(logsSearch, pageInfo);

      /** 设置到属性中去 */
      this.getRequest().setAttribute("pageInfo", pageInfo);
      this.getRequest().setAttribute("loginLogList", loginLogList);
      this.getRequest().setAttribute("searchContent", logsSearch);
      this.getRequest().setAttribute("operator", searchOperator);
      this.getRequest().setAttribute("eventType", searchEventType);
      this.getRequest().setAttribute("startLogDate", searchStartLogDate);
      this.getRequest().setAttribute("endLogDate", searchEndLogDate);

      this.getRequest()
          .setAttribute(
              "actionName",
              "searchLoginLogs."
                  + Global.ACTION_EXT
                  + "?operator="
                  + StringUtils.trimWhitespace(searchOperator)
                  + "&eventType="
                  + StringUtils.trimWhitespace(searchEventType)
                  + "&startLogDate="
                  + StringUtils.trimWhitespace(searchStartLogDate)
                  + "&endLogDate="
                  + StringUtils.trimWhitespace(searchEndLogDate));

      /** 记录日志 */
      LogsApplication appLog = new LogsApplication();
      appLog.setOperator(this.getUserSessionInfo().getName());
      appLog.setLogDate(new Date());
      appLog.setEventType("搜索登陆日志");
      appLog.setContent("操作员[" + this.getUserSessionInfo().getName() + "]" + "搜索登陆日志!");
      appLog.setPriority(LogsApplication.NORMAL);
      appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
      this.appLogManager.saveOrUpdateAppLog(appLog);

      return SUCCESS;
    } catch (Exception e) {

      /** 记录日志 */
      LogsApplication appLog = new LogsApplication();
      appLog.setOperator(this.getUserSessionInfo().getName());
      appLog.setLogDate(new Date());
      appLog.setEventType("搜索登陆日志异常");
      appLog.setContent(
          "操作员["
              + this.getUserSessionInfo().getName()
              + "]"
              + "搜索登陆日志出现异常,异常信息:["
              + e.getMessage()
              + "]!");
      appLog.setPriority(LogsApplication.EXCEPTION);
      appLog.setIpAddress(this.getUserSessionInfo().getIpAddress());
      this.appLogManager.saveOrUpdateAppLog(appLog);

      /** 输出错误信息 */
      logger.error(e.getMessage());
      e.printStackTrace();

      return ERROR;
    }
  }