/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.widget.searchandfilter.search.dto;

import java.util.Set;

/**
 * Criterion with attribute criteria.
 * 
 * @author Kristof Heirwegh
 * 
 */
public class AttributeCriterion implements Criterion {

	private static final long serialVersionUID = 1L;

	private String serverLayerId;

	private String attributeName;

	public AttributeCriterion() {};

	public AttributeCriterion(String serverLayerId, String attributeName, String operator, String value) {
		this.serverLayerId = serverLayerId;
		this.attributeName = attributeName;
		this.operator = operator;
		this.value = value;
	}

	private static final String OPERATORS = "<,<=,=,<>,>,=>,LIKE,DURING,BEFORE,AFTER";
	/**
	 * <, <=, =, <>, >=, >, like, during, before, after.
	 */
	private String operator;

	private String value;

	// ----------------------------------------------------------

	public String getAttributeName() {
		return attributeName;
	}

	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getServerLayerId() {
		return serverLayerId;
	}

	public void setServerLayerId(String serverLayerId) {
		this.serverLayerId = serverLayerId;
	}

	public boolean isValid() {
		return (serverLayerId != null && attributeName != null && isValidOperator() && value != null);
	}

	public boolean isValidOperator() {
		if (operator != null) {
			return (OPERATORS.contains(operator.toUpperCase()) && !",".equals(operator));
		} else {
			return false;
		}
	}

	public void serverLayerIdVisitor(Set<String> layerIds) {
		layerIds.add(serverLayerId);
	}
}

@@ -2238,10 +2238,110 @@
 Id);%0A%09%7D%0A
+%09%0A%09public String toString() %7B%0A%09%09return %22(%22 + attributeName + %22 %22 + operator + %22 %22 + value + %22)%22;%0A%09%7D%0A
 %7D%0A

Create filter widget, that allows combined filters on the map