<%@page import="com.gridnine.xtrip.server.ibecorp.helpers.RuleBinder"%>
<%@page import="java.util.*"%>
<%@page import="java.util.zip.ZipEntry"%>
<%@page import="java.util.zip.ZipOutputStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="javax.servlet.ServletOutputStream"%>
<%@page language="java" session="false" pageEncoding="UTF-8"
	contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%
String thisPage = "rule-env-bindings.jsp";
    if (request.getParameter("start") != null) {
        String value = request.getParameter("value");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (value.equals("all")) {
            Map<String, String> result = RuleBinder.process();
            try {
                ZipOutputStream zos = new ZipOutputStream(baos);
                try {
                    result.entrySet().forEach(e -> {
                        try {
                            ZipEntry ze = new ZipEntry(e.getKey());
                            zos.putNextEntry(ze);
                            zos.write(e.getValue().getBytes());
                            zos.closeEntry();
                        } catch(Exception ex) {
                        }
                    });
                } finally {
                    zos.close();
                }
            } finally {
                baos.close();
            }
        } else {
            String result = RuleBinder.process(value);
            try {
                ZipOutputStream zos = new ZipOutputStream(baos);
                try {
                    ZipEntry ze = new ZipEntry(value + ".txt");
                    zos.putNextEntry(ze);
                    zos.write(result.getBytes());
                    zos.closeEntry();
                } finally {
                    zos.close();
                }
            } finally {
                baos.close();
            }
        }
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition","attachment; filename=\"" + value + ".zip\"");
        ServletOutputStream os = response.getOutputStream();
        os.write(baos.toByteArray());
        os.flush();
        os.close();
        return;
    }
%>

<!doctype html>
<html>
<head>
<title>Rule environment bindings</title>
</head>
<body style="padding: 1em">
	<h1>Rule environment bindings</h1>
	<form action="<%=thisPage%>" method="post">
		<p>
            <label for="value">Value</label>
            <select name="value">
                <option value="all">All</option>
                <%
                    Iterator<String> iterator = RuleBinder.getList().iterator();
                    while (iterator.hasNext()) {
                        String e = iterator.next();
                        try {
                        %>
                            <option value="<%= e %>" ><%= e %></option>
                        <%
                        } catch (Exception ex) {
                        }
                    }
                %>
            </select>
        </p>
		<p>
			<input type="submit" name="start" value="Start">
		</p>
	</form>
</body>
</html>

