ServicesListQuery.java

/*
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License
 * at:
 *
 *   http://opensource.org/licenses/ecl2.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

package org.opencastproject.index.service.resources.list.query;


import org.opencastproject.index.service.resources.list.provider.BooleanListProvider;
import org.opencastproject.index.service.resources.list.provider.ServersListProvider;
import org.opencastproject.index.service.resources.list.provider.ServicesListProvider;
import org.opencastproject.index.service.util.FiltersUtils;
import org.opencastproject.list.api.ResourceListFilter;
import org.opencastproject.list.api.ResourceListQuery;
import org.opencastproject.list.impl.ResourceListQueryImpl;

import java.util.Optional;

/**
 * Query for the services list.
 *
 * The following filters can be used:
 * <ul>
 * <li>name</li>
 * <li>host</li>
 * <li>status</li>
 * <li>actions</li>
 * </ul>
 */
public class ServicesListQuery extends ResourceListQueryImpl {

  /** Prefix for the filter labels. */
  private static final String FILTER_PREFIX = "FILTERS.SERVICES";
  /** Name filter name. */
  public static final String FILTER_NAME_NAME = "name";
  /** Name filter label. */
  public static final String FILTER_LABEL_NAME = FILTER_PREFIX + ".NAME.LABEL";
  /** Hostname filter name. */
  public static final String FILTER_NAME_HOSTNAME = "hostname";
  /** Hostname filter label. */
  public static final String FILTER_LABEL_HOSTNAME = FILTER_PREFIX + ".HOSTNAME.LABEL";
  /** NodeName filter name. */
  public static final String FILTER_NAME_NODE_NAME = "nodeName";
  /** NodeName filter label. */
  public static final String FILTER_LABEL_NODE_NAME = FILTER_PREFIX + ".NODE_NAME.LABEL";
  /** Status filter name. */
  public static final String FILTER_NAME_STATUS = "status";
  /** Status filter label. */
  public static final String FILTER_LABEL_STATUS = FILTER_PREFIX + ".STATUS.LABEL";
  /** Actions filter name. */
  public static final String FILTER_NAME_ACTIONS = "actions";
  /** Actions filter label. */
  public static final String FILTER_LABEL_ACTIONS = FILTER_PREFIX + ".ACTIONS.LABEL";

  /** Default constructor. */
  public ServicesListQuery() {
    super();
    this.availableFilters.add(createNameFilter(Optional.<String> empty()));
    this.availableFilters.add(createHostnameFilter(Optional.<String> empty()));
    this.availableFilters.add(createNodeNameFilter(Optional.<String> empty()));
    this.availableFilters.add(createStatusFilter(Optional.<String> empty()));
    this.availableFilters.add(createActionsFilter(Optional.<Boolean> empty()));
  }

  /**
   * Copy constructor for the base class {@code ResourceListQuery}.
   *
   * @param query copy values from the given query
   */
  public ServicesListQuery(ResourceListQuery query) {
    this();
    availableFilters.addAll(query.getAvailableFilters());

    for (ResourceListFilter filter : query.getFilters())
      addFilter(filter);

    sortBy = query.getSortBy();
    if (query.getOffset().isPresent())
      setOffset(query.getOffset().get());
    if (query.getLimit().isPresent())
      setLimit(query.getLimit().get());
  }

  /**
   * Add a {@link ResourceListFilter} filter to the query with the given name.
   *
   * @param name the name to filter for
   */
  public void withName(Optional<String> name) {
    addFilter(createNameFilter(name));
  }

  /**
   * Add a {@link ResourceListFilter} filter to the query with the given hostname.
   *
   * @param hostname the hostname to filter for
   */
  public void withHostname(Optional<String> hostname) {
    addFilter(createHostnameFilter(hostname));
  }

  /**
   * Add a {@link ResourceListFilter} filter to the query with the given node name.
   *
   * @param nodeName the node name to filter for
   */
  public void withNodeName(String nodeName) {
    addFilter(createNodeNameFilter(Optional.ofNullable(nodeName)));
  }

  /**
   * Add a {@link ResourceListFilter} filter to the query with the given status.
   *
   * @param status the status to filter for
   */
  public void withStatus(Optional<String> status) {
    addFilter(createStatusFilter(status));
  }

  /**
   * Add a {@link ResourceListFilter} filter to the query with any actions.
   *
   * @param hasActions any actions to filter
   */
  public void withActions(Optional<Boolean> hasActions) {
    addFilter(createActionsFilter(hasActions));
  }

  /**
   * Add a {@link ResourceListFilter} filter to the query with the given free text.
   *
   * @param freeText the free text to filter for
   */
  public void withFreeText(Optional<String> freeText) {
    addFilter(createFreeTextFilter(freeText));
  }

  /**
   * Returns an {@link Optional} containing the name used to filter if set.
   * {@link Optional#empty()} otherwise.
   *
   * @return an {@link Optional} containing the name or none.
   */
  public Optional<String> getName() {
    return getFilterValue(FILTER_NAME_NAME);
  }

