All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.server.issue.IssueQuery Maven / Gradle / Ivy

There is a newer version: 7.2.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.issue;

import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.sonar.api.rule.RuleKey;

import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.server.es.SearchOptions.MAX_LIMIT;

/**
 * @since 3.6
 */
public class IssueQuery {

  public static final String SORT_BY_CREATION_DATE = "CREATION_DATE";
  public static final String SORT_BY_UPDATE_DATE = "UPDATE_DATE";
  public static final String SORT_BY_CLOSE_DATE = "CLOSE_DATE";
  public static final String SORT_BY_ASSIGNEE = "ASSIGNEE";
  public static final String SORT_BY_SEVERITY = "SEVERITY";
  public static final String SORT_BY_STATUS = "STATUS";

  /**
   * Sort by project, file path then line id
   */
  public static final String SORT_BY_FILE_LINE = "FILE_LINE";

  public static final Set SORTS = ImmutableSet.of(SORT_BY_CREATION_DATE, SORT_BY_UPDATE_DATE, SORT_BY_CLOSE_DATE, SORT_BY_ASSIGNEE, SORT_BY_SEVERITY,
    SORT_BY_STATUS, SORT_BY_FILE_LINE);

  private final Collection issueKeys;
  private final Collection severities;
  private final Collection statuses;
  private final Collection resolutions;
  private final Collection components;
  private final Collection modules;
  private final Collection moduleRoots;
  private final Collection projects;
  private final Collection directories;
  private final Collection files;
  private final Collection views;
  private final Collection rules;
  private final Collection assignees;
  private final Collection authors;
  private final Collection languages;
  private final Collection tags;
  private final Collection types;
  private final Map createdAfterByProjectUuids;
  private final Boolean onComponentOnly;
  private final Boolean assigned;
  private final Boolean resolved;
  private final Date createdAt;
  private final PeriodStart createdAfter;
  private final Date createdBefore;
  private final String sort;
  private final Boolean asc;
  private final String facetMode;
  private final String organizationUuid;
  private final String branchUuid;
  private final boolean mainBranch;
  private final boolean checkAuthorization;

  private IssueQuery(Builder builder) {
    this.issueKeys = defaultCollection(builder.issueKeys);
    this.severities = defaultCollection(builder.severities);
    this.statuses = defaultCollection(builder.statuses);
    this.resolutions = defaultCollection(builder.resolutions);
    this.components = defaultCollection(builder.components);
    this.modules = defaultCollection(builder.modules);
    this.moduleRoots = defaultCollection(builder.moduleRoots);
    this.projects = defaultCollection(builder.projects);
    this.directories = defaultCollection(builder.directories);
    this.files = defaultCollection(builder.files);
    this.views = defaultCollection(builder.views);
    this.rules = defaultCollection(builder.rules);
    this.assignees = defaultCollection(builder.assignees);
    this.authors = defaultCollection(builder.authors);
    this.languages = defaultCollection(builder.languages);
    this.tags = defaultCollection(builder.tags);
    this.types = defaultCollection(builder.types);
    this.createdAfterByProjectUuids = defaultMap(builder.createdAfterByProjectUuids);
    this.onComponentOnly = builder.onComponentOnly;
    this.assigned = builder.assigned;
    this.resolved = builder.resolved;
    this.createdAt = builder.createdAt;
    this.createdAfter = builder.createdAfter;
    this.createdBefore = builder.createdBefore;
    this.sort = builder.sort;
    this.asc = builder.asc;
    this.checkAuthorization = builder.checkAuthorization;
    this.facetMode = builder.facetMode;
    this.organizationUuid = builder.organizationUuid;
    this.branchUuid = builder.branchUuid;
    this.mainBranch = builder.mainBranch;
  }

  public Collection issueKeys() {
    return issueKeys;
  }

  public Collection severities() {
    return severities;
  }

  public Collection statuses() {
    return statuses;
  }

