001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
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 java.util.Date;
029    
030    public class PastSnapshot {
031    
032      private int index;
033      private String mode, modeParameter;
034      private Snapshot projectSnapshot;
035      private Date targetDate;
036    
037      public PastSnapshot(String mode, Date targetDate, Snapshot projectSnapshot) {
038        this.mode = mode;
039        this.targetDate = targetDate;
040        this.projectSnapshot = projectSnapshot;
041      }
042    
043      public PastSnapshot(String mode, Date targetDate) {
044        this(mode, targetDate, null);
045      }
046    
047      /**
048       * See SONAR-2428 : even if previous analysis does not exist (no snapshot and no target date), we should perform comparison.
049       */
050      public PastSnapshot(String mode) {
051        this(mode, null, null);
052      }
053    
054      public PastSnapshot setIndex(int index) {
055        this.index = index;
056        return this;
057      }
058    
059      public int getIndex() {
060        return index;
061      }
062    
063      public boolean isRelatedToSnapshot() {
064        return projectSnapshot != null;
065      }
066    
067      public Snapshot getProjectSnapshot() {
068        return projectSnapshot;
069      }
070    
071      public Date getDate() {
072        return (projectSnapshot != null ? projectSnapshot.getCreatedAt() : null);
073      }
074    
075      public String getMode() {
076        return mode;
077      }
078    
079      public String getModeParameter() {
080        return modeParameter;
081      }
082    
083      public PastSnapshot setModeParameter(String s) {
084        this.modeParameter = s;
085        return this;
086      }
087    
088      Integer getProjectSnapshotId() {
089        return (projectSnapshot != null ? projectSnapshot.getId() : null);
090      }
091    
092      public String getQualifier() {
093        return (projectSnapshot != null ? projectSnapshot.getQualifier() : null);
094      }
095    
096      public Date getTargetDate() {
097        return targetDate;
098      }
099    
100      @Override
101      public String toString() {
102        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_VERSION)) {
103          String label = String.format("Compare to version %s", modeParameter);
104          if (getTargetDate() != null) {
105            label += String.format(" (%s)", DateUtils.formatDate(getTargetDate()));
106          }
107          return label;
108        }
109        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DAYS)) {
110          String label = String.format("Compare over %s days (%s", modeParameter, DateUtils.formatDate(getTargetDate()));
111          if (isRelatedToSnapshot()) {
112            label += ", analysis of " + getDate();
113          }
114          label += ")";
115          return label;
116        }
117        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS)) {
118          String label = "Compare to previous analysis";
119          if (isRelatedToSnapshot()) {
120            label += String.format(" (%s)", DateUtils.formatDate(getDate()));
121          }
122          return label;
123        }
124        if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DATE)) {
125          String label = "Compare to date " + DateUtils.formatDate(getTargetDate());
126          if (isRelatedToSnapshot()) {
127            label += String.format(" (analysis of %s)", DateUtils.formatDate(getDate()));
128          }
129          return label;
130        }
131        return ReflectionToStringBuilder.toString(this);
132      }
133    
134    }