001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.model;
018:
019: import java.sql.SQLException;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import velosurf.sql.Database;
026: import velosurf.util.Logger;
027: import velosurf.util.StringLists;
028:
029: /** This class corresponds to custom update, delete and insert queries.
030: *
031: * @author <a href=mailto:claude.brisson@gmail.com>Claude Brisson</a>
032: *
033: */
034: public class Action {
035: /** Constructor.
036: *
037: * @param name name
038: * @param entity entity
039: */
040: public Action(String name, Entity entity) {
041: this .entity = entity;
042: db = this .entity.getDB();
043: this .name = name;
044: }
045:
046: /** Gets the parent entity
047: * @return parent entity
048: */
049: public Entity getEntity() {
050: return entity;
051: }
052:
053: /**
054: * Add a parameter name.
055: * @param paramName
056: */
057: public void addParamName(String paramName) {
058: paramNames.add(paramName);
059: }
060:
061: /**
062: * Sets the query.
063: * @param query query
064: */
065: public void setQuery(String query) {
066: this .query = query;
067: }
068:
069: /** Executes this action.
070: *
071: * @param source the object on which apply the action
072: * @exception SQLException an SQL problem occurs
073: * @return number of impacted rows
074: */
075: public int perform(Map<String, Object> source) throws SQLException {
076: List params = buildArrayList(source);
077: return db.prepare(query).update(params);
078: }
079:
080: /** Get the list of values for all parameters.
081: *
082: * @param source the ReadOnlyMap
083: * @exception SQLException thrown by the ReadOnlyMap
084: * @return the list of values
085: */
086: public List<Object> buildArrayList(Map<String, Object> source)
087: throws SQLException {
088: List<Object> result = new ArrayList<Object>();
089: if (source != null)
090: for (Iterator i = paramNames.iterator(); i.hasNext();) {
091: String paramName = (String) i.next();
092: Object value = source.get(paramName);
093: if (value == null) {
094: /* TODO: same problem than in Entity.extractColumnValues... we need a case-insensitive algorithm */
095: value = source.get(paramName.toUpperCase());
096: if (value == null) {
097: value = source.get(paramName.toLowerCase());
098: }
099: }
100: if (entity.isObfuscated(paramName))
101: value = db.deobfuscate(value);
102: if (value == null)
103: Logger.warn("Action " + getEntity().getName() + "."
104: + name + ": param " + paramName
105: + " is null!");
106: result.add(value);
107: }
108: return result;
109: }
110:
111: /** Get the name of the action.
112: *
113: * @return the name
114: */
115: public String getName() {
116: return name;
117: }
118:
119: /** For debugging purposes.
120: *
121: * @return definition string
122: */
123: public String toString() {
124: String result = "";
125: if (paramNames.size() > 0)
126: result += "(" + StringLists.join(paramNames, ",") + ")";
127: result += ":" + query;
128: return result;
129: }
130:
131: /** Get the database connection.
132: *
133: * @return the database connection
134: */
135: public Database getDB() {
136: return db;
137: }
138:
139: /** The database connection.
140: */
141: protected Database db = null;
142: /** The entity this action belongs to.
143: */
144: private Entity entity = null;
145: /** The name of this action.
146: */
147: private String name = null;
148:
149: /** Parameter names of this action.
150: */
151: protected List<String> paramNames = new ArrayList<String>();
152: /** Query.
153: */
154: private String query = null;
155:
156: }
|