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 43 public class DatabaseUpdateAction extends AbstractDatabaseAction implements ThreadSafe { 44 private static final Map updateStatements = new HashMap (); 45 46 51 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param) throws Exception { 52 DataSourceComponent datasource = null; 53 Connection conn = null; 54 int currentIndex = 0; 55 56 boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT; 58 if (this.settings.containsKey("reloadable")) 59 reloadable = Boolean.valueOf((String ) this.settings.get("reloadable")).booleanValue(); 60 try { 62 Configuration conf = 63 this.getConfiguration(param.getParameter("descriptor", (String ) this.settings.get("descriptor")), resolver, 64 param.getParameterAsBoolean("reloadable",reloadable)); 65 66 String 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 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 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 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 123 protected String getUpdateQuery(Configuration conf) throws ConfigurationException { 124 String query = null; 125 126 synchronized (DatabaseUpdateAction.updateStatements) { 127 query = (String ) 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 queryBuffer = new StringBuffer ("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 |