public Boolean updateKeyMapping()

in linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/TemplateConfigKeyServiceImpl.java [81:210]


  public Boolean updateKeyMapping(
      String templateUid,
      String templateName,
      String engineType,
      String operator,
      List<ConfigKeyLimitVo> itemList,
      Boolean isFullMode)
      throws ConfigurationException {

    // Query the corresponding data and check the validity of the data(查询对应的数据 并做数据合法性检查)
    List<String> keyList = itemList.stream().map(e -> e.getKey()).collect(Collectors.toList());
    List<ConfigKey> configKeyList =
        configMapper.selectKeyByEngineTypeAndKeyList(engineType, keyList);
    // List of key ids to be updated(待更新的key id 列表)
    List<Long> keyIdList = configKeyList.stream().map(e -> e.getId()).collect(Collectors.toList());
    if (configKeyList.size() != itemList.size()) {
      List<String> dbKeyList =
          configKeyList.stream().map(e -> e.getKey()).collect(Collectors.toList());
      String msg =
          MessageFormat.format(
              "The num of configuration item data from the DB is inconsistent with input(从DB中获取到的配置数据条数不一致) :"
                  + "engineType:{0}, input keys:{1}, db keys:{2}",
              engineType, String.join(",", keyList), String.join(",", dbKeyList));
      throw new ConfigurationException(msg);
    }

    List<TemplateConfigKey> toUpdateOrInsertList = new ArrayList<>();

    // map k:v---> key:ConfigKey
    Map<String, ConfigKey> configKeyMap =
        configKeyList.stream().collect(Collectors.toMap(ConfigKey::getKey, item -> item));
    for (ConfigKeyLimitVo item : itemList) {

      String key = item.getKey();
      ConfigKey temp = configKeyMap.get(item.getKey());
      String validateType = temp.getValidateType();
      String validateRange = temp.getValidateRange();
      String configValue = item.getConfigValue();
      String maxValue = item.getMaxValue();

      if (StringUtils.isNotEmpty(configValue)
          && !validatorManager
              .getOrCreateValidator(validateType)
              .validate(configValue, validateRange)) {
        String msg =
            MessageFormat.format(
                "Parameter configValue verification failed(参数configValue校验失败):"
                    + "key:{0}, ValidateType:{1}, ValidateRange:{2},ConfigValue:{3}",
                key, validateType, validateRange, configValue);
        throw new ConfigurationException(msg);
      }

      if (StringUtils.isNotEmpty(maxValue)
          && BoundaryTypeEnum.WITH_BOTH.getId().equals(temp.getBoundaryType())) {
        if (!validatorManager
            .getOrCreateValidator(validateType)
            .validate(maxValue, validateRange)) {
          String msg =
              MessageFormat.format(
                  "Parameter maxValue verification failed(参数maxValue校验失败):"
                      + "key:{0}, ValidateType:{1}, ValidateRange:{2}, maxValue:{3}",
                  key, validateType, validateRange, maxValue);
          throw new ConfigurationException(msg);
        }

        try {
          Integer maxVal = Integer.valueOf(maxValue.replaceAll("[^0-9]", ""));
          Integer configVal = Integer.valueOf(configValue.replaceAll("[^0-9]", ""));
          if (configVal > maxVal) {
            String msg =
                MessageFormat.format(
                    "Parameter key:{0},config value:{1} verification failed, "
                        + "exceeds the specified max value: {2}:(参数校验失败,超过指定的最大值):",
                    key, configVal, maxVal);
            throw new ConfigurationException(msg);
          }
        } catch (Exception exception) {
          if (exception instanceof ConfigurationException) {
            throw exception;
          } else {
            logger.warn(
                "Failed to check special limit setting for key:"
                    + key
                    + ",config value:"
                    + configValue);
          }
        }
      }
      ;

      Long keyId = temp.getId();

      TemplateConfigKey templateConfigKey = new TemplateConfigKey();
      templateConfigKey.setTemplateName(templateName);
      templateConfigKey.setTemplateUuid(templateUid);
      templateConfigKey.setKeyId(keyId);
      templateConfigKey.setConfigValue(configValue);
      templateConfigKey.setMaxValue(maxValue);
      templateConfigKey.setCreateBy(operator);
      templateConfigKey.setUpdateBy(operator);
      toUpdateOrInsertList.add(templateConfigKey);
    }
    // Update data according to different mode
    if (isFullMode) {
      // The data previously in the database needs to be removed
      List<TemplateConfigKey> oldList =
          templateConfigKeyMapper.selectListByTemplateUuid(templateUid);
      List<Long> needToRemoveList =
          oldList.stream()
              .filter(
                  item -> {
                    return !keyIdList.contains(item.getKeyId());
                  })
              .map(e -> e.getKeyId())
              .collect(Collectors.toList());
      if (needToRemoveList.size() > 0) {
        logger.info(
            "Try to remove old data:[" + needToRemoveList + "] for templateUid:" + templateUid);
        templateConfigKeyMapper.deleteByTemplateUuidAndKeyIdList(templateUid, needToRemoveList);
      }
    }

    if (toUpdateOrInsertList.size() == 0) {
      String msg = "No key data to update, Please check if the keys are correct";
      throw new ConfigurationException(msg);
    }
    templateConfigKeyMapper.batchInsertOrUpdateList(toUpdateOrInsertList);

    return true;
  }