@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;
  }
  @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;
  }