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.ServersListProvider;
28  import org.opencastproject.list.util.FiltersUtils;
29  
30  import java.util.Optional;
31  
32  /**
33   * Query for the servers list.
34   *
35   * The following filters can be used:
36   * <ul>
37   * <li>hostname</li>
38   * <li>status</li>
39   * </ul>
40   */
41  public class ServersListQuery extends DefaultResourceListQuery {
42  
43    /** Prefix for the filter labels. */
44    private static final String FILTER_LABEL_PREFIX = "FILTERS.SERVERS";
45    /** Hostname filter name. */
46    public static final String FILTER_NAME_HOSTNAME = "hostname";
47    /** Hostname filter label. */
48    public static final String FILTER_LABEL_HOSTNAME = FILTER_LABEL_PREFIX + ".HOSTNAME.LABEL";
49    /** NodeName filter name. */
50    public static final String FILTER_NAME_NODE_NAME = "nodeName";
51    /** NodeName filter label. */
52    public static final String FILTER_LABEL_NODE_NAME = FILTER_LABEL_PREFIX + ".NODE_NAME.LABEL";
53    /** Status filter name. */
54    public static final String FILTER_NAME_STATUS = "status";
55    /** Status filter label. */
56    public static final String FILTER_LABEL_STATUS = FILTER_LABEL_PREFIX + ".STATUS.LABEL";
57  
58    /** Default constructor. */
59    public ServersListQuery() {
60      super();
61      availableFilters.add(createHostnameFilter(Optional.<String> empty()));
62      availableFilters.add(createNodeNameFilter(Optional.<String> empty()));
63      availableFilters.add(createStatusFilter(Optional.<String> empty()));
64    }
65  
66    /**
67     * Copy constructor for the base class {@code ResourceListQuery}.
68     *
69     * @param query copy values from the given query
70     */
71    public ServersListQuery(ResourceListQuery query) {
72      this();
73      availableFilters.addAll(query.getAvailableFilters());
74  
75      for (ResourceListFilter filter : query.getFilters()) {
76        addFilter(filter);
77      }
78  
79      sortBy = query.getSortBy();
80      if (query.getOffset().isPresent()) {
81        setOffset(query.getOffset().get());
82      }
83      if (query.getLimit().isPresent()) {
84        setLimit(query.getLimit().get());
85      }
86    }
87  
88    /**
89     * Add a {@link ResourceListFilter} filter to the query with the given hostname.
90     *
91     * @param hostname the hostname to filter for
92     */
93    public void withHostname(String hostname) {
94      addFilter(createHostnameFilter(Optional.ofNullable(hostname)));
95    }
96  
97    /**
98     * Add a {@link ResourceListFilter} filter to the query with the given node name.
99     *
100    * @param nodeName the node name to filter for
101    */
102   public void withNodeName(String nodeName) {
103     addFilter(createNodeNameFilter(Optional.ofNullable(nodeName)));
104   }
105 
106   /**
107    * Add a {@link ResourceListFilter} filter to the query with the given status.
108    *
109    * @param status the status to filter for
110    */
111   public void withStatus(String status) {
112     addFilter(createStatusFilter(Optional.ofNullable(status)));
113   }
114 
115   /**
116    * Add a {@link ResourceListFilter} filter to the query with the given free text.
117    *
118    * @param freeText the free text to filter for
119    */
120   public void withFreeText(String freeText) {
121     addFilter(createFreeTextFilter(Optional.ofNullable(freeText)));
122   }
123 
124   /**
125    * Returns an {@link Optional} containing the hostname used to filter if set.
126    * {@link Optional#empty()} otherwise.
127    *
128    * @return an {@link Optional} containing the hostname or none.
129    */
130   public Optional<String> getHostname() {
131     return getFilterValue(FILTER_NAME_HOSTNAME);
132   }
133 
134   /**
135    * Returns an {@link Optional} containing the node name used to filter if set.
136    * {@link Optional#empty()} otherwise.
137    *
138    * @return an {@link Optional} containing the node name or none.
139    */
140   public Optional<String> getNodeName() {
141     return getFilterValue(FILTER_NAME_NODE_NAME);
142   }
143 
144   /**
145    * Returns an {@link Optional} containing the status used to filter if set.
146    * {@link Optional#empty()} otherwise.
147    *
148    * @return an {@link Optional} containing the status or none.
149    */
150   public Optional<String> getStatus() {
151     return getFilterValue(FILTER_NAME_STATUS);
152   }
153 
154   /**
155    * Returns an {@link Optional} containing the free text used to filter if set.
156    * {@link Optional#empty()} otherwise.
157    *
158    * @return an {@link Optional} containing the free text or none.
159    */
160   public Optional<String> getFreeText() {
161     return getFilterValue(ResourceListFilter.FREETEXT);
162   }
163 
164   /**
165    * Create a new {@link ResourceListFilter} based on a hostname.
166    *
167    * @param value the hostname to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
168    * @return a new {@link ResourceListFilter} for a hostname based query
169    */
170   public static ResourceListFilter<String> createHostnameFilter(Optional<String> value) {
171     return FiltersUtils.generateFilter(
172             value,
173             FILTER_NAME_HOSTNAME,
174             FILTER_LABEL_HOSTNAME,
175             ResourceListFilter.SourceType.SELECT,
176             Optional.of(ServersListProvider.LIST_HOSTNAME));
177   }
178 
179   /**
180    * Create a new {@link ResourceListFilter} based on a nodeName.
181    *
182    * @param value the nodeName to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
183    * @return a new {@link ResourceListFilter} for a nodeName based query
184    */
185   public static ResourceListFilter<String> createNodeNameFilter(Optional<String> value) {
186     return FiltersUtils.generateFilter(
187             value,
188             FILTER_NAME_NODE_NAME,
189             FILTER_LABEL_NODE_NAME,
190             ResourceListFilter.SourceType.SELECT,
191             Optional.of(ServersListProvider.LIST_NODE_NAME));
192   }
193 
194   /**
195    * Create a new {@link ResourceListFilter} based on a status.
196    *
197    * @param value the status to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
198    * @return a new {@link ResourceListFilter} for a status based query
199    */
200   public static ResourceListFilter<String> createStatusFilter(Optional<String> value) {
201     return FiltersUtils.generateFilter(
202             value,
203             FILTER_NAME_STATUS,
204             FILTER_LABEL_STATUS,
205             ResourceListFilter.SourceType.SELECT,
206             Optional.of(ServersListProvider.LIST_STATUS));
207   }
208 
209   /**
210    * Create a new {@link ResourceListFilter} based on a free text.
211    *
212    * @param value the free text to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
213    * @return a new {@link ResourceListFilter} for a free text based query
214    */
215   public static <String> ResourceListFilter<String> createFreeTextFilter(Optional<String> value) {
216     return FiltersUtils.generateFilter(
217             value,
218             ResourceListFilter.FREETEXT,
219             ResourceListFilter.FREETEXT,
220             ResourceListFilter.SourceType.FREETEXT,
221             null);
222   }
223 }