Ejemplo n.º 1
0
 @Override
 public int create(Config config) {
   Config configFound = configDao.findConfigByKey(config.getKey());
   if (configFound != null) {
     Project project = projectDao.getProject(configFound.getProjectId());
     throw new RuntimeBusinessException(
         "该配置项已存在(project: "
             + (project != null ? project.getName() : "***")
             + ", desc: "
             + configFound.getDesc()
             + ")!");
   }
   Integer currentUserId = SecurityUtils.getCurrentUserId();
   int projectId = config.getProjectId();
   if (config.getCreateUserId() == null) {
     config.setCreateUserId(currentUserId);
   }
   if (config.getModifyUserId() == null) {
     config.setModifyUserId(currentUserId);
   }
   projectDao.lockProject(projectId);
   if (!config.isPrivatee()) {
     config.setPrivatee(isSharedProject(projectId));
   }
   config.setSeq(configDao.getMaxSeq(projectId) + 1);
   return configDao.create(config);
 }
Ejemplo n.º 2
0
 public int updateConfig(Config config) {
   try {
     config.setModifyUserId(SecurityUtils.getCurrentUserId());
     return configDao.update(config);
   } finally {
     cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getId());
     cacheClient.remove(ServiceConstants.CACHE_CONFIG_PREFIX + config.getKey());
   }
 }
Ejemplo n.º 3
0
 private void updateConfigModifyStatus(int configId, int envId) {
   int updated = configDao.updateModifyStatus(configId, envId);
   if (updated == 0) {
     ConfigStatus status = new ConfigStatus(configId, envId);
     Integer currentUserId = SecurityUtils.getCurrentUserId();
     status.setCreateUserId(currentUserId);
     status.setModifyUserId(currentUserId);
     configDao.createStatus(status);
   }
 }
Ejemplo n.º 4
0
  public int createInstance(ConfigInstance instance, ConfigSetType setType) {
    Integer currentUserId = SecurityUtils.getCurrentUserId();
    if (instance.getCreateUserId() == null) {
      instance.setCreateUserId(currentUserId);
    }
    if (instance.getModifyUserId() == null) {
      instance.setModifyUserId(currentUserId);
    }
    processInstanceIfReferenceType(instance);
    int retryTimes = 0;
    while (true) {
      try {
        instance.setSeq(configDao.getMaxInstSeq(instance.getConfigId(), instance.getEnvId()) + 1);
        int instId = configDao.createInstance(instance);
        updateConfigModifyStatus(instance.getConfigId(), instance.getEnvId());

        Config config = getConfig(instance.getConfigId());
        List<ConfigInstance> refInstances =
            getInstanceReferencedTo(config.getKey(), instance.getEnvId());

        // 确保注册操作是在最后一步
        if (setType == ConfigSetType.RegisterAndPush) {
          registerAndPush(instance.getConfigId(), instance.getEnvId());
        } else if (setType == ConfigSetType.Register) {
          register(instance.getConfigId(), instance.getEnvId());
        }

        updateReferencedInstances(config, refInstances, setType);

        return instId;
      } catch (RuntimeException e) {
        if (DBUtils.isDuplicateKeyError(e)) {
          if (retryTimes++ >= 1) {
            String errorMsg =
                StringUtils.isNotBlank(instance.getContext())
                    ? "该上下文[context]下配置值已存在!"
                    : "默认配置值已存在!";
            throw new RuntimeBusinessException(errorMsg);
          }
        } else {
          throw e;
        }
      }
    }
  }
Ejemplo n.º 5
0
  private int updateInstance(ConfigInstance instance, ConfigSetType setType) {
    instance.setModifyUserId(SecurityUtils.getCurrentUserId());
    processInstanceIfReferenceType(instance);
    int instId = configDao.updateInstance(instance);
    updateConfigModifyStatus(instance.getConfigId(), instance.getEnvId());

    Config config = getConfig(instance.getConfigId());
    List<ConfigInstance> refInstances =
        getInstanceReferencedTo(config.getKey(), instance.getEnvId());

    // 确保注册操作是在最后一步(后续的都需要try-catch掉所有error)
    if (setType == ConfigSetType.RegisterAndPush) {
      registerAndPush(instance.getConfigId(), instance.getEnvId());
    } else if (setType == ConfigSetType.Register) {
      register(instance.getConfigId(), instance.getEnvId());
    }

    updateReferencedInstances(config, refInstances, setType);
    return instId;
  }