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 import org.apache.commons.lang.StringUtils; 30 31 import java.sql.Connection ; 32 import java.sql.PreparedStatement ; 33 import java.sql.ResultSet ; 34 import java.sql.SQLException ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 38 47 public class DatabaseSelectAction extends AbstractDatabaseAction implements ThreadSafe { 48 49 private static final Map selectStatements = new HashMap (); 50 51 56 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param) throws Exception { 57 DataSourceComponent datasource = null; 58 Connection conn = null; 59 int currentIndex = 0; 60 61 boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT; 63 if (this.settings.containsKey("reloadable")) 64 reloadable = Boolean.valueOf((String ) this.settings.get("reloadable")).booleanValue(); 65 try { 67 Configuration conf = 68 this.getConfiguration(param.getParameter("descriptor", (String ) this.settings.get("descriptor")), 69 resolver, 70 param.getParameterAsBoolean("reloadable",reloadable)); 71 72 Request request = ObjectModelHelper.getRequest(objectModel); 73 74 Configuration[] keys = conf.getChild("table").getChild("keys").getChildren("key"); 75 Configuration[] values = conf.getChild("table").getChild("values").getChildren("value"); 76 77 PreparedStatement statement = null; 78 ResultSet rset = null; 79 boolean result = false; 80 81 for (int i = 0; i < keys.length; i++) { 82 final String parameter = keys[i].getAttribute("param"); 83 Object value = request.getParameter(parameter); 84 if (StringUtils.isEmpty((String )value)) { 85 if (statement == null) { 86 final String query = this.getSelectQuery(conf); 87 datasource = this.getDataSource(conf); 88 conn = datasource.getConnection(); 89 statement = conn.prepareStatement(query); 90 currentIndex = 1; 91 for (int j = 0; j < keys.length; j++, currentIndex++) { 92 this.setColumn(statement, currentIndex, request, keys[j]); 93 } 94 rset = statement.executeQuery(); 95 result = rset.next(); 96 } 97 if (result) { 98 value = this.getColumn(rset, request, keys[i]); 99 } 100 } 101 if (value != null) { 102 request.setAttribute(parameter, value.toString()); 103 } 104 } 105 106 for (int i = 0; i < values.length; i++) { 107 final String parameter = values[i].getAttribute("param"); 108 Object value = request.getParameter(parameter); 109 if (StringUtils.isEmpty((String )value)) { 110 if (statement == null) { 111 final String query = this.getSelectQuery(conf); 112 datasource = this.getDataSource(conf); 113 conn = datasource.getConnection(); 114 statement = conn.prepareStatement(query); 115 currentIndex = 1; 116 for (int j = 0; j < keys.length; j++, currentIndex++) { 117 this.setColumn(statement, currentIndex, request, keys[j]); 118 } 119 rset = statement.executeQuery(); 120 result = rset.next(); 121 } 122 if (result) { 123 value = this.getColumn(rset, request, values[i]); 124 } 125 } 126 if (value != null) { 127 request.setAttribute(parameter, value.toString()); 128 } 129 } 130 if(statement != null) { 131 statement.close(); 132 } 133 return EMPTY_MAP; 134 } catch (Exception e) { 135 throw new ProcessingException("Could not prepare statement :position = " + currentIndex, e); 136 } finally { 137 if (conn != null) { 138 try { 139 conn.close(); 140 } catch (SQLException sqe) { 141 getLogger().warn("There was an error closing the datasource", sqe); 142 } 143 } 144 if (datasource != null) { 145 this.dbselector.release(datasource); 146 } 147 } 148 149 } 151 152 157 protected String getSelectQuery(Configuration conf) throws ConfigurationException { 158 String query = null; 159 160 synchronized (DatabaseSelectAction.selectStatements) { 161 query = (String )DatabaseSelectAction.selectStatements.get(conf); 162 163 if (query == null) { 164 Configuration table = conf.getChild("table"); 165 Configuration[] keys = table.getChild("keys").getChildren("key"); 166 Configuration[] values = table.getChild("values").getChildren("value"); 167 168 StringBuffer queryBuffer = new StringBuffer ("SELECT "); 169 queryBuffer.append(buildList(keys, 0)); 170 queryBuffer.append(buildList(values, keys.length)); 171 queryBuffer.append(" FROM "); 172 queryBuffer.append(table.getAttribute("name")); 173 174 queryBuffer.append(" WHERE "); 175 queryBuffer.append(buildList(keys, " AND ")); 176 query = queryBuffer.toString(); 177 178 DatabaseSelectAction.selectStatements.put(conf, query); 179 } 180 } 181 return query; 182 } 183 } | Popular Tags |