private boolean isRuleOidValid(AuditableBeanWrapper<RuleBean> ruleBeanWrapper) {
   boolean isValid = true;
   try {
     oidGenerator.validate(ruleBeanWrapper.getAuditableBean().getOid());
   } catch (Exception e) {
     ruleBeanWrapper.error("OID is not Valid, The OID can only be made of A-Z_0-9");
     isValid = false;
   }
   return isValid;
 }
 private boolean isRuleSetExpressionValid(AuditableBeanWrapper<RuleSetBean> beanWrapper) {
   boolean isValid = true;
   ExpressionBean expressionBean =
       isExpressionValid(beanWrapper.getAuditableBean().getTarget(), beanWrapper);
   ExpressionObjectWrapper eow = new ExpressionObjectWrapper(ds, currentStudy, expressionBean);
   ExpressionProcessor ep = ExpressionProcessorFactory.createExpressionProcessor(eow);
   String errorString = ep.isRuleAssignmentExpressionValid();
   if (errorString != null) {
     beanWrapper.error(errorString);
     isValid = false;
   }
   return isValid;
 }
  @RequestMapping(value = "/studies/{study}/validateAndSaveRule", method = RequestMethod.POST)
  public @ResponseBody Response validateAndSave(
      @RequestBody org.openclinica.ns.rules.v31.Rules rules,
      Model model,
      HttpSession session,
      @PathVariable("study") String studyOid,
      @RequestParam("ignoreDuplicates") Boolean ignoreDuplicates)
      throws Exception {
    ResourceBundleProvider.updateLocale(new Locale("en_US"));
    RulesPostImportContainer rpic = mapRulesToRulesPostImportContainer(rules);
    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);
        }
      }
    } else if ((rpic.getDuplicateRuleDefs().size() > 0) && !ignoreDuplicates) {
      response.setValid(Boolean.FALSE);
      for (AuditableBeanWrapper<RuleBean> beanWrapper : rpic.getDuplicateRuleDefs()) {
        org.openclinica.ns.response.v31.MessagesType messageType = new MessagesType();
        messageType.setMessage(DUPLICATE_MESSAGE);
        response.getMessages().add(messageType);
      }
    } else {
      getRuleSetService().saveImportFromDesigner(rpic);
    }
    logger.debug("RPIC READY");
    return response;
  }
  public RulesPostImportContainer validateRuleDefs(RulesPostImportContainer importContainer) {
    for (RuleBean ruleBean : importContainer.getRuleDefs()) {
      AuditableBeanWrapper<RuleBean> ruleBeanWrapper = new AuditableBeanWrapper<RuleBean>(ruleBean);

      if (isRuleOidValid(ruleBeanWrapper) && isRuleExpressionValid(ruleBeanWrapper, null)) {
        RuleBean persistentRuleBean = getRuleDao().findByOid(ruleBeanWrapper.getAuditableBean());
        if (persistentRuleBean != null) {
          String name = ruleBeanWrapper.getAuditableBean().getName();
          String expressionValue = ruleBeanWrapper.getAuditableBean().getExpression().getValue();
          String expressionContextName =
              ruleBeanWrapper.getAuditableBean().getExpression().getContextName();
          Context context =
              expressionContextName != null
                  ? Context.getByName(expressionContextName)
                  : Context.OC_RULES_V1;
          ruleBeanWrapper.setAuditableBean(persistentRuleBean);
          ruleBeanWrapper.getAuditableBean().setName(name);
          ruleBeanWrapper.getAuditableBean().getExpression().setValue(expressionValue);
          ruleBeanWrapper.getAuditableBean().getExpression().setContext(context);
          // ruleBeanWrapper.getAuditableBean().setId(persistentRuleBean.getId());
          // ruleBeanWrapper.getAuditableBean().getExpression().setId(persistentRuleBean.getExpression().getId());
        }
      }
      putRuleInCorrectContainer(ruleBeanWrapper, importContainer);
    }
    logger.info(
        "# of Valid RuleDefs : {} , # of InValid RuleDefs : {} , # of Overwritable RuleDefs : {}",
        new Object[] {
          importContainer.getValidRuleDefs().size(),
          importContainer.getInValidRuleDefs().size(),
          importContainer.getDuplicateRuleDefs().size()
        });
    return importContainer;
  }
  public RulesPostImportContainer validateRuleSetDefs(RulesPostImportContainer importContainer) {
    for (RuleSetBean ruleSetBean : importContainer.getRuleSets()) {
      AuditableBeanWrapper<RuleSetBean> ruleSetBeanWrapper =
          new AuditableBeanWrapper<RuleSetBean>(ruleSetBean);
      ruleSetBeanWrapper.getAuditableBean().setStudy(currentStudy);
      if (isRuleSetExpressionValid(ruleSetBeanWrapper)) {
        RuleSetBean persistentRuleSetBean = getRuleSetDao().findByExpression(ruleSetBean);

        if (persistentRuleSetBean != null) {
          List<RuleSetRuleBean> importedRuleSetRules =
              ruleSetBeanWrapper.getAuditableBean().getRuleSetRules();
          ruleSetBeanWrapper.setAuditableBean(persistentRuleSetBean);
          ruleSetBeanWrapper.getAuditableBean().addRuleSetRules(importedRuleSetRules);
          // ruleSetBeanWrapper.getAuditableBean().setId(persistentRuleSetBean.getId());
        } else {
          if (importContainer
              .getValidRuleSetExpressionValues()
              .contains(ruleSetBeanWrapper.getAuditableBean().getTarget().getValue())) {
            ruleSetBeanWrapper.error(
                "You have two rule assignments with exact same Target, Combine and try again");
          }
          ruleSetBeanWrapper
              .getAuditableBean()
              .setStudyEventDefinition(
                  getExpressionService()
                      .getStudyEventDefinitionFromExpression(ruleSetBean.getTarget().getValue()));
          ruleSetBeanWrapper
              .getAuditableBean()
              .setCrf(
                  getExpressionService().getCRFFromExpression(ruleSetBean.getTarget().getValue()));
          ruleSetBeanWrapper
              .getAuditableBean()
              .setCrfVersion(
                  getExpressionService()
                      .getCRFVersionFromExpression(ruleSetBean.getTarget().getValue()));
        }
        isRuleSetRuleValid(importContainer, ruleSetBeanWrapper);
      }
      putRuleSetInCorrectContainer(ruleSetBeanWrapper, importContainer);
    }
    logger.info("# of Valid RuleSetDefs : " + importContainer.getValidRuleSetDefs().size());
    logger.info("# of InValid RuleSetDefs : " + importContainer.getInValidRuleSetDefs().size());
    logger.info(
        "# of Overwritable RuleSetDefs : " + importContainer.getDuplicateRuleSetDefs().size());
    return importContainer;
  }
 private void putRuleInCorrectContainer(
     AuditableBeanWrapper<RuleBean> ruleBeanWrapper, RulesPostImportContainer importContainer) {
   if (!ruleBeanWrapper.isSavable()) {
     importContainer.getInValidRuleDefs().add(ruleBeanWrapper);
     importContainer
         .getInValidRules()
         .put(ruleBeanWrapper.getAuditableBean().getOid(), ruleBeanWrapper);
   } else if (ruleBeanWrapper.getAuditableBean().getId() == null) {
     importContainer.getValidRuleDefs().add(ruleBeanWrapper);
     importContainer
         .getValidRules()
         .put(ruleBeanWrapper.getAuditableBean().getOid(), ruleBeanWrapper);
   } else if (ruleBeanWrapper.getAuditableBean().getId() != null) {
     importContainer.getDuplicateRuleDefs().add(ruleBeanWrapper);
     importContainer
         .getValidRules()
         .put(ruleBeanWrapper.getAuditableBean().getOid(), ruleBeanWrapper);
   }
 }
 private void putRuleSetInCorrectContainer(
     AuditableBeanWrapper<RuleSetBean> ruleSetBeanWrapper,
     RulesPostImportContainer importContainer) {
   if (!ruleSetBeanWrapper.isSavable()) {
     importContainer.getInValidRuleSetDefs().add(ruleSetBeanWrapper);
   } else if (getExpressionService()
       .getEventDefinitionCRF(ruleSetBeanWrapper.getAuditableBean().getTarget().getValue())
       .getStatus()
       .isDeleted()) {
     importContainer.getInValidRuleSetDefs().add(ruleSetBeanWrapper);
   } else if (ruleSetBeanWrapper.getAuditableBean().getId() == null) {
     importContainer.getValidRuleSetDefs().add(ruleSetBeanWrapper);
     importContainer
         .getValidRuleSetExpressionValues()
         .add(ruleSetBeanWrapper.getAuditableBean().getTarget().getValue());
   } else if (ruleSetBeanWrapper.getAuditableBean().getId() != null) {
     importContainer.getDuplicateRuleSetDefs().add(ruleSetBeanWrapper);
   }
 }
  private ExpressionBean isExpressionValid(
      ExpressionBean expressionBean, AuditableBeanWrapper<?> beanWrapper) {

    if (expressionBean.getContextName() == null && expressionBean.getContext() == null) {
      expressionBean.setContext(Context.OC_RULES_V1);
    }
    if (expressionBean.getContextName() != null && expressionBean.getContext() == null) {
      beanWrapper.warning("The Context you selected is not support we will use the default one");
      expressionBean.setContext(Context.OC_RULES_V1);
    }
    return expressionBean;
  }
 /**
  * If the RuleSet contains any RuleSetRule object with an invalid RuleRef OID (OID that is not in
  * DB or in the Valid Rule Lists) , Then add an error to the ruleSetBeanWrapper, which in terms
  * will make the RuleSet inValid.
  *
  * @param importContainer
  * @param ruleSetBeanWrapper
  */
 private void isRuleSetRuleValid(
     RulesPostImportContainer importContainer,
     AuditableBeanWrapper<RuleSetBean> ruleSetBeanWrapper) {
   for (RuleSetRuleBean ruleSetRuleBean :
       ruleSetBeanWrapper.getAuditableBean().getRuleSetRules()) {
     String ruleDefOid = ruleSetRuleBean.getOid();
     if (ruleSetRuleBean.getId() == null) {
       if (getExpressionService()
           .getEventDefinitionCRF(ruleSetBeanWrapper.getAuditableBean().getTarget().getValue())
           .getStatus()
           .isDeleted()) {
         ruleSetBeanWrapper.error(
             "This is an invalid Rule Set because the target is pointing to an item in the event definition CRF that has a status of removed");
       }
       if (importContainer.getInValidRules().get(ruleDefOid) != null
           || importContainer.getValidRules().get(ruleDefOid) == null
               && getRuleDao().findByOid(ruleDefOid) == null) {
         ruleSetBeanWrapper.error(
             "The Rule you are trying to reference does not exist or is Invalid");
       }
       if (importContainer.getValidRules().get(ruleDefOid) != null) {
         AuditableBeanWrapper<RuleBean> r = importContainer.getValidRules().get(ruleDefOid);
         if (!isRuleExpressionValid(r, ruleSetBeanWrapper.getAuditableBean()))
           ruleSetBeanWrapper.error(
               "The Contextual expression in one of the Rules does not validate against the Target expression in the Current RuleSet");
       }
     }
   }
 }
示例#10
0
  @RequestMapping(value = "/studies/{study}/validateAndTestRule", method = RequestMethod.POST)
  public @ResponseBody 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;
  }