  /**
   * Returns an {@link Optional} containing the hostname used to filter if set.
   * {@link Optional#empty()} otherwise.
   *
   * @return an {@link Optional} containing the hostname or none.
   */
  public Optional<String> getHostname() {
    return getFilterValue(FILTER_NAME_HOSTNAME);
  }

  /**
   * Returns an {@link Optional} containing the node name used to filter if set.
   * {@link Optional#empty()} otherwise.
   *
   * @return an {@link Optional} containing the node name or none.
   */
  public Optional<String> getNodeName() {
    return getFilterValue(FILTER_NAME_NODE_NAME);
  }

  /**
   * Returns an {@link Optional} containing the status used to filter if set.
   * {@link Optional#empty()} otherwise.
   *
   * @return an {@link Optional} containing the status or none.
   */
  public Optional<String> getStatus() {
    return getFilterValue(FILTER_NAME_STATUS);
  }

  /**
   * Returns an {@link Optional} containing boolean value used to filter any actions if set.
   * {@link Optional#empty()} otherwise.
   *
   * @return an {@link Optional} containing the boolean value for any actions or none.
   */
  public Optional<Boolean> getActions() {
    Optional result = getFilterValue(FILTER_NAME_ACTIONS);
    if (result.isPresent()) {
      if (result.get() instanceof String) {
        return BooleanListProvider.parseValue((String)result.get());
      }
    }
    return result;
  }

  /**
   * Returns an {@link Optional} containing the free text used to filter if set.
   * {@link Optional#empty()} otherwise.
   *
   * @return an {@link Optional} containing the free text or none.
   */
  public Optional<String> getFreeText() {
    return getFilterValue(ResourceListFilter.FREETEXT);
  }

  /**
   * Create a new {@link ResourceListFilter} based on a name.
   *
   * @param value the name to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
   * @return a new {@link ResourceListFilter} for a name based query
   */
  public static <String> ResourceListFilter<String> createNameFilter(Optional<String> value) {
    return FiltersUtils.generateFilter(
            value,
            FILTER_NAME_NAME,
            FILTER_LABEL_NAME,
            ResourceListFilter.SourceType.SELECT,
            Optional.of(ServicesListProvider.LIST_NAME));
  }

  /**
   * Create a new {@link ResourceListFilter} based on a hostname.
   *
   * @param value the hostname to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
   * @return a new {@link ResourceListFilter} for a hostname based query
   */
  public static <String> ResourceListFilter<String> createHostnameFilter(Optional<String> value) {
    return FiltersUtils.generateFilter(
            value,
            FILTER_NAME_HOSTNAME,
            FILTER_LABEL_HOSTNAME,
            ResourceListFilter.SourceType.SELECT,
            Optional.of(ServersListProvider.LIST_HOSTNAME));
  }

  /**
   * Create a new {@link ResourceListFilter} based on a nodeName.
   *
   * @param value the nodeName to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
   * @return a new {@link ResourceListFilter} for a nodeName based query
   */
  public static ResourceListFilter<String> createNodeNameFilter(Optional<String> value) {
    return FiltersUtils.generateFilter(
            value,
            FILTER_NAME_NODE_NAME,
            FILTER_LABEL_NODE_NAME,
            ResourceListFilter.SourceType.SELECT,
            Optional.of(ServersListProvider.LIST_NODE_NAME));
  }

  /**
   * Create a new {@link ResourceListFilter} based on a status.
   *
   * @param value the status to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
   * @return a new {@link ResourceListFilter} for a status based query
   */
  public static <String> ResourceListFilter<String> createStatusFilter(Optional<String> value) {
    return FiltersUtils.generateFilter(
            value,
            FILTER_NAME_STATUS,
            FILTER_LABEL_STATUS,
            ResourceListFilter.SourceType.SELECT,
            Optional.of(ServicesListProvider.LIST_STATUS));
  }

  /**
   * Create a new {@link ResourceListFilter} based on any actions.
   *
   * @param value the boolean value for any action to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
   * @return a new {@link ResourceListFilter} for a any actions based query
   */
  public static <Boolean> ResourceListFilter<Boolean> createActionsFilter(Optional<Boolean> value) {
    return FiltersUtils.generateFilter(
            value,
            FILTER_NAME_ACTIONS,
            FILTER_LABEL_ACTIONS,
            ResourceListFilter.SourceType.SELECT,
            Optional.of(BooleanListProvider.YES_NO));
  }

  /**
   * Create a new {@link ResourceListFilter} based on a free text.
   *
   * @param value the free text to filter on wrapped in an {@link Optional} or {@link Optional#empty()}
   * @return a new {@link ResourceListFilter} for a free text based query
   */
  public static <String> ResourceListFilter<String> createFreeTextFilter(Optional<String> value) {
    return FiltersUtils.generateFilter(
            value,
            ResourceListFilter.FREETEXT,
            ResourceListFilter.FREETEXT,
            ResourceListFilter.SourceType.FREETEXT,
            null);
  }
}