docs: public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc, OrganizationDto organizationDto) {
      RuleDao dao = dbClient.ruleDao();
      Optional<RuleDto> existingRuleDtoOpt = dao.selectByKey(dbSession, organizationDto, adHoc.getKey());
      RuleMetadataDto metadata;
      long now = system2.now();
      if (!existingRuleDtoOpt.isPresent()) {
        RuleDefinitionDto dto = new RuleDefinitionDto()
          .setRuleKey(adHoc.getKey())
          .setIsExternal(true)
          .setIsAdHoc(true)
          .setName(adHoc.getEngineId() + ":" + adHoc.getRuleId())
          .setScope(ALL)
          .setStatus(READY)
          .setCreatedAt(now)
          .setUpdatedAt(now);
        dao.insert(dbSession, dto);
        metadata = new RuleMetadataDto()
          .setRuleId(dto.getId())
          .setOrganizationUuid(organizationDto.getUuid());
      } else {
        // No need to update the rule, only org specific metadata
        RuleDto ruleDto = existingRuleDtoOpt.get();
        Preconditions.checkState(ruleDto.isExternal() && ruleDto.isAdHoc());
        metadata = ruleDto.getMetadata();
      }
  
      if (adHoc.hasDetails()) {
        boolean changed = false;
        if (!Objects.equals(metadata.getAdHocName(), adHoc.getName())) {
          metadata.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME));
          changed = true;
        }
        if (!Objects.equals(metadata.getAdHocDescription(), adHoc.getDescription())) {
          metadata.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC));
          changed = true;
        }
        if (!Objects.equals(metadata.getAdHocSeverity(), adHoc.getSeverity())) {
          metadata.setAdHocSeverity(adHoc.getSeverity());
          changed = true;
        }
        RuleType ruleType = requireNonNull(adHoc.getRuleType(), "Rule type should not be null");
        if (!Objects.equals(metadata.getAdHocType(), ruleType.getDbConstant())) {
          metadata.setAdHocType(ruleType);
          changed = true;
        }
        if (changed) {
          metadata.setUpdatedAt(now);
          metadata.setCreatedAt(now);
          dao.insertOrUpdate(dbSession, metadata);
        }
  
      }
  
      RuleDto ruleDto = dao.selectOrFailByKey(dbSession, organizationDto, adHoc.getKey());
      ruleIndexer.commitAndIndex(dbSession, ruleDto.getId());
      return ruleDto;
    }