haul 2003/01/31 08:05:47 Modified: src/blocks/databases/samples/mod-db database.xml edit-groups.xsp schema.sql sitemap.xmap user-list.xsp Added: src/blocks/databases/samples/mod-db file-upload-blob.xsp Log: sample cosmetics new file upload sample thanks to Geoff Howard Revision Changes Path 1.2 +37 -3 xml-cocoon2/src/blocks/databases/samples/mod-db/database.xml Index: database.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/src/blocks/databases/samples/mod-db/database.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- database.xml 17 Jan 2003 11:22:37 -0000 1.1 +++ database.xml 31 Jan 2003 16:05:47 -0000 1.2 @@ -44,8 +44,16 @@ <key name="uid" type="int"> <!-- Next we have two different modes: "request" and "attrib". See below for explanation. --> - <mode name="request-param" parameter="user_groups.uid" type="request"/> - <mode name="request-attr" parameter="org.apache.cocoon.components.modules.output.OutputModule:user.uid[0]" type="attrib"/> + <mode name="request-param" type="request"/> + <mode name="request-attr" type="attrib"> + <parameter>org.apache.cocoon.components.modules.output.OutputModule:user.uid[0]</parameter> + <!-- use a different parameter name to query than the automatically + generated one (here "user_groups.uid") This is a feature of + the "request-attr" input module but widely available. + Another option would be to use the SimpleMappingMetaModule + instead. + --> + </mode> <!-- note here, that the actual parameter has a row index to it. We don't expect to insert more than one user plus her groups at any time, so we append just "[0]". However, @@ -76,7 +84,7 @@ --> <!-- special mode type "all" is used for all operations --> - <mode name="request-param" parameter="user_groups.gid" type="all"/> + <mode name="request-param" type="all"/> </key> </keys> </table> @@ -92,6 +100,28 @@ </values> </table> + <table name="media"> + <keys> + <key name="id" type="int" autoincrement="true"> + <mode name="auto" type="autoincr"/> + </key> + </keys> + <values> + <value name="image" type="binary"> + <mode name="raw-request-param" type="add"/> + <!-- for multipart form uploads it is important to use the raw-request-param + module since that does not cast the parameter to String and does no + encoding conversions. + --> + </value> + <value name="mimetype" type="string"> + <mode name="jxpath" type="add"> + <parameter>mimeType</parameter> + <input-module name="raw-request-param" parameter="media.image"/> + </mode> + </value> + </values> + </table> <!-- the existing DB actions try to work on all tables listed in this descriptor file. Just like some other newer actions this @@ -106,6 +136,10 @@ <table-set name="groups"> <table name="groups"/> + </table-set> + + <table-set name="media"> + <table name="media" others-mode="add"/> </table-set> <table-set name="user+groups"> 1.2 +7 -0 xml-cocoon2/src/blocks/databases/samples/mod-db/edit-groups.xsp Index: edit-groups.xsp =================================================================== RCS file: /home/cvs/xml-cocoon2/src/blocks/databases/samples/mod-db/edit-groups.xsp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- edit-groups.xsp 17 Jan 2003 11:22:37 -0000 1.1 +++ edit-groups.xsp 31 Jan 2003 16:05:47 -0000 1.2 @@ -13,6 +13,13 @@ </xsp:structure> <page> + <resources> + <resource type="file" href="database.xml">Descriptor</resource> + <resource type="file" href="schema.sql">Schema</resource> + <resource type="doc" href="userdocs/actions/database-actions.html">Actions</resource> + <resource type="doc" href="userdocs/xsp/esql.html">ESQL</resource> + </resources> + <title>edit-groups</title> 1.3 +8 -0 xml-cocoon2/src/blocks/databases/samples/mod-db/schema.sql Index: schema.sql =================================================================== RCS file: /home/cvs/xml-cocoon2/src/blocks/databases/samples/mod-db/schema.sql,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- schema.sql 17 Jan 2003 23:47:19 -0000 1.2 +++ schema.sql 31 Jan 2003 16:05:47 -0000 1.3 @@ -25,3 +25,11 @@ foreign key (uid) references user(uid), foreign key (gid) references groups(gid) ); + +create table media ( + id integer identity primary key, + image varbinary, + mimetype varchar(50), + primary key (id) +); + 1.2 +52 -23 xml-cocoon2/src/blocks/databases/samples/mod-db/sitemap.xmap Index: sitemap.xmap =================================================================== RCS file: /home/cvs/xml-cocoon2/src/blocks/databases/samples/mod-db/sitemap.xmap,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- sitemap.xmap 17 Jan 2003 11:22:37 -0000 1.1 +++ sitemap.xmap 31 Jan 2003 16:05:47 -0000 1.2 @@ -82,21 +82,39 @@ <map:redirect-to uri="user-list"/> </map:match> + <map:match pattern="file-upload-test"> + <map:generate type="serverpages" src="file-upload-blob.xsp"/> + <map:serialize type="xml"/> + </map:match> + + <!-- Deliver images from database --> + <map:match pattern="image/*"> + <map:read type="databasereader" src="{1}" mime-type="image/jpg"> + <!-- option sitemap parameters --> + <map:parameter name="table" value="media"/> + <map:parameter name="image" value="image"/> + <map:parameter name="key" value="id"/> + <map:parameter name="type-column" value="mimetype"/> + </map:read> + </map:match> + <map:match pattern="*"> - <!-- + <!-- First, the logic to do all the database operations. Note, that we do no parameter validation here, just see if some parameters are present. For a real application, you'd want to check their values as well. Note too, that in a more complex setup you'd want to use an action set for this rather than spell it out everywhere. + Or even better, call these actions from the flow layer! + That way it is easier to react to error conditions. --> - <!-- ______________________________ inserts ______________________________ --> + <!-- ______________________________ inserts ______________________________ --> - <!-- add new groups to a user's groups --> + <!-- add new groups to a user's groups --> <map:act type="req-params"> <map:parameter name="parameters" value="add-groups user_groups.uid user_groups.gid"/> <map:act type="mod-db-add"> @@ -104,83 +122,94 @@ </map:act> </map:act> - <!-- add onw new group --> + <!-- add onw new group --> <map:act type="req-params"> <map:parameter name="parameters" value="add-group groups.gname"/> <map:act type="mod-db-add"> - <map:parameter name="table-set" value="groups"/> + <map:parameter name="table-set" value="groups"/> </map:act> </map:act> - <!-- add one new user --> + <!-- add one new user --> <!-- Actually, this one is not used anymore. The one below works well when no groups are supplied. --> <map:act type="req-params"> <map:parameter name="parameters" value="add-user user.name user.firstname user.uname"/> <map:act type="mod-db-add"> - <map:parameter name="table-set" value="user"/> + <map:parameter name="table-set" value="user"/> </map:act> </map:act> - <!-- add one new user plus groups --> + <!-- add one new user plus groups --> <map:act type="req-params"> <map:parameter name="parameters" value="add-user-groups user.name user.firstname user.uname"/> <map:act type="mod-db-add"> - <map:parameter name="table-set" value="user+groups"/> + <map:parameter name="table-set" value="user+groups"/> </map:act> </map:act> - <!-- ______________________________ updates ______________________________ --> + <!-- upload binary data --> + <map:act type="req-params"> + <map:parameter name="parameters" value="add-image"/> <!-- media.image media.id --> + <map:act type="mod-db-add"> + <map:parameter name="table-set" value="media"/> + </map:act> + </map:act> + + <!-- ______________________________ updates ______________________________ --> - <!-- update one user --> + <!-- update one user --> <map:act type="req-params"> <map:parameter name="parameters" value="upd-user user.uid user.name user.firstname user.uname"/> <map:act type="mod-db-upd"> - <map:parameter name="table-set" value="user"/> + <map:parameter name="table-set" value="user"/> </map:act> </map:act> - <!-- update one group --> + <!-- update one group --> <map:act type="req-params"> <map:parameter name="parameters" value="upd-group groups.gid groups.gname"/> <map:act type="mod-db-upd"> - <map:parameter name="table-set" value="groups"/> + <map:parameter name="table-set" value="groups"/> </map:act> </map:act> - <!-- ______________________________ deletes ______________________________ --> + <!-- ______________________________ deletes ______________________________ --> - <!-- delete one user --> + <!-- delete one user --> <map:act type="req-params"> <map:parameter name="parameters" value="del-user user.uid"/> <map:act type="mod-db-del"> - <map:parameter name="table-set" value="user"/> + <map:parameter name="table-set" value="user"/> </map:act> </map:act> - <!-- delete one group --> + <!-- delete one group --> <map:act type="req-params"> <map:parameter name="parameters" value="del-group groups.gid"/> <map:act type="mod-db-del"> - <map:parameter name="table-set" value="groups"/> + <map:parameter name="table-set" value="groups"/> </map:act> </map:act> - <!-- delete groups from a user's groups --> + <!-- delete groups from a user's groups --> <map:act type="req-params"> <map:parameter name="parameters" value="remove-groups user_groups.uid user_groups.gid"/> <map:act type="mod-db-del"> - <map:parameter name="table-set" value="user_groups"/> + <map:parameter name="table-set" value="user_groups"/> </map:act> </map:act> - <!-- ______________________________ content ______________________________ --> + <!-- ______________________________ content ______________________________ --> <map:generate type="serverpages" src="{1}.xsp"/> <map:transform src="context://samples/stylesheets/dynamic-page2html.xsl"> - <map:parameter name="view-source" value="samples/databases/mod-db/{1}.xsp"/> + <map:parameter name="servletPath" value="{request:servletPath}"/> + <map:parameter name="sitemapURI" value="{request:sitemapURI}"/> + <map:parameter name="contextPath" value="{request:contextPath}"/> + <map:parameter name="file" value=".xsp"/> </map:transform> <map:transform src="stupid.xsl"/> <map:serialize/> 1.2 +8 -1 xml-cocoon2/src/blocks/databases/samples/mod-db/user-list.xsp Index: user-list.xsp =================================================================== RCS file: /home/cvs/xml-cocoon2/src/blocks/databases/samples/mod-db/user-list.xsp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- user-list.xsp 17 Jan 2003 11:22:37 -0000 1.1 +++ user-list.xsp 31 Jan 2003 16:05:47 -0000 1.2 @@ -11,7 +11,14 @@ </xsp:structure> <page> - + + <resources> + <resource type="file" href="database.xml">Descriptor</resource> + <resource type="file" href="schema.sql">Schema</resource> + <resource type="doc" href="userdocs/actions/database-actions.html">Actions</resource> + <resource type="doc" href="userdocs/xsp/esql.html">ESQL</resource> + </resources> + <title>user-list</title> <content> 1.1 xml-cocoon2/src/blocks/databases/samples/mod-db/file-upload-blob.xsp Index: file-upload-blob.xsp =================================================================== <?xml version="1.0" encoding="ISO-8859-1"?> <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">Descriptor</resource> <resource type="file" href="schema.sql">Schema</resource> <resource type="doc" href="userdocs/actions/database-actions.html">Actions</resource> <resource type="doc" href="userdocs/xsp/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> <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 < 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>
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]