1 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 ; 31 import java.sql.PreparedStatement ; 32 import java.sql.SQLException ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 46 public final class DatabaseDeleteAction extends AbstractDatabaseAction implements ThreadSafe { 47 private static final Map deleteStatements = new HashMap (); 48 49 54 public final Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param) throws Exception { 55 DataSourceComponent datasource = null; 56 Connection conn = null; 57 int currentIndex = 0; 58 59 boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT; 61 if (this.settings.containsKey("reloadable")) 62 reloadable = Boolean.valueOf((String ) this.settings.get("reloadable")).booleanValue(); 63 try { 65 Configuration conf = 66 this.getConfiguration(param.getParameter("descriptor", (String ) this.settings.get("descriptor")), resolver, 67 param.getParameterAsBoolean("reloadable",reloadable)); 68 69 String 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 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 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 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 119 private final String getDeleteQuery(Configuration conf) throws ConfigurationException { 120 String query = null; 121 122 synchronized (DatabaseDeleteAction.deleteStatements) { 123 query = (String ) 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 queryBuffer = new StringBuffer ("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 |