Search in sources :

Example 1 with ExpressionProcessor

use of org.akaza.openclinica.domain.rule.expression.ExpressionProcessor in project OpenClinica by OpenClinica.

the class RulesPostImportContainerService method isStratificationExpressionValid.

public boolean isStratificationExpressionValid(ExpressionBean expBean, AuditableBeanWrapper<RuleSetBean> beanWrapper) {
    boolean isValid = true;
    ExpressionBean expressionBean = isExpressionValid(expBean, beanWrapper);
    ExpressionObjectWrapper eow = new ExpressionObjectWrapper(ds, currentStudy, expressionBean, ExpressionObjectWrapper.CONTEXT_TARGET);
    ExpressionProcessor ep = ExpressionProcessorFactory.createExpressionProcessor(eow);
    ep.setRespage(respage);
    // String errorString = ep.isRuleExpressionValid();
    String errorString = ep.isRuleAssignmentExpressionValid();
    if (errorString != null) {
        beanWrapper.error(errorString);
        isValid = false;
    }
    return isValid;
}
Also used : ExpressionProcessor(org.akaza.openclinica.domain.rule.expression.ExpressionProcessor) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) ExpressionBean(org.akaza.openclinica.domain.rule.expression.ExpressionBean)

Example 2 with ExpressionProcessor

use of org.akaza.openclinica.domain.rule.expression.ExpressionProcessor in project OpenClinica by OpenClinica.

the class EventActionValidator method isEventActionValueExpressionValid.

private boolean isEventActionValueExpressionValid(PropertyBean property, AuditableBeanWrapper<RuleSetBean> ruleSetBeanWrapper) {
    StudyDAO studyDAO = new StudyDAO<String, ArrayList>(getDataSource());
    StudyBean study = (StudyBean) studyDAO.findByPK(ruleSetBeanWrapper.getAuditableBean().getStudyId());
    ExpressionBean expressionBean = isExpressionValid(property.getValueExpression(), ruleSetBeanWrapper);
    ExpressionObjectWrapper eow = new ExpressionObjectWrapper(dataSource, study, expressionBean, ruleSetBeanWrapper.getAuditableBean(), ExpressionObjectWrapper.CONTEXT_VALUE_EXPRESSION);
    ExpressionProcessor ep = ExpressionProcessorFactory.createExpressionProcessor(eow);
    ep.setRespage(respage);
    String errorString = ep.isRuleExpressionValid();
    if (errorString != null) {
        ruleSetBeanWrapper.error(errorString);
        return false;
    }
    // Verify expression generates a valid date string.
    try {
        SimpleDateFormat df = new SimpleDateFormat(VALUE_EXPRESSION_DATE_FORMAT);
        df.setLenient(false);
        String valueExpression = ep.testEvaluateExpression();
        Date valueExpressionDate = df.parse(valueExpression);
        // date that was entered, if it's not, we assume that the date is invalid
        if (!df.format(valueExpressionDate).equals(valueExpression)) {
            return false;
        }
    } catch (ParseException ex) {
        return false;
    }
    return true;
}
Also used : ExpressionProcessor(org.akaza.openclinica.domain.rule.expression.ExpressionProcessor) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) ParseException(java.text.ParseException) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) SimpleDateFormat(java.text.SimpleDateFormat) ExpressionBean(org.akaza.openclinica.domain.rule.expression.ExpressionBean) Date(java.util.Date)

Example 3 with ExpressionProcessor

use of org.akaza.openclinica.domain.rule.expression.ExpressionProcessor in project OpenClinica by OpenClinica.

the class RulesPostImportContainerService method isRuleExpressionValid.

private boolean isRuleExpressionValid(AuditableBeanWrapper<RuleBean> ruleBeanWrapper, RuleSetBean ruleSet) {
    boolean isValid = true;
    ExpressionBean expressionBean = isExpressionValid(ruleBeanWrapper.getAuditableBean().getExpression(), ruleBeanWrapper);
    ExpressionObjectWrapper eow = new ExpressionObjectWrapper(ds, currentStudy, expressionBean, ruleSet, ExpressionObjectWrapper.CONTEXT_EXPRESSION);
    ExpressionProcessor ep = ExpressionProcessorFactory.createExpressionProcessor(eow);
    ep.setRespage(respage);
    String errorString = ep.isRuleExpressionValid();
    if (errorString != null) {
        ruleBeanWrapper.error(errorString);
        isValid = false;
    }
    return isValid;
}
Also used : ExpressionProcessor(org.akaza.openclinica.domain.rule.expression.ExpressionProcessor) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) ExpressionBean(org.akaza.openclinica.domain.rule.expression.ExpressionBean)

