package it.mice.voila.runtime.springmvc.bean;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * A single path stach element containing the rest style URL to return and other
 * useful meta information.
 */
public class PathStackElement implements Serializable {
	/**
	 * serial version uid.
	 */
	private static final long serialVersionUID = 7065727530410406622L;
	
	/**
	 * the url of the path.
	 */
	private String url;
	
	/**
	 * the query string of the path.
	 */
	private String queryString;
	
	/**
	 * the message key code of the url header/title.
	 */
	private String headerKey;
	/**
	 * this path represent the first step of a new subflow ?
	 */
	private boolean newFlow = false;

	/**
	 * default Constructor of the Path stack element.
	 * @param url the url.
	 * @param newFlow the newFlow.
	 */
	public PathStackElement(String url, boolean newFlow) {
		super();
		this.url = url;
		this.newFlow = newFlow;
	}

	/**
	 * default Constructor of the Path stack element.
	 * @param url the url.
	 * @param queryString the queryString.
	 * @param headerKey the headerKey.
	 * @param newFlow the newFlow.
	 */
	public PathStackElement(String url, String queryString, String headerKey, boolean newFlow) {
		super();
		this.url = url;
		this.queryString = queryString;
		this.headerKey = headerKey;
		this.newFlow = newFlow;
	}
	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	
	/**
	 * @return the headerKey
	 */
	public String getHeaderKey() {
		return headerKey;
	}

	/**
	 * @param headerKey the headerKey to set
	 */
	public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}

	/**
	 * @return the subFlow
	 */
	public boolean isNewFlow() {
		return newFlow;
	}
	/**
	 * @param newFlow the newFlow.
	 */
	public void setNewFlow(boolean newFlow) {
		this.newFlow = newFlow;
	}
	
	/**
	 * @return the queryString
	 */
	public String getQueryString() {
		return queryString;
	}

	/**
	 * @param queryString the queryString to set
	 */
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
	}
	
	public String getPath() {
		String result = getUrl();
		if (!StringUtils.isEmpty(getQueryString())) {
			result += "?" + getQueryString();
		}
		return result;
	}
}