  public Collection resolutions() {
    return resolutions;
  }

  public Collection componentUuids() {
    return components;
  }

  public Collection moduleUuids() {
    return modules;
  }

  public Collection moduleRootUuids() {
    return moduleRoots;
  }

  public Collection projectUuids() {
    return projects;
  }

  public Collection directories() {
    return directories;
  }

  public Collection fileUuids() {
    return files;
  }

  public Collection viewUuids() {
    return views;
  }

  public Collection rules() {
    return rules;
  }

  public Collection assignees() {
    return assignees;
  }

  public Collection authors() {
    return authors;
  }

  public Collection languages() {
    return languages;
  }

  public Collection tags() {
    return tags;
  }

  public Collection types() {
    return types;
  }

  public Map createdAfterByProjectUuids() {
    return createdAfterByProjectUuids;
  }

  @CheckForNull
  public Boolean onComponentOnly() {
    return onComponentOnly;
  }

  @CheckForNull
  public Boolean assigned() {
    return assigned;
  }

  @CheckForNull
  public Boolean resolved() {
    return resolved;
  }

  @CheckForNull
  public PeriodStart createdAfter() {
    return createdAfter;
  }

  @CheckForNull
  public Date createdAt() {
    return createdAt == null ? null : new Date(createdAt.getTime());
  }

  @CheckForNull
  public Date createdBefore() {
    return createdBefore == null ? null : new Date(createdBefore.getTime());
  }

  @CheckForNull
  public String sort() {
    return sort;
  }

  @CheckForNull
  public Boolean asc() {
    return asc;
  }

  public boolean checkAuthorization() {
    return checkAuthorization;
  }

  @CheckForNull
  public String organizationUuid() {
    return organizationUuid;
  }

  @CheckForNull
  public String branchUuid() {
    return branchUuid;
  }

  public boolean isMainBranch() {
    return mainBranch;
  }

  public String facetMode() {
    return facetMode;
  }

  @Override
  public String toString() {
    return ReflectionToStringBuilder.toString(this);
  }

  public static Builder builder() {
    return new Builder();
  }

  public static class Builder {
    private Collection issueKeys;
    private Collection severities;
    private Collection statuses;
    private Collection resolutions;
    private Collection components;
    private Collection modules;
    private Collection moduleRoots;
    private Collection projects;
    private Collection directories;
    private Collection files;
    private Collection views;
    private Collection rules;
    private Collection assignees;
    private Collection authors;
    private Collection languages;
    private Collection tags;
    private Collection types;
    private Map createdAfterByProjectUuids;
    private Boolean onComponentOnly = false;
    private Boolean assigned = null;
    private Boolean resolved = null;
    private Date createdAt;
    private PeriodStart createdAfter;
    private Date createdBefore;
    private String sort;
    private Boolean asc = false;
    private boolean checkAuthorization = true;
    private String facetMode;
    private String organizationUuid;
    private String branchUuid;
    private boolean mainBranch = true;

    private Builder() {

    }

    public Builder issueKeys(@Nullable Collection l) {
      this.issueKeys = l;
      return this;
    }

    public Builder severities(@Nullable Collection l) {
      this.severities = l;
      return this;
    }

    public Builder statuses(@Nullable Collection l) {
      this.statuses = l;
      return this;
    }

    public Builder resolutions(@Nullable Collection l) {
      this.resolutions = l;
      return this;
    }

    public Builder componentUuids(@Nullable Collection l) {
      this.components = l;
      return this;
    }

    public Builder moduleUuids(@Nullable Collection l) {
      this.modules = l;
      return this;
    }

    public Builder moduleRootUuids(@Nullable Collection l) {
      this.moduleRoots = l;
      return this;
    }

    public Builder projectUuids(@Nullable Collection l) {
      this.projects = l;
      return this;
    }

    public Builder directories(@Nullable Collection l) {
      this.directories = l;
      return this;
    }

    public Builder fileUuids(@Nullable Collection l) {
      this.files = l;
      return this;
    }

