Search in sources :

Example 16 with ResponseOptionBean

use of org.akaza.openclinica.bean.submit.ResponseOptionBean in project OpenClinica by OpenClinica.

the class Validator method isInResponseSetCommaSeperated.

protected boolean isInResponseSetCommaSeperated(String fieldName, ResponseSetBean set, boolean multValues) {
    // prep work - makes checking for a value in the set very fast
    HashMap values = new HashMap();
    ArrayList options = set.getOptions();
    for (int i = 0; i < options.size(); i++) {
        ResponseOptionBean rob = (ResponseOptionBean) options.get(i);
        values.put(rob.getValue(), Boolean.TRUE);
    }
    String[] fieldValues;
    if (multValues) {
        //fieldValues = request.getParameterValues(fieldName);
        fieldValues = request.getParameter(fieldName).split(",");
    } else {
        fieldValues = new String[1];
        String fieldValue = getFieldValue(fieldName);
        fieldValues[0] = fieldValue == null ? "" : fieldValue;
    }
    // trivially, in the response set
    if (fieldValues == null) {
        return true;
    }
    for (String value : fieldValues) {
        // trivially a member of the response set, it's okay
        if (value == null) {
            continue;
        }
        if (!values.containsKey(value)) {
            return false;
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResponseOptionBean(org.akaza.openclinica.bean.submit.ResponseOptionBean)

Example 17 with ResponseOptionBean

use of org.akaza.openclinica.bean.submit.ResponseOptionBean in project OpenClinica by OpenClinica.

the class Validator method isInResponseSet.

protected boolean isInResponseSet(String fieldName, ResponseSetBean set, boolean multValues) {
    // prep work - makes checking for a value in the set very fast
    HashMap values = new HashMap();
    ArrayList options = set.getOptions();
    for (int i = 0; i < options.size(); i++) {
        ResponseOptionBean rob = (ResponseOptionBean) options.get(i);
        values.put(rob.getValue(), Boolean.TRUE);
    }
    String[] fieldValues;
    if (multValues) {
        fieldValues = request.getParameterValues(fieldName);
    } else {
        fieldValues = new String[1];
        String fieldValue = getFieldValue(fieldName);
        fieldValues[0] = fieldValue == null ? "" : fieldValue;
    }
    // trivially, in the response set
    if (fieldValues == null) {
        return true;
    }
    for (String value : fieldValues) {
        // trivially a member of the response set, it's okay
        if (value == null) {
            continue;
        }
        if (!values.containsKey(value)) {
            return false;
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResponseOptionBean(org.akaza.openclinica.bean.submit.ResponseOptionBean)

Example 18 with ResponseOptionBean

use of org.akaza.openclinica.bean.submit.ResponseOptionBean in project OpenClinica by OpenClinica.

the class InsertActionValidator method matchValueWithManyOptions.

@SuppressWarnings("unchecked")
private String matchValueWithManyOptions(String value, List<ResponseOptionBean> options) {
    String returnedValue = null;
    String entireOptions = "";
    String[] simValues = value.split(",");
    String simValue = value.replace(",", "");
    simValue = simValue.replace(" ", "");
    boolean checkComplete = true;
    if (!options.isEmpty()) {
        for (ResponseOptionBean responseOptionBean : options) {
            entireOptions += responseOptionBean.getValue();
        }
        // remove spaces, since they are causing problems:
        entireOptions = entireOptions.replace(" ", "");
        ArrayList nullValues = getEventDefinitionCRFBean().getNullValuesList();
        for (Object nullValue : nullValues) {
            NullValue nullValueTerm = (NullValue) nullValue;
            entireOptions += nullValueTerm.getName();
        }
        for (String sim : simValues) {
            sim = sim.replace(" ", "");
            // Pattern.matches(entireOptions,sim);
            checkComplete = entireOptions.contains(sim);
            if (!checkComplete) {
                return returnedValue;
            }
        }
    }
    return value;
}
Also used : NullValue(org.akaza.openclinica.bean.core.NullValue) ArrayList(java.util.ArrayList) ResponseOptionBean(org.akaza.openclinica.bean.submit.ResponseOptionBean)

Example 19 with ResponseOptionBean

use of org.akaza.openclinica.bean.submit.ResponseOptionBean in project OpenClinica by OpenClinica.

the class ScoreCalculator method getMathContextValue.

protected String getMathContextValue(String value, ItemFormMetadataBean ifm, ItemDataType idt, StringBuffer errorMessage) {
    ResponseOptionBean rob = (ResponseOptionBean) ifm.getResponseSet().getOptions().get(0);
    String widthDecimal = ifm.getWidthDecimal();
    int width = parseWidth(widthDecimal);
    int decimal = parseDecimal(widthDecimal);
    NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
    if (idt.equals(ItemDataType.INTEGER)) {
        try {
            Double d = nf.parse(value).doubleValue();
            int p = 0;
            if (width > 0) {
                p = d >= 0 ? width : width - 1;
            } else {
                p = BigDecimal.valueOf(d).precision();
            }
            MathContext mc = new MathContext(p, RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP));
            value = (new BigDecimal(d, mc)).setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString();
        } catch (ParseException e) {
            logger.error("Number was expected in " + rob.getValue() + " : " + value);
            String exp = rob.getValue();
            exp = exp.replace("##", ",");
            errorMessage.append("Number was expected in" + " " + exp + " : " + value + "; ");
            // errors.append(resexception.getString("number_expected_in")
            // + " " + exp + ": " + value + "; ");
            value = "";
        }
    } else if (idt.equals(ItemDataType.REAL)) {
        try {
            Double d = nf.parse(value).doubleValue();
            // there should be no width specify for calculation item
            int p = BigDecimal.valueOf(d).precision();
            // set default scale as 4
            int scale = decimal > 0 ? decimal : DEFAULT_DECIMAL;
            MathContext mc = new MathContext(p, RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP));
            value = (new BigDecimal(d, mc)).setScale(scale, BigDecimal.ROUND_HALF_UP).toPlainString();
        } catch (Exception ee) {
            String exp = rob.getValue();
            exp = exp.replace("##", ",");
            logger.error("Number was expected in " + exp + " : " + value);
            errorMessage.append("Number was expected in" + " " + exp + " : " + value + "; ");
            // errors.append(resexception.getString("number_expected_in")
            // + " " + exp + ": " + value + "; ");
            value = "";
        }
    }
    return value;
}
Also used : ParseException(java.text.ParseException) ResponseOptionBean(org.akaza.openclinica.bean.submit.ResponseOptionBean) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException) ParseException(java.text.ParseException) ScoreException(org.akaza.openclinica.exception.ScoreException) NumberFormat(java.text.NumberFormat)

Example 20 with ResponseOptionBean

use of org.akaza.openclinica.bean.submit.ResponseOptionBean in project OpenClinica by OpenClinica.

the class ScoreCalculator method doCalculation.

/*
     * Perform all calculations in a CRFVersion. The parameter 'itemdata' might
     * be overwritten.
     * 
     * @param calcItemGroupSizes @param items @param itemdata @param
     * itemGroupSizes @param databaseErrors @return ArrayList<String> which
     * records left_item_text of items who failed to be updated into database.
     * 
     * 
     * //this method has not been fully tested yet
     * 
     * 
     * public ArrayList<String> doCalculations( HashMap<String,ItemBean>
     * items, HashMap<String,String> itemdata, HashMap<Integer,TreeSet<Integer>>
     * itemOrdinals) { ArrayList<String> updateFailedItems = new ArrayList<String>();
     * if(itemdata==null) { //here, actually only record errors but doesn't
     * return errors. logger.error("In scoreCalculator doCalculations(), items
     * are empty!"); errors.add("Calculation cannot be started because needed
     * items are 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) { ItemBean ib =
     * (ItemBean)idao.findByPK(ifmb.getItemId()); ResponseOptionBean rob =
     * (ResponseOptionBean)ifmb.getResponseSet().getOptions().get(0); //YW,
     * 1-16-2008, << enable: 1. evaluate combination of expression and
     * functions 2. scoring for group items. //int groupsize =
     * iddao.getMaxOrdinalForGroup(ecb,
     * (SectionBean)sdao.findByPK(ifmb.getSectionId()), //
     * (ItemGroupBean)igdao.findByName(ifmb.getGroupLabel())); int groupsize =
     * 1; if(itemOrdinals.containsKey(ib.getId())) { groupsize =
     * (itemOrdinals.get(ib.getId())).size(); } String value = ""; String
     * parsedExp = ""; for(int i=0; i<groupsize; ++i) { ItemDataBean idb =
     * (ItemDataBean)iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb
     * .getItemId(), ecb.getId(), i+1); StringBuffer err = new StringBuffer();
     * parsedExp = parser.parse(rob.getValue(), i+1);
     * if(parser.getErrors().length()>0) { err.append(parser.getErrors());
     * parser.setErrors(new StringBuffer()); } else { value =
     * ScoreUtil.eval(parsedExp, err); //YW >> String exp = rob.getValue(); exp =
     * exp.replace("##", ","); if(writeToDB(ib,idb,exp,value,err)) {
     * 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()); } } } }
     * 
     * //YW, 1-16-2008, for group-calculation type. Current restrictions: //1.
     * an item with group-calculation type is not repeatable. //2. only
     * calculate sum(), avg(), min(), max(), median(), stdev() //3. formula
     * arguments only contain item beans //4. only one item bean per argument
     * List<ItemFormMetadataBean> itemList =
     * ifmdao.findAllByCRFVersionIdAndResponseTypeId( ecb.getCRFVersionId(),
     * ResponseType.GROUP_CALCULATION.getId()); if(itemList.size()>0) {
     * Collections.sort(itemList); for (ItemFormMetadataBean ifmb: itemList) {
     * ItemBean ib = (ItemBean)idao.findByPK(ifmb.getItemId());
     * ResponseOptionBean rob =
     * (ResponseOptionBean)ifmb.getResponseSet().getOptions().get(0);
     * StringBuffer err = new StringBuffer(); parser.setErrors(err); String
     * parsedExp = parser.parse(rob.getValue(),itemOrdinals); String value = "";
     * if(parser.getErrors().length()>0) { err.append(parser.getErrors()); }else {
     * value = ScoreUtil.eval(parsedExp,err);
     * 
     * ItemDataBean idb =
     * (ItemDataBean)iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb
     * .getItemId(), ecb.getId(), 1); String exp = rob.getValue(); exp =
     * exp.replace("##", ","); if(writeToDB(ib,idb,exp,value,err)) {
     * 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()); }
     * //ecb.setNeedsRecalc(false); //EventCRFDAO ecdao = new
     * EventCRFDAO(sm.getDataSource()); //ecb = ecdao.update(ecb);
     * 
     * return updateFailedItems; }
     */
/**
     * 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;
}
Also used : ScoreException(org.akaza.openclinica.exception.ScoreException) DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) ItemBean(org.akaza.openclinica.bean.submit.ItemBean) ItemDataType(org.akaza.openclinica.bean.core.ItemDataType) ArrayList(java.util.ArrayList) ResponseOptionBean(org.akaza.openclinica.bean.submit.ResponseOptionBean) ItemFormMetadataBean(org.akaza.openclinica.bean.submit.ItemFormMetadataBean) NumberFormat(java.text.NumberFormat)

Aggregations

ResponseOptionBean (org.akaza.openclinica.bean.submit.ResponseOptionBean)31 ArrayList (java.util.ArrayList)20 DisplayItemBean (org.akaza.openclinica.bean.submit.DisplayItemBean)9 Element (org.jdom.Element)8 ItemBean (org.akaza.openclinica.bean.submit.ItemBean)7 ItemDataBean (org.akaza.openclinica.bean.submit.ItemDataBean)7 ItemFormMetadataBean (org.akaza.openclinica.bean.submit.ItemFormMetadataBean)7 ItemDataDAO (org.akaza.openclinica.dao.submit.ItemDataDAO)5 HashMap (java.util.HashMap)4 NumberFormat (java.text.NumberFormat)3 Iterator (java.util.Iterator)3 NullValue (org.akaza.openclinica.bean.core.NullValue)3 DisplayItemGroupBean (org.akaza.openclinica.bean.submit.DisplayItemGroupBean)3 DisplayItemWithGroupBean (org.akaza.openclinica.bean.submit.DisplayItemWithGroupBean)3 EventCRFBean (org.akaza.openclinica.bean.submit.EventCRFBean)3 Random (java.util.Random)2 HttpSession (javax.servlet.http.HttpSession)2 DiscrepancyNoteBean (org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean)2 ResponseSetBean (org.akaza.openclinica.bean.submit.ResponseSetBean)2 DiscrepancyNoteDAO (org.akaza.openclinica.dao.managestudy.DiscrepancyNoteDAO)2