*/
package org.sonar.core.properties;
+import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;
* @param resourceId the resource id
* @return the list of logins (maybe be empty - obviously)
*/
- public List<String> findUserIdsForFavouriteResource(Integer resourceId) {
+ public List<String> findUserIdsForFavouriteResource(Long resourceId) {
SqlSession session = mybatis.openSession();
PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
try {
MyBatis.closeQuietly(session);
}
}
+
+ public void setProperty(PropertyDto property) {
+ SqlSession session = mybatis.openSession();
+ PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+ try {
+ PropertyDto persistedProperty = mapper.selectByKey(property);
+ if (persistedProperty != null && !StringUtils.equals(persistedProperty.getValue(), property.getValue())) {
+ persistedProperty.setValue(property.getValue());
+ mapper.update(persistedProperty);
+ session.commit();
+
+ } else if (persistedProperty == null) {
+ mapper.insert(property);
+ session.commit();
+ }
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
}
import java.util.List;
-import org.apache.ibatis.annotations.Param;
-
public interface PropertiesMapper {
- List<String> findUserIdsForFavouriteResource(@Param("resource_id") Integer resourceId);
+ List<String> findUserIdsForFavouriteResource(Long resourceId);
List<PropertyDto> selectGlobalProperties();
List<PropertyDto> selectProjectProperties(String resourceKey);
+ PropertyDto selectByKey(PropertyDto key);
+ void update(PropertyDto property);
+ void insert(PropertyDto property);
}
package org.sonar.core.properties;
public final class PropertyDto {
- private Integer id;
+ private Long id;
private String key;
private String value;
- private Integer resourceId;
- private Integer userId;
+ private Long resourceId;
+ private Long userId;
- public Integer getId() {
+ public Long getId() {
return id;
}
- public PropertyDto setId(Integer id) {
+ public PropertyDto setId(Long id) {
this.id = id;
return this;
}
return this;
}
- public Integer getResourceId() {
+ public Long getResourceId() {
return resourceId;
}
- public PropertyDto setResourceId(Integer resourceId) {
+ public PropertyDto setResourceId(Long resourceId) {
this.resourceId = resourceId;
return this;
}
- public Integer getUserId() {
+ public Long getUserId() {
return userId;
}
- public PropertyDto setUserId(Integer userId) {
+ public PropertyDto setUserId(Long userId) {
this.userId = userId;
return this;
}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.properties.PropertiesMapper">
+
+ <select id="findUserIdsForFavouriteResource" parameterType="map" resultType="String">
+ SELECT U.login
+ FROM properties P, users U
+ WHERE P.prop_key = 'favourite' AND P.resource_id = #{id} AND P.user_id = U.id
+ </select>
+
+ <select id="selectGlobalProperties" resultType="Property">
+ select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+ from properties p
+ where p.resource_id is null and p.user_id is null
+ </select>
+
+ <select id="selectProjectProperties" parameterType="String" resultType="Property">
+ select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+ from properties p, projects r
+ where p.resource_id=r.id and p.user_id is null and r.kee=#{id}
+ </select>
+
+ <select id="selectByKey" parameterType="map" resultType="Property">
+ select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+ from properties p
+ where p.prop_key=#{key}
+ <if test="resourceId == null">
+ AND p.resource_id is null
+ </if>
+ <if test="resourceId != null">
+ AND p.resource_id=#{resourceId}
+ </if>
+ <if test="userId == null">
+ AND p.user_id is null
+ </if>
+ <if test="userId != null">
+ AND p.user_id=#{userId}
+ </if>
+ </select>
+
+ <update id="update" parameterType="Property">
+ update properties set text_value = #{value} where id = #{id}
+ </update>
+
+ <insert id="insert" parameterType="Property" useGeneratedKeys="true" keyProperty="id">
+ <selectKey order="BEFORE" resultType="Long" keyProperty="id">
+ select properties_seq.NEXTVAL from DUAL
+ </selectKey>
+ INSERT INTO properties (id, prop_key, resource_id, user_id, text_value)
+ VALUES (#{id}, #{key, jdbcType=VARCHAR}, #{resourceId, jdbcType=INTEGER}, #{userId, jdbcType=INTEGER}, #{value, jdbcType=VARCHAR})
+ </insert>
+
+</mapper>
<select id="findUserIdsForFavouriteResource" parameterType="map" resultType="String">
SELECT U.login
FROM properties P, users U
- WHERE P.prop_key = 'favourite' AND P.resource_id = #{resource_id} AND P.user_id = U.id
+ WHERE P.prop_key = 'favourite' AND P.resource_id = #{id} AND P.user_id = U.id
</select>
<select id="selectGlobalProperties" resultType="Property">
</select>
<select id="selectProjectProperties" parameterType="String" resultType="Property">
- select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
- from properties p, projects r
- where p.resource_id=r.id and p.user_id is null and r.kee=#{id}
- </select>
+ select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+ from properties p, projects r
+ where p.resource_id=r.id and p.user_id is null and r.kee=#{id}
+ </select>
+
+ <select id="selectByKey" parameterType="map" resultType="Property">
+ select p.id as id, p.prop_key as "key", p.text_value as value, p.resource_id as resourceId, p.user_id as userId
+ from properties p
+ where p.prop_key=#{key}
+ <if test="resourceId == null">
+ AND p.resource_id is null
+ </if>
+ <if test="resourceId != null">
+ AND p.resource_id=#{resourceId}
+ </if>
+ <if test="userId == null">
+ AND p.user_id is null
+ </if>
+ <if test="userId != null">
+ AND p.user_id=#{userId}
+ </if>
+ </select>
+
+ <update id="update" parameterType="Property">
+ update properties set text_value = #{value} where id = #{id}
+ </update>
+
+ <insert id="insert" parameterType="Property" useGeneratedKeys="true" keyProperty="id">
+ INSERT INTO properties (prop_key, resource_id, user_id, text_value)
+ VALUES (#{key, jdbcType=VARCHAR}, #{resourceId, jdbcType=INTEGER}, #{userId, jdbcType=INTEGER}, #{value, jdbcType=VARCHAR})
+ </insert>
</mapper>
@Test
public void shouldFindUserIdsForFavouriteResource() throws Exception {
setupData("shouldFindUserIdsForFavouriteResource");
- List<String> userIds = dao.findUserIdsForFavouriteResource(2);
+ List<String> userIds = dao.findUserIdsForFavouriteResource(2L);
assertThat(userIds.size(), is(2));
assertThat(userIds, hasItems("user3", "user4"));
}
assertThat(first.getValue(), is("one"));
}
+ @Test
+ public void setProperty_update() throws Exception {
+ setupData("update");
+
+ dao.setProperty(new PropertyDto().setKey("global.key").setValue("new_global"));
+ dao.setProperty(new PropertyDto().setKey("project.key").setResourceId(10L).setValue("new_project"));
+ dao.setProperty(new PropertyDto().setKey("user.key").setUserId(100L).setValue("new_user"));
+
+ checkTables("update", "properties");
+ }
+
+ @Test
+ public void setProperty_insert() throws Exception {
+ setupData("insert");
+
+ dao.setProperty(new PropertyDto().setKey("global.key").setValue("new_global"));
+ dao.setProperty(new PropertyDto().setKey("project.key").setResourceId(10L).setValue("new_project"));
+ dao.setProperty(new PropertyDto().setKey("user.key").setUserId(100L).setValue("new_user"));
+
+ checkTables("insert", new String[]{"id"}, "properties");
+ }
+
private PropertyDto findById(List<PropertyDto> properties, int id) {
for (PropertyDto property : properties) {
if (property.getId() == id) {
--- /dev/null
+<dataset>
+
+ <!-- global -->
+ <properties prop_key="global.key" text_value="new_global" resource_id="[null]" user_id="[null]"/>
+
+ <!-- project -->
+ <properties prop_key="project.key" text_value="new_project" resource_id="10" user_id="[null]"/>
+
+ <!-- user -->
+ <properties prop_key="user.key" text_value="new_user" resource_id="[null]" user_id="100"/>
+
+</dataset>
--- /dev/null
+<dataset></dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+
+ <!-- global -->
+ <properties id="1" prop_key="global.key" text_value="new_global" resource_id="[null]" user_id="[null]"/>
+
+ <!-- project -->
+ <properties id="2" prop_key="project.key" text_value="new_project" resource_id="10" user_id="[null]"/>
+
+ <!-- user -->
+ <properties id="3" prop_key="user.key" text_value="new_user" resource_id="[null]" user_id="100"/>
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <!-- global -->
+ <properties id="1" prop_key="global.key" text_value="global" resource_id="[null]" user_id="[null]"/>
+
+ <!-- project -->
+ <properties id="2" prop_key="project.key" text_value="project" resource_id="10" user_id="[null]"/>
+
+ <!-- user -->
+ <properties id="3" prop_key="user.key" text_value="user" resource_id="[null]" user_id="100"/>
+
+</dataset>