Package HTTP

Source Code of HTTP.ParameterList

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.

o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.

o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



*/
package HTTP;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import Framework.DataValue;
import Framework.ErrorMgr;
import Framework.HashFuncs;
import Framework.HashTable;
import Framework.IntegerData;
import Framework.TextData;
import Framework.UsageException;

/**
*  ParameterList
*  The ParameterList class provides access to tag parameters in the HandleTag and HandleCondition methods.
*  The two Forte tags FORTE EXECUTE and FORTE IF both allow the use of optional parameters to further qualify a user's request.
*  This class allows you to retrieve by parameter name the values of parameters coded in these tags.
* <p>
* <p>
*/
public class ParameterList

{
  //private DynamicArray members; // Array of ResultSetMember
  //private DynamicArray ParamSet; // Array of Parameter
  //private DynamicArray resultSets; // Array of ResultSet
  /**
   * Table to store the data against.
   */
  private HashTable table;       
  private Logger _log = Logger.getLogger(ParameterList.class);

  public ParameterList() {
    //this.ParamSet = new DynamicArray(Parameter.class);
    //this.resultSets = new DynamicArray(ResultSet.class);
    //this.members = new DynamicArray(ResultSetMember.class);
    this.table = new HashTable();
    this.table.setup(30, new HashFuncs(true, Framework.Constants.SP_KT_STRING), true);
  }

  public ParameterList(String[] pNames, String[] pValues) {
    this();
    int len = Math.min(pNames.length, pValues.length);
    for (int i = 0; i < len; i++) {
      try {
        this.add(pNames[i], new TextData(pValues[i]));
      } catch (Exception e) {
        _log.error("Unhandled exception in ParameterList", e);
      }
    }
  }

  public ParameterList(ArrayList<String> pNames, ArrayList<TextData> pValues) {// DavidQ - Change the parameter to match what we are putting into the list.
    this();
    int len = Math.min(pNames.size(), pValues.size());
    for (int i = 0; i < len; i++) {
      try {
        this.add(pNames.get(i), pValues.get(i));// DavidQ - Change the parameter to match what we are putting into the list.
      } catch (Exception e) {
        _log.error("Unhandled exception in ParameterList", e);
      }
    }
  }

  /**
   * The GetParameter method returns the value of the specified parameter sent with a Forte tag.
   * The parameters can be IntegerData or TextData. The calling method must cast the result into the appropriate class type.
   * @param parameterName
   * @return
   */
  public Object getParameter(TextData parameterName) {
    return getParameter(parameterName.getValue());
  }
  /**
   * The GetParameter method returns the value of the specified parameter sent with a Forte tag.
   * The parameters can be IntegerData or TextData. The calling method must cast the result into the appropriate class type.
   * @param parameterName
   * @return
   */
  public Object getParameter(String parameterName) {
    DataValue item = (DataValue)this.table.find(parameterName);
    if (item == null) {
      UsageException errorVar = new UsageException("Error retrieving value for parameter named " + parameterName + ". No such parameter exists in the current ParameterList.");
      ErrorMgr.addError(errorVar);
      throw errorVar;
    }
    else {
      return item;
    }
  }

  /**
   * The GetParameter method returns the value of the specified parameter sent with a Forte tag.
   * The parameters can be IntegerData or TextData. The calling method must cast the result into the appropriate class type.
   * @param parameterName
   * @param index index used if there is more than one named parameter, the default is 1
   * @return
   */
  public Object getParameter(String parameterName, int kludge) {
    if (kludge!=1) {
      // uh-oh!
      UsageException errorVar = new UsageException("getParameter(string,int) with a non-1 int is used!");
      ErrorMgr.addError(errorVar);
      throw errorVar;
    } else {
      // straight pass through
      return this.getParameter(parameterName);
    }
  }

  private void add(String parameterName, TextData pValue) {
    if (this.table.find(parameterName) != null) {
      UsageException errorVar = new UsageException("Cannot add member name " + parameterName + " to Parameter List - a member already exists with that name.");
      ErrorMgr.addError(errorVar);
      throw errorVar;
    }
    try {
      if (pValue.getIntegerValue() != 0) {
        this.table.enter(new IntegerData(pValue.getIntegerValue()), parameterName);
        return;
      }
    }
    catch (Exception e) {}
    this.table.enter(pValue, parameterName);
  }

