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

org.javasimon.console.action.TreeJsonAction Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package org.javasimon.console.action;

import org.javasimon.Simon;
import org.javasimon.console.ActionContext;
import org.javasimon.console.ActionException;
import org.javasimon.console.json.ArrayJS;
import org.javasimon.console.json.ObjectJS;

import java.io.IOException;

import javax.servlet.ServletException;

/**
 * Export Simons as a hierarchical JSON object for display in a tree-table.
 * All attributes of simons are exported.
 *
 * @author gquintana
 */
public class TreeJsonAction extends AbstractJsonAction {

	public static final String PATH = "/data/tree.json";

	/**
	 * Name of the simon from where to start.
	 * {@code null} means root.
	 */
	private String name;

	public TreeJsonAction(ActionContext context) {
		super(context);
	}

	@Override
	public void readParameters() {
		super.readParameters();
		name = getContext().getParameterAsString("name", null);
	}

	@Override
	protected ObjectJS createObjectJS(Simon simon) {
		ObjectJS simonJS = super.createObjectJS(simon);
		ArrayJS childrenJS = new ArrayJS();
		for (Simon child : simon.getChildren()) {
			ObjectJS childJS = createObjectJS(child);
			childrenJS.addElement(childJS);
		}
		simonJS.setAttribute("children", childrenJS);
		return simonJS;
	}

	@Override
	public void execute() throws ServletException, IOException, ActionException {
		getContext().setContentType("application/json");
		Simon simon = name == null ? getContext().getManager().getRootSimon() : getContext().getManager().getSimon(name);
		ObjectJS simonRootJS = createObjectJS(simon);
		simonRootJS.write(getContext().getWriter());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy