You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2004/03/30 07:50:48 UTC
cvs commit: cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting DatabaseSelectAction.java OraAddAction.java DatabaseDeleteAction.java DatabaseUpdateAction.java DatabaseAuthenticatorAction.java DatabaseAddAction.java AbstractDatabaseAction.java
antonio 2004/03/29 21:50:48
Modified: src/blocks/databases/java/org/apache/cocoon/acting
DatabaseSelectAction.java OraAddAction.java
DatabaseDeleteAction.java DatabaseUpdateAction.java
DatabaseAuthenticatorAction.java
DatabaseAddAction.java AbstractDatabaseAction.java
Log:
Using o.a.commons.util....
Revision Changes Path
1.3 +22 -46 cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseSelectAction.java
Index: DatabaseSelectAction.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseSelectAction.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DatabaseSelectAction.java 5 Mar 2004 13:01:50 -0000 1.2
+++ DatabaseSelectAction.java 30 Mar 2004 05:50:48 -0000 1.3
@@ -26,6 +26,7 @@
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
+import org.apache.commons.lang.StringUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -80,60 +81,55 @@
for (int i = 0; i < keys.length; i++) {
final String parameter = keys[i].getAttribute("param");
Object value = request.getParameter(parameter);
- if (value == null || "".equals(value)) {
+ if (StringUtils.isEmpty((String)value)) {
if (statement == null) {
final String query = this.getSelectQuery(conf);
datasource = this.getDataSource(conf);
conn = datasource.getConnection();
-
statement = conn.prepareStatement(query);
currentIndex = 1;
for (int j = 0; j < keys.length; j++, currentIndex++) {
this.setColumn(statement, currentIndex, request, keys[j]);
}
-
rset = statement.executeQuery();
result = rset.next();
}
-
- if (result)
+ if (result) {
value = this.getColumn(rset, request, keys[i]);
+ }
}
-
- if (value != null)
+ if (value != null) {
request.setAttribute(parameter, value.toString());
+ }
}
for (int i = 0; i < values.length; i++) {
final String parameter = values[i].getAttribute("param");
Object value = request.getParameter(parameter);
- if (value == null || "".equals(value)) {
+ if (StringUtils.isEmpty((String)value)) {
if (statement == null) {
final String query = this.getSelectQuery(conf);
datasource = this.getDataSource(conf);
conn = datasource.getConnection();
-
statement = conn.prepareStatement(query);
currentIndex = 1;
for (int j = 0; j < keys.length; j++, currentIndex++) {
this.setColumn(statement, currentIndex, request, keys[j]);
}
-
rset = statement.executeQuery();
result = rset.next();
}
-
- if (result)
+ if (result) {
value = this.getColumn(rset, request, values[i]);
+ }
}
-
- if (value != null)
+ if (value != null) {
request.setAttribute(parameter, value.toString());
+ }
}
-
- if(statement != null)
+ if(statement != null) {
statement.close();
-
+ }
return EMPTY_MAP;
} catch (Exception e) {
throw new ProcessingException("Could not prepare statement :position = " + currentIndex, e);
@@ -145,8 +141,9 @@
getLogger().warn("There was an error closing the datasource", sqe);
}
}
-
- if (datasource != null) this.dbselector.release(datasource);
+ if (datasource != null) {
+ this.dbselector.release(datasource);
+ }
}
// Result is empty map or exception. No null.
@@ -161,7 +158,7 @@
String query = null;
synchronized (DatabaseSelectAction.selectStatements) {
- query = (String) DatabaseSelectAction.selectStatements.get(conf);
+ query = (String)DatabaseSelectAction.selectStatements.get(conf);
if (query == null) {
Configuration table = conf.getChild("table");
@@ -169,39 +166,18 @@
Configuration[] values = table.getChild("values").getChildren("value");
StringBuffer queryBuffer = new StringBuffer("SELECT ");
- int index = 0;
- for (int i = 0; i < keys.length; i++, index++) {
- if (index > 0) {
- queryBuffer.append(", ");
- }
- queryBuffer.append(keys[i].getAttribute("dbcol"));
- }
- for (int i = 0; i < values.length; i++,index++) {
- if (index > 0) {
- queryBuffer.append(", ");
- }
- queryBuffer.append(values[i].getAttribute("dbcol"));
- }
-
+ queryBuffer.append(buildList(keys, 0));
+ queryBuffer.append(buildList(values, keys.length));
queryBuffer.append(" FROM ");
queryBuffer.append(table.getAttribute("name"));
queryBuffer.append(" WHERE ");
- for (int i = 0; i < keys.length; i++) {
- if (i > 0) {
- queryBuffer.append(" AND ");
- }
-
- queryBuffer.append(keys[i].getAttribute("dbcol"));
- queryBuffer.append(" = ?");
- }
-
+ queryBuffer.append(buildList(keys, " AND "));
query = queryBuffer.toString();
DatabaseSelectAction.selectStatements.put(conf, query);
}
}
-
return query;
}
-}
+}
\ No newline at end of file
1.4 +3 -17 cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/OraAddAction.java
Index: OraAddAction.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/OraAddAction.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- OraAddAction.java 5 Mar 2004 13:01:50 -0000 1.3
+++ OraAddAction.java 30 Mar 2004 05:50:48 -0000 1.4
@@ -359,11 +359,9 @@
for (int i = 0; i < values.length; i++) {
if (this.isLargeObject(values[i].getAttribute("type"))) {
numLobs++;
-
if (numLobs > 1) {
queryBuffer.append(", ");
}
-
queryBuffer.append(values[i].getAttribute("dbcol"));
}
}
@@ -381,25 +379,13 @@
// Process the WHERE clause
if (keys.length > 0) {
queryBuffer.append(" WHERE ");
-
// Add the keys to the query
- for (int i = 0; i < keys.length; i++) {
- if (i > 0) {
- queryBuffer.append(" AND ");
- }
-
- queryBuffer.append(keys[i].getAttribute("dbcol"));
- queryBuffer.append(" = ?");
- }
+ queryBuffer.append(buildList(keys, " AND "));
}
-
query = queryBuffer.toString().trim();
OraAddAction.selectLOBStatements.put(conf, query);
}
}
-
- if ("".equals(query)) return null;
-
- return query;
+ return ("".equals(query)) ? null : query;
}
}
1.3 +6 -17 cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseDeleteAction.java
Index: DatabaseDeleteAction.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseDeleteAction.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DatabaseDeleteAction.java 5 Mar 2004 13:01:50 -0000 1.2
+++ DatabaseDeleteAction.java 30 Mar 2004 05:50:48 -0000 1.3
@@ -87,7 +87,7 @@
conn.commit();
statement.close();
- if(rows > 0){
+ if (rows > 0) {
request.setAttribute("rows", Integer.toString(rows));
return EMPTY_MAP;
}
@@ -95,7 +95,6 @@
if (conn != null) {
conn.rollback();
}
-
throw new ProcessingException("Could not delete record :position = " + currentIndex, e);
} finally {
if (conn != null) {
@@ -105,10 +104,10 @@
getLogger().warn("There was an error closing the datasource", sqe);
}
}
-
- if (datasource != null) this.dbselector.release(datasource);
+ if (datasource != null) {
+ this.dbselector.release(datasource);
+ }
}
-
return null;
}
@@ -130,22 +129,12 @@
StringBuffer queryBuffer = new StringBuffer("DELETE FROM ");
queryBuffer.append(table.getAttribute("name"));
queryBuffer.append(" WHERE ");
-
- for (int i = 0; i < keys.length; i++) {
- if (i > 0) {
- queryBuffer.append(" AND ");
- }
-
- queryBuffer.append((keys[i]).getAttribute("dbcol"));
- queryBuffer.append(" = ?");
- }
-
+ queryBuffer.append(buildList(keys, " AND "));
query = queryBuffer.toString();
DatabaseDeleteAction.deleteStatements.put(conf, query);
}
}
-
return query;
}
}
1.3 +3 -22 cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseUpdateAction.java
Index: DatabaseUpdateAction.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseUpdateAction.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DatabaseUpdateAction.java 5 Mar 2004 13:01:50 -0000 1.2
+++ DatabaseUpdateAction.java 30 Mar 2004 05:50:48 -0000 1.3
@@ -134,33 +134,14 @@
StringBuffer queryBuffer = new StringBuffer("UPDATE ");
queryBuffer.append(table.getAttribute("name"));
queryBuffer.append(" SET ");
-
- for (int i = 0; i < values.length; i++) {
- if (i > 0) {
- queryBuffer.append(", ");
- }
-
- queryBuffer.append(values[i].getAttribute("dbcol"));
- queryBuffer.append(" = ?");
- }
-
+ queryBuffer.append(buildList(values, ", "));
queryBuffer.append(" WHERE ");
-
- for (int i = 0; i < keys.length; i++) {
- if (i > 0) {
- queryBuffer.append(" AND ");
- }
-
- queryBuffer.append(keys[i].getAttribute("dbcol"));
- queryBuffer.append(" = ?");
- }
-
+ queryBuffer.append(buildList(keys, " AND "));
query = queryBuffer.toString();
DatabaseUpdateAction.updateStatements.put(conf, query);
}
}
-
return query;
}
}
1.7 +17 -35 cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseAuthenticatorAction.java
Index: DatabaseAuthenticatorAction.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseAuthenticatorAction.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DatabaseAuthenticatorAction.java 28 Mar 2004 20:51:23 -0000 1.6
+++ DatabaseAuthenticatorAction.java 30 Mar 2004 05:50:48 -0000 1.7
@@ -26,6 +26,7 @@
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.commons.lang.BooleanUtils;
+import org.apache.commons.lang.StringUtils;
import java.sql.Connection;
import java.sql.ResultSet;
@@ -100,10 +101,9 @@
boolean cs = true;
String create_session = parameters.getParameter ("create-session",
(String) this.settings.get("create-session"));
- if (create_session != null &&
- ("no".equals (create_session.trim ()) || "false".equals (create_session.trim ()))) {
- cs = false;
- }
+ if (create_session != null) {
+ cs = BooleanUtils.toBoolean(create_session.trim());
+ }
datasource = this.getDataSource(conf);
conn = datasource.getConnection();
@@ -186,63 +186,53 @@
for (int i = 0; i < columns.length; i++) {
String dbcol = columns[i].getAttribute("dbcol");
boolean nullable = false;
-
if (i > 0) {
queryBuffer.append (", ");
}
-
queryBuffer.append(dbcol);
String requestParameter = columns[i].getAttribute("request-param", null);
- if (requestParameter != null && requestParameter.trim() != "") {
-
+ if (StringUtils.isNotBlank(requestParameter)) {
String nullstr = columns[i].getAttribute("nullable", null);
if (nullstr != null) {
- nullstr = nullstr.trim();
- nullable = BooleanUtils.toBoolean(nullstr);
+ nullable = BooleanUtils.toBoolean(nullstr.trim());
}
-
String constraintValue = req.getParameter(requestParameter);
// if there is a request parameter name,
// but not the value, we exit immediately do
// that authorization fails authomatically
- if ((constraintValue == null || constraintValue.trim().equals("")) && !nullable) {
+ if (StringUtils.isBlank(constraintValue) && !nullable) {
getLogger().debug("DBAUTH: request-param " + requestParameter + " does not exist");
return null;
}
-
if (constraints > 0) {
queryBufferEnd.append(" AND ");
}
-
queryBufferEnd.append(dbcol).append("= ?");
- constraintValues[constraints] = constraintValue;
- constraints++;
+ constraintValues[constraints++] = constraintValue;
}
}
queryBuffer.append(" FROM ");
queryBuffer.append(table.getAttribute("name"));
- if (!queryBufferEnd.toString().trim().equals("")) {
- queryBuffer.append(" WHERE ").append(queryBufferEnd.toString());
+ if (StringUtils.isNotBlank(queryBufferEnd.toString())) {
+ queryBuffer.append(" WHERE ").append(queryBufferEnd);
}
getLogger().debug("DBAUTH: query " + queryBuffer);
PreparedStatement st = conn.prepareStatement(queryBuffer.toString());
- for(int i=0;i<constraints;i++) {
+ for (int i = 0; i < constraints; i++) {
getLogger().debug("DBAUTH: parameter " + (i+1) + " = [" + String.valueOf(constraintValues[i]) + "]");
st.setObject(i+1,constraintValues[i]);
}
-
return st;
}
catch (Exception e) {
getLogger().debug("DBAUTH: got exception: " + e);
}
-
return null;
}
@@ -256,20 +246,12 @@
for (int i = 0; i < select.length; i ++) {
try {
session_param = select[i].getAttribute ("to-session");
- if (session_param != null &&
- !session_param.trim().equals ("")) {
+ if (StringUtils.isNotBlank(session_param)) {
+ Object o = null;
String s = rs.getString (i + 1);
/* propagate to session */
- try {
- type = select[i].getAttribute ("type");
- } catch (Exception e) {
- type = null;
- }
- if (type == null || "".equals (type.trim ())) {
- type = "string";
- }
- Object o = null;
- if ("string".equals (type)) {
+ type = select[i].getAttribute("type", "");
+ if (StringUtils.isBlank(type) || "string".equals (type)) {
o = s;
} else if ("long".equals (type)) {
Long l = Long.decode (s);
@@ -286,6 +268,7 @@
map.put (session_param, o);
}
} catch (Exception e) {
+ // Empty
}
}
return map;
@@ -295,4 +278,3 @@
return null;
}
}
-
1.4 +19 -44 cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseAddAction.java
Index: DatabaseAddAction.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/DatabaseAddAction.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DatabaseAddAction.java 5 Mar 2004 13:01:50 -0000 1.3
+++ DatabaseAddAction.java 30 Mar 2004 05:50:48 -0000 1.4
@@ -38,6 +38,7 @@
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
+import org.apache.commons.lang.StringUtils;
/**
* Adds record in a database. The action can update one or more tables,
@@ -147,24 +148,20 @@
* to be inserting n rows, where 0 <= n
*/
String prefix = wildcardParam.substring(0,wildcardIndex);
- String suffix;
- if (wildcardParam.length() >= wildcardIndex+1) {
- suffix = wildcardParam.substring(wildcardIndex+1);
- } else {
- suffix = "";
- }
+ String suffix = StringUtils.substring(wildcardParam, wildcardIndex + 1);
Enumeration names = request.getParameterNames();
SortedSet matchset = new TreeSet();
+ int prefixLength = prefix.length();
+ int length = prefixLength + suffix.length();
while (names.hasMoreElements()) {
String name = (String)names.nextElement();
if (name.startsWith(prefix) && name.endsWith(suffix)) {
- String wildcard = name.substring(prefix.length());
- wildcard = wildcard.substring(0,wildcard.length()-suffix.length());
+ String wildcard = StringUtils.mid(name, prefixLength, name.length() - length);
matchset.add(wildcard);
}
}
- Iterator iterator = matchset.iterator();
int rowIndex = 1;
+ Iterator iterator = matchset.iterator();
while (iterator.hasNext()) {
String wildcard = (String)iterator.next();
currentIndex = 1;
@@ -239,34 +236,32 @@
String keyname = new StringBuffer("key:").append(table.getAttribute("name"))
.append(':').append(key.getAttribute("dbcol")).toString();
if ("manual".equals(mode)) {
- /** Set the key value using SELECT MAX(keyname)+1 **/
+ // Set the key value using SELECT MAX(keyname)+1
String selectQuery = this.getSelectQuery(key);
PreparedStatement select_statement = conn.prepareStatement(selectQuery);
ResultSet set = select_statement.executeQuery();
set.next();
int value = set.getInt("maxid") + 1;
statement.setInt(currentIndex, value);
- getLogger().debug("Manually setting key to "+value);
+ getLogger().debug("Manually setting key to " + value);
setRequestAttribute(request,keyname,new Integer(value));
results.put(key.getAttribute("dbcol"),String.valueOf(value));
set.close();
select_statement.close();
- return 1;
} else if ("form".equals(mode)) {
- /** Set the key value from the request **/
+ // Set the key value from the request
getLogger().debug("Setting key from form");
this.setColumn(statement, currentIndex, request, key, param);
- return 1;
} else if ("request-attribute".equals(mode)) {
Integer value = (Integer)getRequestAttribute(request,key.getAttribute("request-attribute-name"));
getLogger().debug("Setting key from request attribute "+value);
statement.setInt(currentIndex,value.intValue());
- return 1;
} else {
getLogger().debug("Automatically setting key");
- /** The database automatically creates a key value **/
+ // The database automatically creates a key value
return 0;
}
+ return 1;
}
/**
@@ -309,52 +304,32 @@
queryBuffer.append(table.getAttribute("name"));
queryBuffer.append(" (");
- int numKeys = 0;
+ int numParams = 0;
for (int i = 0; i < keys.length; i++) {
String mode = keys[i].getAttribute("mode", "automatic");
if ("manual".equals(mode) || "form".equals(mode) || "request-attribute".equals(mode)) {
- if (i > 0) {
+ if (numParams > 0) {
queryBuffer.append(", ");
}
-
queryBuffer.append(keys[i].getAttribute("dbcol"));
this.setSelectQuery(table.getAttribute("name"), keys[i]);
- numKeys++;
+ numParams++;
}
}
-
- int numValues = 0;
-
- for (int i = 0; i < values.length; i++) {
- if ((numKeys + numValues) > 0) {
- queryBuffer.append(", ");
- }
-
- queryBuffer.append(values[i].getAttribute("dbcol"));
- numValues++;
- }
-
+ queryBuffer.append(buildList(values, numParams));
+ numParams += values.length;
queryBuffer.append(") VALUES (");
-
- int numParams = numValues + numKeys;
-
- for (int i = 0; i < numParams; i++) {
- if (i > 0) {
- queryBuffer.append(", ");
- }
-
+ if (numParams > 0) {
queryBuffer.append("?");
+ queryBuffer.append(StringUtils.repeat(", ?", numParams - 1));
}
-
queryBuffer.append(")");
-
query = queryBuffer.toString();
DatabaseAddAction.addStatements.put(table, query);
}
}
-
return query;
}
1.5 +39 -1 cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/AbstractDatabaseAction.java
Index: AbstractDatabaseAction.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/AbstractDatabaseAction.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- AbstractDatabaseAction.java 5 Mar 2004 13:01:50 -0000 1.4
+++ AbstractDatabaseAction.java 30 Mar 2004 05:50:48 -0000 1.5
@@ -40,6 +40,7 @@
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
@@ -693,4 +694,41 @@
return request.getAttribute("org.apache.cocoon.acting.AbstractDatabaseAction:"+key);
}
+ /**
+ * Build a separed list with the Values of a Configuration Array
+ * @param values - build the list from
+ * @param separator - Put a separator between the values of the list
+ * @return - an StringBuffer with the builded List
+ * @throws ConfigurationException
+ */
+ protected StringBuffer buildList(Configuration[] values, String separator) throws ConfigurationException {
+ StringBuffer buffer = new StringBuffer();
+ for (int i = 0; i < values.length; i++) {
+ if (i > 0) {
+ buffer.append(separator);
+ }
+ buffer.append(values[i].getAttribute("dbcol"));
+ buffer.append(" = ?");
+ }
+ return buffer;
+ }
+
+ /**
+ * Build a separed list with the Values of a Configuration Array
+ * @param values - build the list from
+ * @param begin - Initial index
+ * @return - an StringBuffer with the builded List
+ * @throws ConfigurationException
+ */
+ protected StringBuffer buildList(Configuration[] values, int begin) throws ConfigurationException {
+ StringBuffer buffer = new StringBuffer();
+ int length = values.length;
+ for (int i = 0; i < length; i++) {
+ if (begin > 0) {
+ buffer.append(", ");
+ }
+ buffer.append(values[i].getAttribute("dbcol"));
+ }
+ return buffer;
+ }
}