balld 01/05/18 13:03:19
Modified: src/org/apache/cocoon/acting AbstractDatabaseAction.java
DatabaseAddAction.java
Log:
some more javadocs, made the DatabaseAddAction return a map
Revision Changes Path
1.3 +22 -6
xml-cocoon2/src/org/apache/cocoon/acting/AbstractDatabaseAction.java
Index: AbstractDatabaseAction.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/org/apache/cocoon/acting/AbstractDatabaseAction.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- AbstractDatabaseAction.java 2001/05/18 18:55:02 1.2
+++ AbstractDatabaseAction.java 2001/05/18 20:03:16 1.3
@@ -173,7 +173,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Donald Ball</a>
- * @version CVS $Revision: 1.2 $ $Date: 2001/05/18 18:55:02 $
+ * @version CVS $Revision: 1.3 $ $Date: 2001/05/18 20:03:16 $
*/
public abstract class AbstractDatabaseAction extends
AbstractComplementaryConfigurableAction implements Configurable, Disposable {
protected Map files = new HashMap();
@@ -307,7 +307,7 @@
break;
}
- request.setAttribute(attribute, value);
+ setRequestAttribute(request,attribute,value);
return value;
}
@@ -417,7 +417,7 @@
/** Store the column value in the request attribute
keyed by the request parameter name. we do this so possible
future
actions can access this data. not sure about the key tho... **/
- request.setAttribute(param, value);
+ setRequestAttribute(request,param,value);
File file;
switch (typeObject.intValue()) {
@@ -580,7 +580,7 @@
statement.setInt(position,
parameters.getParameterAsInteger("image-width", -1));
/** Store the image width in the request attributes.
Why do we do this? **/
- request.setAttribute(param,
parameters.getParameter("image-width", ""));
+
setRequestAttribute(request,param,parameters.getParameter("image-width",""));
}
} else if ("image-height".equals(typeName)) {
/** Get the image height from the cached image data **/
@@ -589,7 +589,7 @@
synchronized (this.files) {
Parameters parameters = (Parameters)
this.files.get(file);
statement.setInt(position,
parameters.getParameterAsInteger("image-height", -1));
- request.setAttribute(param,
parameters.getParameter("image-height", ""));
+
setRequestAttribute(request,param,parameters.getParameter("image-height",""));
}
} else if ("image-size".equals(typeName)) {
/** Get the image file size from the cached image data
**/
@@ -598,7 +598,7 @@
synchronized (this.files) {
Parameters parameters = (Parameters)
this.files.get(file);
statement.setInt(position,
parameters.getParameterAsInteger("image-size", -1));
- request.setAttribute(param,
parameters.getParameter("image-size", ""));
+
setRequestAttribute(request,param,parameters.getParameter("image-size",""));
}
}
default:
@@ -621,4 +621,20 @@
this.manager.release((Component)dbselector);
super.dispose();
}
+
+ /**
+ * Store a key/value pair in the request attributes. We prefix the key
+ * with the name of this class to prevent potential name collisions.
+ */
+ void setRequestAttribute(Request request, String key, Object value) {
+
request.setAttribute("org.apache.cocoon.acting.AbstractDatabaseAction:"+key,value);
+ }
+
+ /**
+ * Retreive a value from the request attributes.
+ */
+ Object getRequestAttribute(Request request, String key) {
+ return
request.getAttribute("org.apache.cocoon.acting.AbstractDatabaseAction:"+key);
+ }
+
}
1.3 +33 -12
xml-cocoon2/src/org/apache/cocoon/acting/DatabaseAddAction.java
Index: DatabaseAddAction.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/org/apache/cocoon/acting/DatabaseAddAction.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DatabaseAddAction.java 2001/05/18 18:55:03 1.2
+++ DatabaseAddAction.java 2001/05/18 20:03:17 1.3
@@ -17,6 +17,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Enumeration;
+import java.util.Collections;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.configuration.Configurable;
@@ -40,7 +41,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Donald Ball</a>
- * @version CVS $Revision: 1.2 $ $Date: 2001/05/18 18:55:03 $
+ * @version CVS $Revision: 1.3 $ $Date: 2001/05/18 20:03:17 $
*/
public class DatabaseAddAction extends AbstractDatabaseAction {
protected static final Map addStatements = new HashMap();
@@ -54,6 +55,7 @@
public Map act(EntityResolver resolver, Map objectModel, String source,
Parameters param) throws Exception {
DataSourceComponent datasource = null;
Connection conn = null;
+ Map results = new HashMap();
try {
Configuration conf =
this.getConfiguration(param.getParameter("form-descriptor", null));
@@ -69,7 +71,7 @@
Configuration[] tables = conf.getChildren("table");
for (int i=0; i<tables.length; i++) {
Configuration table = tables[i];
- processTable(table,conn,request);
+ processTable(table,conn,request,results);
}
conn.commit();
} catch (Exception e) {
@@ -95,7 +97,7 @@
if (datasource != null) this.dbselector.release(datasource);
}
- return null;
+ return Collections.unmodifiableMap(results);
}
/**
@@ -106,7 +108,7 @@
* @param conn the database connection
* @param request the request
*/
- void processTable(Configuration table, Connection conn, Request request)
throws SQLException,ConfigurationException,Exception {
+ void processTable(Configuration table, Connection conn, Request request,
Map results) throws SQLException,ConfigurationException,Exception {
PreparedStatement statement = null;
try {
String query = this.getAddQuery(table);
@@ -141,7 +143,7 @@
currentIndex = 1;
for (int j=0; j<keys.length; j++) {
String myparam =
getActualParam(keys[j].getAttribute("param"),wildcard);
- currentIndex +=
setKey(table,keys[j],conn,statement,currentIndex,request,myparam);
+ currentIndex +=
setKey(table,keys[j],conn,statement,currentIndex,request,myparam,results);
}
for (int j=0; j<values.length; j++) {
String myparam =
getActualParam(values[j].getAttribute("param"),wildcard);
@@ -157,7 +159,7 @@
* be inserting 1 row.
*/
for (int i = 0; i < keys.length; i++) {
- currentIndex +=
setKey(table,keys[i],conn,statement,currentIndex,request,keys[i].getAttribute("param",""));
+ currentIndex +=
setKey(table,keys[i],conn,statement,currentIndex,request,keys[i].getAttribute("param",""),results);
}
for (int i = 0; i < values.length; i++, currentIndex++) {
this.setColumn(statement, currentIndex, request, values[i]);
@@ -175,9 +177,27 @@
}
/**
- * Sets the key value on the prepared statement and store the value in
- * the request object's attributes for use by other inserts.
+ * Sets the key value on the prepared statement. There are four modes:
*
+ * <dl>
+ * <dt>automatic (default)</dt>
+ * <dd>let the database automatically create the key. note this
+ * prohibits the action from storing the key value anywhere.</dd>
+ * <dt>manual</dt>
+ * <dd>create the key value using SELECT(dbcol)+1 from TABLE</dd>
+ * <dt>form</dt>
+ * <dd>look for the key value in the request parameters</dd>
+ * <dt>request-attribute</dt>
+ * <dd>look for the key value in the request attributes</dd>
+ * </dl>
+ *
+ * This method has a couple of side effects. If the mode is manual,
+ * the key value is stored in the request object's attributes for use
+ * by other inserts. The key is the string "key:TABLENAME:DBCOL".
+ * This method also puts the value of manually created keys in the
results
+ * map. That key is simply the value of the dbcol attribute. Note this
+ * stuff is definitely up in the air.
+ *
* @param table the table's configuration object
* @param key the key's configuration object
* @param conn the database connection
@@ -187,9 +207,9 @@
* @param param the actual name of the request parameter
* @return the number of columns by which to increment the currentIndex
*/
- int setKey(Configuration table, Configuration key, Connection conn,
PreparedStatement statement, int currentIndex, Request request, String param)
throws ConfigurationException, SQLException,Exception {
+ int setKey(Configuration table, Configuration key, Connection conn,
PreparedStatement statement, int currentIndex, Request request, String param,
Map results) throws ConfigurationException, SQLException,Exception {
String mode = key.getAttribute("mode","automatic");
- String attribute_name =
"key:"+table.getAttribute("name")+':'+key.getAttribute("dbcol");
+ String keyname =
"key:"+table.getAttribute("name")+':'+key.getAttribute("dbcol");
if ("manual".equals(mode)) {
/** Set the key value using SELECT MAX(keyname)+1 **/
String selectQuery = this.getSelectQuery(key);
@@ -199,7 +219,8 @@
int value = set.getInt("maxid") + 1;
statement.setInt(currentIndex, value);
getLogger().debug("Manually setting key to "+value);
- request.setAttribute(attribute_name,new Integer(value));
+ setRequestAttribute(request,keyname,new Integer(value));
+ results.put(key.getAttribute("dbcol"),String.valueOf(value));
set.close();
select_statement.close();
return 1;
@@ -209,7 +230,7 @@
this.setColumn(statement, currentIndex, request, key, param);
return 1;
} else if ("request-attribute".equals(mode)) {
- Integer value =
(Integer)request.getAttribute(key.getAttribute("request-attribute-name"));
+ Integer value =
(Integer)getRequestAttribute(request,key.getAttribute("request-attribute-name"));
getLogger().debug("Setting key from request attribute "+value);
statement.setInt(currentIndex,value.intValue());
return 1;
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]