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.GroupsListProvider;
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 GroupsListQuery extends DefaultResourceListQuery {
43  
44    public static final String FILTER_NAME_NAME = "Name";
45    public static final String FILTER_ROLE_NAME = "Role";
46    private static final String FILTER_NAME_LABEL = "FILTERS.USERS.NAME.LABEL";
47    private static final String FILTER_ROLE_LABEL = "FILTERS.USERS.ROLE.LABEL";
48  
49    public static final String FILTER_TEXT_NAME = "textFilter";
50  
51    public GroupsListQuery() {
52      super();
53      this.availableFilters.add(createRoleFilter(Optional.empty()));
54    }
55  
56    /**
57     * Add a {@link ResourceListFilter} filter to the query with the given name
58     *
59     * @param name
60     *          the name to filter for
61     */
62    public void withName(String name) {
63      this.addFilter(createNameFilter(Optional.ofNullable(name)));
64    }
65  
66    /**
67     * Returns an {@link Optional} containing the name used to filter if set
68     *
69     * @return an {@link Optional} containing the name or none.
70     */
71    public Optional<String> getName() {
72      return this.getFilterValue(FILTER_NAME_NAME);
73    }
74  
75    /**
76     * Add a {@link ResourceListFilter} filter to the query with the given role
77     *
78     * @param role
79     *          the role to filter for
80     */
81    public void withRole(String role) {
82      this.addFilter(createRoleFilter(Optional.ofNullable(role)));
83    }
84  
85    /**
86     * Returns an {@link Optional} containing the role used to filter if set
87     *
88     * @return an {@link Optional} containing the role or none.
89     */
90    public Optional<String> getRole() {
91      return this.getFilterValue(FILTER_ROLE_NAME);
92    }
93  
94    /**
95     * Create a new {@link ResourceListFilter} based on a name
96     *
97     * @param name
98     *          the name to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
99     * @return a new {@link ResourceListFilter} for a name based query
100    */
101   public static ResourceListFilter<String> createNameFilter(Optional<String> name) {
102     return FiltersUtils.generateFilter(name, FILTER_NAME_NAME, FILTER_NAME_LABEL, SourceType.SELECT,
103             Optional.of(GroupsListProvider.NAME));
104   }
105 
106   public static ResourceListFilter<String> createRoleFilter(Optional<String> role) {
107     return FiltersUtils.generateFilter(role, FILTER_ROLE_NAME, FILTER_ROLE_LABEL, SourceType.SELECT,
108         Optional.of(GroupsListProvider.ROLE_ONLY));
109   }
110 
111 }