All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.lshift.diffa.participant.scanning.ConstraintsBuilder Maven / Gradle / Ivy

Go to download

Diffa Participant Support provides tools for implementing Diffa participants in Java (and other JVM languages)

There is a newer version: 1.5.10
Show newest version
/**
 * Copyright (C) 2010-2011 LShift Ltd.
 *
 * Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 net.lshift.diffa.participant.scanning;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Helper for building constraints from a web request.
 */
public class ConstraintsBuilder {
  private final HttpServletRequest req;
  private final List result;

  public ConstraintsBuilder(HttpServletRequest req) {
    this.req = req;
    this.result = new ArrayList();
  }

  /**
   * Transforms the builder into a list of constraints.
   * @return the constraint list.
   */
  public List toList() {
    return result;
  }

  /**
   * Attempt to add a date range constraint for the given attribute. The constraint will be added if
   * one or both of [attrName]-start, [attrName]-end are present in the request.
   * @param attrName the name of the attribute
   */
  public void maybeAddDateRangeConstraint(String attrName) {
    String startVal = req.getParameter(attrName + "-start");
    String endVal = req.getParameter(attrName + "-end");

    if (startVal != null || endVal != null) {
      result.add(new DateRangeConstraint(attrName, startVal, endVal));
    }
  }

  /**
   * Attempt to add a time range constraint for the given attribute. The constraint will be added if
   * one or both of [attrName]-start, [attrName]-end are present in the request.
   * @param attrName the name of the attribute
   */
  public void maybeAddTimeRangeConstraint(String attrName) {
    String startVal = req.getParameter(attrName + "-start");
    String endVal = req.getParameter(attrName + "-end");

    if (startVal != null || endVal != null) {
      result.add(new TimeRangeConstraint(attrName, startVal, endVal));
    }
  }

  /**
   * Attempt to add a set constraint for the given attribute. The constraint will be added if there are
   * any arguments in the request with the given attribute name.
   * @param attrName the name of the attribute
   */
  public void maybeAddSetConstraint(String attrName) {
    String[] values = req.getParameterValues(attrName);

    if (values != null && values.length > 0) {
      HashSet set = new HashSet();
      Collections.addAll(set,values);
      result.add(new SetConstraint(attrName,set));
    }
  }

  /**
   * Attempt to add a integer range constraint for the given attribute. The constraint will be added if
   * one or both of [attrName]-start, [attrName]-end are present in the request.
   * @param attrName the name of the attribute
   */
  public void maybeAddIntegerRangeConstraint(String attrName) {
    String startVal = req.getParameter(attrName + "-start");
    String endVal = req.getParameter(attrName + "-end");

    if (startVal != null || endVal != null) {
      result.add(new IntegerRangeConstraint(attrName, startVal, endVal));
    }
  }

  /**
   * Attempt to add a string prefix constraint for the given attribute. The constraint will be added if
   * [attrName]-prefix is present in the request.
   * @param attrName the name of the attribute
   */
  public void maybeAddStringPrefixConstraint(String attrName) {
    String prefix = req.getParameter(attrName + "-prefix");

    if (prefix != null) {
      result.add(new StringPrefixConstraint(attrName, prefix));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy