001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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    
021    package org.sonar.wsclient.services;
022    
023    import java.util.Date;
024    
025    public class EventQuery extends Query<Event> {
026    
027      public static final String BASE_URL = "/api/events?";
028    
029      private Date fromDate;
030      private boolean includeFromTime;
031      private Date toDate;
032      private boolean includeToTime;
033      private String resourceKey;
034      private String[] categories;
035    
036      public EventQuery() {
037      }
038    
039      public EventQuery(String resourceKey) {
040        this.resourceKey = resourceKey;
041      }
042    
043      public Date getFrom() {
044        return fromDate;
045      }
046    
047      public EventQuery setFrom(Date fromDate, boolean includeTime) {
048        this.fromDate = fromDate;
049        this.includeFromTime = includeTime;
050        return this;
051      }
052    
053      public boolean isIncludeFromTime() {
054        return includeFromTime;
055      }
056    
057      public Date getTo() {
058        return toDate;
059      }
060    
061      public EventQuery setTo(Date toDate, boolean includeTime) {
062        this.toDate = toDate;
063        this.includeFromTime = includeTime;
064        return this;
065      }
066    
067      public boolean isIncludeToTime() {
068        return includeToTime;
069      }
070    
071      public String getResourceKey() {
072        return resourceKey;
073      }
074    
075      public EventQuery setResourceKey(String resourceKey) {
076        this.resourceKey = resourceKey;
077        return this;
078      }
079    
080      public String[] getCategories() {
081        return categories;
082      }
083    
084      public EventQuery setCategories(String[] categories) {
085        this.categories = categories;
086        return this;
087      }
088    
089      @Override
090      public String getUrl() {
091        StringBuilder url = new StringBuilder(BASE_URL);
092        appendUrlParameter(url, "resource", resourceKey);
093        appendUrlParameter(url, "categories", categories);
094        if (fromDate != null) {
095          if (includeFromTime) {
096            appendUrlParameter(url, "fromDateTime", fromDate, true);
097          } else {
098            appendUrlParameter(url, "fromDate", fromDate, false);
099          }
100        }
101        if (toDate != null) {
102          if (includeToTime) {
103            appendUrlParameter(url, "toDateTime", toDate, true);
104          } else {
105            appendUrlParameter(url, "toDate", toDate, false);
106          }
107        }
108        return url.toString();
109      }
110    
111      @Override
112      public Class<Event> getModelClass() {
113        return Event.class;
114      }
115    }