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

net.abstractfactory.plum.view.web.PlumForm Maven / Gradle / Ivy

There is a newer version: 0.3.21
Show newest version
package net.abstractfactory.plum.view.web;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.abstractfactory.common.ListValueMap;

import org.apache.log4j.Logger;

/**
 * populate PLUM specific HTML FORM elements.
 * 
 * @author jack
 *
 */
public class PlumForm {
	private Logger logger = Logger.getLogger(getClass());

	public static final String SPECIAL_PARAMETER_PREFIX = "_plum_";

	static final String COMPONENT = SPECIAL_PARAMETER_PREFIX + "component";
	static final String TAG = SPECIAL_PARAMETER_PREFIX + "tag";
	static final String EVENT = SPECIAL_PARAMETER_PREFIX + "event";
	public static final String EVENT_PARAMETER = SPECIAL_PARAMETER_PREFIX + "event_parameter";
	public static final String VIEW_VERSION = SPECIAL_PARAMETER_PREFIX + "view_version";
	static Set specialParameterNames = new HashSet();

	static {
		specialParameterNames.add(COMPONENT);
		specialParameterNames.add(TAG);
		specialParameterNames.add(EVENT);
		specialParameterNames.add(EVENT_PARAMETER);
		specialParameterNames.add(VIEW_VERSION);
	}

	private Map specialParameters = new HashMap();

	public PlumForm(ListValueMap parameterMap) {
		processComponentInputs(parameterMap);
	}

	// group by web component
	private Map> componentParameters;

	private void processComponentInputs(ListValueMap parameterMap) {
		componentParameters = new HashMap>();

		for (String name : parameterMap.keySet()) {

			List value = parameterMap.get(name);

			if (logger.isDebugEnabled()) {
				StringBuilder sb = new StringBuilder();
				for (Object obj : value)
					sb.append(obj.toString() + ",");

				logger.debug(name + "=" + sb.toString());
			}

			if (name.startsWith(SPECIAL_PARAMETER_PREFIX)) {
				if (specialParameterNames.contains(name)) {
					specialParameters.put(name, (String) value.get(0));
				}

				continue;
			}

			InputTagName inputTagName = InputTagName.valueOf(name);
			if (inputTagName == null)
				throw new RuntimeException("invalid input tag name:" + name);

			String componentId = inputTagName.getComponentId();
			String tagName = inputTagName.getWebTagName();

			ListValueMap parameters = componentParameters.get(componentId);
			if (parameters == null) {
				parameters = new ListValueMap();
				componentParameters.put(componentId, parameters);
			}
			parameters.put(tagName, value);
		}

	}

	public Map getSpecialParameters() {
		return specialParameters;
	}

	public Map> getComponentParameters() {
		return componentParameters;
	}

	public String getComponentId() {
		return specialParameters.get(PlumForm.COMPONENT);
	}

	public String getTag() {
		return specialParameters.get(PlumForm.TAG);
	}

	public String getEvent() {
		return specialParameters.get(PlumForm.EVENT);
	}

	public String getEventParameter() {
		return specialParameters.get(PlumForm.EVENT_PARAMETER);
	}

	public String getViewVersion() {
		return specialParameters.get(PlumForm.VIEW_VERSION);
	}
}