public void bulkUpdateKey(String projectKey, String stringToReplace, String replacementString) {
UserSession.get().checkProjectPermission(UserRole.ADMIN, projectKey);
- DbSession session = dbClient.openSession(false);
+ // Open a batch session
+ DbSession session = dbClient.openSession(true);
try {
ComponentDto project = getByKey(session, projectKey);
-
- resourceKeyUpdaterDao.bulkUpdateKey(project.getId(), stringToReplace, replacementString);
- session.commit();
+ resourceKeyUpdaterDao.bulkUpdateKey(session, project.getId(), stringToReplace, replacementString);
ComponentDto newProject = dbClient.componentDao().getById(project.getId(), session);
- previewCache.reportResourceModification(newProject.key());
+ previewCache.reportResourceModification(session, newProject.key());
session.commit();
} finally {
import org.sonar.api.ServerExtension;
import org.sonar.api.platform.ServerFileSystem;
import org.sonar.api.utils.SonarException;
+import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.persistence.PreviewDatabaseFactory;
import org.sonar.core.properties.PropertiesDao;
}
public void reportResourceModification(String resourceKey) {
- ResourceDto rootProject = resourceDao.getRootProjectByComponentKey(resourceKey);
+ DbSession session = mybatis.openSession(false);
+ try {
+ reportResourceModification(session, resourceKey);
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public void reportResourceModification(DbSession session, String resourceKey) {
+ ResourceDto rootProject = resourceDao.getRootProjectByComponentKey(session, resourceKey);
if (rootProject == null) {
throw new SonarException("Unable to find root project for component with [key=" + resourceKey + "]");
}
propertiesDao.setProperty(new PropertyDto().setKey(SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY).setResourceId(rootProject.getId())
- .setValue(String.valueOf(System.currentTimeMillis())));
+ .setValue(String.valueOf(System.currentTimeMillis())), session);
}
}
return result;
}
- public void bulkUpdateKey(long projectId, String stringToReplace, String replacementString) {
- DbSession session = mybatis.openSession(true);
+ public void bulkUpdateKey(DbSession session, long projectId, String stringToReplace, String replacementString) {
ResourceKeyUpdaterMapper mapper = session.getMapper(ResourceKeyUpdaterMapper.class);
- try {
- // 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()));
- }
+ // 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);
- }
+ // 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 bulkUpdateKey(long projectId, String stringToReplace, String replacementString) {
+ DbSession session = mybatis.openSession(true);
+ try {
+ bulkUpdateKey(session, projectId, stringToReplace, replacementString);
session.commit();
} finally {
MyBatis.closeQuietly(session);
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
public class PreviewCacheTest {
@Test
public void test_report_resource_modification() {
- when(resourceDao.getRootProjectByComponentKey("foo")).thenReturn(new ResourceDto().setId(456L));
+ when(resourceDao.getRootProjectByComponentKey(session, "foo")).thenReturn(new ResourceDto().setId(456L));
dryRunCache.reportResourceModification("foo");
new PropertyDto()
.setKey(PreviewCache.SONAR_PREVIEW_CACHE_LAST_UPDATE_KEY)
.setValue(anyString())
- .setResourceId(456L));
+ .setResourceId(456L), eq(session));
}
}