001    package echopoint.externalevent;
002    /*
003     * This file is part of the Echo Point Project.  This project is a collection
004     * of Components that have extended the Echo Web Application Framework.
005     *
006     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     *
018     * Alternatively, the contents of this file may be used under the terms of
019     * either the GNU General Public License Version 2 or later (the "GPL"), or
020     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021     * in which case the provisions of the GPL or the LGPL are applicable instead
022     * of those above. If you wish to allow use of your version of this file only
023     * under the terms of either the GPL or the LGPL, and not to allow others to
024     * use your version of this file under the terms of the MPL, indicate your
025     * decision by deleting the provisions above and replace them with the notice
026     * and other provisions required by the GPL or the LGPL. If you do not delete
027     * the provisions above, a recipient may use your version of this file under
028     * the terms of any one of the MPL, the GPL or the LGPL.
029     */
030    
031    import java.util.EventObject;
032    import java.util.Iterator;
033    import java.util.Map;
034    
035    /**
036     * <code>ExternalEvent</code> represents an event that
037     * has been raised externally to the Echo
038     * web application.
039     * @author Brad Baker <p>Modified by Mikael Soderman 2009-04-28</p>
040     * @version $Id$
041     */
042    public class ExternalEvent extends EventObject {
043    
044            private Map eventParameters;
045            /**
046             * Creates an ExternalEvent.
047             *
048             * @param source the source of the event
049             */
050            public ExternalEvent(Object source, Map eventParameters) {
051                    super(source);
052                    this.eventParameters = eventParameters;
053            }
054    
055            /**
056             * Returns the Map of web request parameters that where
057             * encountered during this ExternalEvent.
058             *
059             * @return - the Map of parameters that where encountered during
060             * this external event.
061             */
062            public Map getParameterMap() {
063                    return eventParameters;
064            }
065    
066            /**
067             * Returns the value of an ExternalEvent request parameter as a String, or null
068             * if the parameter does not exist. You should only use this
069             * method when you are sure the parameter has only one value.
070             * <p>
071             * If the parameter might have more than one value, use
072             * getParameterValues(String).
073             * <p>
074             * If you use this method with a multivalued parameter, the value returned
075             * is equal to the first value in the array returned by getParameterValues().
076         *
077             * @param paramName - the name of the parameter to retrieve
078             * @return the first parameter value or null
079             */
080            public String getParameter(String paramName) {
081                    String[] values = getParameterValues(paramName);
082                    if (values == null || values.length == 0)
083                            return null;
084                    return values[0];
085            }
086    
087            /**
088             * Returns an array of String objects containing all of the values the given
089             * ExternalEvent parameter has, or null if the parameter does not exist.
090             * <p>
091             * If the parameter has a single value, the array has a length of 1.
092             *
093             * @param paramName - the name of the parameter to retrieve
094             * @return the all the parameter values or null
095             */
096            public String[] getParameterValues(String paramName) {
097                    return (String[]) eventParameters.get(paramName);
098            }
099    
100            /**
101             * Returns a String[] containing the names of the parameters contained in this
102             * ExternalEvent. If the external web request has no parameters,
103             * the method returns a zero length array.
104             * <p>
105             * @return a String[] containing the names of the parameters contained in this
106             * ExternalEvent.
107             */
108            public String[] getParameterNames() {
109                    int nameCount = 0;
110                    for (Iterator iter = eventParameters.keySet().iterator(); iter.hasNext();) {
111                            iter.next(); nameCount++;
112                    }
113                    String[] nameArray = new String[nameCount];
114                    nameCount=0;
115                    for (Iterator iter = eventParameters.keySet().iterator(); iter.hasNext();) {
116                            nameArray[nameCount] = (String)iter.next();;
117                            nameCount++;
118                    }
119                    return nameArray;
120            }
121    }
122