package com.base2.kagura.core.reporting.view.report.freemarker;

import freemarker.core.Environment;
import freemarker.template.*;
import org.apache.commons.lang3.BooleanUtils;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: aubels
 * Date: 29/07/13
 * Time: 2:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class FreemarkerWhereClause implements TemplateDirectiveModel
{
    private final List<String> errors;
    private String outputBody;

    public FreemarkerWhereClause(List<String> errors) {
        this.errors = errors;
    }

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        if (params.size() != 1) {
            String message = "This directive doesn't allow multiple parameters.";
            errors.add(message);
            throw new TemplateModelException(message);
        }
        if (!((Map.Entry)params.entrySet().toArray()[0]).getKey().equals("render"))
        {
            String message = "This directive only takes 'render'.";
            errors.add(message);
            throw new TemplateModelException(message);
        }
        Object renderParam = params.get("render");
        if (renderParam == null) return;

        if (renderParam instanceof TemplateBooleanModel)
        {
            TemplateBooleanModel render = (TemplateBooleanModel) renderParam;
            if (BooleanUtils.isNotTrue(render.getAsBoolean()))
                return;
        } else {
            String message = "This directive only accepts boolean values for 'render'.";
            errors.add(message);
            throw new TemplateModelException(message);
        }
        if (loopVars.length != 0) {
            String message = "This directive doesn't allow loop variables.";
            errors.add(message);
            throw new TemplateModelException(message);
        }
        FreemarkerWhere freemarkerWhere = (FreemarkerWhere)env.getVariable("whereTag");
        if (freemarkerWhere == null) {
            String message = "Can not find parent where clause.";
            errors.add(message);
            throw new TemplateModelException(message);
        }
        StringWriter stringWriter = new StringWriter();
        body.render(stringWriter);
        outputBody = stringWriter.toString();
        freemarkerWhere.addWhereClause(this);
    }

    public String getOutputBody() {
        return outputBody;
    }

    public void setOutputBody(String outputBody) {
        this.outputBody = outputBody;
    }
}

@@ -851,18 +851,17 @@
 .size() 
-!=
+%3E
  1) %7B%0A  
@@ -1050,16 +1050,38 @@
     if (
+params.size() != 0 && 
 !((Map.E
@@ -1406,26 +1406,87 @@
 null
-) return;%0A%0A       
+ && params.size() != 0) return;%0A        if (params.size() == 0)%0A        %7B%7D else
  if 

Allow non-render restricted <@and> tags.