View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.list.common.query;
23  
24  import org.opencastproject.list.api.DefaultResourceListQuery;
25  import org.opencastproject.list.api.ResourceListFilter;
26  import org.opencastproject.list.api.ResourceListFilter.SourceType;
27  import org.opencastproject.list.common.provider.UsersListProvider;
28  import org.opencastproject.list.util.FiltersUtils;
29  
30  import java.util.Optional;
31  
32  /**
33   * Query for the users list.
34   *
35   * The following filters can be used:
36   * <ul>
37   * <li>name</li>
38   * <li>role</li>
39   * <li>provider</li>
40   * </ul>
41   */
42  public class UsersListQuery extends DefaultResourceListQuery {
43  
44    public static final String FILTER_NAME_NAME = "Name";
45    private static final String FILTER_NAME_LABEL = "FILTERS.USERS.NAME.LABEL";
46  
47    public static final String FILTER_ROLE_NAME = "Role";
48    private static final String FILTER_ROLE_LABEL = "FILTERS.USERS.ROLE.LABEL";
49  
50    public static final String FILTER_PROVIDER_NAME = "Provider";
51    private static final String FILTER_PROVIDER_LABEL = "FILTERS.USERS.PROVIDER.LABEL";
52  
53    public static final String FILTER_TEXT_NAME = "textFilter";
54  
55    public UsersListQuery() {
56      super();
57      this.availableFilters.add(createRoleFilter(Optional.<String> empty()));
58      this.availableFilters.add(createProviderFilter(Optional.<String> empty()));
59    }
60  
61    /**
62     * Add a {@link ResourceListFilter} filter to the query with the given name
63     *
64     * @param name
65     *          the name to filter for
66     */
67    public void withName(String name) {
68      this.addFilter(createNameFilter(Optional.ofNullable(name)));
69    }
70  
71    /**
72     * Returns an {@link Optional} containing the name used to filter if set
73     *
74     * @return an {@link Optional} containing the name or none.
75     */
76    public Optional<String> getName() {
77      return this.getFilterValue(FILTER_NAME_NAME);
78    }
79  
80    /**
81     * Add a {@link ResourceListFilter} filter to the query with the given role
82     *
83     * @param role
84     *          the role to filter for
85     */
86    public void withRole(String role) {
87      this.addFilter(createRoleFilter(Optional.ofNullable(role)));
88    }
89  
90    /**
91     * Returns an {@link Optional} containing the role used to filter if set
92     *
93     * @return an {@link Optional} containing the role or none.
94     */
95    public Optional<String> getRole() {
96      return this.getFilterValue(FILTER_ROLE_NAME);
97    }
98  
99    /**
100    * Add a {@link ResourceListFilter} filter to the query with the given provider
101    *
102    * @param provider
103    *          the provider to filter for
104    */
105   public void withProvider(String provider) {
106     this.addFilter(createProviderFilter(Optional.ofNullable(provider)));
107   }
108 
109   /**
110    * Returns an {@link Optional} containing the provider used to filter if set
111    *
112    * @return an {@link Optional} containing the provider or none.
113    */
114   public Optional<String> getProvider() {
115     return this.getFilterValue(FILTER_PROVIDER_NAME);
116   }
117 
118   /**
119    * Create a new {@link ResourceListFilter} based on a name
120    *
121    * @param name
122    *          the name to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
123    * @return a new {@link ResourceListFilter} for a name based query
124    */
125   public static ResourceListFilter<String> createNameFilter(Optional<String> name) {
126     return FiltersUtils.generateFilter(name, FILTER_NAME_NAME, FILTER_NAME_LABEL, SourceType.SELECT,
127             Optional.of(UsersListProvider.NAME_ONLY));
128   }
129 
130   /**
131    * Create a new {@link ResourceListFilter} based on a role
132    *
133    * @param role
134    *          the role to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
135    * @return a new {@link ResourceListFilter} for a role based query
136    */
137   public static ResourceListFilter<String> createRoleFilter(Optional<String> role) {
138     return FiltersUtils.generateFilter(role, FILTER_ROLE_NAME, FILTER_ROLE_LABEL, SourceType.SELECT,
139             Optional.of(UsersListProvider.ROLE_ONLY));
140   }
141 
142   /**
143    * Create a new {@link ResourceListFilter} based on a provider
144    *
145    * @param provider
146    *          the provider to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
147    * @return a new {@link ResourceListFilter} for a provider based query
148    */
149   public static ResourceListFilter<String> createProviderFilter(Optional<String> provider) {
150     return FiltersUtils.generateFilter(provider, FILTER_PROVIDER_NAME, FILTER_PROVIDER_LABEL, SourceType.SELECT,
151             Optional.of(UsersListProvider.USERDIRECTORY_ONLY));
152   }
153 
154 }