    public Builder viewUuids(@Nullable Collection l) {
      this.views = l;
      return this;
    }

    public Builder rules(@Nullable Collection rules) {
      this.rules = rules;
      return this;
    }

    public Builder assignees(@Nullable Collection l) {
      this.assignees = l;
      return this;
    }

    public Builder authors(@Nullable Collection l) {
      this.authors = l;
      return this;
    }

    public Builder languages(@Nullable Collection l) {
      this.languages = l;
      return this;
    }

    public Builder tags(@Nullable Collection t) {
      this.tags = t;
      return this;
    }

    public Builder types(@Nullable Collection t) {
      this.types = t;
      return this;
    }

    public Builder createdAfterByProjectUuids(@Nullable Map createdAfterByProjectUuids) {
      this.createdAfterByProjectUuids = createdAfterByProjectUuids;
      return this;
    }

    /**
     * If true, it will return only issues on the passed component(s)
     * If false, it will return all issues on the passed component(s) and their descendants
     */
    public Builder onComponentOnly(@Nullable Boolean b) {
      this.onComponentOnly = b;
      return this;
    }

    /**
     * If true, it will return all issues assigned to someone
     * If false, it will return all issues not assigned to someone
     */
    public Builder assigned(@Nullable Boolean b) {
      this.assigned = b;
      return this;
    }

    /**
     * If true, it will return all resolved issues
     * If false, it will return all none resolved issues
     */
    public Builder resolved(@Nullable Boolean resolved) {
      this.resolved = resolved;
      return this;
    }

    public Builder createdAt(@Nullable Date d) {
      this.createdAt = d == null ? null : new Date(d.getTime());
      return this;
    }

    public Builder createdAfter(@Nullable Date d) {
      this.createdAfter(d, true);
      return this;
    }

    public Builder createdAfter(@Nullable Date d, boolean inclusive) {
      this.createdAfter = d == null ? null : new PeriodStart(new Date(d.getTime()), inclusive);
      return this;
    }

    public Builder createdBefore(@Nullable Date d) {
      this.createdBefore = d == null ? null : new Date(d.getTime());
      return this;
    }

    public Builder sort(@Nullable String s) {
      if (s != null && !SORTS.contains(s)) {
        throw new IllegalArgumentException("Bad sort field: " + s);
      }
      this.sort = s;
      return this;
    }

    public Builder asc(@Nullable Boolean asc) {
      this.asc = asc;
      return this;
    }

    public IssueQuery build() {
      if (issueKeys != null) {
        checkArgument(issueKeys.size() <= MAX_LIMIT, "Number of issue keys must be less than " + MAX_LIMIT + " (got " + issueKeys.size() + ")");
      }
      return new IssueQuery(this);
    }

    public Builder checkAuthorization(boolean checkAuthorization) {
      this.checkAuthorization = checkAuthorization;
      return this;
    }

    public Builder facetMode(String facetMode) {
      this.facetMode = facetMode;
      return this;
    }

    public Builder organizationUuid(String s) {
      this.organizationUuid = s;
      return this;
    }

    public Builder branchUuid(@Nullable String s) {
      this.branchUuid = s;
      return this;
    }

    public Builder mainBranch(boolean mainBranch) {
      this.mainBranch = mainBranch;
      return this;
    }
  }

  private static  Collection defaultCollection(@Nullable Collection c) {
    return c == null ? Collections.emptyList() : Collections.unmodifiableCollection(c);
  }

  private static  Map defaultMap(@Nullable Map map) {
    return map == null ? Collections.emptyMap() : Collections.unmodifiableMap(map);
  }

  public static class PeriodStart {
    private final Date date;
    private final boolean inclusive;

    public PeriodStart(Date date, boolean inclusive) {
      this.date = date;
      this.inclusive = inclusive;

    }

    public Date date() {
      return date;
    }

    public boolean inclusive() {
      return inclusive;
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy