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