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 */
020package org.sonar.api.user;
021
022import org.apache.commons.lang.StringUtils;
023
024import javax.annotation.CheckForNull;
025import javax.annotation.Nullable;
026import java.util.Arrays;
027import java.util.Collection;
028
029/**
030 * @since 3.6
031 */
032public class UserQuery {
033
034  public static final UserQuery ALL_ACTIVES = UserQuery.builder().build();
035
036  private final Collection<String> logins;
037  private final boolean includeDeactivated;
038  private final String searchText;
039
040  // for internal use in MyBatis
041  final String searchTextSql;
042
043  private UserQuery(Builder builder) {
044    this.logins = builder.logins;
045    this.includeDeactivated = builder.includeDeactivated;
046    this.searchText = builder.searchText;
047
048    this.searchTextSql = searchTextToSql(searchText);
049  }
050
051  private String searchTextToSql(@Nullable String s) {
052    String sql = null;
053    if (s != null) {
054      sql = StringUtils.replace(s, "%", "/%");
055      sql = StringUtils.replace(sql, "_", "/_");
056      sql = "%" + sql + "%";
057    }
058    return sql;
059  }
060
061  @CheckForNull
062  public Collection<String> logins() {
063    return logins;
064  }
065
066  public boolean includeDeactivated() {
067    return includeDeactivated;
068  }
069
070  /**
071   * Search for logins or names containing a given string
072   */
073  @CheckForNull
074  public String searchText() {
075    return searchText;
076  }
077
078  public static Builder builder() {
079    return new Builder();
080  }
081
082  public static class Builder {
083    private boolean includeDeactivated = false;
084    private Collection<String> logins;
085    private String searchText;
086
087    private Builder() {
088    }
089
090    public Builder includeDeactivated() {
091      this.includeDeactivated = true;
092      return this;
093    }
094
095    public Builder logins(@Nullable Collection<String> logins) {
096      // TODO clone logins
097      this.logins = logins;
098      return this;
099    }
100
101    public Builder logins(String... logins) {
102      this.logins = Arrays.asList(logins);
103      return this;
104    }
105
106    public Builder searchText(@Nullable String s) {
107      this.searchText = StringUtils.defaultIfBlank(s, null);
108      return this;
109    }
110
111    public UserQuery build() {
112      if (logins != null && logins.size() >= 1000) {
113        throw new IllegalArgumentException("Max number of logins is 1000. Got " + logins.size());
114      }
115      return new UserQuery(this);
116    }
117  }
118}