Example 4 with ExpressionProcessor

use of org.akaza.openclinica.domain.rule.expression.ExpressionProcessor in project OpenClinica by OpenClinica.

the class RulesPostImportContainerService method isRuleSetExpressionValid.

private boolean isRuleSetExpressionValid(AuditableBeanWrapper<RuleSetBean> beanWrapper) {
    boolean isValid = true;
    ExpressionBean expressionBean = isExpressionValid(beanWrapper.getAuditableBean().getTarget(), beanWrapper);
    ExpressionObjectWrapper eow = new ExpressionObjectWrapper(ds, currentStudy, expressionBean, ExpressionObjectWrapper.CONTEXT_TARGET);
    ExpressionProcessor ep = ExpressionProcessorFactory.createExpressionProcessor(eow);
    ep.setRespage(respage);
    String errorString = ep.isRuleAssignmentExpressionValid();
    if (errorString != null) {
        beanWrapper.error(errorString);
        isValid = false;
    }
    return isValid;
}
Also used : ExpressionProcessor(org.akaza.openclinica.domain.rule.expression.ExpressionProcessor) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) ExpressionBean(org.akaza.openclinica.domain.rule.expression.ExpressionBean)

Example 5 with ExpressionProcessor

use of org.akaza.openclinica.domain.rule.expression.ExpressionProcessor in project OpenClinica by OpenClinica.

the class RuleController method create.

