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.wsclient.services;
021    
022    import java.util.Date;
023    
024    /**
025     * @since 2.5
026     */
027    public class TimeMachineQuery extends Query<TimeMachine> {
028    
029      public static final String BASE_URL = "/api/timemachine";
030    
031      private String resourceKeyOrId;
032      private String[] metrics;
033      private Date from;
034      private Date to;
035    
036      private String model;
037      private String[] characteristicKeys;
038    
039      public TimeMachineQuery(String resourceKeyOrId) {
040        this.resourceKeyOrId = resourceKeyOrId;
041      }
042    
043      public String[] getMetrics() {
044        return metrics;
045      }
046    
047      public TimeMachineQuery setMetrics(String... metrics) {
048        this.metrics = metrics;
049        return this;
050      }
051    
052      public Date getFrom() {
053        return from;
054      }
055    
056      public TimeMachineQuery setFrom(Date from) {
057        this.from = from;
058        return this;
059      }
060    
061      public Date getTo() {
062        return to;
063      }
064    
065      public TimeMachineQuery setTo(Date to) {
066        this.to = to;
067        return this;
068      }
069    
070      public TimeMachineQuery setCharacteristicKeys(String model, String... keys) {
071        this.model = model;
072        this.characteristicKeys = keys;
073        return this;
074      }
075    
076      @Override
077      public String getUrl() {
078        StringBuilder url = new StringBuilder(BASE_URL);
079        url.append('?');
080        appendUrlParameter(url, "resource", resourceKeyOrId);
081        appendUrlParameter(url, "metrics", metrics);
082        appendUrlParameter(url, "fromDateTime", from, true);
083        appendUrlParameter(url, "toDateTime", to, true);
084        appendUrlParameter(url, "model", model);
085        appendUrlParameter(url, "characteristics", characteristicKeys);
086        return url.toString();
087      }
088    
089      @Override
090      public Class<TimeMachine> getModelClass() {
091        return TimeMachine.class;
092      }
093    
094      public static TimeMachineQuery createForMetrics(String resourceKeyOrId, String... metricKeys) {
095        return new TimeMachineQuery(resourceKeyOrId).setMetrics(metricKeys);
096      }
097    
098      public static TimeMachineQuery createForMetrics(Resource resource, String... metricKeys) {
099        Integer id = resource.getId();
100        if (id == null) {
101          throw new IllegalArgumentException("id must be set");
102        }
103        return new TimeMachineQuery(id.toString()).setMetrics(metricKeys);
104      }
105    
106    }