/**
   * Method execute
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return ActionForward
   */
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws javax.servlet.ServletException, java.io.IOException {

    HttpSession session = request.getSession();
    DbUserSession user = (DbUserSession) session.getAttribute(SessionValueKeys.DB_USER);
    ReportForm reportForm = (ReportForm) form;
    ActionMessages errors = new ActionMessages();

    // ArrayList <CheckListBean> checkURL = new ArrayList <CheckListBean>();
    // session.setAttribute("checkURL", checkURL);

    String requestType = reportForm.getRequestType();
    if (requestType == null) {
      requestType = "";
    }
    String pageName = "";
    reportForm.setReportURL(pageName);
    setReport(reportForm, user, session, errors, request);

    if (reportForm.getListType() == null) {
      reportForm.setListType(request.getParameter("all"));
    } else {
      reportForm.setListType(reportForm.getListType());
    }

    if (requestType.compareToIgnoreCase("changeLocale") == 0) {
      changeLocale(reportForm, user, session, errors, request);
      // changeCategory(reportForm, user, session, errors, request);
      return (mapping.findForward("localeChange"));
    } else if (requestType.compareToIgnoreCase("changeLocation") == 0) {
      // changeCategory(reportForm, user, session, errors, request);
      return (mapping.findForward("localeChange"));
    } else if (requestType.compareToIgnoreCase("changeCategory") == 0) {
      // changeCategory(reportForm, user, session, errors, request);
      return (mapping.findForward("localeChange"));
    } else {

      // ArrayList <OptionsList> reports = new ArrayList<OptionsList>();
      // reports.add( new OptionsList( "0" ,"--Select--"));
      // request.setAttribute("reports",reports);
    }

    if (requestType.compareToIgnoreCase("print") == 0) {

      //			Timer smsTimer = new Timer("SMSThread", true);
      //			CompanyManagerBean cmBean = new CompanyManagerBean();
      //			CompanyDTO company = cmBean.getCompany(user.getCompanyID());
      //			smsTimer.schedule(new SMSGenerator(company,1), 0l);

      ReportSchedulingDTO reportScheduling = new ReportSchedulingDTO();
      setReportScheduling(reportForm, reportScheduling, errors, user);
      reportScheduling.setReportType(ReportSchedulingDTO.REPORT_TYPE_REALTIME);
      reportScheduling.setStatus("Q");
      ReportSchedulingDAO reportScheDAo = new ReportSchedulingDAO(user);
      try {
        reportScheDAo.addReportScheduling(reportScheduling);
      } catch (Exception e) {

      }

      String crystalFlag =
          UtilSingleton.getInstance()
              .getProperty(user.getConfigID(), "CrystalServer.useReportingService");

      if (crystalFlag != null && "true".equals(crystalFlag)) {
        CompanyManagerBean cmBean = new CompanyManagerBean();
        CompanyDTO company = cmBean.getCompany(user.getCompanyID());
        runNow(reportScheduling, company);
      }

      reportForm.setFromDate(FormatDate.getCurrentDateFormatedMMDDYYYY());
      reportForm.setToDate(FormatDate.getCurrentDateFormatedMMDDYYYY());
      reportForm.setUserLocaleId("0");
      reportForm.setUserLocationId("0");
      reportForm.setCategory("0");

    } else if (requestType.compareToIgnoreCase("schedule") == 0) {

      ReportSchedulingDTO reportScheduling = new ReportSchedulingDTO();
      setReportScheduling(reportForm, reportScheduling, errors, user);

      int repeatNumber = 0;
      try {
        repeatNumber = FormatNumber.parseInteger(reportForm.getRepeatNumber());
      } catch (Exception e) {
        //
      }
      String fromDate = getFromDate(reportForm, errors);
      java.util.Date fDate = FormatDate.convertToDateYYMMDD(fromDate);
      String toDate = getToDate(reportForm, errors);
      java.util.Date tDate = FormatDate.convertToDateYYMMDD(toDate);
      Date rDate = new Date();
      try {
        rDate = FormatDate.convertToDate(reportForm.getRunDate());
      } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.date.rundate"));
        formErrors.add("runDate");
      }

      ReportSchedulingDAO reportScheDAo = new ReportSchedulingDAO(user);

      if (repeatNumber > 0) { // do more than 1 time.

        boolean scheduleNow = true;
        Timer timer = new Timer(true);

        for (int i = 0; i < repeatNumber; i++) {
          try {
            //						ReportSchedulingDTO aSchedule = new ReportSchedulingDTO();
            //						copyObj(reportScheduling,aSchedule);
            reportScheDAo.addReportScheduling(reportScheduling);
            if (scheduleNow) {
              Calendar date = Calendar.getInstance();
              date.setTime((java.util.Date) reportScheduling.getRunDate());
              // date.add(Calendar.MINUTE, 5);
              // set runtime. (CDT)
              date.set(Calendar.HOUR_OF_DAY, reportScheduling.getRunTimeHH()); // 0-23

              date.set(Calendar.MINUTE, reportScheduling.getRunTimeMM());
              if (date.getTimeInMillis() <= System.currentTimeMillis()) {
                CompanyManagerBean cmBean = new CompanyManagerBean();
                CompanyDTO company = cmBean.getCompany(user.getCompanyID());
                runNow(reportScheduling, company);
              } else {
                scheduleNow = false;
              }
            }
            // setSchedule(timer, aSchedule, user);
            ReportSchedulingDTO aSchedule = new ReportSchedulingDTO();
            copyObj(reportScheduling, aSchedule);
            reportScheduling = aSchedule;
          } catch (Exception e) {

          }

          String strFDate = FormatDate.convertDateToMMDDYYYY(fDate);
          String strTDate = FormatDate.convertDateToMMDDYYYY(tDate);
          String strRDate = FormatDate.convertDateToMMDDYYYY(rDate);
          int field = -1;
          if (reportForm.getRepeatType().compareToIgnoreCase("D") == 0) {
            field = 1;

          } else if (reportForm.getRepeatType().compareToIgnoreCase("W") == 0) {
            field = 7;

          } else if (reportForm.getRepeatType().compareToIgnoreCase("M") == 0) {
            field = 0;
          }
          if (field > -1) {
            strRDate = FormatDate.addToDateMMDDYYYY(strRDate, field, (field == 0 ? 1 : 0));
            strFDate = FormatDate.addToDateMMDDYYYY(strFDate, field, (field == 0 ? 1 : 0));
            strTDate = FormatDate.addToDateMMDDYYYY(strTDate, field, (field == 0 ? 1 : 0));
            rDate = FormatDate.convertMMDDYYYYToDateYYMMDD(strRDate);
            fDate = FormatDate.convertMMDDYYYYToDateYYMMDD(strFDate);
            tDate = FormatDate.convertMMDDYYYYToDateYYMMDD(strTDate);
            reportScheduling.setRunDate(new java.sql.Date(rDate.getTime()));
            reportScheduling.setFromDate(new java.sql.Date(fDate.getTime()));
            reportScheduling.setToDate(new java.sql.Date(tDate.getTime()));
          }
        }
      } else { // do only one time
        try {
          reportScheDAo.addReportScheduling(reportScheduling);
          Calendar date = Calendar.getInstance();
          date.setTime((java.util.Date) reportScheduling.getRunDate());
          // date.add(Calendar.MINUTE, 5);
          // set runtime. (CDT)
          date.set(Calendar.HOUR_OF_DAY, reportScheduling.getRunTimeHH()); // 0-23

          date.set(Calendar.MINUTE, reportScheduling.getRunTimeMM());
          if (date.getTimeInMillis() <= System.currentTimeMillis()) {
            CompanyManagerBean cmBean = new CompanyManagerBean();
            CompanyDTO company = cmBean.getCompany(user.getCompanyID());
            runNow(reportScheduling, company);
          }
          // setSchedule( null, reportScheduling,  user);
        } catch (Exception e) {

        }
      }
    }
    // we comment it out because we don't want it to do the pupup but let the user look at the
    // printed report list.
    // reportForm.setReportURL(pageName);
    return (mapping.findForward("localeChange"));
  }
  private void setReportScheduling(
      ReportForm reportForm,
      ReportSchedulingDTO reportScheduling,
      ActionMessages errors,
      DbUserSession user) {
    int formID = 0;
    try {
      formID = FormatNumber.parseInteger(reportForm.getSelectReport());
    } catch (Exception e) {
      //
    }

    // for only locale/location
    // Get selected location
    String location = null;
    try {
      location = reportForm.getUserLocationId();
    } catch (Exception e) {
      //
    }
    // Get selected location
    String locale = null;
    try {
      locale = reportForm.getUserLocaleId();
    } catch (Exception e) {
      //
    }

    // for this array version we don't need.
    //		String strLocale = null;
    //		String strLocation = null;
    //		if (Constants.GLOBAL_LOCALE_STRING.equals(reportForm.getType())) { // for all locales
    //			strLocale = "ALL";
    //			strLocation = "ALL";
    //
    //		} else if (Constants.GLOBAL_LOCATION_STRING.equals(reportForm.getType())) { // for specific
    // locales
    //			String[] localeIds = reportForm.getLocaleIds();
    //			strLocale = "[";
    //			for (int i = 0; i < localeIds.length; i++) {
    //				if (strLocale.length()> 1){
    //	        		strLocale += ",";
    //				}
    //	        	strLocale = strLocale+localeIds[i];
    //			}
    //			 strLocale = strLocale+"]";
    //			 strLocation = "ALL";
    //		}
    //		else { //for specific locations.
    //			strLocale="ALL";
    //			String[] locationIds = reportForm.getLocaleIds();
    //			strLocation="[";
    //			for (int i = 0; i < locationIds.length; i++) {
    //				if (strLocation.length()> 1){
    //					strLocation += ",";
    //				}
    //				strLocation = strLocation+locationIds[i];
    //			}
    //			strLocation = strLocation+"]";
    //		}
    // end for this version

    String repeatType = "N";
    int repeatNumber = 0;

    reportScheduling.setFormID(formID);
    reportScheduling.setUserId(user.getId());
    // for only one locale version
    reportScheduling.setLocale(locale);
    reportScheduling.setLocation(location);
    // end

    // for this locals/locations
    //		reportScheduling.setLocale(strLocale);
    //		reportScheduling.setLocation(strLocation);
    // end version

    String fromDate = getFromDate(reportForm, errors);
    java.util.Date fDate = FormatDate.convertToDateYYMMDD(fromDate);

    try {
      reportScheduling.setFromDate(new java.sql.Date(fDate.getTime()));
    } catch (Exception e) {
      errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.date.fromdate"));
      formErrors.add("fromDate");
    }

    String toDate = getToDate(reportForm, errors);
    java.util.Date tDate = FormatDate.convertToDateYYMMDD(toDate);
    try {
      reportScheduling.setToDate(new java.sql.Date(tDate.getTime()));
    } catch (Exception e) {
      errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.date.todate"));
      formErrors.add("toDate");
    }

    Date rDate = new Date();

    try {
      reportScheduling.setRunDate(new java.sql.Date(rDate.getTime()));
    } catch (Exception e) {
      errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.date.todate"));
      formErrors.add("toDate");
    }

    int runHH = reportForm.getHH();
    reportScheduling.setRunTimeHH(runHH);
    int runMM = reportForm.getMM();
    reportScheduling.setRunTimeMM(runMM);

    reportScheduling.setRepeatType(repeatType);
    reportScheduling.setRepeatNumber(repeatNumber);
    long createdTimestamp = System.currentTimeMillis();
    reportScheduling.setDatetime(createdTimestamp);

    String emailTo = "";
    reportScheduling.setEmailTo(emailTo);
    String emailCC = "";
    reportScheduling.setEmailCC(emailCC);

    reportScheduling.setStatus("");
    reportScheduling.setReportName("");

    String requestType = reportForm.getRequestType();
    if (requestType == null) {
      requestType = "";
    }
    if (requestType.compareToIgnoreCase("schedule") == 0) {
      repeatType = reportForm.getRepeatType();
      repeatNumber = 0;
      try {
        repeatNumber = FormatNumber.parseInteger(reportForm.getRepeatNumber());
      } catch (Exception e) {
        //
      }
      reportScheduling.setRepeatType(repeatType);

      try {
        rDate = FormatDate.convertToDate(reportForm.getRunDate());
      } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.date.rundate"));
        formErrors.add("runDate");
      }

      try {
        reportScheduling.setRunDate(new java.sql.Date(rDate.getTime()));
      } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.date.runDate"));
        formErrors.add("runDate");
      }

      reportScheduling.setRepeatType(repeatType);
      reportScheduling.setRepeatNumber(repeatNumber);
      createdTimestamp = System.currentTimeMillis();
      reportScheduling.setDatetime(createdTimestamp);
      reportScheduling.setReportName("");
      emailTo = "";
      emailTo = reportForm.getRecipientsTo().replace(':', ';');
      emailTo = emailTo.replace(' ', ';');
      emailTo = emailTo.replace(',', ';');
      reportScheduling.setEmailTo(emailTo);
      emailCC = "";
      emailCC = reportForm.getRecipientsCC().replace(':', ';');
      emailCC = emailCC.replace(' ', ';');
      emailCC = emailCC.replace(',', ';');
      reportScheduling.setEmailCC(emailCC);
      reportScheduling.setStatus("Q");
      reportScheduling.setReportType(ReportSchedulingDTO.REPORT_TYPE_SCHEDULING);
    }
  }