public void processRequest() throws Exception {
    FormProcessor fp = new FormProcessor(request);
    int itemId = fp.getInt(ITEM_ID);
    ItemDAO idao = new ItemDAO(sm.getDataSource());
    ItemFormMetadataDAO ifmdao = new ItemFormMetadataDAO(sm.getDataSource());
    CRFVersionDAO cvdao = new CRFVersionDAO(sm.getDataSource());
    CRFDAO cdao = new CRFDAO(sm.getDataSource());

    if (itemId == 0) {
      addPageMessage("Please choose an item first.");
      forwardPage(Page.ITEM_DETAIL);
      return;
    }
    ItemBean item = (ItemBean) idao.findByPK(itemId);
    ArrayList versions = idao.findAllVersionsByItemId(item.getId());
    ArrayList versionItems = new ArrayList();
    // finds each item metadata for each version
    for (int i = 0; i < versions.size(); i++) {
      Integer versionId = (Integer) versions.get(i);
      CRFVersionBean version = (CRFVersionBean) cvdao.findByPK(versionId.intValue());
      if (versionId != null && versionId.intValue() > 0) {
        ItemFormMetadataBean imfBean =
            ifmdao.findByItemIdAndCRFVersionId(item.getId(), versionId.intValue());
        imfBean.setCrfVersionName(version.getName());
        CRFBean crf = (CRFBean) cdao.findByPK(version.getCrfId());
        imfBean.setCrfName(crf.getName());
        versionItems.add(imfBean);
      }
    }
    request.setAttribute(VERSION_ITEMS, versionItems);
    request.setAttribute(ITEM_BEAN, item);
    forwardPage(Page.ITEM_DETAIL);
  }
  /**
   * Performs calculation. <br>
   * Notice: both parameter 'itemdata' and parameter 'errs' might be updated in this method.
   *
   * @param displayItems
   * @param items
   * @param itemdata
   * @param errs
   * @return
   * @author ywang (Jan. 2008)
   */
  public String doCalculation(
      DisplayItemBean displayItem,
      HashMap<String, ItemBean> items,
      HashMap<String, String> itemdata,
      HashMap<Integer, TreeSet<Integer>> itemOrdinals,
      StringBuffer errs,
      int ordinal) {
    if (itemdata == null) {
      logger.error("In DataEntryServlet doCalculation(), itemdata map is empty!");
      errs.append("Calculation cannot be started because needed items are empty" + "; ");
      return "";
    }
    String value = "";
    NumberFormat nf = NumberFormat.getInstance();
    Parser parser = new Parser(items, itemdata);

    ItemBean ib = displayItem.getItem();
    ItemFormMetadataBean ifm = displayItem.getMetadata();
    ResponseOptionBean rob = (ResponseOptionBean) ifm.getResponseSet().getOptions().get(0);
    ArrayList<ScoreToken> parsedExp = new ArrayList<ScoreToken>();
    int type = ifm.getResponseSet().getResponseTypeId();
    if (type == 8) {
      parsedExp = parser.assignVariables(parser.parseScoreTokens(rob.getValue()), ordinal);
    } else if (type == 9) {
      // YW, 1-16-2008, for group-calculation type. Current restrictions:
      // 1. only calculate sum(), avg(), min(), max(), median(), stdev()
      // 2. formula arguments only contain item beans
      // 3. only one item bean per argument
      parsedExp = parser.assignVariables(parser.parseScoreTokens(rob.getValue()), itemOrdinals);
    }
    if (parser.getErrors().length() > 0) {
      errs.append(parser.getErrors());
    } else {
      try {
        value = ScoreUtil.eval(parsedExp);
      } catch (ScoreException se) {
        logger.error(se.getMessage());
      }
      ItemDataType idt = ib.getDataType();
      if (value == null || value.length() == 0) {
        value = "";
        String exp = rob.getValue();
        exp = exp.replace("##", ",");
        errs.append("Result is empty in" + " " + exp + "; ");
        // errors.append(resexception.getString("result_is_empty_in") +
        // " " + exp + "; ");
      } else {
        value = this.getMathContextValue(value, ifm, idt, errs);
      }
      // idb.setStatus(Status.UNAVAILABLE);
      itemdata.put(ib.getId() + "_" + ordinal, value);
    }

    return value;
  }
Пример #3
0
 public EntityBean update(EntityBean eb) {
   ItemBean ib = (ItemBean) eb;
   HashMap variables = new HashMap();
   variables.put(new Integer(1), ib.getName());
   variables.put(new Integer(2), ib.getDescription());
   variables.put(new Integer(3), ib.getUnits());
   variables.put(new Integer(4), new Boolean(ib.isPhiStatus()));
   variables.put(new Integer(5), new Integer(ib.getItemDataTypeId()));
   variables.put(new Integer(6), new Integer(ib.getItemReferenceTypeId()));
   variables.put(new Integer(7), new Integer(ib.getStatus().getId()));
   variables.put(new Integer(8), new Integer(ib.getUpdaterId()));
   variables.put(new Integer(9), new Integer(ib.getId()));
   this.execute(digester.getQuery("update"), variables);
   return eb;
 }
  protected boolean writeToDB(
      ItemBean ib,
      ItemFormMetadataBean ifm,
      ItemDataBean idb,
      String exp,
      String value,
      StringBuffer err) {
    ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
    NumberFormat nf = NumberFormat.getInstance();

    if (idb == null) {
      idb = new ItemDataBean();
    }

    ItemDataType idt = ib.getDataType();
    if (value == null || value.length() == 0) {
      if (idb.isActive() && !"".equals(idb.getValue())) {
        idb.setValue("<erased>");
      } else {
        idb.setValue("");
      }
      err.append("Result is empty in" + " " + exp + "; ");
    } else {
      idb.setValue(this.getMathContextValue(value, ifm, idt, err));
    }

    idb.setStatus(Status.UNAVAILABLE);
    // idb.setNeedsRecalc(false);
    if (!idb.isActive()) {
      // will this need to change for double data entry?
      idb.setCreatedDate(new Date());
      idb.setOwner(ub);
      idb.setItemId(ib.getId());
      idb.setEventCRFId(ecb.getId());

      idb = (ItemDataBean) iddao.create(idb);
    } else {
      idb = (ItemDataBean) iddao.update(idb);
    }

    return idb.isActive();
  }
  /**
   * 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;
  }
  /**
   * Re-do calculations if funcs include changed item(s) and funcs are not included in the current
   * section. If calculation can not sucessfully redo, old value will be erased and "<erased>" will
   * be saved in database. <br>
   * The parameter 'itemdata' might be overwritten.
   *
   * @param itemGroupSizes
   * @param items
   * @param itemdata
   * @param oldItemdata
   * @param updatedData
   * @param sectionId
   * @return ArrayList<String> which records left_item_text of items who failed to be updated into
   *     database.
   */
  public ArrayList<String> redoCalculations(
      HashMap<String, ItemBean> items,
      HashMap<String, String> itemdata,
      TreeSet<String> changedItems,
      HashMap<Integer, TreeSet<Integer>> itemOrdinals,
      int sectionId) {
    ArrayList<String> updateFailedItems = new ArrayList<String>();
    if (itemdata == null) {
      logger.error("In ScoreCalculator redoCalculations(), itemdata is empty!");
      errors.add("In ScoreCalculator redoCalculations(), 'itemdata' map is empty!");
      return updateFailedItems;
    }
    if (changedItems == null) {
      logger.error("In ScoreCalculator redoCalculations(), 'changeItems' set is empty!");
      errors.add("In ScoreCalculator redoCalculations(), 'changeItems' set is empty!");
      return updateFailedItems;
    }
    ItemFormMetadataDAO ifmdao = new ItemFormMetadataDAO(sm.getDataSource());
    ItemDAO idao = new ItemDAO(sm.getDataSource());
    ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());

    NumberFormat nf = NumberFormat.getInstance();
    Parser parser = new Parser(items, itemdata);
    try {
      // for calculation type
      List<ItemFormMetadataBean> derivedItemList =
          ifmdao.findAllByCRFVersionIdAndResponseTypeId(
              ecb.getCRFVersionId(), ResponseType.CALCULATION.getId());
      if (derivedItemList.size() > 0) {
        Collections.sort(derivedItemList);
        for (ItemFormMetadataBean ifmb : derivedItemList) {
          if (ifmb.getSectionId() != sectionId) {
            ItemBean ib = (ItemBean) idao.findByPK(ifmb.getItemId());
            ResponseOptionBean rob = (ResponseOptionBean) ifmb.getResponseSet().getOptions().get(0);
            int groupsize = 1;
            if (itemOrdinals.containsKey(ib.getId())) {
              groupsize = itemOrdinals.get(ib.getId()).size();
            }
            String value = "";
            ArrayList<ScoreToken> parsedExp = new ArrayList<ScoreToken>();
            for (int i = 0; i < groupsize; ++i) {
              ItemDataBean idb =
                  iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb.getItemId(), ecb.getId(), i + 1);
              // is there any changed item
              Parser p = new Parser(items, itemdata);
              parsedExp = parser.parseScoreTokens(rob.getValue());
              if (p.isChanged(changedItems, parsedExp)) {
                StringBuffer err = new StringBuffer();
                parsedExp = parser.assignVariables(parsedExp, i + 1);
                // if parser has error and has been calculated
                // before, set "<erased>"
                if (parser.getErrors().length() > 0) {
                  err.append(parser.getErrors());
                  if (idb.isActive()) {
                    idb.setValue("<erased>");
                    idb.setStatus(Status.UNAVAILABLE);
                    idb = (ItemDataBean) iddao.update(idb);
                    if (!idb.isActive()) {
                      String key =
                          i + 1 > 1
                              ? ifmb.getLeftItemText() + "_" + (i + 1)
                              : ifmb.getLeftItemText();
                      updateFailedItems.add(key);
                    }
                  }
                  parser.setErrors(new StringBuffer());
                }
                // otherwise do calculation
                else {
                  try {
                    value = ScoreUtil.eval(parsedExp);
                  } catch (ScoreException se) {
                    logger.error(se.getMessage());
                  }
                  String exp = rob.getValue();
                  exp = exp.replace("##", ",");
                  if (writeToDB(ib, ifmb, idb, exp, value, err)) {
                    changedItems.add(ib.getName());
                    itemdata.put(ib.getId() + "_" + (i + 1), idb.getValue());
                  } else {
                    String key =
                        i + 1 > 1 ? ifmb.getLeftItemText() + "_" + (i + 1) : ifmb.getLeftItemText();
                    updateFailedItems.add(key);
                  }
                }
                if (err.length() > 0) {
                  String key =
                      i + 1 > 1 ? ifmb.getLeftItemText() + "_" + (i + 1) : ifmb.getLeftItemText();
                  errors.add("Item " + key + " contains calculation errors: " + err.toString());
                }
              }
            }
          }
        }
      }

      List<ItemFormMetadataBean> itemList =
          ifmdao.findAllByCRFVersionIdAndResponseTypeId(
              ecb.getCRFVersionId(), ResponseType.GROUP_CALCULATION.getId());
      if (itemList.size() > 0) {
        Collections.sort(itemList);
        for (ItemFormMetadataBean ifmb : itemList) {
          if (ifmb.getSectionId() != sectionId) {
            ItemBean ib = (ItemBean) idao.findByPK(ifmb.getItemId());
            ResponseOptionBean rob = (ResponseOptionBean) ifmb.getResponseSet().getOptions().get(0);
            String value = "";
            Parser p = new Parser(items, itemdata);
            ArrayList<ScoreToken> parsedExp = parser.parseScoreTokens(rob.getValue());
            if (p.isChanged(changedItems, parsedExp)) {
              StringBuffer err = new StringBuffer();
              parser.setErrors(err);
              parsedExp = parser.assignVariables(parsedExp, itemOrdinals);
              ItemDataBean idb =
                  iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb.getItemId(), ecb.getId(), 1);
              if (parser.getErrors().length() > 0) {
                err.append(parser.getErrors());
                if (idb.isActive()) {
                  idb.setValue("<erased>");
                  idb.setStatus(Status.UNAVAILABLE);
                  idb = (ItemDataBean) iddao.update(idb);
                  if (!idb.isActive()) {
                    updateFailedItems.add(ifmb.getLeftItemText());
                  }
                }
              } else {
                try {
                  value = ScoreUtil.eval(parsedExp);
                } catch (ScoreException se) {
                  logger.error(se.getMessage());
                }
                String exp = rob.getValue();
                exp = exp.replace("##", ",");
                if (writeToDB(ib, ifmb, idb, exp, value, err)) {
                  changedItems.add(ib.getName());
                  itemdata.put(ib.getId() + "_" + idb.getOrdinal(), idb.getValue());
                } else {
                  updateFailedItems.add(ifmb.getLeftItemText());
                }
              }
              if (err.length() > 0) {
                errors.add(
                    "Item "
                        + ifmb.getLeftItemText()
                        + " contains calculation errors: "
                        + err.toString());
              }
            }
          }
        }
      }
    } catch (OpenClinicaException e) {
      logger.error(e.getMessage());
    }

    return updateFailedItems;
  }