KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Update a record in a database. This Action assumes that there is
38  * only one table at a time to update.
39  *
40  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
41  * @version CVS $Id: DatabaseUpdateAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  */

43 public class DatabaseUpdateAction extends AbstractDatabaseAction implements ThreadSafe {
44     private static final Map JavaDoc updateStatements = new HashMap JavaDoc();
45
46     /**
47      * Update a record in the database. This action assumes that
48      * the file referenced by the "descriptor" parameter conforms
49      * to the AbstractDatabaseAction specifications.
50      */

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

123     protected String JavaDoc getUpdateQuery(Configuration conf) throws ConfigurationException {
124         String JavaDoc query = null;
125
126         synchronized (DatabaseUpdateAction.updateStatements) {
127             query = (String JavaDoc) DatabaseUpdateAction.updateStatements.get(conf);
128
129             if (query == null) {
130                 Configuration table = conf.getChild("table");
131                 Configuration[] keys = table.getChild("keys").getChildren("key");
132                 Configuration[] values = table.getChild("values").getChildren("value");
133
134                 StringBuffer JavaDoc queryBuffer = new StringBuffer JavaDoc("UPDATE ");
135                 queryBuffer.append(table.getAttribute("name"));
136                 queryBuffer.append(" SET ");
137                 queryBuffer.append(buildList(values, ", "));
138                 queryBuffer.append(" WHERE ");
139                 queryBuffer.append(buildList(keys, " AND "));
140                 query = queryBuffer.toString();
141
142                 DatabaseUpdateAction.updateStatements.put(conf, query);
143             }
144         }
145         return query;
146     }
147 }
148
Popular Tags