/*
 * Copyright (C) 2009-2013, Free University of Bozen Bolzano
 * This source code is available under the terms of the Affero General Public
 * License v3.
 * 
 * Please see LICENSE.txt for full license terms, including the availability of
 * proprietary exceptions.
 */
package it.unibz.krdb.obda.model;

import java.util.ArrayList;
import java.util.List;

public class OBDAQueryModifiers {

	private boolean isDistinct;

	private long limit;
	private long offset;

	private List<OrderCondition> orderConditions;

	public OBDAQueryModifiers() {
		isDistinct = false;
		limit = -1;
		offset = -1;
		orderConditions = new ArrayList<OrderCondition>();
	}

	public OBDAQueryModifiers clone() {
		OBDAQueryModifiers clone = new OBDAQueryModifiers();
		clone.isDistinct = isDistinct;
		clone.limit = limit;
		clone.offset = offset;
		for (OrderCondition c : orderConditions) {
			try {
				clone.orderConditions.add(c.clone());
			} catch (CloneNotSupportedException e) {
				throw new RuntimeException(e);
			}
		}
		return clone;
	}

	public void copy(OBDAQueryModifiers other) {
		isDistinct = other.isDistinct;
		limit = other.limit;
		offset = other.offset;
		orderConditions.addAll(other.orderConditions);
	}

	public void setDistinct() {
		isDistinct = true;
	}

	public boolean isDistinct() {
		return isDistinct;
	}

	public void setLimit(long limit) {
		this.limit = limit;
	}

	public long getLimit() {
		return limit;
	}

	public void setOffset(long offset) {
		this.offset = offset;
	}

	public long getOffset() {
		return offset;
	}

	public void addOrderCondition(Variable var, int direction) {
		OrderCondition condition = new OrderCondition(var, direction);
		orderConditions.add(condition);
	}

	public List<OrderCondition> getSortConditions() {
		return orderConditions;
	}

	public boolean hasModifiers() {
		return isDistinct || limit != -1 || offset != -1 || !orderConditions.isEmpty();
	}

	/**
	 * A helper class to store the sort conditions
	 */
	public class OrderCondition implements Cloneable {
		public static final int ORDER_ASCENDING = 1;
		public static final int ORDER_DESCENDING = 2;

		private Variable var;
		private int direction;

		OrderCondition(Variable var, int direction) {
			this.var = var;
			this.direction = direction;
		}

		public Variable getVariable() {
			return var;
		}

		public int getDirection() {
			return direction;
		}

		@Override
		public OrderCondition clone() throws CloneNotSupportedException {
			return (OrderCondition) super.clone();
		}
	}
}

@@ -1415,32 +1415,102 @@
 turn limit;%0A%09%7D%0A%0A
+%09public boolean hasLimit() %7B%0A%09%09return limit != -1 ? true : false;%0A%09%7D%0A%0A
 %09public void set
@@ -1599,32 +1599,104 @@
 urn offset;%0A%09%7D%0A%0A
+%09public boolean hasOffset() %7B%0A%09%09return offset != -1 ? true : false;%0A%09%7D%0A%0A
 %09public void add
@@ -1948,100 +1948,167 @@
  has
-Modifiers() %7B%0A%09%09return isDistinct %7C%7C limit != -1 %7C%7C offset != -1 %7C%7C !orderConditions.isEmpty
+Order() %7B%0A%09%09return !orderConditions.isEmpty() ? true : false;%0A%09%7D%0A%0A%09public boolean hasModifiers() %7B%0A%09%09return isDistinct %7C%7C hasLimit() %7C%7C hasOffset() %7C%7C hasOrder
 ();%0A

Add new methods to check if such modifier exists or not.