public Map apply()

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


  public Map<String, Object> apply(
      String templateUid,
      String application,
      String engineType,
      String engineVersion,
      String operator,
      List<String> userList)
      throws ConfigurationException {
    List<Object> successList = new ArrayList<>();
    List<Object> errorList = new ArrayList<>();

    // get the associated config itsm list
    List<String> templateUuidList = new ArrayList<>();
    templateUuidList.add(templateUid);
    List<TemplateConfigKey> templateConfigKeyList =
        templateConfigKeyMapper.selectListByTemplateUuidList(templateUuidList);
    if (templateConfigKeyList.size() == 0) {
      String msg =
          MessageFormat.format(
              "The template configuration is empty. Please check the template associated configuration information in the database table"
                  + "(模板关联的配置为空,请检查数据库表中关于模板id:{0} 关联配置项是否完整)",
              templateUid);
      throw new ConfigurationException(msg);
    }
    // check input engineType is same as template key engineType
    List<Long> keyIdList =
        templateConfigKeyList.stream()
            .map(e -> e.getKeyId())
            .distinct()
            .collect(Collectors.toList());

    if (keyIdList.size() == 0) {
      String msg = "can not get any config key info from db, Please check if the keys are correct";
      throw new ConfigurationException(msg);
    }
    List<ConfigKey> configKeyList = configMapper.selectKeyByKeyIdList(keyIdList);
    // map k:v---> keyId:ConfigKey
    Set<String> configKeyEngineTypeSet =
        configKeyList.stream().map(ConfigKey::getEngineType).collect(Collectors.toSet());

    if (configKeyEngineTypeSet == null || configKeyEngineTypeSet.size() == 0) {
      String msg =
          MessageFormat.format(
              "Unable to get configuration parameter information associated with template id:{0}, please check whether the parameters are correct"
                  + "(无法获取模板:{0} 关联的配置参数信息,请检查参数是否正确)",
              templateUid);
      throw new ConfigurationException(msg);
    }
    // support global template
    configKeyEngineTypeSet.add(org.apache.linkis.common.conf.Configuration.GLOBAL_CONF_SYMBOL());
    if (!configKeyEngineTypeSet.contains(engineType)) {
      String msg =
          MessageFormat.format(
              "The engineType:{0} associated with the template:{1} does not match the input engineType:{2}, please check whether the parameters are correct"
                  + "(模板关联的引擎类型:{0} 和下发的引擎类型:{2} 不匹配,请检查参数是否正确)",
              String.join(",", configKeyEngineTypeSet), templateUid, engineType);
      throw new ConfigurationException(msg);
    }
    for (String user : userList) {
      // try to create combined_userCreator_engineType label for user
      Map res = new HashMap();
      res.put("user", user);
      try {
        CombinedLabel combinedLabel =
            configurationService.generateCombinedLabel(
                engineType, engineVersion, user, application);
        String conbinedLabelKey = combinedLabel.getLabelKey();
        String conbinedLabelStringValue = combinedLabel.getStringValue();
        // check lable is ok

        ConfigLabel configLabel =
            labelMapper.getLabelByKeyValue(conbinedLabelKey, conbinedLabelStringValue);
        if (null == configLabel || configLabel.getId() < 0) {
          configLabel = LabelEntityParser.parseToConfigLabel(combinedLabel);
          labelMapper.insertLabel(configLabel);
          logger.info("succeed to create label: {}", configLabel.getStringValue());
        }

        // batch update config value
        List<ConfigValue> configValues = new ArrayList<>();

        List<ConfigKeyLimitForUser> configKeyLimitForUsers = new ArrayList<>();

        for (TemplateConfigKey templateConfigKey : templateConfigKeyList) {
          Long keyId = templateConfigKey.getKeyId();
          String uuid = templateConfigKey.getTemplateUuid();
          String confVal = templateConfigKey.getConfigValue();
          String maxVal = templateConfigKey.getMaxValue();

          ConfigValue configValue = new ConfigValue();
          configValue.setConfigKeyId(keyId);
          configValue.setConfigValue(confVal);
          configValue.setConfigLabelId(configLabel.getId());
          configValues.add(configValue);

          ConfigKeyLimitForUser configKeyLimitForUser = new ConfigKeyLimitForUser();
          configKeyLimitForUser.setUserName(user);
          configKeyLimitForUser.setCombinedLabelValue(configLabel.getStringValue());
          configKeyLimitForUser.setKeyId(keyId);
          configKeyLimitForUser.setConfigValue(confVal);
          configKeyLimitForUser.setMaxValue(maxVal);
          configKeyLimitForUser.setLatestUpdateTemplateUuid(uuid);
          configKeyLimitForUser.setCreateBy(operator);
          configKeyLimitForUser.setUpdateBy(operator);
          configKeyLimitForUsers.add(configKeyLimitForUser);
        }

        if (configValues.size() == 0) {
          res.put("msg", "can not get any right key form the db");
          errorList.add(res);
        } else {

          DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
          TransactionStatus status =
              platformTransactionManager.getTransaction(transactionDefinition);
          try {
            configMapper.batchInsertOrUpdateValueList(configValues);
            // batch update user ConfigKeyLimitForUserMapper
            configKeyLimitForUserMapper.batchInsertOrUpdateList(configKeyLimitForUsers);

            platformTransactionManager.commit(status); // commit transaction if everything's fine
          } catch (Exception ex) {
            platformTransactionManager.rollback(
                status); // rollback transaction if any error occurred
            throw ex;
          }
          successList.add(res);
        }

      } catch (Exception e) {
        logger.warn("try to update configurations for  user:" + user + " with error", e);
        res.put("msg", e.getMessage());
        errorList.add(res);
      }
    }

    Map<String, Object> result = new HashMap<>();

    Map<String, Object> successResult = new HashMap<>();
    Map<String, Object> errorResult = new HashMap<>();

    successResult.put("num", successList.size());
    successResult.put("infoList", successList);

    errorResult.put("num", errorList.size());
    errorResult.put("infoList", errorList);

    result.put("success", successResult);
    result.put("error", errorResult);
    return result;
  }