bloritsch 01/02/26 13:25:20
Modified: src/org/apache/cocoon/acting Tag: xml-cocoon2
DatabaseAddAction.java
Log:
Add meat to DatabaseAddAction
Revision Changes Path
No revision
No revision
1.1.2.2 +134 -11
xml-cocoon/src/org/apache/cocoon/acting/Attic/DatabaseAddAction.java
Index: DatabaseAddAction.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/acting/Attic/DatabaseAddAction.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -r1.1.2.1 -r1.1.2.2
--- DatabaseAddAction.java 2001/02/26 20:50:46 1.1.2.1
+++ DatabaseAddAction.java 2001/02/26 21:25:13 1.1.2.2
@@ -1,24 +1,147 @@
+/*****************************************************************************
+ * Copyright (C) The Apache Software Foundation. All rights reserved.
*
+ * -------------------------------------------------------------------------
*
+ * This software is published under the terms of the Apache Software License
*
+ * version 1.1, a copy of which has been included with this distribution in
*
+ * the LICENSE file.
*
+
*****************************************************************************/
package org.apache.cocoon.acting;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.Timestamp;
+import java.sql.SQLException;
+
+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;
+import org.apache.avalon.ComponentSelector;
+import org.apache.avalon.ComponentManagerException;
+import org.apache.avalon.Configurable;
+import org.apache.avalon.Configuration;
+import org.apache.avalon.ConfigurationException;
import org.apache.avalon.Parameters;
+import org.apache.cocoon.Roles;
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.environment.http.HttpRequest;
+import org.apache.cocoon.generation.ImageDirectoryGenerator;
+import org.apache.avalon.util.datasource.DataSourceComponent;
+
/**
- * Title:
- * Description:
- * Copyright: Copyright (c) 2000
- * Company:
- * @author
- * @version 1.0
+ * Add a record in a database. This Action assumes that there is
+ * only one table at a time to update.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @version CVS $Revision: 1.1.2.2 $ $Date: 2001/02/26 21:25:13 $
*/
+public class DatabaseAddAction extends AbstractDatabaseAction {
+ private static final Map addStatements = new HashMap();
-public class DatabaseAddAction extends ComposerAction {
+ /**
+ * Delete a record from the database. This action assumes that
+ * the file referenced by the "form-descriptor" parameter conforms
+ * to the AbstractDatabaseAction specifications.
+ */
+ public Map act(EntityResolver resolver, Map objectModel, String source,
Parameters param) throws Exception {
+ DataSourceComponent datasource = null;
+ Connection conn = null;
- public DatabaseAddAction() {
- }
- public Map act(EntityResolver parm1, Map parm2, String parm3, Parameters
parm4) throws java.lang.Exception {
- /[EMAIL PROTECTED]: implement this
org.apache.cocoon.acting.AbstractAction abstract method*/
+ try {
+ Configuration conf =
this.getConfiguration(param.getParameter("form-descriptor", null));
+ String query = this.getAddQuery(conf);
+ datasource = this.getDataSource(conf);
+ conn = datasource.getConnection();
+ HttpRequest request = (HttpRequest)
objectModel.get(Constants.REQUEST_OBJECT);
+
+ PreparedStatement statement = conn.prepareStatement(query);
+
+ Iterator values =
conf.getChild("table").getChild("values").getChildren("value");
+ int currentIndex = 1;
+
+ for (int i = currentIndex; values.hasNext(); i++) {
+ Configuration itemConf = (Configuration) values.next();
+ String parameter = itemConf.getAttribute("param");
+ Object value = request.get(parameter);
+ this.setColumn(statement, i, value, itemConf);
+ currentIndex = i;
+ }
+
+ statement.execute();
+ } catch (Exception e) {
+ throw new ProcessingException("Could not delete record", e);
+ } finally {
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (SQLException sqe) {
+ getLogger().warn("There was an error closing the
datasource", sqe);
+ }
+ }
+
+ if (datasource != null) this.dbselector.release(datasource);
+ }
+
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 getAddQuery(Configuration conf) throws
ConfigurationException {
+ String query = null;
+
+ synchronized (DatabaseAddAction.addStatements) {
+ query = (String) DatabaseAddAction.addStatements.get(conf);
+
+ if (query == null) {
+ Configuration table = conf.getChild("table");
+ Iterator values =
table.getChild("values").getChildren("value");
+
+ StringBuffer queryBuffer = new StringBuffer("INSERT INTO ");
+ queryBuffer.append(table.getAttribute("name"));
+ queryBuffer.append(" (");
+
+ boolean firstIteration = true;
+
+ while (values.hasNext()) {
+ if (firstIteration) {
+ firstIteration = false;
+ } else {
+ queryBuffer.append(", ");
+ }
+
+ queryBuffer.append(((Configuration)
values.next()).getAttribute("dbcol"));
+ }
+
+ queryBuffer.append(") VALUES (");
+
+ values = table.getChild("values").getChildren("value");
+ firstIteration = true;
+
+ while (values.hasNext()) {
+ if (firstIteration) {
+ firstIteration = false;
+ } else {
+ queryBuffer.append(", ");
+ }
+
+ queryBuffer.append("?");
+ }
+ }
+
+ DatabaseAddAction.addStatements.put(conf, query);
+ }
+
+ return query;
}
}