aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentQuery.java
blob: 0c86f8b6e66efda0a1d39e7f83a2e4f8a3edf8c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * SonarQube
 * Copyright (C) 2009-2025 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.db.component;

import java.util.Date;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.db.WildcardPosition;

import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.db.DaoUtils.buildLikeValue;

public class ComponentQuery {
  private final String nameOrKeyQuery;
  private final boolean partialMatchOnKey;
  private final String[] qualifiers;
  private final Boolean isPrivate;
  private final Set<String> componentUuids;
  private final Set<String> componentKeys;
  private final Long analyzedBefore;
  private final Long allBranchesAnalyzedBefore;
  private final Date createdAfter;
  private final boolean onProvisionedOnly;

  private ComponentQuery(Builder builder) {
    this.nameOrKeyQuery = builder.nameOrKeyQuery;
    this.partialMatchOnKey = builder.partialMatchOnKey == null ? false : builder.partialMatchOnKey;
    this.qualifiers = builder.qualifiers;
    this.componentUuids = builder.componentUuids;
    this.componentKeys = builder.componentKeys;
    this.isPrivate = builder.isPrivate;
    this.analyzedBefore = builder.analyzedBefore;
    this.allBranchesAnalyzedBefore = builder.allBranchesAnalyzedBefore;
    this.createdAfter = builder.createdAfter;
    this.onProvisionedOnly = builder.onProvisionedOnly;
  }

  public String[] getQualifiers() {
    return qualifiers;
  }

  @CheckForNull
  public String getNameOrKeyQuery() {
    return nameOrKeyQuery;
  }

  /**
   * Used by MyBatis mapper
   */
  @CheckForNull
  public String getNameOrKeyUpperLikeQuery() {
    return buildLikeValue(nameOrKeyQuery, WildcardPosition.BEFORE_AND_AFTER).toUpperCase(Locale.ENGLISH);
  }

  /**
   * Used by MyBatis mapper
   */
  public boolean isPartialMatchOnKey() {
    return partialMatchOnKey;
  }

  @CheckForNull
  public Set<String> getComponentUuids() {
    return componentUuids;
  }

  @CheckForNull
  public Set<String> getComponentKeys() {
    return componentKeys;
  }

  @CheckForNull
  public Boolean getPrivate() {
    return isPrivate;
  }

  @CheckForNull
  public Long getAnalyzedBefore() {
    return analyzedBefore;
  }

  @CheckForNull
  public Long getAllBranchesAnalyzedBefore() {
    return allBranchesAnalyzedBefore;
  }

  @CheckForNull
  public Date getCreatedAfter() {
    return createdAfter;
  }

  public boolean isOnProvisionedOnly() {
    return onProvisionedOnly;
  }

  boolean hasEmptySetOfComponents() {
    return Stream.of(componentKeys, componentUuids)
      .anyMatch(list -> list != null && list.isEmpty());
  }

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

  public static class Builder {
    private String nameOrKeyQuery;
    private Boolean partialMatchOnKey;
    private String[] qualifiers;
    private Boolean isPrivate;
    private Set<String> componentUuids;
    private Set<String> componentKeys;
    private Long analyzedBefore;
    private Long allBranchesAnalyzedBefore;
    private Date createdAfter;
    private boolean onProvisionedOnly = false;

    public Builder setNameOrKeyQuery(@Nullable String nameOrKeyQuery) {
      this.nameOrKeyQuery = nameOrKeyQuery;
      return this;
    }

    /**
     * Beware, can be resource intensive! Should be used with precautions.
     */
    public Builder setPartialMatchOnKey(@Nullable Boolean partialMatchOnKey) {
      this.partialMatchOnKey = partialMatchOnKey;
      return this;
    }

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

    public Builder setComponentUuids(@Nullable Set<String> componentUuids) {
      this.componentUuids = componentUuids;
      return this;
    }

    public Builder setComponentKeys(@Nullable Set<String> componentKeys) {
      this.componentKeys = componentKeys;
      return this;
    }

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

    public Builder setAnalyzedBefore(@Nullable Long l) {
      this.analyzedBefore = l;
      return this;
    }

    public Builder setAllBranchesAnalyzedBefore(@Nullable Long l) {
      this.allBranchesAnalyzedBefore = l;
      return this;
    }

    public Builder setCreatedAfter(@Nullable Date l) {
      this.createdAfter = l;
      return this;
    }

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

    public ComponentQuery build() {
      checkArgument(qualifiers != null && qualifiers.length > 0, "At least one qualifier must be provided");
      checkArgument(nameOrKeyQuery != null || partialMatchOnKey == null, "A query must be provided if a partial match on key is specified.");

      return new ComponentQuery(this);
    }
  }
}