Search in sources :

Example 91 with ItemFormMetadataBean

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

the class ImportCRFDataService method attachValidator.

private void attachValidator(DisplayItemBean displayItemBean, ImportHelper importHelper, DiscrepancyValidator discValidator, HashMap<String, String> hardv, javax.servlet.http.HttpServletRequest request, String eventCRFRepeatKey, String studySubjectOID) throws OpenClinicaException {
    org.akaza.openclinica.bean.core.ResponseType rt = displayItemBean.getMetadata().getResponseSet().getResponseType();
    String itemOid = displayItemBean.getItem().getOid() + "_" + eventCRFRepeatKey + "_" + displayItemBean.getData().getOrdinal() + "_" + studySubjectOID;
    if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.TEXT) || rt.equals(org.akaza.openclinica.bean.core.ResponseType.TEXTAREA)) {
        ItemFormMetadataBean ifm = displayItemBean.getMetadata();
        String widthDecimal = ifm.getWidthDecimal();
        // mm/dd/yyyy
        if (displayItemBean.getItem().getDataType().equals(ItemDataType.DATE)) {
            // pass it if it is blank, tbh
            if (!"".equals(displayItemBean.getData().getValue())) {
                String dateValue = displayItemBean.getData().getValue();
                SimpleDateFormat sdf_sqldate = new SimpleDateFormat("yyyy-MM-dd");
                sdf_sqldate.setLenient(false);
                try {
                    // htaycher: database exspects format YYYY-MM-DD
                    Date originalDate = sdf_sqldate.parse(dateValue);
                    // String replacementValue = new SimpleDateFormat("MM/dd/yyyy").format(originalDate);
                    String replacementValue = new SimpleDateFormat("yyyy-MM-dd").format(originalDate);
                    displayItemBean.getData().setValue(replacementValue);
                } catch (ParseException pe1) {
                    // next version; fail if it does not pass iso 8601
                    MessageFormat mf = new MessageFormat("");
                    mf.applyPattern(respage.getString("you_have_a_date_value_which_is_not"));
                    Object[] arguments = { displayItemBean.getItem().getOid() };
                    hardv.put(itemOid, mf.format(arguments));
                }
            }
        } else if (displayItemBean.getItem().getDataType().equals(ItemDataType.ST)) {
            int width = Validator.parseWidth(widthDecimal);
            if (width > 0 && displayItemBean.getData().getValue().length() > width) {
                hardv.put(itemOid, "This value exceeds required width=" + width);
            }
        } else // what if it's a number? should be only numbers
        if (displayItemBean.getItem().getDataType().equals(ItemDataType.INTEGER)) {
            try {
                Integer testInt = new Integer(displayItemBean.getData().getValue());
                int width = Validator.parseWidth(widthDecimal);
                if (width > 0 && displayItemBean.getData().getValue().length() > width) {
                    hardv.put(itemOid, "This value exceeds required width=" + width);
                }
            // now, didn't check decimal for testInt.
            } catch (Exception e) {
                // pass if blank, tbh 07/2010
                if (!"".equals(displayItemBean.getData().getValue())) {
                    hardv.put(itemOid, "This value is not an integer.");
                }
            }
        } else // what if it's a float? should be only numbers
        if (displayItemBean.getItem().getDataType().equals(ItemDataType.REAL)) {
            try {
                Float testFloat = new Float(displayItemBean.getData().getValue());
                int width = Validator.parseWidth(widthDecimal);
                if (width > 0 && displayItemBean.getData().getValue().length() > width) {
                    hardv.put(itemOid, "This value exceeds required width=" + width);
                }
                int decimal = Validator.parseDecimal(widthDecimal);
                if (decimal > 0 && BigDecimal.valueOf(new Double(displayItemBean.getData().getValue()).doubleValue()).scale() > decimal) {
                    hardv.put(itemOid, "This value exceeds required decimal=" + decimal);
                }
            } catch (Exception ee) {
                // pass if blank, tbh
                if (!"".equals(displayItemBean.getData().getValue())) {
                    hardv.put(itemOid, "This value is not a real number.");
                }
            }
        }
        // what if it's a phone number? how often does that happen?
        request.setAttribute(itemOid, displayItemBean.getData().getValue());
        displayItemBean = importHelper.validateDisplayItemBeanText(discValidator, displayItemBean, itemOid);
    // errors = v.validate();
    } else if (rt.equals(ResponseType.CALCULATION) || rt.equals(ResponseType.GROUP_CALCULATION)) {
        if (displayItemBean.getItem().getDataType().equals(ItemDataType.REAL)) {
            ItemFormMetadataBean ifm = displayItemBean.getMetadata();
            String widthDecimal = ifm.getWidthDecimal();
            int decimal = Validator.parseDecimal(widthDecimal);
            if (decimal > 0) {
                try {
                    Double d = new Double(displayItemBean.getData().getValue());
                    if (BigDecimal.valueOf(d).scale() > decimal) {
                        hardv.put(itemOid, "This value exceeds required decimal=" + decimal);
                    }
                } catch (Exception e) {
                    if (!"".equals(displayItemBean.getData().getValue())) {
                        hardv.put(itemOid, "This value is not a real number.");
                    }
                }
            }
        }
    } else if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.RADIO) || rt.equals(org.akaza.openclinica.bean.core.ResponseType.SELECT)) {
        // logger.info(itemOid + "is a RADIO or
        // a SELECT ");
        // adding a new hard edit check here; response_option mismatch
        String theValue = matchValueWithOptions(displayItemBean, displayItemBean.getData().getValue(), displayItemBean.getMetadata().getResponseSet().getOptions());
        request.setAttribute(itemOid, theValue);
        logger.debug("        found the value for radio/single: " + theValue);
        if (theValue == null && displayItemBean.getData().getValue() != null && !displayItemBean.getData().getValue().isEmpty()) {
            // fail it here
            logger.debug("-- theValue was NULL, the real value was " + displayItemBean.getData().getValue());
            hardv.put(itemOid, "This is not in the correct response set.");
        // throw new OpenClinicaException("One of your items did not
        // have the correct response set. Please review Item OID "
        // + displayItemBean.getItem().getOid() + " repeat key " +
        // displayItemBean.getData().getOrdinal(), "");
        }
        displayItemBean = importHelper.validateDisplayItemBeanSingleCV(discValidator, displayItemBean, itemOid);
    // errors = v.validate();
    } else if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.CHECKBOX) || rt.equals(org.akaza.openclinica.bean.core.ResponseType.SELECTMULTI)) {
        // logger.info(itemOid + "is a CHECKBOX
        // or a SELECTMULTI ");
        String theValue = matchValueWithManyOptions(displayItemBean, displayItemBean.getData().getValue(), displayItemBean.getMetadata().getResponseSet().getOptions());
        request.setAttribute(itemOid, theValue);
        // logger.debug("        found the value for checkbx/multi: " + theValue);
        if (theValue == null && displayItemBean.getData().getValue() != null && !displayItemBean.getData().getValue().isEmpty()) {
            // fail it here? found an 0,1 in the place of a NULL
            // logger.debug("--  theValue was NULL, the real value was " + displayItemBean.getData().getValue());
            hardv.put(itemOid, "This is not in the correct response set.");
        }
        displayItemBean = importHelper.validateDisplayItemBeanMultipleCV(discValidator, displayItemBean, itemOid);
    // errors = v.validate();
    }
}
Also used : MessageFormat(java.text.MessageFormat) Date(java.util.Date) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException) ParseException(java.text.ParseException) ResponseType(org.akaza.openclinica.bean.core.ResponseType) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ItemFormMetadataBean(org.akaza.openclinica.bean.submit.ItemFormMetadataBean)

Example 92 with ItemFormMetadataBean

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

the class ImportHelper method validateDisplayItemBeanSingleCV.

/**
 * Peform validation on a item which has a RADIO or SINGLESELECTresponse
 * type. This function checks that the input isn't blank, and that its value
 * comes from the controlled vocabulary (ResponseSetBean) in the
 * DisplayItemBean.
 *
 * @param v
 *            The Validator to add validations to.
 * @param dib
 *            The DisplayItemBean to validate.
 * @return The DisplayItemBean which is validated.
 */
public DisplayItemBean validateDisplayItemBeanSingleCV(DiscrepancyValidator v, DisplayItemBean dib, String inputName) {
    if (StringUtil.isBlank(inputName)) {
        inputName = getInputName(dib);
    }
    ItemFormMetadataBean ibMeta = dib.getMetadata();
    ItemDataBean idb = dib.getData();
    if (StringUtil.isBlank(idb.getValue())) {
        if (ibMeta.isRequired()) {
            v.addValidation(inputName, Validator.IS_REQUIRED);
        }
    } else {
    // commented out because it should be a hard edit check, tbh 05/2008
    // v.addValidation(inputName,
    // Validator.IN_RESPONSE_SET_SINGLE_VALUE,
    // dib.getMetadata().getResponseSet());
    }
    return dib;
}
Also used : ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) ItemFormMetadataBean(org.akaza.openclinica.bean.submit.ItemFormMetadataBean)

Example 93 with ItemFormMetadataBean

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

the class ImportHelper method validateDisplayItemBeanText.

/**
 * Peform validation on a item which has a TEXT or TEXTAREA response type.
 * If the item has a null value, it's automatically validated. Otherwise,
 * it's checked against its data type.
 *
 * @param v
 *            The Validator to add validations to.
 * @param dib
 *            The DisplayItemBean to validate.
 * @return The DisplayItemBean which is validated.
 */
public DisplayItemBean validateDisplayItemBeanText(DiscrepancyValidator discValidator, DisplayItemBean dib, String inputName) {
    if (StringUtil.isBlank(inputName)) {
        // for single items
        inputName = getInputName(dib);
    }
    ItemBean ib = dib.getItem();
    ItemFormMetadataBean ibMeta = dib.getMetadata();
    ItemDataType idt = ib.getDataType();
    ItemDataBean idb = dib.getData();
    boolean isNull = false;
    if (!isNull) {
        if (StringUtil.isBlank(idb.getValue())) {
            // check required first
            if (ibMeta.isRequired()) {
                discValidator.addValidation(inputName, Validator.IS_REQUIRED);
            }
        } else {
            if (idt.equals(ItemDataType.ST)) {
                // a string's size could be more than 255, which is more
                // than
                // the db field length
                discValidator.addValidation(inputName, Validator.LENGTH_NUMERIC_COMPARISON, NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO, 3999);
            } else if (idt.equals(ItemDataType.INTEGER)) {
            // hard edit check, will comment out for now, tbh 05/2008
            // v.addValidation(inputName, Validator.IS_AN_INTEGER);
            // v.alwaysExecuteLastValidation(inputName);
            } else if (idt.equals(ItemDataType.REAL)) {
            // hard edit check, will comment out for now, tbh 05/08
            // v.addValidation(inputName, Validator.IS_A_NUMBER);
            // v.alwaysExecuteLastValidation(inputName);
            } else if (idt.equals(ItemDataType.BL)) {
            // there is no validation here since this data type is
            // explicitly
            // allowed to be null
            // if the string input for this field parses to a non-zero
            // number, the
            // value will be true; otherwise, 0
            } else if (idt.equals(ItemDataType.BN)) {
            } else if (idt.equals(ItemDataType.SET)) {
                // v.addValidation(inputName, Validator.NO_BLANKS_SET);
                discValidator.addValidation(inputName, Validator.IN_RESPONSE_SET_SINGLE_VALUE, dib.getMetadata().getResponseSet());
            } else if (idt.equals(ItemDataType.DATE)) {
            // hard edit check, will comment out for now, tbh 05/08
            // v.addValidation(inputName, Validator.IS_A_DATE);
            // v.alwaysExecuteLastValidation(inputName);
            }
            String customValidationString = dib.getMetadata().getRegexp();
            if (!StringUtil.isBlank(customValidationString)) {
                Validation customValidation = null;
                if (customValidationString.startsWith("func:")) {
                    try {
                        customValidation = Validator.processCRFValidationFunction(customValidationString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else if (customValidationString.startsWith("regexp:")) {
                    try {
                        customValidation = Validator.processCRFValidationRegex(customValidationString);
                    } catch (Exception e) {
                    }
                }
                if (customValidation != null) {
                    customValidation.setErrorMessage(dib.getMetadata().getRegexpErrorMsg());
                    discValidator.addValidation(inputName, customValidation);
                }
            }
        }
    }
    return dib;
}
Also used : Validation(org.akaza.openclinica.control.form.Validation) DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) ItemBean(org.akaza.openclinica.bean.submit.ItemBean) ItemDataType(org.akaza.openclinica.bean.core.ItemDataType) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) ItemFormMetadataBean(org.akaza.openclinica.bean.submit.ItemFormMetadataBean)

Aggregations

ItemFormMetadataBean (org.akaza.openclinica.bean.submit.ItemFormMetadataBean)93 ArrayList (java.util.ArrayList)58 HashMap (java.util.HashMap)36 ItemBean (org.akaza.openclinica.bean.submit.ItemBean)36 DisplayItemBean (org.akaza.openclinica.bean.submit.DisplayItemBean)28 ItemDataBean (org.akaza.openclinica.bean.submit.ItemDataBean)26 ItemFormMetadataDAO (org.akaza.openclinica.dao.submit.ItemFormMetadataDAO)25 DynamicsItemFormMetadataBean (org.akaza.openclinica.domain.crfdata.DynamicsItemFormMetadataBean)25 Iterator (java.util.Iterator)21 ItemDAO (org.akaza.openclinica.dao.submit.ItemDAO)18 ItemGroupBean (org.akaza.openclinica.bean.submit.ItemGroupBean)16 SectionBean (org.akaza.openclinica.bean.submit.SectionBean)16 EventCRFBean (org.akaza.openclinica.bean.submit.EventCRFBean)14 ItemGroupMetadataBean (org.akaza.openclinica.bean.submit.ItemGroupMetadataBean)13 DisplayItemGroupBean (org.akaza.openclinica.bean.submit.DisplayItemGroupBean)12 ItemDataDAO (org.akaza.openclinica.dao.submit.ItemDataDAO)12 CRFBean (org.akaza.openclinica.bean.admin.CRFBean)9 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)9 CRFVersionBean (org.akaza.openclinica.bean.submit.CRFVersionBean)8 ResponseSetBean (org.akaza.openclinica.bean.submit.ResponseSetBean)8