/**
  * 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");
       }
     }
   }
 }
 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 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);
   }
 }
  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;
  }
 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;
 }
  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;
  }