KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > modular > 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
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: DatabaseDeleteAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class DatabaseDeleteAction extends DatabaseAction {
36
37     /**
38      * determine which mode to use as default mode
39      * here: DELETE
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 key 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             if ( queryData.columns[i].isKey ) {
65                 columnValues[i] = this.getColumnValue( tableConf, queryData.columns[i], objectModel );
66             } else {
67                 // columnValues[i] = new Object[1]; // this should not be needed
68
}
69         }
70         return columnValues;
71     }
72
73
74
75     /**
76      * Get the String representation of the PreparedStatement. This is
77      * mapped to the Configuration object itself, so if it doesn't exist,
78      * it will be created.
79      *
80      * @param table the table's configuration object
81      * @return the insert query as a string
82      */

83     protected CacheHelper getQuery( Configuration table, Map JavaDoc modeTypes, Map JavaDoc defaultModeNames )
84         throws ConfigurationException, ServiceException {
85
86         LookUpKey lookUpKey = new LookUpKey( table, modeTypes );
87         CacheHelper queryData = null;
88         synchronized( this.cachedQueryData ) {
89             queryData = (CacheHelper) this.cachedQueryData.get( lookUpKey );
90             if (queryData == null) {
91                 Configuration[] keys = table.getChild("keys").getChildren("key");
92
93                 queryData = new CacheHelper( keys.length, keys.length );
94                 fillModes( keys, true, defaultModeNames, modeTypes, queryData );
95
96                 StringBuffer JavaDoc queryBuffer = new StringBuffer JavaDoc("DELETE FROM ");
97                 queryBuffer.append(table.getAttribute("name")).append(" WHERE ");
98                 for (int i = 0; i < queryData.columns.length; i++) {
99                     if ( i > 0 ) {
100                         queryBuffer.append(" AND ");
101                     }
102                     queryBuffer
103                         .append( queryData.columns[i].columnConf.getAttribute( "name" ) )
104                         .append( "= ?" );
105                 }
106
107                 queryData.queryString = queryBuffer.toString();
108
109                 this.cachedQueryData.put( lookUpKey, queryData );
110             }
111         }
112
113         return queryData;
114     }
115
116
117
118     /**
119      * set all necessary ?s and execute the query
120      */

121     protected int processRow ( Map JavaDoc objectModel, Connection JavaDoc conn, PreparedStatement JavaDoc statement, String JavaDoc outputMode,
122                                Configuration table, CacheHelper queryData, Object JavaDoc[][] columnValues,
123                                int rowIndex, Map JavaDoc results )
124         throws SQLException JavaDoc, ConfigurationException, Exception JavaDoc {
125
126         int currentIndex = 1;
127
128         // ordering is different for DELETE just needs keys
129
for (int i = 0; i < queryData.columns.length; i++) {
130             Column col = queryData.columns[i];
131             if ( col.isKey ) {
132                 this.setColumn( objectModel, outputMode, results, table, col.columnConf, rowIndex,
133                                 columnValues[ i ][ ( col.isSet ? rowIndex : 0 ) ],
134                                 statement, currentIndex );
135                 currentIndex++;
136             }
137         }
138         int rowCount = statement.executeUpdate();
139         return rowCount;
140     }
141
142 }
143
Popular Tags