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); } }