KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > modular > 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
17 package org.apache.cocoon.acting.modular;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.service.ServiceException;
27
28 /**
29  * Updates a record in a database. The action can update one or more
30  * tables, and can update more than one row to a table at a time.
31  *
32  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
33  * @version CVS $Id: DatabaseUpdateAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class DatabaseUpdateAction extends DatabaseAction {
36
37     /**
38      * determine which mode to use as default mode
39      * here: UPDATE
40      * highly specific to operation INSERT / UPDATE / DELETE / SELECT
41      */

42     protected String JavaDoc selectMode ( boolean isAutoIncrement, Map JavaDoc modes ) {
43
44         return (String JavaDoc) modes.get( MODE_OTHERS );
45     }
46
47
48     /**
49      * determine whether autoincrement columns should be honoured by
50      * this operation. This is usually snsible only for INSERTs.
51      */

52     protected boolean honourAutoIncrement() { return false; }
53
54
55     /**
56      * Fetch all values for all columns that are needed to do the
57      * database operation.
58      */

59     protected Object JavaDoc[][] getColumnValues( Configuration tableConf, CacheHelper queryData, Map JavaDoc objectModel )
60         throws ConfigurationException, ServiceException {
61
62         Object JavaDoc[][] columnValues = new Object JavaDoc[ queryData.columns.length ][];
63         for ( int i = 0; i < queryData.columns.length; i++ ){
64             columnValues[i] = this.getColumnValue( tableConf, queryData.columns[i], objectModel );
65         }
66         return columnValues;
67     }
68
69
70     /**
71      * Get the String representation of the PreparedStatement. This is
72      * mapped to the Configuration object itself, so if it doesn't exist,
73      * it will be created.
74      *
75      * @param table the table's configuration object
76      * @return the insert query as a string
77      */

78     protected CacheHelper getQuery( Configuration table, Map JavaDoc modeTypes, Map JavaDoc defaultModeNames )
79         throws ConfigurationException, ServiceException {
80
81         LookUpKey lookUpKey = new LookUpKey( table, modeTypes );
82         CacheHelper queryData = null;
83         synchronized( this.cachedQueryData ) {
84             queryData = (CacheHelper) this.cachedQueryData.get( lookUpKey );
85             if (queryData == null) {
86                 Configuration[] keys = table.getChild("keys").getChildren("key");
87                 Configuration[] values = table.getChild("values").getChildren("value");
88
89                 queryData = new CacheHelper( keys.length, keys.length + values.length );
90                 fillModes( keys, true, defaultModeNames, modeTypes, queryData );
91                 fillModes( values, false, defaultModeNames, modeTypes, queryData );
92
93                 StringBuffer JavaDoc queryBuffer = new StringBuffer JavaDoc("UPDATE ");
94                 queryBuffer.append(table.getAttribute("name"));
95
96                 if (values.length > 0){
97                     queryBuffer.append(" SET ");
98                     int cols = 0;
99                     for (int i = 0; i < queryData.columns.length; i++) {
100                         if ( !queryData.columns[i].isKey ) {
101                             if ( cols > 0 ) {
102                                 queryBuffer.append(", ");
103                             }
104                             cols++;
105                             queryBuffer
106                                 .append( queryData.columns[i].columnConf.getAttribute( "name" ) )
107                                 .append( "= ?" );
108                         }
109                     }
110                 }
111                 
112                 queryBuffer.append(" WHERE ");
113                 for (int i = 0; i < queryData.columns.length; i++) {
114                     if ( queryData.columns[i].isKey ) {
115                         if ( i > 0 ) {
116                             queryBuffer.append(" AND ");
117                         }
118                         queryBuffer
119                             .append( queryData.columns[i].columnConf.getAttribute( "name" ) )
120                             .append( "= ?" );
121                     }
122                 }
123
124                 queryData.queryString = queryBuffer.toString();
125
126                 this.cachedQueryData.put( lookUpKey, queryData );
127             }
128         }
129
130         return queryData;
131     }
132
133
134     /**
135      * set all necessary ?s and execute the query
136      */

137     protected int processRow ( Map JavaDoc objectModel, Connection JavaDoc conn, PreparedStatement JavaDoc statement, String JavaDoc outputMode,
138                                Configuration table, CacheHelper queryData, Object JavaDoc[][] columnValues,
139                                int rowIndex, Map JavaDoc results )
140         throws SQLException JavaDoc, ConfigurationException, Exception JavaDoc {
141
142
143         int currentIndex = 1;
144
145         // ordering is different for UPDATE than for INSERT: values, keys
146
for (int i = 0; i < queryData.columns.length; i++) {
147             Column col = queryData.columns[i];
148             if ( !col.isKey ) {
149                 this.setColumn( objectModel, outputMode, results, table, col.columnConf, rowIndex,
150                                 columnValues[ i ][ ( col.isSet ? rowIndex : 0 ) ], statement, currentIndex );
151                 currentIndex++;
152             }
153         }
154         for (int i = 0; i < queryData.columns.length; i++) {
155             Column col = queryData.columns[i];
156             if ( col.isKey ) {
157                 this.setColumn( objectModel, outputMode, results, table, col.columnConf, rowIndex,
158                                 columnValues[ i ][ ( col.isSet ? rowIndex : 0 ) ], statement, currentIndex );
159                 currentIndex++;
160             }
161         }
162         int rowCount = statement.executeUpdate();
163         return rowCount;
164     }
165
166 }
167
Popular Tags