bloritsch 01/02/26 12:49:04
Modified: src/org/apache/cocoon/acting Tag: xml-cocoon2
DatabaseDeleteAction.java
Added: src/org/apache/cocoon/acting Tag: xml-cocoon2
AbstractDatabaseAction.java
Log:
Abstract out the Database Actions, and make them runtime configurable
Revision Changes Path
No revision
No revision
1.1.2.2 +58 -66
xml-cocoon/src/org/apache/cocoon/acting/Attic/DatabaseDeleteAction.java
Index: DatabaseDeleteAction.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/acting/Attic/DatabaseDeleteAction.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -r1.1.2.1 -r1.1.2.2
--- DatabaseDeleteAction.java 2001/02/23 22:34:27 1.1.2.1
+++ DatabaseDeleteAction.java 2001/02/26 20:49:01 1.1.2.2
@@ -12,9 +12,12 @@
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.SQLException;
-import java.util.Map;
-import java.util.Iterator;
+
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
import org.xml.sax.EntityResolver;
import org.apache.avalon.Component;
@@ -40,31 +43,12 @@
* the keys.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @version CVS $Revision: 1.1.2.1 $ $Date: 2001/02/23 22:34:27 $
+ * @version CVS $Revision: 1.1.2.2 $ $Date: 2001/02/26 20:49:01 $
*/
-public class DatabaseDeleteAction extends ComposerAction implements
Configurable {
- private ComponentSelector dbselector;
- private String database;
+public final class DatabaseDeleteAction extends AbstractDatabaseAction {
+ private static final Map deleteStatements = new HashMap();
/**
- * Configure the <code>Action</code> so that we can use the same database
- * for all instances.
- */
- public void configure(Configuration conf) throws ConfigurationException {
- super.configure(conf);
-
- Configuration connElement = conf.getChild("use-connection");
-
- try {
- this.dbselector = (ComponentSelector)
this.manager.lookup(Roles.DB_CONNECTION);
- this.database = connElement.getValue();
- } catch (ComponentManagerException cme) {
- getLogger().error("Could not get the
DataSourceComponentSelector", cme);
- throw new ConfigurationException("Could not get the DataSource
ComponentSelector", cme);
- }
- }
-
- /**
* Delete a record from the database. This action assumes that
* the parameter names are the request parameter names and the parameter
* values are the database column names. The two notable exceptions
@@ -82,59 +66,28 @@
* </li>
* </ul>
*/
- public Map act(EntityResolver resolver, Map objectModel, String source,
Parameters param) throws Exception {
+ public final Map act(EntityResolver resolver, Map objectModel, String
source, Parameters param) throws Exception {
DataSourceComponent datasource = null;
Connection conn = null;
try {
- datasource = (DataSourceComponent)
this.dbselector.select(this.database);
+ Configuration conf =
this.getConfiguration(param.getParameter("form-descriptor", null));
+ String query = this.getDeleteQuery(conf);
+ datasource = this.getDataSource(conf);
conn = datasource.getConnection();
HttpRequest request = (HttpRequest)
objectModel.get(Constants.REQUEST_OBJECT);
- Iterator lookup = param.getParameterNames();
- ArrayList keys = new ArrayList();
- ArrayList keyNames = new ArrayList();
- ArrayList values = new ArrayList();
-
- while (lookup.hasNext()) {
- String test = (String) lookup.next();
-
- if (test.startsWith("key:")) {
- keys.add(param.getParameter(test, null));
- keyNames.add(test.substring("key:".length()));
- } else if ("table".equals(test) == false) {
- values.add(param.getParameter(test, null));
- }
- }
+ PreparedStatement statement = conn.prepareStatement(query);
- StringBuffer query = new StringBuffer("DELETE FROM ");
- query.append(param.getParameter("table", null));
- query.append(" WHERE ");
-
- boolean firstIteration = true;
- Iterator keyIterator = keys.iterator();
-
- while (keyIterator.hasNext()) {
- if (firstIteration) {
- firstIteration = false;
- } else {
- query.append(" AND ");
- }
+ Iterator keys =
conf.getChild("table").getChild("keys").getChildren("key");
- query.append((String) keyIterator.next());
- query.append(" = ?");
+ for (int i = 1; keys.hasNext(); i++) {
+ Configuration itemConf = (Configuration) keys.next();
+ String parameter = itemConf.getAttribute("param");
+ Object value = request.get(parameter);
+ this.setColumn(statement, i, value, itemConf);
}
- PreparedStatement statement =
conn.prepareStatement(query.toString());
-
- keyIterator = keyNames.iterator();
- int i = 1;
-
- while (keyIterator.hasNext()) {
- statement.setString(i, request.getParameter((String)
keyIterator.next()));
- i++;
- }
-
statement.execute();
} catch (Exception e) {
throw new ProcessingException("Could not delete record", e);
@@ -151,5 +104,44 @@
}
return null;
+ }
+
+ /**
+ * Get the String representation of the PreparedStatement. This is
+ * mapped to the Configuration object itself, so if it doesn't exist,
+ * it will be created.
+ */
+ private final String getDeleteQuery(Configuration conf) throws
ConfigurationException {
+ String query = null;
+
+ synchronized (DatabaseDeleteAction.deleteStatements) {
+ query = (String) DatabaseDeleteAction.deleteStatements.get(conf);
+
+ if (query == null) {
+ Configuration table = conf.getChild("table");
+ Iterator keys = table.getChild("keys").getChildren("key");
+
+ StringBuffer queryBuffer = new StringBuffer("DELETE FROM ");
+ queryBuffer.append(table.getAttribute("name"));
+ queryBuffer.append(" WHERE ");
+
+ boolean firstIteration = true;
+
+ while (keys.hasNext()) {
+ if (firstIteration) {
+ firstIteration = false;
+ } else {
+ queryBuffer.append(" AND ");
+ }
+
+ queryBuffer.append(((Configuration)
keys.next()).getAttribute("dbcol"));
+ queryBuffer.append(" = ?");
+ }
+ }
+
+ DatabaseDeleteAction.deleteStatements.put(conf, query);
+ }
+
+ return query;
}
}
No revision
No revision
1.1.2.1 +212 -0
xml-cocoon/src/org/apache/cocoon/acting/Attic/AbstractDatabaseAction.java