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.ResourceListQuery;
27  import org.opencastproject.list.common.provider.BooleanListProvider;
28  import org.opencastproject.list.common.provider.ServersListProvider;
29  import org.opencastproject.list.common.provider.ServicesListProvider;
30  import org.opencastproject.list.util.FiltersUtils;
31  
32  import java.util.Optional;
33  
34  /**
35   * Query for the services list.
36   *
37   * The following filters can be used:
38   * <ul>
39   * <li>name</li>
40   * <li>host</li>
41   * <li>status</li>
42   * <li>actions</li>
43   * </ul>
44   */
45  public class ServicesListQuery extends DefaultResourceListQuery {
46  
47    /** Prefix for the filter labels. */
48    private static final String FILTER_PREFIX = "FILTERS.SERVICES";
49    /** Name filter name. */
50    public static final String FILTER_NAME_NAME = "name";
51    /** Name filter label. */
52    public static final String FILTER_LABEL_NAME = FILTER_PREFIX + ".NAME.LABEL";
53    /** Hostname filter name. */
54    public static final String FILTER_NAME_HOSTNAME = "hostname";
55    /** Hostname filter label. */
56    public static final String FILTER_LABEL_HOSTNAME = FILTER_PREFIX + ".HOSTNAME.LABEL";
57    /** NodeName filter name. */
58    public static final String FILTER_NAME_NODE_NAME = "nodeName";
59    /** NodeName filter label. */
60    public static final String FILTER_LABEL_NODE_NAME = FILTER_PREFIX + ".NODE_NAME.LABEL";
61    /** Status filter name. */
62    public static final String FILTER_NAME_STATUS = "status";
63    /** Status filter label. */
64    public static final String FILTER_LABEL_STATUS = FILTER_PREFIX + ".STATUS.LABEL";
65    /** Actions filter name. */
66    public static final String FILTER_NAME_ACTIONS = "actions";
67    /** Actions filter label. */
68    public static final String FILTER_LABEL_ACTIONS = FILTER_PREFIX + ".ACTIONS.LABEL";
69  
70    /** Default constructor. */
71    public ServicesListQuery() {
72      super();
73      this.availableFilters.add(createNameFilter(Optional.<String> empty()));
74      this.availableFilters.add(createHostnameFilter(Optional.<String> empty()));
75      this.availableFilters.add(createNodeNameFilter(Optional.<String> empty()));
76      this.availableFilters.add(createStatusFilter(Optional.<String> empty()));
77      this.availableFilters.add(createActionsFilter(Optional.<Boolean> empty()));
78    }
79  
80    /**
81     * Copy constructor for the base class {@code ResourceListQuery}.
82     *
83     * @param query copy values from the given query
84     */
85    public ServicesListQuery(ResourceListQuery query) {
86      this();
87      availableFilters.addAll(query.getAvailableFilters());
88  
89      for (ResourceListFilter filter : query.getFilters()) {
90        addFilter(filter);
91      }
92  
93      sortBy = query.getSortBy();
94      if (query.getOffset().isPresent()) {
95        setOffset(query.getOffset().get());
96      }
97      if (query.getLimit().isPresent()) {
98        setLimit(query.getLimit().get());
99      }
100   }
101 
102   /**
103    * Add a {@link ResourceListFilter} filter to the query with the given name.
104    *
105    * @param name the name to filter for
106    */
107   public void withName(Optional<String> name) {
108     addFilter(createNameFilter(name));
109   }
110 
111   /**
112    * Add a {@link ResourceListFilter} filter to the query with the given hostname.
113    *
114    * @param hostname the hostname to filter for
115    */
116   public void withHostname(Optional<String> hostname) {
117     addFilter(createHostnameFilter(hostname));
118   }
119 
120   /**
121    * Add a {@link ResourceListFilter} filter to the query with the given node name.
122    *
123    * @param nodeName the node name to filter for
124    */
125   public void withNodeName(String nodeName) {
126     addFilter(createNodeNameFilter(Optional.ofNullable(nodeName)));
127   }
128 
129   /**
130    * Add a {@link ResourceListFilter} filter to the query with the given status.
131    *
132    * @param status the status to filter for
133    */
134   public void withStatus(Optional<String> status) {
135     addFilter(createStatusFilter(status));
136   }
137 
138   /**
139    * Add a {@link ResourceListFilter} filter to the query with any actions.
140    *
141    * @param hasActions any actions to filter
142    */
143   public void withActions(Optional<Boolean> hasActions) {
144     addFilter(createActionsFilter(hasActions));
145   }
146 
147   /**
148    * Add a {@link ResourceListFilter} filter to the query with the given free text.
149    *
150    * @param freeText the free text to filter for
151    */
152   public void withFreeText(Optional<String> freeText) {
153     addFilter(createFreeTextFilter(freeText));
154   }
155 
156   /**
157    * Returns an {@link Optional} containing the name used to filter if set.
158    * {@link Optional#empty()} otherwise.
159    *
160    * @return an {@link Optional} containing the name or none.
161    */
162   public Optional<String> getName() {
163     return getFilterValue(FILTER_NAME_NAME);
164   }
165 
166   /**
167    * Returns an {@link Optional} containing the hostname used to filter if set.
168    * {@link Optional#empty()} otherwise.
169    *
170    * @return an {@link Optional} containing the hostname or none.
171    */
172   public Optional<String> getHostname() {
173     return getFilterValue(FILTER_NAME_HOSTNAME);
174   }
175 
176   /**
177    * Returns an {@link Optional} containing the node name used to filter if set.
178    * {@link Optional#empty()} otherwise.
179    *
180    * @return an {@link Optional} containing the node name or none.
181    */
182   public Optional<String> getNodeName() {
183     return getFilterValue(FILTER_NAME_NODE_NAME);
184   }
185 
186   /**
187    * Returns an {@link Optional} containing the status used to filter if set.
188    * {@link Optional#empty()} otherwise.
189    *
190    * @return an {@link Optional} containing the status or none.
191    */
192   public Optional<String> getStatus() {
193     return getFilterValue(FILTER_NAME_STATUS);
194   }
195 
196   /**
197    * Returns an {@link Optional} containing boolean value used to filter any actions if set.
198    * {@link Optional#empty()} otherwise.
199    *
200    * @return an {@link Optional} containing the boolean value for any actions or none.
201    */
202   public Optional<Boolean> getActions() {
203     Optional result = getFilterValue(FILTER_NAME_ACTIONS);
204     if (result.isPresent()) {
205       if (result.get() instanceof String) {
206         return BooleanListProvider.parseValue((String)result.get());
207       }
208     }
209     return result;
210   }
211 
212   /**
213    * Returns an {@link Optional} containing the free text used to filter if set.
214    * {@link Optional#empty()} otherwise.
215    *
216    * @return an {@link Optional} containing the free text or none.
217    */
218   public Optional<String> getFreeText() {
219     return getFilterValue(ResourceListFilter.FREETEXT);
220   }
221 
222   /**
223    * Create a new {@link ResourceListFilter} based on a name.
224    *
225    * @param value the name to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
226    * @return a new {@link ResourceListFilter} for a name based query
227    */
228   public static <String> ResourceListFilter<String> createNameFilter(Optional<String> value) {
229     return FiltersUtils.generateFilter(
230             value,
231             FILTER_NAME_NAME,
232             FILTER_LABEL_NAME,
233             ResourceListFilter.SourceType.SELECT,
234             Optional.of(ServicesListProvider.LIST_NAME));
235   }
236 
237   /**
238    * Create a new {@link ResourceListFilter} based on a hostname.
239    *
240    * @param value the hostname to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
241    * @return a new {@link ResourceListFilter} for a hostname based query
242    */
243   public static <String> ResourceListFilter<String> createHostnameFilter(Optional<String> value) {
244     return FiltersUtils.generateFilter(
245             value,
246             FILTER_NAME_HOSTNAME,
247             FILTER_LABEL_HOSTNAME,
248             ResourceListFilter.SourceType.SELECT,
249             Optional.of(ServersListProvider.LIST_HOSTNAME));
250   }
251 
252   /**
253    * Create a new {@link ResourceListFilter} based on a nodeName.
254    *
255    * @param value the nodeName to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
256    * @return a new {@link ResourceListFilter} for a nodeName based query
257    */
258   public static ResourceListFilter<String> createNodeNameFilter(Optional<String> value) {
259     return FiltersUtils.generateFilter(
260             value,
261             FILTER_NAME_NODE_NAME,
262             FILTER_LABEL_NODE_NAME,
263             ResourceListFilter.SourceType.SELECT,
264             Optional.of(ServersListProvider.LIST_NODE_NAME));
265   }
266 
267   /**
268    * Create a new {@link ResourceListFilter} based on a status.
269    *
270    * @param value the status to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
271    * @return a new {@link ResourceListFilter} for a status based query
272    */
273   public static <String> ResourceListFilter<String> createStatusFilter(Optional<String> value) {
274     return FiltersUtils.generateFilter(
275             value,
276             FILTER_NAME_STATUS,
277             FILTER_LABEL_STATUS,
278             ResourceListFilter.SourceType.SELECT,
279             Optional.of(ServicesListProvider.LIST_STATUS));
280   }
281 
282   /**
283    * Create a new {@link ResourceListFilter} based on any actions.
284    *
285    * @param value the boolean value for any action to filter on wrapped in an {@link Optional} or
286    *        {@link Optional#empty()}
287    * @return a new {@link ResourceListFilter} for a any actions based query
288    */
289   public static <Boolean> ResourceListFilter<Boolean> createActionsFilter(Optional<Boolean> value) {
290     return FiltersUtils.generateFilter(
291             value,
292             FILTER_NAME_ACTIONS,
293             FILTER_LABEL_ACTIONS,
294             ResourceListFilter.SourceType.SELECT,
295             Optional.of(BooleanListProvider.YES_NO));
296   }
297 
298   /**
299    * Create a new {@link ResourceListFilter} based on a free text.
300    *
301    * @param value the free text to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
302    * @return a new {@link ResourceListFilter} for a free text based query
303    */
304   public static <String> ResourceListFilter<String> createFreeTextFilter(Optional<String> value) {
305     return FiltersUtils.generateFilter(
306             value,
307             ResourceListFilter.FREETEXT,
308             ResourceListFilter.FREETEXT,
309             ResourceListFilter.SourceType.FREETEXT,
310             null);
311   }
312 }