package org.openntf.xrest.xsp.model.strategy;

import org.openntf.xrest.xsp.dsl.DSLBuilder;
import org.openntf.xrest.xsp.exec.Context;
import org.openntf.xrest.xsp.exec.DatabaseProvider;
import org.openntf.xrest.xsp.exec.ExecutorException;
import org.openntf.xrest.xsp.exec.convertor.Document2JsonConverter;
import org.openntf.xrest.xsp.exec.datacontainer.DocumentDataContainer;
import org.openntf.xrest.xsp.model.DataContainer;
import org.openntf.xrest.xsp.model.RouteProcessor;

import com.ibm.commons.util.io.json.JsonObject;

import groovy.lang.Closure;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.View;

public class GetByKey extends AbstractKeyViewDatabaseStrategy implements StrategyModel<DocumentDataContainer, JsonObject> {

	private String formValue;
	private Closure<?> formCl;

	public void form(final String name) {
		this.formValue = name;
	}

	public void form(final Closure<?> formCl) {
		this.formCl = formCl;
	}

	public String getFormValue(final Context context) {
		if (formCl != null) {
			return (String) DSLBuilder.callClosure(formCl, context);
		} else {
			return formValue;
		}
	}

	@Override
	public DocumentDataContainer buildDataContainer(final Context context) throws ExecutorException {
		try {
			Database dbAccess = DatabaseProvider.INSTANCE.getDatabase(getDatabaseNameValue(context), context.getDatabase(), context
					.getSession());
			View viewAccess = dbAccess.getView(getViewNameValue(context));
			String key = context.getRouterVariables().get(getKeyVariableValue(context));
			Document doc = viewAccess.getDocumentByKey(key, true);
			if (null == doc) {
				throw new ExecutorException(404, "Not found", "", "getmodel");
			}
			return new DocumentDataContainer(doc, viewAccess, dbAccess);
		} catch (ExecutorException exe) {
			throw exe;
		} catch (Exception ex) {
			throw new ExecutorException(500, ex, "", "getmodel");
		}

	}

	@Override
	public JsonObject buildResponse(final Context context, final RouteProcessor routeProcessor, final DataContainer<?> dc)
			throws NotesException {
		Document2JsonConverter d2j = new Document2JsonConverter(((DocumentDataContainer) dc).getData(), routeProcessor, context);
		return d2j.buildJsonFromDocument();
	}

}

@@ -474,16 +474,56 @@
 essor;%0A%0A
+import com.ibm.commons.util.StringUtil;%0A
 import c
@@ -1541,16 +1541,17 @@
 text));%0A
+%0A
 %09%09%09Strin
@@ -1622,16 +1622,296 @@
 text));%0A
+%09%09%09if (key.equalsIgnoreCase(%22@new%22)) %7B%0A%09%09%09%09Document doc = dbAccess.createDocument();%0A%09%09%09%09String form = getFormValue(context);%0A%09%09%09%09if (!StringUtil.isEmpty(form)) %7B%0A%09%09%09%09%09doc.replaceItemValue(%22Form%22, form);%0A%09%09%09%09%7D%0A%09%09%09%09return new DocumentDataContainer(doc, viewAccess, dbAccess);%0A%09%09%09%7D%0A
 %09%09%09Docum

Revert "remove code that probably got here by copypasting"