<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<xsp:page language="java"
    xmlns:xsp="http://apache.org/xsp"
	  xmlns:esql="http://apache.org/cocoon/SQL/v2"
    xmlns:xsp-request="http://apache.org/xsp/request/2.0"
  >
  <!-- Need this import for dumping the request parameters -->
  <xsp:structure>
    <xsp:include>java.util.Enumeration</xsp:include>
  </xsp:structure>

  <page>
    <resources>
       <resource type="file" href="database.xml?cocoon-view=pretty-content">Descriptor</resource>
       <resource type="file" href="../schema.sql">Schema</resource>
       <resource type="doc" href="http://cocoon.apache.org/2.1/userdocs/database-actions.html">Actions</resource>
       <resource type="doc" href="http://cocoon.apache.org/2.1/userdocs/logicsheets/esql.html">ESQL</resource>
    </resources>

    <title>file-upload-blob</title>

   <!-- create image tags out of the images stored in the database, the browser will subsequently
        request those images and they will be served by a special pipeline in the sitemap using
        the database reader component
   -->
   <esql:connection>
      <esql:pool>personnel</esql:pool>

      <esql:execute-query>
             <esql:query>select media.id from media</esql:query>
             <esql:results>
               <esql:row-results>
                 <img><xsp:attribute name="src">image/<esql:get-string column="id"/></xsp:attribute>
                 </img>
               </esql:row-results>
             </esql:results>
             <esql:no-results>no images yet</esql:no-results>
      </esql:execute-query>
    </esql:connection>


      <h1>Upload Media</h1>
      <p>What's happening?  This example interacts with a simple database schema, set up by
      default in the sample "personnel" hsql database installed with the database block.  There
      is one table "media" with two fields: a primary key, and a binary field "image".
      When a file upload is received from the simple form on this page, the modular db action
      auto increments the primary key, and loads the binary file contents into the column (declared
      as type="binary" in database.xml).  That's it.</p>
      <p>Additionally, when this page is loaded, esql queries the media table and places an image tag
      for each row, using "primarykeyvalue".jpg for the src.  The sitemap is configured to serve
	  *.jpg using the database reader (declared in the root database samples sitemap) which simply
	  retrieves the binary data from the database and streams it to the browser.</p>

      <p>For this sample uploads need to be enabled in web.xml. This happens by default
      via the property <code>config.enable-uploads</code> in build.properties.</p>


      <table cellpadding="2" cellspacing="2" border="0">
        <tbody>
          <form method="POST" enctype="multipart/form-data">
            <tr>
              <td valign="Top" align="Right">File (must be an image)</td>
              <td valign="Top">
                <input type="file" name="media.image"/>
              </td>
            <td>
              <input type="submit" name="add-image" value="add image"/>
            </td>
          </tr>
        </form>
        </tbody>
        </table>



    <!-- the following is absolutely irrelevant for the example. It only serves to show what happens
         during the processing e.g. what parameters were sent to the server and what request attributes
         were set by the database actions. You might find such information valuable when debugging your
         own applications :-) -->
    <hr/>

    <p><h3>Request Attributes</h3></p>
    <p>
      <table border="0">
        <tbody>
          <xsp:logic>{
             Enumeration e=request.getAttributeNames();
             while ( e.hasMoreElements() ) {
                 String attribute = (String) e.nextElement();
                 Object value = request.getAttribute(attribute);
                 <tr>
                   <td align="right"><xsp:expr>attribute</xsp:expr></td>
                   <td>="<xsp:expr>value</xsp:expr>"</td>
                 </tr>
             }
          }</xsp:logic>
        </tbody>
      </table>
    </p>

    <hr/>

    <p><h3>Request Parameters</h3></p>
    <p>
      <table border="0">
        <tbody>
          <xsp:logic>{
               Enumeration e=request.getParameterNames();
               while ( e.hasMoreElements() ) {
                   String attribute = (String) e.nextElement();
                   Object[] value = request.getParameterValues(attribute);
                   for (int i=0; i &lt; value.length; i++) {
                      <tr>
                        <td align="right"><xsp:expr>attribute</xsp:expr>[<xsp:expr>i</xsp:expr>]</td>
                        <td>="<xsp:expr>value[i]</xsp:expr>"</td>
                      </tr>
                   }
               }
          }</xsp:logic>
        </tbody>
      </table>
    </p>

    <hr/>

  </page>

</xsp:page>
