private static void checkNewNameOfAllModules(Set<ResourceDto> modules, String stringToReplace, String replacementString, ComponentKeyUpdaterMapper mapper) { for (ResourceDto module : modules) { String newKey = computeNewKey(module.getKey(), stringToReplace, replacementString); checkArgument(isValidProjectKey(newKey), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", newKey); if (mapper.countResourceByKey(newKey) > 0) { throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists."); } } }
@Test public void compute_new_key() { assertThat(computeNewKey("my_project", "my_", "your_")).isEqualTo("your_project"); assertThat(computeNewKey("my_project", "my_", "$()_")).isEqualTo("$()_project"); }
/** * * @return a map with currentKey/newKey is a bulk update was executed */ public Map<String, String> simulateBulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) { return collectAllModules(projectUuid, stringToReplace, mapper(dbSession), false) .stream() .collect(Collectors.toMap( ResourceDto::getKey, component -> { String newKey = computeNewKey(component.getKey(), stringToReplace, replacementString); checkProjectKey(newKey); return newKey; })); }
String oldModuleKey = module.getKey(); oldModuleKey = branchBaseKeys.getOrDefault(oldModuleKey, oldModuleKey); String newModuleKey = computeNewKey(oldModuleKey, stringToReplace, replacementString); Collection<ResourceDto> resources = Lists.newArrayList(module); resources.addAll(allResourcesByModuleMap.get(module));
private static void checkNewNameOfAllModules(Set<ResourceDto> modules, String stringToReplace, String replacementString, ComponentKeyUpdaterMapper mapper) { for (ResourceDto module : modules) { String newKey = computeNewKey(module.getKey(), stringToReplace, replacementString); checkArgument(isValidModuleKey(newKey), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", newKey); if (mapper.countResourceByKey(newKey) > 0) { throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists."); } } }
/** * * @return a map with currentKey/newKey is a bulk update was executed */ public Map<String, String> simulateBulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) { return collectAllModules(projectUuid, stringToReplace, mapper(dbSession)) .stream() .collect(Collectors.toMap( ResourceDto::getKey, component -> { String newKey = computeNewKey(component.getKey(), stringToReplace, replacementString); checkModuleKey(newKey); return newKey; })); }
public void bulkUpdateKey(DbSession session, String projectUuid, String stringToReplace, String replacementString) { ComponentKeyUpdaterMapper mapper = session.getMapper(ComponentKeyUpdaterMapper.class); // must SELECT first everything Set<ResourceDto> modules = collectAllModules(projectUuid, stringToReplace, mapper); checkNewNameOfAllModules(modules, stringToReplace, replacementString, mapper); Map<ResourceDto, List<ResourceDto>> allResourcesByModuleMap = Maps.newHashMap(); for (ResourceDto module : modules) { allResourcesByModuleMap.put(module, mapper.selectProjectResources(module.getUuid())); } // and then proceed with the batch UPDATE at once for (ResourceDto module : modules) { String oldModuleKey = module.getKey(); String newModuleKey = computeNewKey(module.getKey(), stringToReplace, replacementString); Collection<ResourceDto> resources = Lists.newArrayList(module); resources.addAll(allResourcesByModuleMap.get(module)); runBatchUpdateForAllResources(resources, oldModuleKey, newModuleKey, mapper); } }
public Map<String, String> checkModuleKeysBeforeRenaming(String projectUuid, String stringToReplace, String replacementString) { SqlSession session = mybatis.openSession(false); ComponentKeyUpdaterMapper mapper = session.getMapper(ComponentKeyUpdaterMapper.class); Map<String, String> result = Maps.newHashMap(); try { Set<ResourceDto> modules = collectAllModules(projectUuid, stringToReplace, mapper); for (ResourceDto module : modules) { String newKey = computeNewKey(module.getKey(), 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; }