mapper.deleteProjectProperties(key, value);
}
- public void deleteProjectProperties(String key, String value) {
+ public void deleteProjectProperties(String key, String value) {
SqlSession session = mybatis.openSession();
try {
deleteProjectProperties(key, value, session);
}
}
}
+
+ /**
+ * Update all properties (global and projects ones) with a given key and value to a new value
+ */
+ public void updateProperties(String key, String oldValue, String newValue) {
+ SqlSession session = mybatis.openSession();
+ try {
+ updateProperties(key, oldValue, newValue, session);
+ session.commit();
+
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public void updateProperties(String key, String oldValue, String newValue, SqlSession session) {
+ PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+ mapper.updateProperties(key, oldValue, newValue);
+ }
+
}
void deleteGlobalProperties();
void renamePropertyKey(@Param("oldKey") String oldKey, @Param("newKey") String newKey);
+
+ void updateProperties(@Param("key") String key, @Param("oldValue")String oldValue, @Param("newValue") String newValue);
}
}
}
- public List<QualityProfileDto> selectChildren(String name, String language) {
+ public List<QualityProfileDto> selectChildren(String name, String language, SqlSession session) {
+ return session.getMapper(QualityProfileMapper.class).selectChildren(StringUtils.upperCase(name), language);
+ }
+
+ public List<QualityProfileDto> selectChildren(String name, String language) {
SqlSession session = mybatis.openSession();
try {
- return session.getMapper(QualityProfileMapper.class).selectChildren(StringUtils.upperCase(name), language);
+ return selectChildren(StringUtils.upperCase(name), language, session);
} finally {
MyBatis.closeQuietly(session);
}
</delete>
<update id="renamePropertyKey" parameterType="map">
- update properties set prop_key = #{newKey} where prop_key=#{oldKey}
+ update properties set prop_key=#{newKey} where prop_key=#{oldKey}
+ </update>
+
+ <update id="updateProperties" parameterType="map">
+ update properties set text_value=#{newValue} where text_value=#{oldValue} and prop_key=#{key}
</update>
</mapper>
dao.renamePropertyKey(null, "foo");
}
+ @Test
+ public void updatePropertiesFromKeyAndValueToNewValue() {
+ setupData("updatePropertiesFromKeyAndValueToNewValue");
+
+ dao.updateProperties("sonar.profile.java", "Sonar Way", "Default");
+
+ checkTable("updatePropertiesFromKeyAndValueToNewValue", "properties", "prop_key", "text_value", "resource_id", "user_id");
+ }
+
private PropertyDto findById(List<PropertyDto> properties, int id) {
for (PropertyDto property : properties) {
if (property.getId() == id) {
--- /dev/null
+<dataset>
+
+ <properties id="1" prop_key="sonar.profile.java" text_value="Default" resource_id="[null]" user_id="[null]"/>
+ <properties id="2" prop_key="sonar.profile.java" text_value="Default" resource_id="1" user_id="[null]"/>
+
+ <properties id="3" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="[null]" user_id="[null]"/>
+ <properties id="4" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="2" user_id="[null]"/>
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <properties id="1" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="[null]" user_id="[null]"/>
+ <properties id="2" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="1" user_id="[null]"/>
+
+ <properties id="3" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="[null]" user_id="[null]"/>
+ <properties id="4" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="2" user_id="[null]"/>
+
+</dataset>
}
public List<QProfile> children(QProfile profile) {
- return toQProfiles(dao.selectChildren(profile.name(), profile.language()));
+ SqlSession session = myBatis.openSession();
+ try {
+ return children(profile, session);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public List<QProfile> children(QProfile profile, SqlSession session) {
+ return toQProfiles(dao.selectChildren(profile.name(), profile.language(), session));
}
public List<QProfile> ancestors(QProfile profile) {
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import java.util.List;
import java.util.Map;
public class QProfileOperations implements ServerComponent {
checkPermission(userSession);
SqlSession session = myBatis.openSession();
try {
- QualityProfileDto profile = findNotNull(profileId, session);
+ QualityProfileDto profileDto = findNotNull(profileId, session);
+ String oldName = profileDto.getName();
- // TODO rename children and properties
+ QProfile profile = QProfile.from(profileDto);
+ if (!oldName.equals(newName)) {
+ checkNotAlreadyExists(newName, profile.language(), session);
+ }
+ profileDto.setName(newName);
+ dao.update(profileDto, session);
- if (!profile.getName().equals(newName)) {
- checkNotAlreadyExists(newName, profile.getLanguage(), session);
+ List<QProfile> children = profileLookup.children(profile, session);
+ for (QProfile child : children) {
+ dao.update(child.setParent(newName).toDto(), session);
}
- profile.setName(newName);
- dao.update(profile, session);
+ propertiesDao.updateProperties(PROFILE_PROPERTY_PREFIX + profile.language(), oldName, newName, session);
+
session.commit();
} finally {
MyBatis.closeQuietly(session);
@Test
public void search_children_profiles() throws Exception {
search.children(new QProfile().setName("Sonar Way").setLanguage("java"));
- verify(dao).selectChildren("Sonar Way", "java");
+ verify(dao).selectChildren("Sonar Way", "java", session);
}
@Test
import org.sonar.server.user.MockUserSession;
import org.sonar.server.user.UserSession;
+import java.util.Collections;
+
+import static org.elasticsearch.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import static org.mockito.Matchers.any;
@Test
public void rename_profile() throws Exception {
- when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java"));
+ when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Old Default").setLanguage("java"));
+ when(profileLookup.children(any(QProfile.class), eq(session))).thenReturn(Collections.<QProfile>emptyList());
operations.renameProfile(1, "Default profile", authorizedUserSession);
ArgumentCaptor<QualityProfileDto> profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class);
verify(qualityProfileDao).update(profileArgument.capture(), eq(session));
-
+ assertThat(profileArgument.getValue().getId()).isEqualTo(1);
assertThat(profileArgument.getValue().getName()).isEqualTo("Default profile");
assertThat(profileArgument.getValue().getLanguage()).isEqualTo("java");
+ verify(propertiesDao).updateProperties("sonar.profile.java", "Old Default", "Default profile", session);
verify(session).commit();
}
}
}
+
+ @Test
+ public void rename_children_profile() throws Exception {
+ QualityProfileDto profile = new QualityProfileDto().setId(1).setName("Old Default").setLanguage("java");
+ when(qualityProfileDao.selectById(1, session)).thenReturn(profile);
+ when(profileLookup.children(any(QProfile.class), eq(session))).thenReturn(newArrayList(
+ new QProfile().setId(2).setName("Child1").setLanguage("java").setParent("Old Default")
+ ));
+
+ operations.renameProfile(1, "Default profile", authorizedUserSession);
+
+ ArgumentCaptor<QualityProfileDto> profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class);
+ // One call to update current profile and one other for child
+ verify(qualityProfileDao, times(2)).update(profileArgument.capture(), eq(session));
+ assertThat(profileArgument.getAllValues()).hasSize(2);
+ QualityProfileDto child = profileArgument.getAllValues().get(1);
+ assertThat(child.getId()).isEqualTo(2);
+ assertThat(child.getParent()).isEqualTo("Default profile");
+
+ verify(session).commit();
+ }
+
@Test
public void update_default_profile() throws Exception {
when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java"));