diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-19 10:46:23 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-19 10:46:23 +0200 |
commit | d4a0a5ac884a5381bc20b0ab12015063704b830f (patch) | |
tree | 39c86b33ca98fc2dceeabe72aed6657d5284b02b /sonar-core | |
parent | c66a047068cc13b5e2fbcdb6572143ab296afe57 (diff) | |
download | sonarqube-d4a0a5ac884a5381bc20b0ab12015063704b830f.tar.gz sonarqube-d4a0a5ac884a5381bc20b0ab12015063704b830f.zip |
SONAR-5305 Return periods in /api/components/app
Diffstat (limited to 'sonar-core')
5 files changed, 292 insertions, 8 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java index 7637e4c70e1..3d6ac2c7deb 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceDao.java @@ -102,10 +102,22 @@ public class ResourceDao { return session.getMapper(ResourceMapper.class).selectResource(projectId); } + @CheckForNull public SnapshotDto getLastSnapshot(String resourceKey, SqlSession session) { return session.getMapper(ResourceMapper.class).selectLastSnapshotByResourceKey(resourceKey); } + @CheckForNull + public SnapshotDto getLastSnapshotByResourceId(long resourceId) { + SqlSession session = mybatis.openSession(false); + try { + return getLastSnapshotByResourceId(resourceId, session); + } finally { + MyBatis.closeQuietly(session); + } + } + + @CheckForNull public SnapshotDto getLastSnapshotByResourceId(long resourceId, SqlSession session) { return session.getMapper(ResourceMapper.class).selectLastSnapshotByResourceId(resourceId); } diff --git a/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java b/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java index a2f17e5a4fe..aa5207fb190 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/SnapshotDto.java @@ -19,13 +19,16 @@ */ package org.sonar.core.resource; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + import java.util.Date; public final class SnapshotDto { private Long id; private Long parentId; private Long rootId; - + private Date date; private Date buildDate; private Long resourceId; @@ -39,6 +42,24 @@ public final class SnapshotDto { private Integer depth; private Long rootProjectId; + private String period1Mode; + private String period2Mode; + private String period3Mode; + private String period4Mode; + private String period5Mode; + + private String period1Param; + private String period2Param; + private String period3Param; + private String period4Param; + private String period5Param; + + private Date period1Date; + private Date period2Date; + private Date period3Date; + private Date period4Date; + private Date period5Date; + public Long getId() { return id; } @@ -67,20 +88,20 @@ public final class SnapshotDto { } public Date getDate() { - return date;//NOSONAR May expose internal representation by returning reference to mutable object + return date; } public SnapshotDto setDate(Date date) { - this.date = date;// NOSONAR May expose internal representation by incorporating reference to mutable object + this.date = date; return this; } public Date getBuildDate() { - return buildDate;//NOSONAR May expose internal representation by returning reference to mutable object + return buildDate; } public SnapshotDto setBuildDate(Date buildDate) { - this.buildDate = buildDate;// NOSONAR May expose internal representation by incorporating reference to mutable object + this.buildDate = buildDate; return this; } @@ -173,4 +194,205 @@ public final class SnapshotDto { this.rootProjectId = rootProjectId; return this; } + + @CheckForNull + public String getPeriod1Mode() { + return period1Mode; + } + + public SnapshotDto setPeriod1Mode(@Nullable String period1Mode) { + this.period1Mode = period1Mode; + return this; + } + + @CheckForNull + public String getPeriod2Mode() { + return period2Mode; + } + + public SnapshotDto setPeriod2Mode(@Nullable String period2Mode) { + this.period2Mode = period2Mode; + return this; + } + + @CheckForNull + public String getPeriod3Mode() { + return period3Mode; + } + + public SnapshotDto setPeriod3Mode(@Nullable String period3Mode) { + this.period3Mode = period3Mode; + return this; + } + + @CheckForNull + public String getPeriod4Mode() { + return period4Mode; + } + + public SnapshotDto setPeriod4Mode(@Nullable String period4Mode) { + this.period4Mode = period4Mode; + return this; + } + + @CheckForNull + public String getPeriod5Mode() { + return period5Mode; + } + + public SnapshotDto setPeriod5Mode(@Nullable String period5Mode) { + this.period5Mode = period5Mode; + return this; + } + + public String getPeriodMode(int index) { + switch (index) { + case 1: + return period1Mode; + case 2: + return period2Mode; + case 3: + return period3Mode; + case 4: + return period4Mode; + case 5: + return period5Mode; + default: + throw new IndexOutOfBoundsException("Index of periodMode is between 1 and 5"); + } + } + + @CheckForNull + public String getPeriod1Param() { + return period1Param; + } + + public SnapshotDto setPeriod1Param(@Nullable String period1Param) { + this.period1Param = period1Param; + return this; + } + + @CheckForNull + public String getPeriod2Param() { + return period2Param; + } + + public SnapshotDto setPeriod2Param(@Nullable String period2Param) { + this.period2Param = period2Param; + return this; + } + + @CheckForNull + public String getPeriod3Param() { + return period3Param; + } + + public SnapshotDto setPeriod3Param(@Nullable String period3Param) { + this.period3Param = period3Param; + return this; + } + + @CheckForNull + public String getPeriod4Param() { + return period4Param; + } + + public SnapshotDto setPeriod4Param(@Nullable String period4Param) { + this.period4Param = period4Param; + return this; + } + + @CheckForNull + public String getPeriod5Param() { + return period5Param; + } + + public SnapshotDto setPeriod5Param(@Nullable String period5Param) { + this.period5Param = period5Param; + return this; + } + + public String getPeriodModeParameter(int periodIndex) { + switch (periodIndex) { + case 1: + return period1Param; + case 2: + return period2Param; + case 3: + return period3Param; + case 4: + return period4Param; + case 5: + return period5Param; + default: + throw new IndexOutOfBoundsException("Index of periodModeParameter is between 1 and 5"); + } + } + + @CheckForNull + public Date getPeriod1Date() { + return period1Date; + } + + public SnapshotDto setPeriod1Date(@Nullable Date period1Date) { + this.period1Date = period1Date; + return this; + } + + @CheckForNull + public Date getPeriod2Date() { + return period2Date; + } + + public SnapshotDto setPeriod2Date(@Nullable Date period2Date) { + this.period2Date = period2Date; + return this; + } + + @CheckForNull + public Date getPeriod3Date() { + return period3Date; + } + + public SnapshotDto setPeriod3Date(@Nullable Date period3Date) { + this.period3Date = period3Date; + return this; + } + + @CheckForNull + public Date getPeriod4Date() { + return period4Date; + } + + public SnapshotDto setPeriod4Date(@Nullable Date period4Date) { + this.period4Date = period4Date; + return this; + } + + @CheckForNull + public Date getPeriod5Date() { + return period5Date; + } + + public SnapshotDto setPeriod5Date(@Nullable Date period5Date) { + this.period5Date = period5Date; + return this; + } + + public Date getPeriodDate(int periodIndex) { + switch (periodIndex) { + case 1: + return period1Date; + case 2: + return period2Date; + case 3: + return period3Date; + case 4: + return period4Date; + case 5: + return period5Date; + default: + throw new IndexOutOfBoundsException("Index of periodDate is between 1 and 5"); + } + } } diff --git a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java index e0edb8cab56..d5932a7aba3 100644 --- a/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java +++ b/sonar-core/src/main/java/org/sonar/core/timemachine/Periods.java @@ -27,6 +27,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.database.model.Snapshot; import org.sonar.api.i18n.I18n; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.text.ParseException; @@ -44,38 +45,46 @@ public class Periods implements BatchExtension, ServerComponent { this.i18n = i18n; } + @CheckForNull public String label(Snapshot snapshot, int periodIndex) { return label(snapshot.getPeriodMode(periodIndex), snapshot.getPeriodModeParameter(periodIndex), snapshot.getPeriodDate(periodIndex)); } + @CheckForNull public String abbreviation(Snapshot snapshot, int periodIndex) { return abbreviation(snapshot.getPeriodMode(periodIndex), snapshot.getPeriodModeParameter(periodIndex), snapshot.getPeriodDate(periodIndex)); } + @CheckForNull public String label(int periodIndex) { String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex); PeriodParameters periodParameters = new PeriodParameters(periodProperty); return label(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); } + @CheckForNull public String abbreviation(int periodIndex) { String periodProperty = settings.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + periodIndex); PeriodParameters periodParameters = new PeriodParameters(periodProperty); return abbreviation(periodParameters.getMode(), periodParameters.getParam(), periodParameters.getDate()); } + @CheckForNull public String label(String mode, String param, Date date) { return label(mode, param, convertDate(date), false); } + @CheckForNull public String label(String mode, String param, String date) { return label(mode, param, date, false); } + @CheckForNull public String abbreviation(String mode, String param, Date date) { return label(mode, param, convertDate(date), true); } + @CheckForNull private String label(String mode, @Nullable String param, @Nullable String date, boolean shortLabel) { String label; if (CoreProperties.TIMEMACHINE_MODE_DAYS.equals(mode)) { @@ -117,6 +126,7 @@ public class Periods implements BatchExtension, ServerComponent { return i18n.message(getLocale(), msgKey, null, parameters); } + @CheckForNull private String convertDate(Date date) { if (date != null) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd"); diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml index d7fc5a305b6..df5db9f95bc 100644 --- a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceMapper.xml @@ -48,6 +48,21 @@ <result property="path" column="path"/> <result property="depth" column="depth"/> <result property="rootProjectId" column="root_project_id"/> + <result property="period1Mode" column="period1_mode"/> + <result property="period2Mode" column="period2_mode"/> + <result property="period3Mode" column="period3_mode"/> + <result property="period4Mode" column="period4_mode"/> + <result property="period5Mode" column="period5_mode"/> + <result property="period1Param" column="period1_param"/> + <result property="period2Param" column="period2_param"/> + <result property="period3Param" column="period3_param"/> + <result property="period4Param" column="period4_param"/> + <result property="period5Param" column="period5_param"/> + <result property="period1Date" column="period1_date"/> + <result property="period2Date" column="period2_date"/> + <result property="period3Date" column="period3_date"/> + <result property="period4Date" column="period4_date"/> + <result property="period5Date" column="period5_date"/> </resultMap> <resultMap id="resourceResultMap" type="Resource"> @@ -111,12 +126,21 @@ </select> <select id="selectLastSnapshotByResourceKey" parameterType="string" resultMap="snapshotResultMap"> - select s.* from snapshots s, projects p where p.kee=#{id} and p.enabled=${_true} and p.copy_resource_id is null and s.islast=${_true} and p.id=s.project_id + SELECT s.* FROM snapshots s + INNER JOIN projects p on p.id=s.project_id AND p.enabled=${_true} AND p.copy_resource_id IS NULL + <where> + AND p.kee=#{id} + AND s.islast=${_true} + </where> </select> <select id="selectLastSnapshotByResourceId" parameterType="string" resultMap="snapshotResultMap"> - select s.* from snapshots s where s.project_id=#{id} and s.islast=${_true} - </select> + SELECT s.* from snapshots s + <where> + AND s.project_id=#{id} + AND s.islast=${_true} + </where> + </select> <select id="selectDescendantProjects" parameterType="long" resultMap="resourceResultMap"> select * from projects where scope='PRJ' and root_id=#{id} diff --git a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java index 2ab9a786848..c40a43ea280 100644 --- a/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/resource/ResourceDaoTest.java @@ -439,6 +439,22 @@ public class ResourceDaoTest extends AbstractDaoTestCase { assertThat(dao.selectProvisionedProject("unknown")).isNull(); } + @Test + public void get_last_snapshot_by_resource_id() { + setupData("fixture"); + + SnapshotDto snapshotDto = dao.getLastSnapshotByResourceId(1L); + assertThat(snapshotDto.getId()).isEqualTo(1); + + snapshotDto = dao.getLastSnapshotByResourceId(2L); + assertThat(snapshotDto.getId()).isEqualTo(2L); + + snapshotDto = dao.getLastSnapshotByResourceId(3L); + assertThat(snapshotDto.getId()).isEqualTo(3L); + + assertThat(dao.getLastSnapshotByResourceId(42L)).isNull(); + } + private List<String> getKeys(final List<Component> components) { return newArrayList(Iterables.transform(components, new Function<Component, String>() { @Override |