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.api.batch;
021    
022    import com.google.common.collect.Lists;
023    import org.apache.commons.lang.builder.ToStringBuilder;
024    import org.sonar.api.measures.Metric;
025    import org.sonar.api.resources.Resource;
026    
027    import java.util.Arrays;
028    import java.util.Date;
029    import java.util.List;
030    
031    /**
032     * A class to query TimeMachine on a given resource
033     * <p/>
034     * <p>The query is constructed by setting filters on metrics and on dates</p>
035     * <p>It is to be noted that all filters will be applied regardless of their coherence</p>
036     *
037     * @since 1.10
038     */
039    public class TimeMachineQuery {
040    
041      private Resource resource;
042      private List<Metric> metrics;
043      private List<String> metricKeys;
044      private Date from;
045      private Date to;
046      private boolean onlyLastAnalysis = false;
047      private boolean fromCurrentAnalysis = false;
048      private boolean toCurrentAnalysis = false;
049    
050      /**
051       * <p>Create a TimeMachine query for a given resource
052       * </p>
053       * Apart from the resource the query is empty, i.e. will return all data for the resource
054       *
055       * @param resource the resource
056       */
057      public TimeMachineQuery(Resource resource) {
058        this.resource = resource;
059      }
060    
061      /**
062       * @return the resource of the Query
063       */
064      public Resource getResource() {
065        return resource;
066      }
067    
068      /**
069       * Sets the resource of the query
070       *
071       * @param resource the resource
072       * @return this
073       */
074      public TimeMachineQuery setResource(Resource resource) {
075        this.resource = resource;
076        return this;
077      }
078    
079      /**
080       * @return the metrics beloging to the query
081       */
082      public List<Metric> getMetrics() {
083        return metrics;
084      }
085    
086      /**
087       * Sets the metrics to return
088       *
089       * @param metrics the list of metrics
090       * @return this
091       */
092      public TimeMachineQuery setMetrics(List<Metric> metrics) {
093        this.metrics = metrics;
094        this.metricKeys = Lists.newLinkedList();
095        for (Metric metric : this.metrics) {
096          this.metricKeys.add(metric.getKey());
097        }
098        return this;
099      }
100    
101      public TimeMachineQuery setMetricKeys(String... metricKeys) {
102        this.metricKeys = Arrays.asList(metricKeys);
103        return this;
104      }
105    
106      public List<String> getMetricKeys() {
107        return metricKeys;
108      }
109    
110      public TimeMachineQuery setMetricKeys(List<String> metricKeys) {
111        this.metricKeys = metricKeys;
112        return this;
113      }
114    
115      /**
116       * Sets the metrics to return
117       *
118       * @param metrics the list of metrics
119       * @return this
120       */
121      public TimeMachineQuery setMetrics(Metric... metrics) {
122        this.metrics = Arrays.asList(metrics);
123        this.metricKeys = Lists.newLinkedList();
124        for (Metric metric : this.metrics) {
125          this.metricKeys.add(metric.getKey());
126        }
127        return this;
128      }
129    
130      /**
131       * Unsets the metrics
132       *
133       * @return this
134       */
135      public TimeMachineQuery unsetMetrics() {
136        this.metrics = null;
137        return this;
138      }
139    
140      /**
141       * @return the from date of the query
142       */
143      public Date getFrom() {
144        return from;
145      }
146    
147      /**
148       * Sets the from date to be used in the query
149       *
150       * @param from the from date
151       * @return this
152       */
153      public TimeMachineQuery setFrom(Date from) {
154        this.from = from;
155        return this;
156      }
157    
158      /**
159       * @param b whether to use the latest analysis as a from date
160       * @return this
161       */
162      public TimeMachineQuery setFromCurrentAnalysis(boolean b) {
163        this.fromCurrentAnalysis = b;
164        return this;
165      }
166    
167      /**
168       * @param b whether to use the latest analysis as a to date
169       * @return this
170       */
171      public TimeMachineQuery setToCurrentAnalysis(boolean b) {
172        this.toCurrentAnalysis = b;
173        return this;
174      }
175    
176      /**
177       * @return whether the latest analysis is used as a from date
178       */
179      public boolean isFromCurrentAnalysis() {
180        return fromCurrentAnalysis;
181      }
182    
183      /**
184       * @return whether the latest analysis is used as a to date
185       */
186      public boolean isToCurrentAnalysis() {
187        return toCurrentAnalysis;
188      }
189    
190      /**
191       * @return the to date of the query
192       */
193      public Date getTo() {
194        return to;
195      }
196    
197      /**
198       * Sets the to date to be used in the query
199       *
200       * @param to the to date
201       * @return this
202       */
203      public TimeMachineQuery setTo(Date to) {
204        this.to = to;
205        return this;
206      }
207    
208      /**
209       * @return whether to return only the latest analysis
210       */
211      public boolean isOnlyLastAnalysis() {
212        return onlyLastAnalysis;
213      }
214    
215      /**
216       *
217       * @param onlyLastAnalysis whether to only return the latest analysis
218       * @return this
219       */
220      public TimeMachineQuery setOnlyLastAnalysis(boolean onlyLastAnalysis) {
221        this.onlyLastAnalysis = onlyLastAnalysis;
222        return this;
223      }
224    
225      @Override
226      public String toString() {
227        return new ToStringBuilder(this)
228            .append("resource", resource)
229            .append("metrics", metrics)
230            .append("from", from)
231            .append("to", to)
232            .toString();
233      }
234    }