appendChildProjects(subProject.getId(), mapper, resources);
}
}
+
+ public ResourceDao insertOrUpdate(ResourceDto... resources) {
+ SqlSession session = mybatis.openSession();
+ ResourceMapper mapper = session.getMapper(ResourceMapper.class);
+ try {
+ for (ResourceDto resource : resources) {
+ if (resource.getId()==null) {
+ mapper.insert(resource);
+ } else {
+ mapper.update(resource);
+ }
+ }
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ return this;
+ }
}
private String key;
private String name;
private String longName;
- private Integer rootId;
+ private Long rootId;
private String scope;
private String qualifier;
+ private boolean enabled = true;
+ private String description;
+ private String language;
+ private Long copyResourceId;
+ private Long personId;
+ private Long profileId;
public Long getId() {
return id;
return this;
}
- public Integer getRootId() {
+ public Long getRootId() {
return rootId;
}
- public ResourceDto setRootId(Integer rootId) {
+ public ResourceDto setRootId(Long rootId) {
this.rootId = rootId;
return this;
}
return this;
}
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((id == null) ? 0 : id.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- ResourceDto other = (ResourceDto) obj;
- if (id == null) {
- if (other.id != null) {
- return false;
- }
- } else if (!id.equals(other.id)) {
- return false;
- }
- return true;
+ public boolean isEnabled() {
+ return enabled;
}
+ public ResourceDto setEnabled(boolean b) {
+ this.enabled = b;
+ return this;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public ResourceDto setDescription(String description) {
+ this.description = description;
+ return this;
+ }
+
+ public String getLanguage() {
+ return language;
+ }
+
+ public ResourceDto setLanguage(String language) {
+ this.language = language;
+ return this;
+ }
+
+ public Long getCopyResourceId() {
+ return copyResourceId;
+ }
+
+ public ResourceDto setCopyResourceId(Long copyResourceId) {
+ this.copyResourceId = copyResourceId;
+ return this;
+ }
+
+ public Long getPersonId() {
+ return personId;
+ }
+
+ public ResourceDto setPersonId(Long personId) {
+ this.personId = personId;
+ return this;
+ }
+
+ public Long getProfileId() {
+ return profileId;
+ }
+
+ public ResourceDto setProfileId(Long profileId) {
+ this.profileId = profileId;
+ return this;
+ }
}
ResourceIndexerMapper mapper = session.getMapper(ResourceIndexerMapper.class);
ResourceDto resource = mapper.selectResourceToIndex(id);
if (resource != null) {
- Integer rootId = resource.getRootId();
+ Long rootId = resource.getRootId();
if (rootId == null) {
- rootId = resource.getId().intValue();
+ rootId = resource.getId();
}
- indexed = indexResource(resource.getId().intValue(), resource.getName(), resource.getQualifier(), rootId, session, mapper);
+ indexed = indexResource(resource.getId(), resource.getName(), resource.getQualifier(), rootId, session, mapper);
}
return indexed;
} finally {
return indexed;
}
- private boolean indexResource(int id, String name, String qualifier, int rootId, SqlSession session, ResourceIndexerMapper mapper) {
+ private boolean indexResource(long id, String name, String qualifier, long rootId, SqlSession session, ResourceIndexerMapper mapper) {
boolean indexed = false;
String key = nameToKey(name);
if (key.length() >= MINIMUM_KEY_SIZE) {
* If the resource is indexed with a different key, then this index is dropped and the
* resource must be indexed again.
*/
- private boolean sanitizeIndex(int resourceId, String key, ResourceIndexerMapper mapper) {
+ private boolean sanitizeIndex(long resourceId, String key, ResourceIndexerMapper mapper) {
ResourceIndexDto masterIndex = mapper.selectMasterIndexByResourceId(resourceId);
if (masterIndex != null && !StringUtils.equals(key, masterIndex.getKey())) {
// resource has been renamed -> drop existing indexes
* @since 3.2
*/
void selectResources(ResourceQuery query, ResultHandler resultHandler);
+
+ void insert(ResourceDto resource);
+
+ void update(ResourceDto resource);
}
*/
public final class ResourceQuery {
private String[] qualifiers = null;
+ private String key = null;
+ private boolean excludeDisabled = false;
private ResourceQuery() {
}
this.qualifiers = qualifiers;
return this;
}
+
+ public String getKey() {
+ return key;
+ }
+
+ public ResourceQuery setKey(String key) {
+ this.key = key;
+ return this;
+ }
+
+ public boolean isExcludeDisabled() {
+ return excludeDisabled;
+ }
+
+ public ResourceQuery setExcludeDisabled(boolean b) {
+ this.excludeDisabled = b;
+ 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.resource.ResourceMapper">
+
+ <resultMap id="snapshotResultMap" type="Snapshot">
+ <id property="id" column="id"/>
+ <result property="parentId" column="parent_snapshot_id"/>
+ <result property="rootId" column="root_snapshot_id"/>
+ <result property="date" column="created_at"/>
+ <result property="buildDate" column="build_date"/>
+ <result property="resourceId" column="project_id"/>
+ <result property="status" column="status"/>
+ <result property="purgeStatus" column="purge_status"/>
+ <result property="last" column="islast"/>
+ <result property="scope" column="scope"/>
+ <result property="qualifier" column="qualifier"/>
+ <result property="version" column="version"/>
+ <result property="path" column="path"/>
+ <result property="depth" column="depth"/>
+ <result property="rootProjectId" column="root_project_id"/>
+ </resultMap>
+
+ <resultMap id="resourceResultMap" type="Resource">
+ <id property="id" column="id"/>
+ <result property="key" column="kee"/>
+ <result property="name" column="name"/>
+ <result property="longName" column="long_name"/>
+ <result property="rootId" column="root_id"/>
+ <result property="scope" column="scope"/>
+ <result property="qualifier" column="qualifier"/>
+ <result property="enabled" column="enabled"/>
+ <result property="description" column="description"/>
+ <result property="language" column="language"/>
+ <result property="copyResourceId" column="copy_resource_id"/>
+ <result property="personId" column="person_id"/>
+ <result property="profileId" column="profile_id"/>
+ </resultMap>
+
+ <select id="selectResources" parameterType="map" resultMap="resourceResultMap">
+ select *
+ from projects p
+ <where>
+ <if test="qualifiers != null and qualifiers.length!=0">
+ and p.qualifier in
+ <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
+ </if>
+ <if test="key != null">
+ and p.kee=#{key}
+ </if>
+ <if test="excludeDisabled">
+ and p.enabled=${_true}
+ </if>
+ </where>
+ </select>
+
+ <select id="selectResourceIds" parameterType="map" resultType="long">
+ select p.id
+ from projects p
+ <where>
+ <if test="qualifiers != null and qualifiers.length!=0">
+ and p.qualifier in
+ <foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
+ </if>
+ <if test="key != null">
+ and p.kee=#{key}
+ </if>
+ <if test="excludeDisabled">
+ and p.enabled=${_true}
+ </if>
+ </where>
+ </select>
+
+ <select id="selectResource" parameterType="long" resultMap="resourceResultMap">
+ select * from projects where id=#{id}
+ </select>
+
+ <select id="selectSnapshot" parameterType="long" resultMap="snapshotResultMap">
+ select * from snapshots where id=#{id}
+ </select>
+
+ <select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap">
+ select * from projects where scope='PRJ' and root_id=#{id}
+ </select>
+
+ <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id">
+ <selectKey order="BEFORE" resultType="Long" keyProperty="id">
+ select projects_seq.NEXTVAL from DUAL
+ </selectKey>
+
+ insert into projects
+ (id, name, long_name, description, scope, qualifier, kee, language, root_id, copy_resource_id, person_id, profile_id, enabled)
+ values (
+ #{id}, #{name, jdbcType=VARCHAR}, #{longName, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{scope, jdbcType=VARCHAR}, #{qualifier, jdbcType=VARCHAR},
+ #{key, jdbcType=VARCHAR}, #{language, jdbcType=VARCHAR}, #{rootId, jdbcType=INTEGER}, #{copyResourceId, jdbcType=INTEGER},
+ #{personId, jdbcType=INTEGER}, #{profileId, jdbcType=INTEGER}, #{enabled, jdbcType=BOOLEAN}
+ )
+ </insert>
+
+ <update id="update" parameterType="Resource">
+ update projects set name=#{name, jdbcType=VARCHAR}, long_name=#{longName, jdbcType=VARCHAR}, description=#{description, jdbcType=VARCHAR},
+ scope=#{scope, jdbcType=VARCHAR}, qualifier=#{qualifier, jdbcType=VARCHAR}, kee=#{key, jdbcType=VARCHAR},
+ language=#{language, jdbcType=VARCHAR}, root_id=#{rootId, jdbcType=INTEGER}, copy_resource_id=#{copyResourceId, jdbcType=INTEGER},
+ person_id=#{personId, jdbcType=INTEGER}, profile_id=#{profileId, jdbcType=INTEGER}, enabled=#{enabled, jdbcType=BOOLEAN} where id=#{id,jdbcType=INTEGER}
+ </update>
+</mapper>
+
<result property="rootId" column="root_id"/>
<result property="scope" column="scope"/>
<result property="qualifier" column="qualifier"/>
+ <result property="enabled" column="enabled"/>
+ <result property="description" column="description"/>
+ <result property="language" column="language"/>
+ <result property="copyResourceId" column="copy_resource_id"/>
+ <result property="personId" column="person_id"/>
+ <result property="profileId" column="profile_id"/>
</resultMap>
<select id="selectResources" parameterType="map" resultMap="resourceResultMap">
and p.qualifier in
<foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
</if>
+ <if test="key != null">
+ and p.kee=#{key}
+ </if>
+ <if test="excludeDisabled">
+ and p.enabled=${_true}
+ </if>
</where>
</select>
and p.qualifier in
<foreach item="qualifier" index="index" collection="qualifiers" open="(" separator="," close=")">#{qualifier}</foreach>
</if>
+ <if test="key != null">
+ and p.kee=#{key}
+ </if>
+ <if test="excludeDisabled">
+ and p.enabled=${_true}
+ </if>
</where>
</select>
<select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap">
select * from projects where scope='PRJ' and root_id=#{id}
</select>
+
+ <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="id">
+ insert into projects
+ (name, long_name, description, scope, qualifier, kee, language, root_id, copy_resource_id, person_id, profile_id, enabled)
+ values (
+ #{name, jdbcType=VARCHAR}, #{longName, jdbcType=VARCHAR}, #{description, jdbcType=VARCHAR}, #{scope, jdbcType=VARCHAR}, #{qualifier, jdbcType=VARCHAR},
+ #{key, jdbcType=VARCHAR}, #{language, jdbcType=VARCHAR}, #{rootId, jdbcType=INTEGER}, #{copyResourceId, jdbcType=INTEGER},
+ #{personId, jdbcType=INTEGER}, #{profileId, jdbcType=INTEGER}, #{enabled, jdbcType=BOOLEAN}
+ )
+ </insert>
+
+ <update id="update" parameterType="Resource">
+ update projects set name=#{name, jdbcType=VARCHAR}, long_name=#{longName, jdbcType=VARCHAR}, description=#{description, jdbcType=VARCHAR},
+ scope=#{scope, jdbcType=VARCHAR}, qualifier=#{qualifier, jdbcType=VARCHAR}, kee=#{key, jdbcType=VARCHAR},
+ language=#{language, jdbcType=VARCHAR}, root_id=#{rootId, jdbcType=INTEGER}, copy_resource_id=#{copyResourceId, jdbcType=INTEGER},
+ person_id=#{personId, jdbcType=INTEGER}, profile_id=#{profileId, jdbcType=INTEGER}, enabled=#{enabled, jdbcType=BOOLEAN} where id=#{id,jdbcType=INTEGER}
+ </update>
</mapper>
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.List;
assertThat(resource.getName()).isEqualTo("Struts");
assertThat(resource.getLongName()).isEqualTo("Apache Struts");
assertThat(resource.getScope()).isEqualTo("PRJ");
+ assertThat(resource.getDescription()).isEqualTo("the description");
+ assertThat(resource.getLanguage()).isEqualTo("java");
+ assertThat(resource.isEnabled()).isTrue();
}
@Test
assertThat(resources).hasSize(4);
}
+ @Test
+ public void getResources_filter_by_key() {
+ setupData("fixture");
+
+ List<ResourceDto> resources = dao.getResources(ResourceQuery.create().setKey("org.struts:struts-core"));
+ assertThat(resources).hasSize(1);
+ assertThat(resources.get(0).getKey()).isEqualTo("org.struts:struts-core");
+ }
+
@Test
public void getResourceIds_all() {
setupData("fixture");
ids = dao.getResourceIds(ResourceQuery.create().setQualifiers(new String[]{}));
assertThat(ids).hasSize(4);
}
+
+ @Test
+ public void getResources_exclude_disabled() {
+ setupData("getResources_exclude_disabled");
+
+ assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(false))).containsOnly(1L, 2L);
+ assertThat(dao.getResourceIds(ResourceQuery.create().setExcludeDisabled(true))).containsOnly(2L);
+ }
+
+ @Test
+ public void insertOrUpdate() {
+ setupData("insertOrUpdate");
+
+ // update because already persisted
+ ResourceDto project = new ResourceDto()
+ .setKey("org.struts:struts").setScope(Scopes.PROJECT).setQualifier(Qualifiers.PROJECT)
+ .setName("Struts").setLongName("Apache Struts").setLanguage("java").setDescription("MVC Framework")
+ .setId(1L);
+
+ // insert
+ ResourceDto file1 = new ResourceDto()
+ .setKey("org.struts:struts:org.struts.Action").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
+ .setLanguage("java").setName("Action").setLongName("org.struts.Action");
+ ResourceDto file2 = new ResourceDto()
+ .setKey("org.struts:struts:org.struts.Filter").setScope(Scopes.FILE).setQualifier(Qualifiers.FILE)
+ .setLanguage("java").setName("Filter").setLongName("org.struts.Filter");
+
+ dao.insertOrUpdate(project, file1, file2);
+
+ assertThat(project.getId()).isNotNull();
+ assertThat(file1.getId()).isNotNull();
+ assertThat(file2.getId()).isNotNull();
+ checkTables("insertOrUpdate", "projects");
+ }
+
}
<!-- root project -->
<projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
- description="[null]" long_name="Apache Struts"
+ description="the description" long_name="Apache Struts"
enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
<snapshots id="1" project_id="1" parent_snapshot_id="[null]" root_project_id="1" root_snapshot_id="[null]"
status="P" islast="[false]" purge_status="[null]"
--- /dev/null
+<dataset>
+
+ <!-- disabled -->
+ <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ description="the description" long_name="Apache Struts"
+ enabled="[false]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+
+ <!-- enabled -->
+ <projects id="2" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"/>
+</dataset>
--- /dev/null
+<dataset>
+
+ <projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ description="MVC Framework" long_name="Apache Struts"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/>
+
+ <projects id="2" root_id="[null]" scope="FIL" qualifier="FIL" kee="org.struts:struts:org.struts.Action" name="Action"
+ description="[null]" long_name="org.struts.Action"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/>
+
+ <projects id="3" root_id="[null]" scope="FIL" qualifier="FIL" kee="org.struts:struts:org.struts.Filter" name="Filter"
+ description="[null]" long_name="org.struts.Filter"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" profile_id="[null]"/>
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <projects id="1" root_id="200" scope="PRJ" qualifier="TRK" kee="old key" name="old name"
+ description="old name" long_name="old long name"
+ enabled="[false]" language="old" copy_resource_id="2" person_id="3" profile_id="4"/>
+
+</dataset>