/** 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;
  }
 public ExtractBean generateExtractBean(
     DatasetBean dsetBean, StudyBean currentStudy, StudyBean parentStudy) {
   ExtractBean eb = new ExtractBean(ds);
   eb.setDataset(dsetBean);
   eb.setShowUniqueId(CoreResources.getField("show_unique_id"));
   eb.setStudy(currentStudy);
   eb.setParentStudy(parentStudy);
   eb.setDateCreated(new java.util.Date());
   return eb;
 }
  /**
   * createSPSSFile, added by tbh, 01/2009
   *
   * @param db
   * @param eb
   * @param currentstudyid
   * @param parentstudy
   * @return
   */
  public HashMap<String, Integer> createSPSSFile(
      DatasetBean db,
      ExtractBean eb2,
      StudyBean currentStudy,
      StudyBean parentStudy,
      long sysTimeBegin,
      String generalFileDir,
      SPSSReportBean answer,
      String generalFileDirCopy) {
    setUpResourceBundles();

    String SPSSFileName = db.getName() + "_data_spss.dat";
    String DDLFileName = db.getName() + "_ddl_spss.sps";
    String ZIPFileName = db.getName() + "_spss";

    SPSSVariableNameValidationBean svnvbean = new SPSSVariableNameValidationBean();
    answer.setDatFileName(SPSSFileName);
    // DatasetDAO dsdao = new DatasetDAO(ds);

    // create the extract bean here, tbh
    // ExtractBean eb = this.generateExtractBean(db, currentStudy,
    // parentStudy);

    // eb = dsdao.getDatasetData(eb, currentStudy.getId(),
    // parentStudy.getId());

    // eb.getMetadata();

    // eb.computeReport(answer);

    answer.setItems(eb2.getItemNames()); // set up items here to get
    // itemMetadata

    // set up response sets for each item here
    ItemDAO itemdao = new ItemDAO(ds);
    ItemFormMetadataDAO imfdao = new ItemFormMetadataDAO(ds);
    ArrayList items = answer.getItems();
    for (int i = 0; i < items.size(); i++) {
      DisplayItemHeaderBean dih = (DisplayItemHeaderBean) items.get(i);
      ItemBean item = dih.getItem();
      ArrayList metas = imfdao.findAllByItemId(item.getId());
      // for (int h = 0; h < metas.size(); h++) {
      // ItemFormMetadataBean ifmb = (ItemFormMetadataBean)
      // metas.get(h);
      // logger.info("group name found:
      // "+ifmb.getGroupLabel());
      // }
      // logger.info("crf versionname" +
      // meta.getCrfVersionName());
      item.setItemMetas(metas);
    }

    HashMap eventDescs = new HashMap<String, String>();

    eventDescs = eb2.getEventDescriptions();

    eventDescs.put("SubjID", resword.getString("study_subject_ID"));
    eventDescs.put("ProtocolID", resword.getString("protocol_ID_site_ID"));
    eventDescs.put("DOB", resword.getString("date_of_birth"));
    eventDescs.put("YOB", resword.getString("year_of_birth"));
    eventDescs.put("Gender", resword.getString("gender"));
    answer.setDescriptions(eventDescs);

    ArrayList generatedReports = new ArrayList<String>();
    try {
      // YW <<
      generatedReports.add(answer.getMetadataFile(svnvbean, eb2).toString());
      generatedReports.add(answer.getDataFile().toString());
      // YW >>
    } catch (IndexOutOfBoundsException i) {
      generatedReports.add(answer.getMetadataFile(svnvbean, eb2).toString());
      logger.debug("throw the error here");
    }

    long sysTimeEnd = System.currentTimeMillis() - sysTimeBegin;

    ArrayList titles = new ArrayList();
    // YW <<
    titles.add(DDLFileName);
    titles.add(SPSSFileName);
    // YW >>

    // create new createFile method that accepts array lists to
    // put into zip files
    int fId =
        this.createFile(
            ZIPFileName,
            titles,
            generalFileDir,
            generatedReports,
            db,
            sysTimeEnd,
            ExportFormatBean.TXTFILE,
            true);
    if (!"".equals(generalFileDirCopy)) {
      int fId2 =
          this.createFile(
              ZIPFileName,
              titles,
              generalFileDirCopy,
              generatedReports,
              db,
              sysTimeEnd,
              ExportFormatBean.TXTFILE,
              false);
    }
    // return DDLFileName;
    HashMap answerMap = new HashMap<String, Integer>();
    answerMap.put(DDLFileName, 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;
  }