KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > DatabaseDeleteAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.acting;
17
18 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23 import org.apache.cocoon.Constants;
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Redirector;
27 import org.apache.cocoon.environment.Request;
28 import org.apache.cocoon.environment.SourceResolver;
29
30 import java.sql.Connection JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * Delete a record from a database. This Action assumes that all
38  * dependant data is either automatically cleaned up by cascading
39  * deletes, or that multiple instances of this action are being used
40  * in the correct order. In other words, it removes one record by
41  * the keys.
42  *
43  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
44  * @version CVS $Id: DatabaseDeleteAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
45  */

46 public final class DatabaseDeleteAction extends AbstractDatabaseAction implements ThreadSafe {
47     private static final Map JavaDoc deleteStatements = new HashMap JavaDoc();
48
49     /**
50      * Delete a record from the database. This action assumes that
51      * the file referenced by the "descriptor" parameter conforms
52      * to the AbstractDatabaseAction specifications.
53      */

54     public final Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc source, Parameters param) throws Exception JavaDoc {
55         DataSourceComponent datasource = null;
56         Connection JavaDoc conn = null;
57         int currentIndex = 0;
58
59         // read global parameter settings
60
boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT;
61         if (this.settings.containsKey("reloadable"))
62             reloadable = Boolean.valueOf((String JavaDoc) this.settings.get("reloadable")).booleanValue();
63         // read local parameter settings
64
try {
65             Configuration conf =
66                 this.getConfiguration(param.getParameter("descriptor", (String JavaDoc) this.settings.get("descriptor")), resolver,
67                                       param.getParameterAsBoolean("reloadable",reloadable));
68
69             String JavaDoc query = this.getDeleteQuery(conf);
70             datasource = this.getDataSource(conf);
71             conn = datasource.getConnection();
72             Request request = ObjectModelHelper.getRequest(objectModel);
73
74             if (conn.getAutoCommit()) {
75                 conn.setAutoCommit(false);
76             }
77
78             PreparedStatement JavaDoc statement = conn.prepareStatement(query);
79
80             Configuration[] keys = conf.getChild("table").getChild("keys").getChildren("key");
81
82             for (int i = 0; i < keys.length; i++) {
83                 this.setColumn(statement, i + 1, request, keys[i]);
84             }
85
86             int rows = statement.executeUpdate();
87             conn.commit();
88             statement.close();
89
90             if (rows > 0) {
91                 request.setAttribute("rows", Integer.toString(rows));
92                 return EMPTY_MAP;
93             }
94         } catch (Exception JavaDoc e) {
95             if (conn != null) {
96                 conn.rollback();
97             }
98             throw new ProcessingException("Could not delete record :position = " + currentIndex, e);
99         } finally {
100             if (conn != null) {
101                 try {
102                     conn.close();
103                 } catch (SQLException JavaDoc sqe) {
104                     getLogger().warn("There was an error closing the datasource", sqe);
105                 }
106             }
107             if (datasource != null) {
108                 this.dbselector.release(datasource);
109             }
110         }
111         return null;
112     }
113
114     /**
115      * Get the String representation of the PreparedStatement. This is
116      * mapped to the Configuration object itself, so if it doesn't exist,
117      * it will be created.
118      */

119     private final String JavaDoc getDeleteQuery(Configuration conf) throws ConfigurationException {
120         String JavaDoc query = null;
121
122         synchronized (DatabaseDeleteAction.deleteStatements) {
123             query = (String JavaDoc) DatabaseDeleteAction.deleteStatements.get(conf);
124
125             if (query == null) {
126                 Configuration table = conf.getChild("table");
127                 Configuration[] keys = table.getChild("keys").getChildren("key");
128
129                 StringBuffer JavaDoc queryBuffer = new StringBuffer JavaDoc("DELETE FROM ");
130                 queryBuffer.append(table.getAttribute("name"));
131                 queryBuffer.append(" WHERE ");
132                 queryBuffer.append(buildList(keys, " AND "));
133                 query = queryBuffer.toString();
134
135                 DatabaseDeleteAction.deleteStatements.put(conf, query);
136             }
137         }
138         return query;
139     }
140 }
141
Popular Tags