view src/nabble/view/web/forum/NodeEditorNamespace.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
line wrap: on
line source

package nabble.view.web.forum;

import nabble.model.Message;
import nabble.model.ModelException;
import nabble.model.Node;
import nabble.model.export.Export;
import nabble.naml.compiler.Command;
import nabble.naml.compiler.CommandSpec;
import nabble.naml.compiler.IPrintWriter;
import nabble.naml.compiler.Interpreter;
import nabble.naml.compiler.Namespace;
import nabble.naml.compiler.ScopedInterpreter;
import nabble.naml.namespaces.TemplateException;
import nabble.view.lib.Permissions;
import nabble.view.web.template.NodeNamespace;
import nabble.view.web.template.ServletNamespace;
import nabble.view.web.template.UrlMapperNamespace;

import java.util.Calendar;


@Namespace (
	name = "node_editor",
	global = true
)
public final class NodeEditorNamespace {
	private final Node node;
	private final ServletNamespace servletNs;

	public NodeEditorNamespace(Node node,ServletNamespace servletNs) {
		this.node = node;
		this.servletNs = servletNs;
	}

	public Node node() {
		return node;
	}

	private Node getLocalNode(String url) throws ModelException.InvalidPermalink {
		url = url.trim();
		if (url.length() == 0)
			return null;
		Node n = UrlMapperNamespace.getNodeFromUrl(url);
		if (n != null) {
			return n.getSite().equals(node.getSite())? n : null;
		}
		// No app found. Let's check if this is a permalink url...
		n = Permalink.getPostFromUrl(url);
		if (n == null)
			throw new ModelException.InvalidPermalink();
		return n.getSite().equals(node.getSite())? n : null;
	}

	public static final CommandSpec edited_node = CommandSpec.DO;

	@Command public void edited_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
		NodeNamespace ns = new NodeNamespace(node);
		out.print( interp.getArg(ns,"do") );
	}

	public static final CommandSpec save_node = CommandSpec.NO_OUTPUT;

	@Command public void save_node(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		if( node.isInDb() )
			node.update();
		else
			node.insert(true);
	}

	public static final CommandSpec set_parent_url = new CommandSpec.Builder()
		.parameters("parent_url")
		.build()
	;

	@Command public void set_parent_url(IPrintWriter out,Interpreter interp)
		throws TemplateException
	{
		String url = interp.getArgString("parent_url");
		Node parent = getLocalNode(url);
		if( node.equals(parent) )
			throw TemplateException.newInstance("same_node_loop");
		node.changeParent(parent);
	}

	public static final CommandSpec set_subject = new CommandSpec.Builder()
		.parameters("subject")
		.build()
	;

	@Command public void set_subject(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		String subject = interp.getArgString("subject");
		node.setSubject(subject);
	}

	public static final CommandSpec set_message = new CommandSpec.Builder()
		.parameters("message","is_html")
		.build()
	;

	@Command public void set_message(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		Message.Format msgFmt = interp.getArgAsBoolean("is_html") ? Message.Format.HTML : Message.Format.TEXT;
		String message = interp.getArgString("message");
		node.setMessage(message,msgFmt);
	}

	public static final CommandSpec set_type = new CommandSpec.Builder()
		.parameters("type")
		.build()
	;

	@Command public void set_type(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		String type = interp.getArgString("type");
		node.setType(type);
	}

	public static final CommandSpec set_when_created = new CommandSpec.Builder()
		.parameters("date")
		.build()
	;

	@Command public void set_when_created(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		String date = interp.getArgString("date");
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(Long.valueOf(date));
			node.setWhenCreated(cal.getTime());
		} catch (NumberFormatException e) {
			throw ModelException.newInstance("invalid_date", "Invalid date in milliseconds");
		}
	}


	public static final CommandSpec remove_permissions = CommandSpec.NO_OUTPUT;

	@Command public void remove_permissions(IPrintWriter out,Interpreter interp) {
		Permissions.removePermissions(node);
	}

	public static final CommandSpec add_permission = CommandSpec.NO_OUTPUT()
		.parameters("permission")
		.optionalParameters("group")
		.build()
	;

	@Command public void add_permission(IPrintWriter out,Interpreter interp) {
		String perm = interp.getArgString("permission");
		String group = interp.getArgString("group");
		if( group == null )
			Permissions.addPermission(node,perm);
		else
			Permissions.addPermission(node,perm,group);
	}

	public static final CommandSpec remove_permission = CommandSpec.NO_OUTPUT()
		.parameters("permission")
		.optionalParameters("group")
		.build()
	;

	@Command public void remove_permission(IPrintWriter out,Interpreter interp) {
		String group = interp.getArgString("group");
		String perm = interp.getArgString("permission");
		if (group == null)
			Permissions.removePermission(node,perm);
		else
			Permissions.removePermission(node,perm,group);
	}

	public static final CommandSpec is_valid_export_permalink = new CommandSpec.Builder()
		.parameters("permalink")
		.build()
	;

	@Command public void is_valid_export_permalink(IPrintWriter out,Interpreter interp)
		throws ModelException
	{
		out.print(Export.isValidExportServer(interp.getArgString("permalink")));
	}
}