001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.components;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.ReflectionToStringBuilder;
024    import org.sonar.api.CoreProperties;
025    import org.sonar.api.database.model.Snapshot;
026    import org.sonar.api.utils.DateUtils;
027    
028    import javax.annotation.Nullable;
029    
030    import java.util.Calendar;
031    import java.util.Date;
032    
033    public class PastSnapshot {
034    
035      private int index;
036      private String mode, modeParameter;
037      private Snapshot projectSnapshot;
038      private Date targetDate = null;
039    
040      public PastSnapshot(String mode, @Nullable Date targetDate, @Nullable Snapshot projectSnapshot) {
041        this.mode = mode;
042        if (targetDate != null) {
043          this.targetDate = org.apache.commons.lang.time.DateUtils.truncate(targetDate, Calendar.SECOND);
044        }
045        this.projectSnapshot = projectSnapshot;
046      }
047    
048      public PastSnapshot(String mode, @Nullable Date targetDate) {
049        this(mode, targetDate, null);
050      }
051    
052      /**
053       * See SONAR-2428 : even if previous analysis does not exist (no snapshot and no target date), we should perform comparison.
054       */
055      public PastSnapshot(String mode) {
056        this(mode, null, null);
057      }
058    
059      public PastSnapshot setIndex(int index) {
060        this.index = index;
061        return this;
062      }
063    
064      public int getIndex() {
065        return index;
066      }
067    
068      public boolean isRelatedToSnapshot() {
069        return projectSnapshot != null;
070      }
071    
072      public Snapshot getProjectSnapshot() {
073        return projectSnapshot;
074      }
075    
076      public Date getDate() {
077        return projectSnapshot != null ? projectSnapshot.getCreatedAt() : null;
078      }
079    
080      public PastSnapshot setMode(String mode) {
081        this.mode = mode;
082        return this;
083      }
084    
085      public String getMode() {
086        return mode;
087      }
088    
089      public String getModeParameter() {
090        return modeParameter;
091      }
092    
093      public PastSnapshot setModeParameter(String s) {
094        this.modeParameter = s;
095        return this;
096      }
097    
098      Integer getProjectSnapshotId() {
099        return projectSnapshot != null ? projectSnapshot.getId() : null;
100      }
101    
102      public String getQualifier() {
103        return projectSnapshot != null ? projectSnapshot.getQualifier() : null;
104      }
105    
106      /**
107       * @deprecated in 4.2. Target date should only be used in labels.
108       */
109      @Deprecated
110      public Date getTargetDate() {
111        return targetDate;
112      }
113    
114      public PastSnapshot clonePastSnapshot(){
115        PastSnapshot clone = new PastSnapshot(mode, targetDate, projectSnapshot);
116        clone.setIndex(index);
117        clone.setModeParameter(modeParameter);
118        return clone;
119      }
120    
121      @Override
122      public String toString() {
123        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_VERSION)) {
124          String label = String.format("Compare to version %s", modeParameter);
125          if (targetDate != null) {
126            label += String.format(" (%s)", DateUtils.formatDate(getDate()));
127          }
128          return label;
129        }
130        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DAYS)) {
131          String label = String.format("Compare over %s days (%s", modeParameter, DateUtils.formatDate(targetDate));
132          if (isRelatedToSnapshot()) {
133            label += ", analysis of " + getDate();
134          }
135          label += ")";
136          return label;
137        }
138        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS)) {
139          String label = "Compare to previous analysis";
140          if (isRelatedToSnapshot()) {
141            label += String.format(" (%s)", DateUtils.formatDate(getDate()));
142          }
143          return label;
144        }
145        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION)) {
146          String label = "Compare to previous version";
147          if (isRelatedToSnapshot()) {
148            label += String.format(" (%s)", DateUtils.formatDate(getDate()));
149          }
150          return label;
151        }
152        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DATE)) {
153          String label = "Compare to date " + DateUtils.formatDate(targetDate);
154          if (isRelatedToSnapshot()) {
155            label += String.format(" (analysis of %s)", DateUtils.formatDate(getDate()));
156          }
157          return label;
158        }
159        return ReflectionToStringBuilder.toString(this);
160      }
161    
162    }