haul 02/04/28 13:02:22 Modified: src/scratchpad/src/org/apache/cocoon/acting/modular DatabaseAction.java DatabaseSelectAction.java DatabaseDeleteAction.java DatabaseUpdateAction.java DatabaseAddAction.java TestAction.java Log: * aliases now work * row-count reports number of affected rows to sitemap * other fixes Revision Changes Path 1.2 +40 -11 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseAction.java Index: DatabaseAction.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DatabaseAction.java 15 Mar 2002 15:23:43 -0000 1.1 +++ DatabaseAction.java 28 Apr 2002 20:02:22 -0000 1.2 @@ -124,6 +124,9 @@ * descriptor.xml file are looked up in the component service. Default * mode names can only be set during action setup. </p> * + * <p>The number of affected rows is returned to the sitemap with the + * "row-count" parameter if at least one row was affected.</p> + * <table> * <tr><td colspan="2">Configuration options (setup):</td></tr> * <tr><td>input </td><td>default mode name for reading values</td></tr> @@ -139,7 +142,7 @@ * </table> * * @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a> - * @version CVS $Id: DatabaseAction.java,v 1.1 2002/03/15 15:23:43 haul Exp $ + * @version CVS $Id: DatabaseAction.java,v 1.2 2002/04/28 20:02:22 haul Exp $ * @see org.apache.cocoon.modules.input * @see org.apache.cocoon.modules.output * @see org.apache.cocoon.modules.database @@ -345,6 +348,7 @@ DataSourceComponent datasource = null; Connection conn = null; Map results = new HashMap(); + int rows = 0; // read global parameter settings boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT; @@ -424,15 +428,22 @@ modeTypes.put( MODE_AUTOINCR, "autoincr" ); modeTypes.put( MODE_OTHERS, "others" ); }; + for (int i=0; i<tables.length; i++) { - if ( set_tables == null || + if (set_tables == null || set_tables.containsKey( tables[i].getAttribute( "name" ) ) || ( tables[i].getAttribute( "alias", null ) != null && set_tables.containsKey( tables[i].getAttribute( "alias" ) ) ) ) { if (tablesetname != null) { - modeTypes = (HashMap) set_tables.get(tables[i].getAttribute("name")); + if (tables[i].getAttribute("alias", null) != null && set_tables.containsKey(tables[i].getAttribute("alias"))){ + modeTypes = (HashMap) set_tables.get(tables[i].getAttribute("alias")); + set_tables.remove(tables[i].getAttribute("alias")); + } else { + modeTypes = (HashMap) set_tables.get(tables[i].getAttribute("name")); + set_tables.remove(tables[i].getAttribute("name")); + } } - processTable( tables[i], conn, request, results, modeTypes ); + rows += processTable( tables[i], conn, request, results, modeTypes ); } } @@ -468,6 +479,7 @@ if (getLogger().isDebugEnabled()) getLogger().debug( "Rolling back transaction. Caused by " + e.getMessage() ); conn.rollback(); + results = null; // obtain output mode module and commit output ComponentSelector outputSelector = null; @@ -520,9 +532,21 @@ if (datasource != null) this.dbselector.release(datasource); } + if (results != null) { + if (rows>0) { + results.put("row-count",new Integer(rows)); + } else { + results = null; + } + } else { + if (rows>0) { + results = new HashMap(1); + results.put("row-count",new Integer(rows)); + } + } - return Collections.unmodifiableMap(results); - } + return (results == null? results : Collections.unmodifiableMap(results)); + } @@ -534,15 +558,18 @@ * @param conn the database connection * @param request the request */ - protected void processTable( Configuration table, Connection conn, Request request, + protected int processTable( Configuration table, Connection conn, Request request, Map results, HashMap modeTypes ) throws SQLException, ConfigurationException, Exception { PreparedStatement statement = null; + int rows = 0; try { LookUpKey luk = new LookUpKey(table, modeTypes); CacheHelper queryData = null; + getLogger().debug("modeTypes : "+ modeTypes); + // get cached data // synchronize complete block since we don't want 100s of threads // generating the same cached data set. In the long run all data @@ -573,7 +600,7 @@ for ( int rowIndex = 0; rowIndex < setLength; rowIndex++ ) { if (getLogger().isDebugEnabled()) getLogger().debug( "====> row no. " + rowIndex ); - processRow( request, conn, statement, table, queryData, columnValues, rowIndex, results ); + rows += processRow( request, conn, statement, table, queryData, columnValues, rowIndex, results ); } } finally { @@ -583,6 +610,7 @@ } } catch (SQLException e) {} } + return rows; } @@ -844,13 +872,14 @@ /** * set all necessary ?s and execute the query + * return number of rows processed * * This method is intended to be overridden by classes that * implement other operations e.g. delete */ - protected abstract void processRow( Request request, Connection conn, PreparedStatement statement, - Configuration table, CacheHelper queryData, Object[][] columnValues, - int rowIndex, Map results ) + protected abstract int processRow( Request request, Connection conn, PreparedStatement statement, + Configuration table, CacheHelper queryData, Object[][] columnValues, + int rowIndex, Map results ) throws SQLException, ConfigurationException, Exception; /** 1.2 +19 -13 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseSelectAction.java Index: DatabaseSelectAction.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseSelectAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DatabaseSelectAction.java 15 Mar 2002 15:23:43 -0000 1.1 +++ DatabaseSelectAction.java 28 Apr 2002 20:02:22 -0000 1.2 @@ -80,7 +80,7 @@ * a state of flux. * * @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a> - * @version CVS $Id: DatabaseSelectAction.java,v 1.1 2002/03/15 15:23:43 haul Exp $ + * @version CVS $Id: DatabaseSelectAction.java,v 1.2 2002/04/28 20:02:22 haul Exp $ */ public class DatabaseSelectAction extends DatabaseAction { @@ -90,7 +90,7 @@ * highly specific to operation INSERT / UPDATE / DELETE / SELECT */ protected String selectMode ( boolean isAutoIncrement, HashMap modes ) { - + return (String) modes.get( MODE_OTHERS ); } @@ -121,32 +121,36 @@ Configuration[] keys = table.getChild("keys").getChildren("key"); Configuration[] values = table.getChild("values").getChildren("value"); - queryData = new CacheHelper( keys.length, keys.length ); - fillModes( keys, true, defaultModeNames, modeTypes, queryData ); + queryData = new CacheHelper( keys.length, keys.length + values.length ); + fillModes( keys , true , defaultModeNames, modeTypes, queryData ); fillModes( values, false, defaultModeNames, modeTypes, queryData ); - + StringBuffer queryBuffer = new StringBuffer("SELECT "); - queryBuffer.append(table.getAttribute("name")).append(" WHERE "); + //queryBuffer.append(table.getAttribute("name")).append(" WHERE "); + int count = 0; for (int i = 0; i < queryData.columns.length; i++) { if ( !queryData.columns[i].isKey ) { - if ( i > 0 ) { + if ( count > 0 ) { queryBuffer.append(", "); } queryBuffer .append( queryData.columns[i].columnConf.getAttribute( "name" ) ); + count++; } } queryBuffer.append(" FROM ").append(table.getAttribute("name")).append(" WHERE "); + count = 0; for (int i = 0; i < queryData.columns.length; i++) { if ( queryData.columns[i].isKey ) { - if ( i > 0 ) { + if ( count > 0 ) { queryBuffer.append(" AND "); } queryBuffer .append( queryData.columns[i].columnConf.getAttribute( "name" ) ) .append( "= ?"); + count++; } } @@ -182,8 +186,8 @@ /** * set all necessary ?s and execute the query */ - protected void processRow ( Request request, Connection conn, PreparedStatement statement, Configuration table, - CacheHelper queryData, Object[][] columnValues, int rowIndex, Map results ) + protected int processRow ( Request request, Connection conn, PreparedStatement statement, Configuration table, + CacheHelper queryData, Object[][] columnValues, int rowIndex, Map results ) throws SQLException, ConfigurationException, Exception { int currentIndex = 1; @@ -202,19 +206,21 @@ statement.execute(); // retrieve values ResultSet resultset = statement.getResultSet(); - rowIndex = -1; + rowIndex = 0; while ( resultset.next() ){ - if ( ! ( rowIndex == -1 && resultset.isLast() ) ) { + //if ( ! ( rowIndex == -1 && resultset.isLast() ) ) { rowIndex++; - } + //} for (int i = 0; i < queryData.columns.length; i++) { if ( !queryData.columns[i].isKey ) { Object value = this.getColumn( resultset, queryData.columns[i].columnConf ); String param = getOutputName( table, queryData.columns[i].columnConf, rowIndex ); this.setRequestAttribute( request, param, value ); + results.put(param,value); } } } + return rowIndex; } 1.2 +6 -5 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseDeleteAction.java Index: DatabaseDeleteAction.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseDeleteAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DatabaseDeleteAction.java 15 Mar 2002 15:23:43 -0000 1.1 +++ DatabaseDeleteAction.java 28 Apr 2002 20:02:22 -0000 1.2 @@ -70,7 +70,7 @@ * flux. * * @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a> - * @version CVS $Id: DatabaseDeleteAction.java,v 1.1 2002/03/15 15:23:43 haul Exp $ + * @version CVS $Id: DatabaseDeleteAction.java,v 1.2 2002/04/28 20:02:22 haul Exp $ */ public class DatabaseDeleteAction extends DatabaseAction { @@ -158,9 +158,9 @@ /** * set all necessary ?s and execute the query */ - protected void processRow ( Request request, Connection conn, PreparedStatement statement, - Configuration table, CacheHelper queryData, Object[][] columnValues, - int rowIndex, Map results ) + protected int processRow ( Request request, Connection conn, PreparedStatement statement, + Configuration table, CacheHelper queryData, Object[][] columnValues, + int rowIndex, Map results ) throws SQLException, ConfigurationException, Exception { int currentIndex = 1; @@ -176,7 +176,8 @@ currentIndex++; } } - statement.execute(); + int rowCount = statement.executeUpdate(); + return rowCount; } } 1.2 +5 -4 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseUpdateAction.java Index: DatabaseUpdateAction.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseUpdateAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DatabaseUpdateAction.java 15 Mar 2002 15:23:43 -0000 1.1 +++ DatabaseUpdateAction.java 28 Apr 2002 20:02:22 -0000 1.2 @@ -70,7 +70,7 @@ * flux. * * @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a> - * @version CVS $Id: DatabaseUpdateAction.java,v 1.1 2002/03/15 15:23:43 haul Exp $ + * @version CVS $Id: DatabaseUpdateAction.java,v 1.2 2002/04/28 20:02:22 haul Exp $ */ public class DatabaseUpdateAction extends DatabaseAction { @@ -171,8 +171,8 @@ /** * set all necessary ?s and execute the query */ - protected void processRow ( Request request, Connection conn, PreparedStatement statement, Configuration table, - CacheHelper queryData, Object[][] columnValues, int rowIndex, Map results ) + protected int processRow ( Request request, Connection conn, PreparedStatement statement, Configuration table, + CacheHelper queryData, Object[][] columnValues, int rowIndex, Map results ) throws SQLException, ConfigurationException, Exception { @@ -199,7 +199,8 @@ currentIndex++; } } - statement.execute(); + int rowCount = statement.executeUpdate(); + return rowCount; } } 1.2 +6 -5 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseAddAction.java Index: DatabaseAddAction.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/DatabaseAddAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DatabaseAddAction.java 15 Mar 2002 15:23:43 -0000 1.1 +++ DatabaseAddAction.java 28 Apr 2002 20:02:22 -0000 1.2 @@ -72,7 +72,7 @@ * {@see DatabaseAction} for details. * * @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a> - * @version CVS $Id: DatabaseAddAction.java,v 1.1 2002/03/15 15:23:43 haul Exp $ + * @version CVS $Id: DatabaseAddAction.java,v 1.2 2002/04/28 20:02:22 haul Exp $ */ public class DatabaseAddAction extends DatabaseAction { @@ -80,9 +80,9 @@ /** * set all necessary ?s and execute the query */ - protected void processRow ( Request request, Connection conn, PreparedStatement statement, - Configuration table, CacheHelper queryData, Object[][] columnValues, - int rowIndex, Map results ) + protected int processRow ( Request request, Connection conn, PreparedStatement statement, + Configuration table, CacheHelper queryData, Object[][] columnValues, + int rowIndex, Map results ) throws SQLException, ConfigurationException, Exception { int currentIndex = 1; @@ -99,7 +99,7 @@ currentIndex++; } } - statement.execute(); + int rowCount = statement.executeUpdate(); // get resulting ids for autoincrement columns for (int i = 0; i < queryData.columns.length; i++) { if ( queryData.columns[i].isAutoIncrement && queryData.columns[i].isKey ) { @@ -107,6 +107,7 @@ conn, statement, request, results ); } } + return rowCount; } 1.2 +72 -21 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/TestAction.java Index: TestAction.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/acting/modular/TestAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestAction.java 15 Mar 2002 15:23:43 -0000 1.1 +++ TestAction.java 28 Apr 2002 20:02:22 -0000 1.2 @@ -52,9 +52,9 @@ package org.apache.cocoon.acting.modular; import org.apache.avalon.framework.activity.Initializable; +import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.avalon.framework.parameters.Parameterizable; import org.apache.avalon.framework.parameters.ParameterException; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; @@ -79,29 +79,44 @@ * they should instead use components to access their inputs and * outputs. Available components include request parameters, * attributes, headers, and session attributes. Which component to use - * can be specified from sitemap via "input" and "output" parameters. + * can be specified upon setup via "input-module" and + * "output-module" tags through the name attribute. * * This particular action copies all available parameters obtained - * from the input component to the output component. - * */ + * from the input component to the output component or, if a specific + * parameter is specified through parameter-name, just one parameter. + */ public class TestAction extends ComposerAction - implements Parameterizable, ThreadSafe { + implements Configurable, ThreadSafe { String INPUT_MODULE_ROLE = InputModule.ROLE; String OUTPUT_MODULE_ROLE = OutputModule.ROLE; String INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE+"Selector"; String OUTPUT_MODULE_SELECTOR = OUTPUT_MODULE_ROLE+"Selector"; + Configuration inputConf = null; + Configuration outputConf = null; + String inputName = null; + String outputName = null; + String defaultParameterName = null; + boolean useGetValues = false; + String inputHint = "request"; // default to request parameters String outputHint = "attribute"; // default to request attributes - public void parameterize(Parameters parameters) throws ParameterException { - if (parameters != null) { - this.inputHint = parameters.getParameter("input", inputHint); - this.outputHint = parameters.getParameter("output", outputHint); - } + + public void configure(Configuration config) throws ConfigurationException { + + this.inputConf = config.getChild("input-module"); + this.inputName = this.inputConf.getAttribute("name"); + this.outputConf = config.getChild("output-module"); + this.outputName = this.outputConf.getAttribute("name"); + this.defaultParameterName = config.getChild("parameter-name").getValue(null); + this.useGetValues = config.getChild("use-getValues").getValueAsBoolean(this.useGetValues); } + + public Map act( Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param ) throws Exception { @@ -111,12 +126,12 @@ getLogger().error("no request object!"); return(null); } + String parameterName = param.getParameter("parameter-name",this.defaultParameterName); + boolean useGetValues = param.getParameterAsBoolean("use-getValues",this.useGetValues); InputModule input = null; OutputModule output = null; ComponentSelector inputSelector = null; ComponentSelector outputSelector = null; - String inputName = param.getParameter("input", this.inputHint); - String outputName = param.getParameter("output", this.outputHint); try { if (getLogger().isDebugEnabled()) getLogger().debug("start..."); @@ -129,20 +144,56 @@ if (outputName != null && outputSelector != null && outputSelector.hasComponent(outputName)){ output = (OutputModule) outputSelector.select(outputName); } + + if (input != null && output != null) { - if (getLogger().isDebugEnabled()) getLogger().debug("got input and output adaptors"); + if (getLogger().isDebugEnabled()) getLogger().debug("got input and output modules"); // do something - // for a test, read all parameters from input and write them to outout - Enumeration enum = input.getAttributeNames(null,request); - while (enum.hasMoreElements()) { - String parameterName = (String) enum.nextElement(); - Object value = input.getAttribute(parameterName, null, request); - if (getLogger().isDebugEnabled()) getLogger().debug("["+parameterName+"] = ["+value+"]"); - output.setAttribute(null, request, parameterName, value); + + if (parameterName == null) { + if (getLogger().isDebugEnabled()) getLogger().debug("reading all parameter values"); + // for a test, read all parameters from input and write them to outout + // get names first, then (one) value per name + Enumeration enum = input.getAttributeNames(this.inputConf,request); + while (enum.hasMoreElements()) { + parameterName = (String) enum.nextElement(); + Object value = input.getAttribute(parameterName, this.inputConf, request); + output.setAttribute(this.outputConf, request, parameterName, value); + + if (getLogger().isDebugEnabled()) + getLogger().debug("["+parameterName+"] = ["+value+"]"); + } + // ------------------------------------------------------------------------ + } else { + + if (useGetValues) { + // get all existing values + Object[] value = input.getAttributeValues(parameterName, this.inputConf, request); + output.setAttribute(this.outputConf, request, parameterName, value); + + if (getLogger().isDebugEnabled()) + for (int i=0; i<value.length; i++) + getLogger().debug("["+parameterName+"["+i+"]] = ["+value[i]+"]"); + // ------------------------------------------------------------------------ + + } else { + // get just one parameter value + if (getLogger().isDebugEnabled()) + getLogger().debug("reading parameter values for "+parameterName); + + Object value = input.getAttribute(parameterName, this.inputConf, request); + output.setAttribute(this.outputConf, request, parameterName, value); + + if (getLogger().isDebugEnabled()) getLogger().debug("["+parameterName+"] = ["+value+"]"); + // ------------------------------------------------------------------------ + } } - output.commit(null,request); + output.commit(this.outputConf,request); if (getLogger().isDebugEnabled()) getLogger().debug("done commit"); + // done } + + } catch (Exception e) { throw e; } finally {
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]