/** createTabFile, added by tbh, 01/2009 */
  public HashMap<String, Integer> createTabFile(
      ExtractBean eb,
      long sysTimeBegin,
      String generalFileDir,
      DatasetBean datasetBean,
      int activeStudyId,
      int parentStudyId,
      String generalFileDirCopy) {

    TabReportBean answer = new TabReportBean();

    DatasetDAO dsdao = new DatasetDAO(ds);
    // create the extract bean here, tbh
    eb = dsdao.getDatasetData(eb, activeStudyId, parentStudyId);
    eb.getMetadata();
    eb.computeReport(answer);

    long sysTimeEnd = System.currentTimeMillis() - sysTimeBegin;
    String TXTFileName = datasetBean.getName() + "_tab.xls";

    int fId =
        this.createFile(
            TXTFileName,
            generalFileDir,
            answer.toString(),
            datasetBean,
            sysTimeEnd,
            ExportFormatBean.TXTFILE,
            true);
    if (!"".equals(generalFileDirCopy)) {
      int fId2 =
          this.createFile(
              TXTFileName,
              generalFileDirCopy,
              answer.toString(),
              datasetBean,
              sysTimeEnd,
              ExportFormatBean.TXTFILE,
              false);
    }
    logger.info("created txt file");
    // return TXTFileName;
    HashMap answerMap = new HashMap<String, Integer>();
    answerMap.put(TXTFileName, new Integer(fId));
    return answerMap;
  }
  /** createODMfile, added by tbh, 01/2009 */
  public HashMap<String, Integer> createODMFile(
      String odmVersion,
      long sysTimeBegin,
      String generalFileDir,
      DatasetBean datasetBean,
      StudyBean currentStudy,
      String generalFileDirCopy,
      ExtractBean eb,
      Integer currentStudyId,
      Integer parentStudyId,
      String studySubjectNumber) {

    Integer ssNumber = getStudySubjectNumber(studySubjectNumber);
    MetaDataCollector mdc = new MetaDataCollector(ds, datasetBean, currentStudy);
    AdminDataCollector adc = new AdminDataCollector(ds, datasetBean, currentStudy);
    ClinicalDataCollector cdc = new ClinicalDataCollector(ds, datasetBean, currentStudy);
    MetaDataCollector.setTextLength(200);
    if (odmVersion != null) {
      // by default odmVersion is 1.2
      if ("1.3".equals(odmVersion)) {
        ODMBean odmb = new ODMBean();
        odmb.setSchemaLocation("http://www.cdisc.org/ns/odm/v1.3 ODM1-3-0.xsd");
        ArrayList<String> xmlnsList = new ArrayList<String>();
        xmlnsList.add("xmlns=\"http://www.cdisc.org/ns/odm/v1.3\"");
        odmb.setXmlnsList(xmlnsList);
        odmb.setODMVersion("1.3");
        mdc.setODMBean(odmb);
        adc.setOdmbean(odmb);
        cdc.setODMBean(odmb);
      } else if ("oc1.2".equals(odmVersion)) {
        ODMBean odmb = new ODMBean();
        // odmb.setSchemaLocation("http://www.cdisc.org/ns/odm/v1.2 OpenClinica-ODM1-2-1.xsd");
        odmb.setSchemaLocation("http://www.cdisc.org/ns/odm/v1.2 OpenClinica-ODM1-2-1-OC1.xsd");
        ArrayList<String> xmlnsList = new ArrayList<String>();
        xmlnsList.add("xmlns=\"http://www.cdisc.org/ns/odm/v1.2\"");
        xmlnsList.add("xmlns:OpenClinica=\"http://www.openclinica.org/ns/openclinica_odm/v1.2\"");
        odmb.setXmlnsList(xmlnsList);
        odmb.setODMVersion("oc1.2");
        mdc.setODMBean(odmb);
        adc.setOdmbean(odmb);
        cdc.setODMBean(odmb);
      } else if ("oc1.3".equals(odmVersion)) {
        ODMBean odmb = mdc.getODMBean();
        // odmb.setSchemaLocation("http://www.cdisc.org/ns/odm/v1.3 OpenClinica-ODM1-3-0.xsd");
        odmb.setSchemaLocation("http://www.cdisc.org/ns/odm/v1.3 OpenClinica-ODM1-3-0-OC1.xsd");
        ArrayList<String> xmlnsList = new ArrayList<String>();
        xmlnsList.add("xmlns=\"http://www.cdisc.org/ns/odm/v1.3\"");
        xmlnsList.add("xmlns:OpenClinica=\"http://www.openclinica.org/ns/openclinica_odm/v1.3\"");
        odmb.setXmlnsList(xmlnsList);
        odmb.setODMVersion("oc1.3");
        mdc.setODMBean(odmb);
        adc.setOdmbean(odmb);
        cdc.setODMBean(odmb);
      }
    }

    //////////////////////////////////////////
    ////////// MetaData Extraction //////////
    mdc.collectFileData();
    MetaDataReportBean metaReport = new MetaDataReportBean(mdc.getOdmStudyMap());
    metaReport.setODMVersion(odmVersion);
    metaReport.setOdmBean(mdc.getODMBean());
    metaReport.createChunkedOdmXml(Boolean.TRUE);

    long sysTimeEnd = System.currentTimeMillis() - sysTimeBegin;
    String ODMXMLFileName = mdc.getODMBean().getFileOID() + ".xml";
    int fId =
        this.createFileK(
            ODMXMLFileName,
            generalFileDir,
            metaReport.getXmlOutput().toString(),
            datasetBean,
            sysTimeEnd,
            ExportFormatBean.XMLFILE,
            false);
    if (!"".equals(generalFileDirCopy)) {
      int fId2 =
          this.createFileK(
              ODMXMLFileName,
              generalFileDirCopy,
              metaReport.getXmlOutput().toString(),
              datasetBean,
              sysTimeEnd,
              ExportFormatBean.XMLFILE,
              false);
    }
    //////////////////////////////////////////
    ////////// AdminData Extraction //////////

    adc.collectFileData();
    AdminDataReportBean adminReport = new AdminDataReportBean(adc.getOdmAdminDataMap());
    adminReport.setODMVersion(odmVersion);
    adminReport.setOdmBean(mdc.getODMBean());
    adminReport.createChunkedOdmXml(Boolean.TRUE);

    sysTimeEnd = System.currentTimeMillis() - sysTimeBegin;
    fId =
        this.createFileK(
            ODMXMLFileName,
            generalFileDir,
            adminReport.getXmlOutput().toString(),
            datasetBean,
            sysTimeEnd,
            ExportFormatBean.XMLFILE,
            false);
    if (!"".equals(generalFileDirCopy)) {
      int fId2 =
          this.createFileK(
              ODMXMLFileName,
              generalFileDirCopy,
              adminReport.getXmlOutput().toString(),
              datasetBean,
              sysTimeEnd,
              ExportFormatBean.XMLFILE,
              false);
    }

    //////////////////////////////////////////
    ////////// ClinicalData Extraction ///////

    DatasetDAO dsdao = new DatasetDAO(ds);
    String sql = eb.getDataset().getSQLStatement();
    String st_sed_in = dsdao.parseSQLDataset(sql, true, true);
    String st_itemid_in = dsdao.parseSQLDataset(sql, false, true);
    int datasetItemStatusId = eb.getDataset().getDatasetItemStatus().getId();
    String ecStatusConstraint = dsdao.getECStatusConstraint(datasetItemStatusId);
    String itStatusConstraint = dsdao.getItemDataStatusConstraint(datasetItemStatusId);

    Iterator<OdmStudyBase> it = cdc.getStudyBaseMap().values().iterator();
    while (it.hasNext()) {
      OdmStudyBase u = it.next();
      ArrayList newRows =
          dsdao.selectStudySubjects(
              u.getStudy().getId(),
              0,
              st_sed_in,
              st_itemid_in,
              dsdao.genDatabaseDateConstraint(eb),
              ecStatusConstraint,
              itStatusConstraint);

      ///////////////
      int fromIndex = 0;
      boolean firstIteration = true;
      while (fromIndex < newRows.size()) {
        int toIndex =
            fromIndex + ssNumber < newRows.size() ? fromIndex + ssNumber : newRows.size() - 1;
        List x = newRows.subList(fromIndex, toIndex + 1);
        fromIndex = toIndex + 1;
        String studySubjectIds = "";
        for (int i = 0; i < x.size(); i++) {
          StudySubjectBean sub = new StudySubjectBean();
          sub = (StudySubjectBean) x.get(i);
          studySubjectIds += "," + sub.getId();
        } // for
        studySubjectIds = studySubjectIds.replaceFirst(",", "");

        ClinicalDataUnit cdata =
            new ClinicalDataUnit(
                ds,
                datasetBean,
                cdc.getOdmbean(),
                u.getStudy(),
                cdc.getCategory(),
                studySubjectIds);
        cdata.setCategory(cdc.getCategory());
        cdata.collectOdmClinicalData();

        FullReportBean report = new FullReportBean();
        report.setClinicalData(cdata.getOdmClinicalData());
        report.setOdmStudyMap(mdc.getOdmStudyMap());
        report.setODMVersion(odmVersion);
        // report.setOdmStudy(mdc.getOdmStudy());
        report.setOdmBean(mdc.getODMBean());
        if (firstIteration && fromIndex >= newRows.size()) {
          report.createChunkedOdmXml(Boolean.TRUE, true, true);
          firstIteration = false;
        } else if (firstIteration) {
          report.createChunkedOdmXml(Boolean.TRUE, true, false);
          firstIteration = false;
        } else if (fromIndex >= newRows.size()) {
          report.createChunkedOdmXml(Boolean.TRUE, false, true);
        } else {
          report.createChunkedOdmXml(Boolean.TRUE, false, false);
        }
        fId =
            this.createFileK(
                ODMXMLFileName,
                generalFileDir,
                report.getXmlOutput().toString(),
                datasetBean,
                sysTimeEnd,
                ExportFormatBean.XMLFILE,
                false);
        if (!"".equals(generalFileDirCopy)) {
          int fId2 =
              this.createFileK(
                  ODMXMLFileName,
                  generalFileDirCopy,
                  report.getXmlOutput().toString(),
                  datasetBean,
                  sysTimeEnd,
                  ExportFormatBean.XMLFILE,
                  false);
        }
      }
    }

    sysTimeEnd = System.currentTimeMillis() - sysTimeBegin;
    fId =
        this.createFileK(
            ODMXMLFileName,
            generalFileDir,
            "</ODM>",
            datasetBean,
            sysTimeEnd,
            ExportFormatBean.XMLFILE,
            true);
    if (!"".equals(generalFileDirCopy)) {
      int fId2 =
          this.createFileK(
              ODMXMLFileName,
              generalFileDirCopy,
              "</ODM>",
              datasetBean,
              sysTimeEnd,
              ExportFormatBean.XMLFILE,
              false);
    }

    //////////////////////////////////////////
    ////////// pre pagination extraction /////
    /*
    mdc.collectFileData();
    adc.collectOdmAdminDataMap();
    cdc.collectOdmClinicalDataMap();
    FullReportBean report = new FullReportBean();
    report.setClinicalDataMap(cdc.getOdmClinicalDataMap());
    report.setAdminDataMap(adc.getOdmAdminDataMap());
    report.setOdmStudyMap(mdc.getOdmStudyMap());
    report.setOdmBean(mdc.getODMBean());
    report.setODMVersion(odmVersion);
    report.createOdmXml(Boolean.TRUE);
    long sysTimeEnd = System.currentTimeMillis() - sysTimeBegin;
    String ODMXMLFileName = mdc.getODMBean().getFileOID() + ".xml";
    int fId = this.createFile(ODMXMLFileName, generalFileDir, report.getXmlOutput().toString(), datasetBean, sysTimeEnd, ExportFormatBean.XMLFILE, true);
    if (!"".equals(generalFileDirCopy)) {
    	int fId2 = this.createFile(ODMXMLFileName, generalFileDirCopy, report.getXmlOutput().toString(), datasetBean, sysTimeEnd, ExportFormatBean.XMLFILE, false);
    } */

    // return ODMXMLFileName;
    HashMap answerMap = new HashMap<String, Integer>();
    answerMap.put(ODMXMLFileName, new Integer(fId));
    return answerMap;
  }
  @Override
  protected void processRequest() throws Exception {
    FormProcessor fp = new FormProcessor(request);
    // changes to this servlet, we now look at group name too, tbh 05/2009
    String triggerName = fp.getString("tname");
    String gName = fp.getString("gname");
    String groupName = "";
    if (gName.equals("") || gName.equals("0")) {
      groupName = TRIGGER_GROUP;
    } else { // if (gName.equals("1")) {
      groupName = TRIGGER_IMPORT_GROUP;
    }
    // << tbh 09/03/2009 #4143
    scheduler = getScheduler();
    Trigger trigger = scheduler.getTrigger(triggerName.trim(), groupName);

    // trigger bean is a wrapper for the trigger, to serve as a link btw
    // quartz classes and oc classes
    // is it really necessary? DRY

    if (trigger == null) {
      System.out.println("*** reset trigger group name");
      groupName = TRIGGER_GROUP;
      trigger = scheduler.getTrigger(triggerName.trim(), groupName);
    }
    // << tbh 09/03/2009 #4143
    // above is a hack, if we add more trigger groups this will have
    // to be redone
    System.out.println("found trigger name: " + triggerName);
    System.out.println("found group name: " + groupName);
    // System.out.println("found trigger on the other side, full name: " +
    // trigger.getFullName());
    TriggerBean triggerBean = new TriggerBean();
    JobDataMap dataMap = new JobDataMap();
    AuditEventDAO auditEventDAO = new AuditEventDAO(sm.getDataSource());

    try {
      triggerBean.setFullName(trigger.getName());
      triggerBean.setPreviousDate(trigger.getPreviousFireTime());
      triggerBean.setNextDate(trigger.getNextFireTime());
      // >> set active here, tbh 10/08/2009
      if (scheduler.getTriggerState(triggerName, groupName) == Trigger.STATE_PAUSED) {
        triggerBean.setActive(false);
        System.out.println("setting active to false for trigger: " + trigger.getName());
      } else {
        triggerBean.setActive(true);
        System.out.println("setting active to TRUE for trigger: " + trigger.getName());
      }
      // <<
      if (trigger.getDescription() != null) {
        triggerBean.setDescription(trigger.getDescription());
      }
      if (trigger.getJobDataMap().size() > 0) {
        dataMap = trigger.getJobDataMap();
        String contactEmail = dataMap.getString(ExampleSpringJob.EMAIL);
        System.out.println("found email: " + contactEmail);
        // String datasetId =
        // dataMap.getString(ExampleSpringJob.DATASET_ID);
        // int dsId = new Integer(datasetId).intValue();
        if (gName.equals("") || gName.equals("0")) {
          String tab = dataMap.getString(ExampleSpringJob.TAB);
          String cdisc = dataMap.getString(ExampleSpringJob.CDISC);
          String spss = dataMap.getString(ExampleSpringJob.SPSS);
          String periodToRun = dataMap.getString(ExampleSpringJob.PERIOD);
          // int userId = new Integer(userAcctId).intValue();
          int dsId = dataMap.getInt(ExampleSpringJob.DATASET_ID);
          triggerBean.setCdisc(cdisc);
          triggerBean.setSpss(spss);
          triggerBean.setTab(tab);
          triggerBean.setPeriodToRun(periodToRun);
          DatasetDAO datasetDAO = new DatasetDAO(sm.getDataSource());
          DatasetBean dataset = (DatasetBean) datasetDAO.findByPK(dsId);
          triggerBean.setDataset(dataset);
        }
        int userId = dataMap.getInt(ExampleSpringJob.USER_ID);
        // need to set information, extract bean, user account bean

        UserAccountDAO userAccountDAO = new UserAccountDAO(sm.getDataSource());

        triggerBean.setContactEmail(contactEmail);

        UserAccountBean userAccount = (UserAccountBean) userAccountDAO.findByPK(userId);

        triggerBean.setUserAccount(userAccount);

        ArrayList<AuditEventBean> triggerLogs =
            auditEventDAO.findAllByAuditTable(trigger.getName());

        // set the table for the audit event beans here

        ArrayList allRows = AuditEventRow.generateRowsFromBeans(triggerLogs);

        EntityBeanTable table = fp.getEntityBeanTable();
        String[] columns = {
          resword.getString("date_and_time"),
          resword.getString("action_message"),
          resword.getString("entity_operation"),
          // resword.getString("study_site"),
          // resword.getString("study_subject_ID"),
          resword.getString("changes_and_additions"),
          resword.getString("actions")
        };

        table.setColumns(new ArrayList(Arrays.asList(columns)));
        table.setAscendingSort(false);
        table.hideColumnLink(1);
        table.hideColumnLink(3);
        table.hideColumnLink(4);

        table.setQuery("ViewSingleJob?tname=" + triggerName + "&gname=" + gName, new HashMap());
        table.setRows(allRows);
        table.computeDisplay();

        request.setAttribute("table", table);
      }

    } catch (NullPointerException e) {
      // TODO Auto-generated catch block
      System.out.println(" found NPE " + e.getMessage());
      e.printStackTrace();
    }
    // need to show the extract for which this runs, which files, etc
    // in other words the job data map

    request.setAttribute("triggerBean", triggerBean);

    request.setAttribute("groupName", groupName);

    forwardPage(Page.VIEW_SINGLE_JOB);
  }
  public void processRequest() throws Exception {

    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    FormProcessor fp = new FormProcessor(request);
    int studyId = fp.getInt("id");

    StudyBean study = (StudyBean) sdao.findByPK(studyId);
    // find all sites
    ArrayList sites = (ArrayList) sdao.findAllByParent(studyId);

    // find all user and roles in the study, include ones in sites
    UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
    ArrayList userRoles = udao.findAllByStudyId(studyId);

    // find all subjects in the study, include ones in sites
    StudySubjectDAO ssdao = new StudySubjectDAO(sm.getDataSource());
    ArrayList subjects = ssdao.findAllByStudy(study);

    // find all events in the study, include ones in sites
    StudyEventDefinitionDAO sefdao = new StudyEventDefinitionDAO(sm.getDataSource());
    ArrayList definitions = sefdao.findAllByStudy(study);

    String action = request.getParameter("action");
    if (studyId == 0) {
      addPageMessage("Please choose a study to restore.");
      forwardPage(Page.STUDY_LIST_SERVLET);
    } else {
      if ("confirm".equalsIgnoreCase(action)) {
        request.setAttribute("studyToRestore", study);

        request.setAttribute("sitesToRestore", sites);

        request.setAttribute("userRolesToRestore", userRoles);

        request.setAttribute("subjectsToRestore", subjects);

        request.setAttribute("definitionsToRRestore", definitions);
        forwardPage(Page.RESTORE_STUDY);
      } else {
        logger.info("submit to restore the study");
        // change all statuses to unavailable
        StudyDAO studao = new StudyDAO(sm.getDataSource());
        study.setStatus(Status.AVAILABLE);
        study.setUpdater(ub);
        study.setUpdatedDate(new Date());
        studao.update(study);

        // remove all sites
        for (int i = 0; i < sites.size(); i++) {
          StudyBean site = (StudyBean) sites.get(i);
          site.setStatus(Status.AVAILABLE);
          site.setUpdater(ub);
          site.setUpdatedDate(new Date());
          sdao.update(site);
        }

        // remove all users and roles
        for (int i = 0; i < userRoles.size(); i++) {
          StudyUserRoleBean role = (StudyUserRoleBean) userRoles.get(i);
          role.setStatus(Status.AVAILABLE);
          role.setUpdater(ub);
          role.setUpdatedDate(new Date());
          udao.updateStudyUserRole(role, role.getUserName());
        }

        // remove all subjects
        for (int i = 0; i < subjects.size(); i++) {
          StudySubjectBean subject = (StudySubjectBean) subjects.get(i);
          subject.setStatus(Status.AVAILABLE);
          subject.setUpdater(ub);
          subject.setUpdatedDate(new Date());
          ssdao.update(subject);
        }

        // remove all study_group
        StudyGroupDAO sgdao = new StudyGroupDAO(sm.getDataSource());
        SubjectGroupMapDAO sgmdao = new SubjectGroupMapDAO(sm.getDataSource());
        ArrayList groups = sgdao.findAllByStudy(study);
        for (int i = 0; i < groups.size(); i++) {
          StudyGroupBean group = (StudyGroupBean) groups.get(i);
          group.setStatus(Status.AVAILABLE);
          group.setUpdater(ub);
          group.setUpdatedDate(new Date());
          sgdao.update(group);
          // all subject_group_map
          ArrayList subjectGroupMaps = sgmdao.findAllByStudyGroupId(group.getId());
          for (int j = 0; j < subjectGroupMaps.size(); j++) {
            SubjectGroupMapBean sgMap = (SubjectGroupMapBean) subjectGroupMaps.get(j);
            sgMap.setStatus(Status.AVAILABLE);
            sgMap.setUpdater(ub);
            sgMap.setUpdatedDate(new Date());
            sgmdao.update(sgMap);
          }
        }

        // remove all event definitions and event
        EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(sm.getDataSource());
        StudyEventDAO sedao = new StudyEventDAO(sm.getDataSource());
        for (int i = 0; i < definitions.size(); i++) {
          StudyEventDefinitionBean definition = (StudyEventDefinitionBean) definitions.get(i);
          definition.setStatus(Status.AVAILABLE);
          definition.setUpdater(ub);
          definition.setUpdatedDate(new Date());
          sefdao.update(definition);
          ArrayList edcs = (ArrayList) edcdao.findAllByDefinition(definition.getId());
          for (int j = 0; j < edcs.size(); j++) {
            EventDefinitionCRFBean edc = (EventDefinitionCRFBean) edcs.get(j);
            edc.setStatus(Status.AVAILABLE);
            edc.setUpdater(ub);
            edc.setUpdatedDate(new Date());
            edcdao.update(edc);
          }

          ArrayList events = (ArrayList) sedao.findAllByDefinition(definition.getId());
          EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());

          for (int j = 0; j < events.size(); j++) {
            StudyEventBean event = (StudyEventBean) events.get(j);
            event.setStatus(Status.AVAILABLE);
            event.setUpdater(ub);
            event.setUpdatedDate(new Date());
            sedao.update(event);

            ArrayList eventCRFs = ecdao.findAllByStudyEvent(event);

            ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
            for (int k = 0; k < eventCRFs.size(); k++) {
              EventCRFBean eventCRF = (EventCRFBean) eventCRFs.get(k);
              eventCRF.setStatus(Status.AVAILABLE);
              eventCRF.setUpdater(ub);
              eventCRF.setUpdatedDate(new Date());
              ecdao.update(eventCRF);

              ArrayList itemDatas = iddao.findAllByEventCRFId(eventCRF.getId());
              for (int a = 0; a < itemDatas.size(); a++) {
                ItemDataBean item = (ItemDataBean) itemDatas.get(a);
                item.setStatus(Status.AVAILABLE);
                item.setUpdater(ub);
                item.setUpdatedDate(new Date());
                iddao.update(item);
              }
            }
          }
        } // for definitions

        DatasetDAO datadao = new DatasetDAO(sm.getDataSource());
        ArrayList dataset = datadao.findAllByStudyId(study.getId());
        for (int i = 0; i < dataset.size(); i++) {
          DatasetBean data = (DatasetBean) dataset.get(i);
          data.setStatus(Status.AVAILABLE);
          data.setUpdater(ub);
          data.setUpdatedDate(new Date());
          datadao.update(data);
        }

        addPageMessage("This study has been restored successfully.");
        forwardPage(Page.STUDY_LIST_SERVLET);
      }
    }
  }
  @Override
  protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
    // need to generate a Locale so that user beans and other things will
    // generate normally
    Locale locale = new Locale("en-US");
    ResourceBundleProvider.updateLocale(locale);
    ResourceBundle pageMessages = ResourceBundleProvider.getPageMessagesBundle();
    // logger.debug("--");
    // logger.debug("-- executing a job " + message + " at " + new
    // java.util.Date().toString());
    JobDataMap dataMap = context.getMergedJobDataMap();
    SimpleTrigger trigger = (SimpleTrigger) context.getTrigger();
    try {
      ApplicationContext appContext =
          (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
      String studySubjectNumber =
          ((CoreResources) appContext.getBean("coreResources")).getField("extract.number");
      coreResources = (CoreResources) appContext.getBean("coreResources");
      ruleSetRuleDao = (RuleSetRuleDao) appContext.getBean("ruleSetRuleDao");
      dataSource = (DataSource) appContext.getBean("dataSource");
      mailSender = (OpenClinicaMailSender) appContext.getBean("openClinicaMailSender");
      AuditEventDAO auditEventDAO = new AuditEventDAO(dataSource);
      // Scheduler scheduler = context.getScheduler();
      // JobDetail detail = context.getJobDetail();
      // jobDetailBean = (JobDetailBean) detail;
      /*
       * data map here should coincide with the job data map found in
       * CreateJobExportServlet, with the following code: jobDataMap = new
       * JobDataMap(); jobDataMap.put(DATASET_ID, datasetId);
       * jobDataMap.put(PERIOD, period); jobDataMap.put(EMAIL, email);
       * jobDataMap.put(TAB, tab); jobDataMap.put(CDISC, cdisc);
       * jobDataMap.put(SPSS, spss);
       */
      String alertEmail = dataMap.getString(EMAIL);
      String localeStr = dataMap.getString(LOCALE);
      if (localeStr != null) {
        locale = new Locale(localeStr);
        ResourceBundleProvider.updateLocale(locale);
        pageMessages = ResourceBundleProvider.getPageMessagesBundle();
      }
      int dsId = dataMap.getInt(DATASET_ID);
      String tab = dataMap.getString(TAB);
      String cdisc = dataMap.getString(CDISC);
      String cdisc12 = dataMap.getString(CDISC12);
      if (cdisc12 == null) {
        cdisc12 = "0";
      }
      String cdisc13 = dataMap.getString(CDISC13);
      if (cdisc13 == null) {
        cdisc13 = "0";
      }
      String cdisc13oc = dataMap.getString(CDISC13OC);
      if (cdisc13oc == null) {
        cdisc13oc = "0";
      }
      String spss = dataMap.getString(SPSS);
      int userId = dataMap.getInt(USER_ID);
      int studyId = dataMap.getInt(STUDY_ID);

      // String datasetId = dataMap.getString(DATASET_ID);
      // int dsId = new Integer(datasetId).intValue();
      // String userAcctId = dataMap.getString(USER_ID);
      // int userId = new Integer(userAcctId).intValue();
      // why the flip-flop? if one property is set to 'true' we can
      // see jobs in another screen but all properties have to be
      // strings

      logger.debug("-- found the job: " + dsId + " dataset id");

      // for (Iterator it = dataMap.entrySet().iterator(); it.hasNext();)
      // {
      // java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
      // Object key = entry.getKey();
      // Object value = entry.getValue();
      // // logger.debug("-- found datamap property: " + key.toString() +
      // // " : " + value.toString());
      // }
      HashMap fileName = new HashMap<String, Integer>();
      if (dsId > 0) {
        // trying to not throw an error if there's no dataset id
        DatasetDAO dsdao = new DatasetDAO(dataSource);
        DatasetBean datasetBean = (DatasetBean) dsdao.findByPK(dsId);
        StudyDAO studyDao = new StudyDAO(dataSource);
        UserAccountDAO userAccountDAO = new UserAccountDAO(dataSource);
        // hmm, three lines in the if block DRY?
        String generalFileDir = "";
        String generalFileDirCopy = "";
        String exportFilePath = SQLInitServlet.getField("exportFilePath");
        String pattern =
            "yyyy"
                + File.separator
                + "MM"
                + File.separator
                + "dd"
                + File.separator
                + "HHmmssSSS"
                + File.separator;
        SimpleDateFormat sdfDir = new SimpleDateFormat(pattern);
        generalFileDir =
            DATASET_DIR
                + datasetBean.getId()
                + File.separator
                + sdfDir.format(new java.util.Date());
        if (!"".equals(exportFilePath)) {
          generalFileDirCopy =
              SQLInitServlet.getField("filePath") + exportFilePath + File.separator;
        }
        // logger.debug("-- created the following dir: " +
        // generalFileDir);
        long sysTimeBegin = System.currentTimeMillis();
        // set up the user bean here, tbh
        // logger.debug("-- gen tab file 00");

        userBean = (UserAccountBean) userAccountDAO.findByPK(userId);
        // needs to also be captured by the servlet, tbh
        // logger.debug("-- gen tab file 00");
        generateFileService =
            new GenerateExtractFileService(dataSource, userBean, coreResources, ruleSetRuleDao);

        // logger.debug("-- gen tab file 00");

        // tbh #5796 - covers a bug when the user changes studies, 10/2010
        StudyBean activeStudy = (StudyBean) studyDao.findByPK(studyId);
        StudyBean parentStudy = new StudyBean();
        logger.debug(
            "active study: " + studyId + " parent study: " + activeStudy.getParentStudyId());
        if (activeStudy.getParentStudyId() > 0) {
          // StudyDAO sdao = new StudyDAO(sm.getDataSource());
          parentStudy = (StudyBean) studyDao.findByPK(activeStudy.getParentStudyId());
        } else {
          parentStudy = activeStudy;
          // covers a bug in tab file creation, tbh 01/2009
        }

        logger.debug("-- found extract bean ");

        ExtractBean eb =
            generateFileService.generateExtractBean(datasetBean, activeStudy, parentStudy);
        MessageFormat mf = new MessageFormat("");
        StringBuffer message = new StringBuffer();
        StringBuffer auditMessage = new StringBuffer();
        // use resource bundle page messages to generate the email, tbh
        // 02/2009
        // message.append(pageMessages.getString("html_email_header_1")
        // + " " + alertEmail +
        // pageMessages.getString("html_email_header_2") + "<br/>");
        message.append(
            "<p>"
                + pageMessages.getString("email_header_1")
                + " "
                + EmailEngine.getAdminEmail()
                + " "
                + pageMessages.getString("email_header_2")
                + " Job Execution "
                + pageMessages.getString("email_header_3")
                + "</p>");
        message.append("<P>Dataset: " + datasetBean.getName() + "</P>");
        message.append("<P>Study: " + activeStudy.getName() + "</P>");
        message.append(
            "<p>"
                + pageMessages.getString("html_email_body_1")
                + datasetBean.getName()
                + pageMessages.getString("html_email_body_2")
                + SQLInitServlet.getField("sysURL")
                + pageMessages.getString("html_email_body_3")
                + "</p>");
        // logger.debug("-- gen tab file 00");
        if ("1".equals(tab)) {

          logger.debug("-- gen tab file 01");
          fileName =
              generateFileService.createTabFile(
                  eb,
                  sysTimeBegin,
                  generalFileDir,
                  datasetBean,
                  activeStudy.getId(),
                  parentStudy.getId(),
                  generalFileDirCopy);
          message.append(
              "<p>"
                  + pageMessages.getString("html_email_body_4")
                  + " "
                  + getFileNameStr(fileName)
                  + pageMessages.getString("html_email_body_4_5")
                  + SQLInitServlet.getField("sysURL.base")
                  + "AccessFile?fileId="
                  + getFileIdInt(fileName)
                  + pageMessages.getString("html_email_body_3")
                  + "</p>");
          // MessageFormat mf = new MessageFormat("");
          // mf.applyPattern(pageMessages.getString(
          // "you_can_access_tab_delimited"));
          // Object[] arguments = { getFileIdInt(fileName) };
          // auditMessage.append(mf.format(arguments));

          // auditMessage.append(
          // "You can access your tab-delimited file <a href='AccessFile?fileId="
          // + getFileIdInt(fileName) + "'>here</a>.<br/>");
          auditMessage.append(
              pageMessages.getString("you_can_access_tab_delimited")
                  + getFileIdInt(fileName)
                  + pageMessages.getString("access_end"));
        }

        if ("1".equals(cdisc)) {
          String odmVersion = "oc1.2";
          fileName =
              generateFileService.createODMFile(
                  odmVersion,
                  sysTimeBegin,
                  generalFileDir,
                  datasetBean,
                  activeStudy,
                  generalFileDirCopy,
                  eb,
                  activeStudy.getId(),
                  parentStudy.getId(),
                  studySubjectNumber,
                  true,
                  true,
                  true,
                  null);
          logger.debug("-- gen odm file");
          message.append(
              "<p>"
                  + pageMessages.getString("html_email_body_4")
                  + " "
                  + getFileNameStr(fileName)
                  + pageMessages.getString("html_email_body_4_5")
                  + SQLInitServlet.getField("sysURL.base")
                  + "AccessFile?fileId="
                  + getFileIdInt(fileName)
                  + pageMessages.getString("html_email_body_3")
                  + "</p>");

          // MessageFormat mf = new MessageFormat("");
          // mf.applyPattern(pageMessages.getString(
          // "you_can_access_odm_12"));
          // Object[] arguments = { getFileIdInt(fileName) };
          // auditMessage.append(mf.format(arguments));

          // auditMessage.append(
          // "You can access your ODM 1.2 w/OpenClinica Extension XML file <a
          // href='AccessFile?fileId="
          // + getFileIdInt(fileName)
          // + "'>here</a>.<br/>");
          auditMessage.append(
              pageMessages.getString("you_can_access_odm_12")
                  + getFileIdInt(fileName)
                  + pageMessages.getString("access_end"));
        }

        if ("1".equals(cdisc12)) {
          String odmVersion = "1.2";
          fileName =
              generateFileService.createODMFile(
                  odmVersion,
                  sysTimeBegin,
                  generalFileDir,
                  datasetBean,
                  activeStudy,
                  generalFileDirCopy,
                  eb,
                  activeStudy.getId(),
                  parentStudy.getId(),
                  studySubjectNumber,
                  true,
                  true,
                  true,
                  null);
          logger.debug("-- gen odm file 1.2 default");
          message.append(
              "<p>"
                  + pageMessages.getString("html_email_body_4")
                  + " "
                  + getFileNameStr(fileName)
                  + pageMessages.getString("html_email_body_4_5")
                  + SQLInitServlet.getField("sysURL.base")
                  + "AccessFile?fileId="
                  + getFileIdInt(fileName)
                  + pageMessages.getString("html_email_body_3")
                  + "</p>");

          // mf.applyPattern(pageMessages.getString(
          // "you_can_access_odm_12_xml"));
          // Object[] arguments = { getFileIdInt(fileName) };
          // auditMessage.append(mf.format(arguments));
          // // auditMessage.append(
          // "You can access your ODM 1.2 XML file <a href='AccessFile?fileId="
          // + getFileIdInt(fileName) + "'>here</a>.<br/>");
          auditMessage.append(
              pageMessages.getString("you_can_access_odm_12_xml")
                  + getFileIdInt(fileName)
                  + pageMessages.getString("access_end"));
        }

        if ("1".equals(cdisc13)) {
          String odmVersion = "1.3";
          fileName =
              generateFileService.createODMFile(
                  odmVersion,
                  sysTimeBegin,
                  generalFileDir,
                  datasetBean,
                  activeStudy,
                  generalFileDirCopy,
                  eb,
                  activeStudy.getId(),
                  parentStudy.getId(),
                  studySubjectNumber,
                  true,
                  true,
                  true,
                  null);
          logger.debug("-- gen odm file 1.3");
          message.append(
              "<p>"
                  + pageMessages.getString("html_email_body_4")
                  + " "
                  + getFileNameStr(fileName)
                  + pageMessages.getString("html_email_body_4_5")
                  + SQLInitServlet.getField("sysURL.base")
                  + "AccessFile?fileId="
                  + getFileIdInt(fileName)
                  + pageMessages.getString("html_email_body_3")
                  + "</p>");

          // MessageFormat mf = new MessageFormat("");
          // mf.applyPattern(pageMessages.getString(
          // "you_can_access_odm_13"));
          // Object[] arguments = { getFileIdInt(fileName) };
          // auditMessage.append(mf.format(arguments));

          // auditMessage.append(
          // "You can access your ODM 1.3 XML file <a href='AccessFile?fileId="
          // + getFileIdInt(fileName) + "'>here</a>.<br/>");
          auditMessage.append(
              pageMessages.getString("you_can_access_odm_13")
                  + getFileIdInt(fileName)
                  + pageMessages.getString("access_end"));
        }

        if ("1".equals(cdisc13oc)) {
          String odmVersion = "oc1.3";
          fileName =
              generateFileService.createODMFile(
                  odmVersion,
                  sysTimeBegin,
                  generalFileDir,
                  datasetBean,
                  activeStudy,
                  generalFileDirCopy,
                  eb,
                  activeStudy.getId(),
                  parentStudy.getId(),
                  studySubjectNumber,
                  true,
                  true,
                  true,
                  null);
          logger.debug("-- gen odm file 1.3 oc");
          message.append(
              "<p>"
                  + pageMessages.getString("html_email_body_4")
                  + " "
                  + getFileNameStr(fileName)
                  + pageMessages.getString("html_email_body_4_5")
                  + SQLInitServlet.getField("sysURL.base")
                  + "AccessFile?fileId="
                  + getFileIdInt(fileName)
                  + pageMessages.getString("html_email_body_3")
                  + "</p>");

          // MessageFormat mf = new MessageFormat("");
          // mf.applyPattern(pageMessages.getString(
          // "you_can_access_odm_13_xml"));
          // Object[] arguments = { getFileIdInt(fileName) };
          // auditMessage.append(mf.format(arguments));

          // auditMessage.append(
          // "You can access your ODM 1.3 w/OpenClinica Extension XML file <a
          // href='AccessFile?fileId="
          // + getFileIdInt(fileName)
          // + "'>here</a>.<br/>");
          auditMessage.append(
              pageMessages.getString("you_can_access_odm_13_xml")
                  + getFileIdInt(fileName)
                  + pageMessages.getString("access_end"));
        }
        if ("1".equals(spss)) {
          SPSSReportBean answer = new SPSSReportBean();
          fileName =
              generateFileService.createSPSSFile(
                  datasetBean,
                  eb,
                  activeStudy,
                  parentStudy,
                  sysTimeBegin,
                  generalFileDir,
                  answer,
                  generalFileDirCopy);
          logger.debug("-- gen spss file");
          message.append(
              "<p>"
                  + pageMessages.getString("html_email_body_4")
                  + " "
                  + getFileNameStr(fileName)
                  + pageMessages.getString("html_email_body_4_5")
                  + SQLInitServlet.getField("sysURL.base")
                  + "AccessFile?fileId="
                  + getFileIdInt(fileName)
                  + pageMessages.getString("html_email_body_3")
                  + "</p>");

          // MessageFormat mf = new MessageFormat("");
          // mf.applyPattern(pageMessages.getString(
          // "you_can_access_spss"));
          // Object[] arguments = { getFileIdInt(fileName) };
          // auditMessage.append(mf.format(arguments));

          // auditMessage.append(
          // "You can access your SPSS files <a href='AccessFile?fileId="
          // + getFileIdInt(fileName) + "'>here</a>.<br/>");
          auditMessage.append(
              pageMessages.getString("you_can_access_spss")
                  + getFileIdInt(fileName)
                  + pageMessages.getString("access_end"));
        }

        // wrap up the message, and send the email
        message.append(
            "<p>"
                + pageMessages.getString("html_email_body_5")
                + "</P><P>"
                + pageMessages.getString("email_footer"));
        try {
          mailSender.sendEmail(
              alertEmail.trim(),
              pageMessages.getString("job_ran_for") + " " + datasetBean.getName(),
              message.toString(),
              true);
        } catch (OpenClinicaSystemException ose) {
          // Do Nothing, In the future we might want to have an email
          // status added to system.
        }
        TriggerBean triggerBean = new TriggerBean();
        triggerBean.setDataset(datasetBean);
        triggerBean.setUserAccount(userBean);
        triggerBean.setFullName(trigger.getName());
        auditEventDAO.createRowForExtractDataJobSuccess(triggerBean, auditMessage.toString());
      } else {
        TriggerBean triggerBean = new TriggerBean();
        // triggerBean.setDataset(datasetBean);
        triggerBean.setUserAccount(userBean);
        triggerBean.setFullName(trigger.getName());
        auditEventDAO.createRowForExtractDataJobFailure(triggerBean);
        // logger.debug("-- made it here for some reason, ds id: "
        // + dsId);
      }

      // logger.debug("-- generated file: " + fileNameStr);
      // dataSource.
    } catch (Exception e) {
      // TODO Auto-generated catch block -- ideally should generate a fail
      // msg here, tbh 02/2009
      logger.debug("-- found exception: " + e.getMessage());
      e.printStackTrace();
    }
  }