DataCommand.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DataCommand.java,v 1.6 2005/10/13 18:17:25 rbair Exp $
  3.  *
  4.  * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.dataset;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. /**
  25.  * <p>Represents a command that can be executed against a data store by a {@link 
  26.  * DataProvider}, for example, commands for retrieving data, or for persisting
  27.  * data to the data store. For an example of how this is used, see {@link
  28.  * org.jdesktop.dataset.provider.sql.AbstractSqlCommand}. A concrete DataCommand
  29.  * is assigned to a DataProvider using {@link DataProvider#setCommand(DataCommand)}
  30.  * and retrieved using {@link DataProvider#getCommand()}.
  31.  *
  32.  * <p>A DataCommand holds a set of named parameters, each of which is assigned a value.
  33.  * These parameters are then used in executing the command.
  34.  *
  35.  * <p>A DataCommand allows a {@link DataProvider} to provide a harness around load and save
  36.  * operations, without needing the specifics of interaction with a data store. For
  37.  * SQL databases, a DataCommand may be a SELECT for reads and an INSERT, UPDATE
  38.  * or DELETE for writes. The DataProvider doesn't need to know how these SQL statements
  39.  * are actually built for the table in question--in fact, stored procedures could be
  40.  * used just by substituting the DataCommand used for reads or writes. Note that this 
  41.  * abstract DataCommand class does not define the semantics of the commands--that must 
  42.  * be done in its subclasses.
  43.  *
  44.  * <p><b>Internal</b>--This is a class used internally in this package and is not of 
  45.  * general purpose use.
  46.  *
  47.  * <p>TODO: Appears to be a bug with getParamValues() -- it should only return
  48.  * the values associated with the specified param names. Futher, set/get/clear
  49.  * methods should only work for the specified param names. Alternatively, there
  50.  * should be a default implementation of getParamNames, that returns the keys
  51.  * used in the param map.</p>
  52.  *
  53.  * <p> The struggle here is that the SQL based DataCommands have a specific
  54.  * set of params -- no more, no less. However, and HTTP based DataCommand can
  55.  * have any number of parameters. It could be a dynamic set.
  56.  *
  57.  * @author rbair
  58.  */
  59. public abstract class DataCommand {
  60.     /**
  61.      * A short description of the command.
  62.      */
  63.     private String shortDescription;
  64.     
  65.     /**
  66.      * A special marker indicating that a parameter has been undefined.
  67.      */
  68.     private static final Object UNDEFINED = new Object();
  69.     
  70.     /**
  71.      * Contains all of the params
  72.      */
  73.     private Map<String,Object> params = new HashMap<String,Object>();
  74.     
  75.     /**
  76.      * Set a short description for this Task. This description is used
  77.      * within a GUI builder to describe what the Task does, or wherever a short
  78.      * description might be useful, such as within some logging statements.
  79.      */
  80.     public void setShortDescription(String shortDescription) {
  81.         this.shortDescription = shortDescription == null ? "" : shortDescription;
  82.     }
  83.     
  84.     /**
  85.      * Returns the short description of this DataCommand
  86.      * @return the short description
  87.      */
  88.     public String getShortDescription() {
  89.         return shortDescription;
  90.     }
  91.     
  92.     /**
  93.      * Sets the given named parameter to the given value, overwriting any value
  94.      * already assigned. Passing in a value of
  95.      * "null" will *not* clear the parameter, but will set the parameter to the
  96.      * null value.
  97.      * @param name The parameter's name
  98.      * @param value The parameter's value
  99.      */
  100.     public void setParameter(String name, Object value) {
  101.         params.put(name, value);
  102.     }
  103.     
  104.     /**
  105.      * Clears the given named parameter of any associated value. The parameter is
  106.      * still mapped, but has an undefined value.
  107.      * @param name The named parameter to clear.
  108.      */
  109.     public void clearParameter(String name) {
  110.         params.put(name, UNDEFINED);
  111.     }
  112.     
  113.     /**
  114.      * Clears all of the parameters; see {@link #clearParameter(String)}
  115.      */
  116.     public void clearParameters() {
  117.         for (String name : params.keySet()) {
  118.             params.put(name, UNDEFINED);
  119.         }
  120.     }
  121.     
  122.     /**
  123.      * Returns the value for the given named parameter.
  124.      * @param name The name of the parameter to look up
  125.      * @return the named parameter's value; null if the parameter
  126.      * was never assigned a value.
  127.      */
  128.     public Object getParameter(String name) {
  129.         return params.get(name);
  130.     }
  131.     
  132.     /**
  133.      * Returns an array containing all of the parameter names for this DataCommand
  134.      * @return an array of the parameter names for this DataCommand
  135.      */
  136.     public abstract String[] getParameterNames();
  137.     
  138.     /**
  139.      * Returns an object array containing all of the parameter values for this
  140.      * DataCommand.
  141.      * @return an object array of the parameter values for this
  142.      * DataCommand.
  143.      */
  144.     public Object[] getParameterValues() {
  145.         return params.values().toArray();
  146.     }
  147. }