Package HTTP

Source Code of HTTP.ResultSet

/*
Copyright (c) 2003-2009 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.text.MessageFormat;
import java.util.Iterator;

import Framework.CloneHelper;
import Framework.DataValue;
import Framework.DynamicArray;
import Framework.GenericException;
import Framework.IntegerData;
import Framework.TextData;

/**
*  The ResultSet class defines an object used for temporary storage of data. A ResultSet object is typically generated in a HandleTag; its data can then be displayed in a Web page that was defined by a converted HTML template.
<p>Result sets need not be "regular" in shape. A result set can be as simple as one member that is a single text or numeric value. Or a result set can have several members, including one or more members that are lists or arrays. All members are created using one of two variations of the add() method:To add a simple (scalar) member to a result set, use the variation of the add() method with the rsMember parameter. For example:</p>
<p><code>rset.add("announcement", new TextData("Hello World!"));</code></p>
<p>To add a complex (list type) member to a result set, use the variation of the Add method with the listName parameter.</p>
*/
public class ResultSet

{
    public ResultSet() {
        this.init();
    }

    public ResultSet(TextData pName) {
        this.name = pName;
    }

    // ---------------------------------------------------
    // Implementation of Clonable, DeepClonable interfaces
    // ---------------------------------------------------
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public Object clone(boolean pDeep) {
        if (pDeep)
            return CloneHelper.deepClone(this);
        else {
            try {
                return super.clone();
            }
            catch (CloneNotSupportedException e) {
                HTTPAccess.getWebEntLogger().error("Clone not supported for object of type " + this.getClass());
                return this;
            }
        }
    }
    // ----------
    // Attributes
    // ----------
    private TextData name;
    private DynamicArray<ResultSetMember> WebEntMembers;  // Array of WebEntResultSetMember

    // --------------------------------
    // Accessor methods for attributes.
    // --------------------------------
    public void setName(TextData pValue) {
        this.name = pValue;
    }

    public TextData getName() {
        return this.name;
    }

    public void setWebEntMembers(DynamicArray<ResultSetMember> pValue) {
        this.WebEntMembers = pValue;
    }

    public DynamicArray<ResultSetMember> getWebEntMembers() {
        return this.WebEntMembers;
    }
    public void add(TextData rsMember, String value) {
        add(rsMember, new TextData(value));
    }
    /**
     *  add
     * <p>
     * This variation of the Add method adds a new result set member that
     * is a single data item by specifying the member name and member value.
     * <p>
     * @param rsMember Type: TextData
     * @param value Type: DataValue
     */
    public void add(TextData rsMember, DataValue value)
    {

        if (rsMember == null) {

            GenericException e = new GenericException("Member Name is NIL" );
            e.setDetectingMethod("ResultSet::Add");
            throw e;
        }

        if (value == null) {
            Object[] qq_Args = { rsMember, this.name };
            GenericException e = new GenericException(MessageFormat.format( "Cannot add member name {0} to result set {1} - value specified is NIL.", qq_Args ) );
            e.setDetectingMethod("ResultSet::Add");
            throw e;
        }

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

        if (HTTPAccess.getWebEntLogger().isDebugEnabled()) {
          HTTPAccess.getWebEntLogger().debug(new TextData().replaceParameters(" +%2.%1 [%3]", rsMember, this.name, value) );
        }
        ResultSetMember newmember = new ResultSetMember(null, ResultSetMember.HS_SINGLETON_MEMBER, rsMember.toString(), value);
        this.WebEntMembers.add(newmember);
    }

    public void add(String rsMember, DataValue value) {
        this.add(new TextData(rsMember), value);
    }

    public void add(String rsMember, String value) {
        this.add(new TextData(rsMember), new TextData(value));
    }

    /**
     *  add
     * <p>
     * This variation of the Add method adds a new result set member
     * that is a list data item by specifying the list name and
     * values for each data item in the list.
     * <p>
     * @param listName Type: TextData
     * @param row Type: int
     * @param attributeName Type: TextData
     * @param value Type: DataValue
     */
    public void add(TextData listName, int row, TextData attributeName, DataValue value)
    {

        if (listName == null) {
            GenericException e = new GenericException("List Name is NIL" );
            e.setDetectingMethod("ResultSet::Add");
            throw e;
        }

        if (attributeName == null) {
            Object[] qq_Args = { listName };
            GenericException e = new GenericException(MessageFormat.format( "Cannot add to list {0} - attributeName specified is NIL.", qq_Args ) );
            e.setDetectingMethod("ResultSet::Add");
            throw e;
        }

        if (value == null) {
            Object[] qq_Args = { listName };
            GenericException e = new GenericException(MessageFormat.format( "Cannot add to list {0} - value specified is NIL.", qq_Args ) );
            e.setDetectingMethod("ResultSet::Add");
            throw e;
        }
        ResultSetMember listmem = this._findMember(listName);

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

            this.WebEntMembers.add(listmem);

          if (HTTPAccess.getWebEntLogger().isDebugEnabled()) {
            HTTPAccess.getWebEntLogger().debug(new TextData().replaceParameters("resultset %2 +[%1]", this.name, listName) );
          }
        }

        if ((((boolean)(row < 1)) || ((boolean)(row > (listmem.getListValue().size()+1))))) {
            Object[] qq_Args = { listName, new IntegerData(row) };
            GenericException e = new GenericException(MessageFormat.format( "Cannot add to list {0} - row ({1}) is less than one or skips rows.", qq_Args ) );
            e.setDetectingMethod("ResultSet::Add");
            throw e;
        }

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

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

