package com.udapsoft.modular.web.tags;

import java.util.Map;

import javax.servlet.jsp.tagext.Tag;

import com.udapsoft.modular.core.util.StringUtils;

public class CommandTag extends TemplateBasedRequestContextAwareTag {

	private String id;
	private String type;
	private String input;
	private String output;

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getInput() {
		return this.input;
	}

	public void setInput(String in) {
		this.input = in;
	}

	public String getOutput() {
		return this.output;
	}

	public void setOutput(String out) {
		this.output = out;
	}

	protected int doBuildModelForStartTag(Map model) throws Exception {
		Tag tag = getParent();
		if ((tag instanceof ControlTag)) {
			ControlTag ct = (ControlTag) tag;
			model.put("controlId", ct.getId());
		}
		evaluateStringAndSetModel(model, "id", this.pageContext);
		evaluateStringAndSetModel(model, "type", this.pageContext);
		evaluateStringAndSetModel(model, "input", this.pageContext);
		evaluateStringAndSetModel(model, "output", this.pageContext);

		String output = (String) model.get("output");
		String[] outputs = null;
		if (StringUtils.hasText(output)) {
			outputs = output.split(",");
		} else {
			outputs = new String[0];
		}
		model.put("output", outputs);

		return 0;
	}

}
