Beispiel #1
0
 public static ComponentDto toComponent(ResourceDto resourceDto) {
   return new ComponentDto()
       .setId(resourceDto.getId())
       .setKey(resourceDto.getKey())
       .setLongName(resourceDto.getLongName())
       .setName(resourceDto.getName())
       .setQualifier(resourceDto.getQualifier());
 }
 public Map<String, String> checkModuleKeysBeforeRenaming(
     long projectId, String stringToReplace, String replacementString) {
   SqlSession session = mybatis.openSession(false);
   ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
   Map<String, String> result = Maps.newHashMap();
   try {
     Set<ResourceDto> modules = collectAllModules(projectId, stringToReplace, mapper);
     for (ResourceDto module : modules) {
       String newKey = computeNewKey(module, stringToReplace, replacementString);
       if (mapper.countResourceByKey(newKey) > 0) {
         result.put(module.getKey(), "#duplicate_key#");
       } else {
         result.put(module.getKey(), newKey);
       }
     }
   } finally {
     MyBatis.closeQuietly(session);
   }
   return result;
 }
 private static Set<ResourceDto> collectAllModules(
     long projectId, String stringToReplace, ResourceKeyUpdaterMapper mapper) {
   ResourceDto project = mapper.selectProject(projectId);
   Set<ResourceDto> modules = Sets.newHashSet();
   if (project.getKey().contains(stringToReplace)) {
     modules.add(project);
   }
   for (ResourceDto submodule : mapper.selectDescendantProjects(projectId)) {
     modules.addAll(collectAllModules(submodule.getId(), stringToReplace, mapper));
   }
   return modules;
 }
 private static void runBatchUpdateForAllResources(
     Collection<ResourceDto> resources,
     String oldKey,
     String newKey,
     ResourceKeyUpdaterMapper mapper) {
   for (ResourceDto resource : resources) {
     String resourceKey = resource.getKey();
     resource.setKey(newKey + resourceKey.substring(oldKey.length(), resourceKey.length()));
     String resourceDeprecatedKey = resource.getDeprecatedKey();
     if (StringUtils.isNotBlank(resourceDeprecatedKey)) {
       resource.setDeprecatedKey(
           newKey
               + resourceDeprecatedKey.substring(oldKey.length(), resourceDeprecatedKey.length()));
     }
     mapper.update(resource);
   }
 }
  public void bulkUpdateKey(
      DbSession session, long projectId, String stringToReplace, String replacementString) {
    ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
    // must SELECT first everything
    Set<ResourceDto> modules = collectAllModules(projectId, stringToReplace, mapper);
    checkNewNameOfAllModules(modules, stringToReplace, replacementString, mapper);
    Map<ResourceDto, List<ResourceDto>> allResourcesByModuleMap = Maps.newHashMap();
    for (ResourceDto module : modules) {
      allResourcesByModuleMap.put(module, mapper.selectProjectResources(module.getId()));
    }

    // and then proceed with the batch UPDATE at once
    for (ResourceDto module : modules) {
      String oldModuleKey = module.getKey();
      String newModuleKey = computeNewKey(module, stringToReplace, replacementString);
      Collection<ResourceDto> resources = Lists.newArrayList(module);
      resources.addAll(allResourcesByModuleMap.get(module));
      runBatchUpdateForAllResources(resources, oldModuleKey, newModuleKey, mapper);
    }
  }
  public void updateKey(long projectId, String newKey) {
    DbSession session = mybatis.openSession(true);
    ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
    try {
      if (mapper.countResourceByKey(newKey) > 0) {
        throw new IllegalStateException(
            "Impossible to update key: a resource with \"" + newKey + "\" key already exists.");
      }

      // must SELECT first everything
      ResourceDto project = mapper.selectProject(projectId);
      String projectOldKey = project.getKey();
      List<ResourceDto> resources = mapper.selectProjectResources(projectId);
      resources.add(project);

      // and then proceed with the batch UPDATE at once
      runBatchUpdateForAllResources(resources, projectOldKey, newKey, mapper);

      session.commit();
    } finally {
      MyBatis.closeQuietly(session);
    }
  }
 private static String computeNewKey(
     ResourceDto resource, String stringToReplace, String replacementString) {
   return resource.getKey().replaceAll(stringToReplace, replacementString);
 }