        HTTPAccess.getWebEntLogger().debug( new TextData().replaceParameters(" [%1][%2]: ", this.name, new IntegerData(row)) );

        ((ResultSet)listmem.getListValue().get(row-1)).add(attributeName, value);
    }

    public boolean isMemberSet(TextData rsMember) {
        return this._findMember(rsMember) != null;
    }

    public boolean isMemberSet(String rsMember) {
        return this.isMemberSet(new TextData(rsMember));
    }

    /**
     *  findMember
     * <p>
     * <p>
     * @param rsMember Type: TextData
     * @return DataValue
     */
    public DataValue findMember(TextData rsMember)
        {

        ResultSetMember foundRSMem = this._findMember(rsMember);


        if (foundRSMem == null) {
            Object[] qq_Args = { rsMember };
            GenericException e = new GenericException(MessageFormat.format( "Cannot find member name {0}.", qq_Args ) );
            e.setDetectingMethod("ResultSet::FindMember");
            throw e;

        }

        if ((boolean)(foundRSMem.getMemberType() != ResultSetMember.HS_SINGLETON_MEMBER)) {
            Object[] qq_Args = { rsMember };
            GenericException e = new GenericException(MessageFormat.format( "The rsMember={0} attribute represents a list member\n\\n    Use the appropriate overloaded FindMember method to retrieve it.", qq_Args ) );
            e.setDetectingMethod("ResultSet::FindMember");
            throw e;

        }
        return foundRSMem.getScalarValue();
    }
    public DataValue findMember(String rsMember) {
        return this.findMember(new TextData(rsMember));
    }

    /**
     *  findMember
     * <p>
     * <p>
     * @param listName Type: TextData
     * @param row Type: int
     * @param attributeName Type: TextData
     * @return DataValue
     */
    public DataValue findMember(TextData listName, int row, TextData attributeName)
    {
        ResultSetMember foundListMem = this._findMember(listName);


        if (foundListMem == null) {

            Object[] qq_Args = { listName };
            GenericException e = new GenericException(MessageFormat.format( "Cannot find list name {0}.", qq_Args ) );
            e.setDetectingMethod("ResultSet::FindMember");
            throw e;

        }

        if ((boolean)(foundListMem.getMemberType() != ResultSetMember.HS_LIST_MEMBER)) {
            Object[] qq_Args = { listName };
            GenericException e = new GenericException(MessageFormat.format( "The listName={0} attribute does not represent a list member\n\\n    Use the appropriate overloaded FindMember method to retrieve it.", qq_Args ) );
            e.setDetectingMethod("ResultSet::FindMember");
            throw e;

        }

        if ((foundListMem.getListValue().get(row-1)) == null) {
            Object[] qq_Args = { listName, new IntegerData(row) };
            GenericException e = new GenericException(MessageFormat.format( "The list {0} does not contain the requested row={1}\n\\n    Use the ListItems method to determine correct number of rows for this list.", qq_Args ) );
            e.setDetectingMethod("ResultSet::FindMember");
            throw e;


        }
        ResultSetMember rowRSMember = ((ResultSet)foundListMem.getListValue().get(row-1))._findMember(attributeName);


        if (rowRSMember == null) {
            Object[] qq_Args = { attributeName };
            GenericException e = new GenericException(MessageFormat.format( "Cannot find member name {0}.", qq_Args ) );
            e.setDetectingMethod("ResultSet::FindMember");
            throw e;

        }
        return rowRSMember.getScalarValue();
    }
    public DataValue findMember(String listName, int row, String attributeName) {
        return this.findMember(new TextData( listName ), row, new TextData( attributeName ) );
    }

    /**
     *  init
     * <p>
     * <p>
     */
    private void init()
    {
        this.WebEntMembers = new DynamicArray<ResultSetMember>(ResultSetMember.class );
    }

    /**
     *  listItems
     * <p>
     * <p>
     * @param listName Type: TextData
     * @return int
     */
    public int listItems(TextData listName)
    {
        //  locate the 'list' first
        ResultSetMember foundListMem = this._findMember(listName);


        if (foundListMem == null) {
            // If we don't find the member, then it may never have been added. Forte will continue, so we do too.
            return 0;
//            Object[] qq_Args = { listName };
//            GenericException e = new GenericException(MessageFormat.format( "Cannot find list name {0}.", qq_Args ) );
//            e.setDetectingMethod("ResultSet::FindMember");
//            throw e;
        }

        if ((boolean)(foundListMem.getMemberType() != ResultSetMember.HS_LIST_MEMBER)) {
            Object[] qq_Args = { listName };
            GenericException e = new GenericException(MessageFormat.format( "The listName={0} attribute does not represent a list member", qq_Args ) );
            e.setDetectingMethod("ResultSet::FindMember");
            throw e;

        }
        return foundListMem.getListValue().size();
    }
    public int listItems(String listName) {
        return this.listItems(new TextData(listName));
    }

    /**
     *  findMember
     * <p>
     * <p>
     * @param MemberName Type: TextData
     * @return ResultSetMember
     */
    private ResultSetMember _findMember(TextData MemberName)
    {
        ResultSetMember result = null;
        DynamicArray<ResultSetMember> qq_localVector = this.WebEntMembers;
        for (Iterator<ResultSetMember> qq_it = qq_localVector.iterator(); qq_it.hasNext();) {
            ResultSetMember row = qq_it.next();

            if (row.getName().equalsIgnoreCase(MemberName.toString())) {
                result = row;
                break;
            }
        }
        return result;
    }

// end class ResultSet
TOP

Related Classes of HTTP.ResultSet

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.