@RequestMapping(value = "/studies/{study}/validateAndTestRule", method = RequestMethod.POST)
@ResponseBody
public org.openclinica.ns.rules_test.v31.RulesTest create(@RequestBody org.openclinica.ns.rules_test.v31.RulesTest ruleTest, Model model, HttpSession session, @PathVariable("study") String studyOid) throws Exception {
    ResourceBundleProvider.updateLocale(new Locale("en_US"));
    RulesPostImportContainer rpic = mapRulesToRulesPostImportContainer(ruleTest.getRules());
    StudyDAO studyDao = new StudyDAO(dataSource);
    StudyBean currentStudy = studyDao.findByOid(studyOid);
    UserAccountBean userAccount = getUserAccount();
    mayProceed(userAccount, currentStudy);
    getRulePostImportContainerService(currentStudy, userAccount);
    rpic = getRulePostImportContainerService(currentStudy, userAccount).validateRuleDefs(rpic);
    rpic = getRulePostImportContainerService(currentStudy, userAccount).validateRuleSetDefs(rpic);
    Response response = new Response();
    response.setValid(Boolean.TRUE);
    if (rpic.getInValidRuleDefs().size() > 0 || rpic.getInValidRuleSetDefs().size() > 0) {
        response.setValid(Boolean.FALSE);
        for (AuditableBeanWrapper<RuleBean> beanWrapper : rpic.getInValidRuleDefs()) {
            for (String error : beanWrapper.getImportErrors()) {
                org.openclinica.ns.response.v31.MessagesType messageType = new MessagesType();
                messageType.setMessage(error);
                response.getMessages().add(messageType);
            }
        }
        for (AuditableBeanWrapper<RuleSetBean> beanWrapper : rpic.getInValidRuleSetDefs()) {
            for (String error : beanWrapper.getImportErrors()) {
                org.openclinica.ns.response.v31.MessagesType messageType = new MessagesType();
                messageType.setMessage(error);
                response.getMessages().add(messageType);
            }
        }
    }
    HashMap<String, String> p = new HashMap<String, String>();
    for (ParameterType parameterType : ruleTest.getParameters()) {
        p.put(parameterType.getKey(), parameterType.getValue());
    }
    ExpressionObjectWrapper eow = new ExpressionObjectWrapper(dataSource, currentStudy, rpic.getRuleDefs().get(0).getExpression(), rpic.getRuleSets().get(0));
    ExpressionProcessor ep = ExpressionProcessorFactory.createExpressionProcessor(eow);
    // Run expression with populated HashMap
    DateTime start = new DateTime();
    HashMap<String, String> result = ep.testEvaluateExpression(p);
    DateTime end = new DateTime();
    Duration dur = new Duration(start, end);
    PeriodFormatter yearsAndMonths = new PeriodFormatterBuilder().printZeroAlways().appendSecondsWithMillis().appendSuffix(" second", " seconds").toFormatter();
    yearsAndMonths.print(dur.toPeriod());
    // Run expression with empty HashMap to check rule validity, because
    // using illegal test values will cause invalidity
    HashMap<String, String> k = new HashMap<String, String>();
    HashMap<String, String> theResult = ep.testEvaluateExpression(k);
    ruleTest.getParameters().clear();
    for (Map.Entry<String, String> entry : result.entrySet()) {
        ParameterType parameterType = new ParameterType();
        parameterType.setKey(entry.getKey());
        parameterType.setValue(entry.getValue());
        ruleTest.getParameters().add(parameterType);
    }
    // if (theResult.get("ruleValidation").equals("rule_valid") && result.get("ruleValidation").equals("rule_invalid")) {
    // result.put("ruleValidation", "rule_valid");
    // result.put("ruleEvaluatesTo", resword.getString("test_rules_rule_fail") + " " +
    // result.get("ruleValidationFailMessage"));
    // result.remove("ruleValidationFailMessage");
    // }
    // Put on screen
    // request.setAttribute("duration", yearsAndMonths.print(dur.toPeriod()));
    RulesTestMessagesType messageType = new RulesTestMessagesType();
    messageType.setKey("duration");
    messageType.setValue(yearsAndMonths.print(dur.toPeriod()));
    ruleTest.getRulesTestMessages().add(messageType);
    return ruleTest;
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) DateTime(org.joda.time.DateTime) ExpressionProcessor(org.akaza.openclinica.domain.rule.expression.ExpressionProcessor) org.openclinica.ns.rules.v31(org.openclinica.ns.rules.v31) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) RulesTestMessagesType(org.openclinica.ns.rules_test.v31.RulesTestMessagesType) ParameterType(org.openclinica.ns.rules_test.v31.ParameterType) PeriodFormatter(org.joda.time.format.PeriodFormatter) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) MessagesType(org.openclinica.ns.response.v31.MessagesType) RulesTestMessagesType(org.openclinica.ns.rules_test.v31.RulesTestMessagesType) Duration(org.joda.time.Duration) MessagesType(org.openclinica.ns.response.v31.MessagesType) Response(org.openclinica.ns.response.v31.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) RulesPostImportContainer(org.akaza.openclinica.domain.rule.RulesPostImportContainer) PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) Map(java.util.Map) HashMap(java.util.HashMap) RuleSetBean(org.akaza.openclinica.domain.rule.RuleSetBean) RuleSetRuleBean(org.akaza.openclinica.domain.rule.RuleSetRuleBean) RuleBean(org.akaza.openclinica.domain.rule.RuleBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ExpressionObjectWrapper (org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper)6 ExpressionProcessor (org.akaza.openclinica.domain.rule.expression.ExpressionProcessor)6 ExpressionBean (org.akaza.openclinica.domain.rule.expression.ExpressionBean)5 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)2 StudyDAO (org.akaza.openclinica.dao.managestudy.StudyDAO)2 RuleSetBean (org.akaza.openclinica.domain.rule.RuleSetBean)2 DateTime (org.joda.time.DateTime)2 Duration (org.joda.time.Duration)2 PeriodFormatter (org.joda.time.format.PeriodFormatter)2 PeriodFormatterBuilder (org.joda.time.format.PeriodFormatterBuilder)2 MessageFormat (java.text.MessageFormat)1 Date (java.util.Date)1 Locale (java.util.Locale)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)1 StudyEventDefinitionBean (org.akaza.openclinica.bean.managestudy.StudyEventDefinitionBean)1