  /*
        public Object getParameter(TextData parameterName, int index) {
            int SINGLEMEM = 1;
            int paramCount = 0;
            {
                for (Iterator it = this.members.iterator(); it.hasNext();) {
                    Parameter aParam = (Parameter)it.next();

                    if (aParam.getParmName().compare(parameterName, true) == 0) {
                        paramCount++;
                        if (paramCount == index) {
                            switch (aParam.getParmRef().getRefType()) {
                                case HTTP.Constants.HS_VR_TEXT:
                                    return aParam.getParmRef().getTextVal();

                                case HTTP.Constants.HS_VR_NUMERIC:
                                    IntegerData aInt = new IntegerData(aParam.getParmRef().getTextVal().getIntegerValue());
                                    return aInt;

                                case HTTP.Constants.HS_VR_RSMEMBER:
                                    ResultSet rSet = null;
                                    rSet = this.getResultSet(aParam.getParmRef().getRsName());
                                    for (Iterator it1 = rSet.getWebEntMembers().iterator(); it1.hasNext();) {
                                        ResultSetMember aMem = (ResultSetMember)it1.next();

                                        if (aParam.getParmRef().getRsMember().compare(aMem.getName(), true) == 0) {

                                            if (aMem.getMemberType() == SINGLEMEM) {
                                                return aMem.getScalarValue();
                                            }
                                        }
                                    }
                                    break;

                                default:
                                    GenericException e = null;
                                    throw e;
                            }
                        }
                    }
                }
                GenericException e = null;
                Object[] qq_Args = { parameterName };
                e = new GenericException(
                        MessageFormat
                                .format(
                                        "Error retrieving value for parameter named {0}. No such parameter exists in the current ParameterList.",
                                        qq_Args));
                e.setDetectingMethod("ParameterList::GetParameter");
                throw e;
            }
        }

        private void add(TextData MemberName, DataValue Value) {

            if (this.findMember(MemberName) != null) {
                GenericException e = null;
                Object[] qq_Args = { MemberName };
                e = new GenericException(
                        MessageFormat
                                .format(
                                        "Cannot add member name {0} to Parameter List - a member already exists with that name.",
                                        qq_Args));
                e.setDetectingMethod("ResultSet::Add");
                throw e;
            }

            _log.debug( new TextData().replaceParameters(" +%1 [%2]", MemberName, Value));
            ResultSetMember newmember = new ResultSetMember(null,
                    ResultSetMember.HS_SINGLETON_MEMBER, MemberName.toString(),
                    Value);
            this.members.add(newmember);
        }

        private void add(TextData RSName, int row, TextData AttrName, DataValue value) {
            ResultSetMember listmem = this.findMember(RSName);

            if (listmem == null) {
                listmem = new ResultSetMember(null, ResultSetMember.HS_LIST_MEMBER, RSName.toString(), null);
                listmem.setListValue(new DynamicArray(ResultSet.class));

                this.members.add(listmem);

                if (_log.isDebugEnabled()) {
                    _log.debug( new TextData().replaceParameters("ParameterList +[%1]", RSName));
                }

            }

            if (listmem.getListValue().get(row - 1) == null) {

                if (_log.isDebugEnabled()) {
                    _log.debug( new TextData().replaceParameters("   +[%1][%2]", RSName, new IntegerData(row)));
                }
                listmem.getListValue().set(row - 1, new ResultSet());
            }

            if (_log.isDebugEnabled()) {
                _log.debug(new TextData().replaceParameters(" [%1]: ",  new IntegerData(row)));
            }
            ((ResultSet)listmem.getListValue().get(row - 1)).add(AttrName, value);
        }

        private void addResultSet(ResultSet rset) {
            if (this.findResultSet(rset.getName()) != null) {
                GenericException e = null;
                Object[] qq_Args = { rset.getName() };
                e = new GenericException(MessageFormat.format(
                        "A result set named {0} already exists.", qq_Args));
                e.setDetectingMethod("ExecContext::AddResultSet");
                throw e;
            }
            this.resultSets.add(rset);
        }

        private ResultSetMember findMember(TextData MemberName) {
            ResultSetMember result = null;
            for (Iterator it = this.members.iterator(); it.hasNext();) {
                ResultSetMember row = (ResultSetMember) it.next();

                if (MemberName.compare(row.getName(), true) == 0) {
                    result = row;
                    break;
                }
            }
            return result;
        }

        private ResultSet findResultSet(TextData ResultSetname) {
            ResultSet result = null;
            for (Iterator it = this.resultSets.iterator(); it.hasNext();) {
                ResultSet row = (ResultSet) it.next();

                if (ResultSetname.compare(row.getName(), true) == 0) {
                    result = row;
                    break;
                }
            }
            return result;
        }

        private ResultSet getResultSet(TextData ResultSetName) {
            ResultSet result = this.findResultSet(ResultSetName);

            if (result == null) {
                result = new ResultSet(new TextData(ResultSetName.getValue()));
                this.addResultSet(result);
            }
            return result;
        }
   */
  public static void main(String []args) {
    // Run a test of this class
    String []name = {"a", "b", "c", "d"};
    String []params = {"1", "2", "3", "4"};
    ParameterList aParamList = new ParameterList(name, params);
    System.out.println(aParamList.getParameter("a"));
    System.out.println(aParamList.getParameter("c"));
  }
} // end class ParameterList
TOP

Related Classes of HTTP.ParameterList

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.