From d4a0a5ac884a5381bc20b0ab12015063704b830f Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 19 May 2014 10:46:23 +0200 Subject: [PATCH] SONAR-5305 Return periods in /api/components/app --- .../org/sonar/core/resource/ResourceDao.java | 12 + .../org/sonar/core/resource/SnapshotDto.java | 232 +++++++++++++++++- .../org/sonar/core/timemachine/Periods.java | 10 + .../sonar/core/resource/ResourceMapper.xml | 30 ++- .../sonar/core/resource/ResourceDaoTest.java | 16 ++ .../component/ws/ComponentAppAction.java | 74 ++++-- .../ws/components-app-example-show.json | 6 +- .../component/ws/ComponentAppActionTest.java | 14 +- .../server/component/ws/ComponentsWsTest.java | 3 +- .../measure/MeasureFilterExecutorTest.java | 1 + .../ws/ComponentAppActionTest/app.json | 3 + 11 files changed, 365 insertions(+), 36 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 @@ + + + + + + + + + + + + + + + @@ -111,12 +126,21 @@ + SELECT s.* from snapshots s + + AND s.project_id=#{id} + AND s.islast=${_true} + +