Package com.sourcetap.sfa.event

Source Code of com.sourcetap.sfa.event.DataBuffer

/*
*
* Copyright (c) 2004 SourceTap - www.sourcetap.com
*
*  The contents of this file are subject to the SourceTap Public License
* ("License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
* Software distributed under the License is distributed on an  "AS IS"  basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*/

package com.sourcetap.sfa.event;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilTimer;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.model.ModelEntity;


/**
* DOCUMENT ME!
*
*/
public class DataBuffer {
  public static final String module = DataBuffer.class.getName();
    private static final boolean TIMER = false;
    protected ArrayList contents = new ArrayList();
    protected GenericDelegator delegator = null;
    protected DataMatrix parentDataMatrix = null;
    protected int rowCount = 0;

    public DataBuffer(GenericDelegator delegator, DataMatrix parentDataMatrix) {
        setDelegator(delegator);
        setParentDataMatrix(parentDataMatrix);
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public ArrayList getContents() {
        return contents;
    }

    /**
     * DOCUMENT ME!
     *
     * @param row
     *
     * @return
     */
    public Vector getContentsRow(int row) {
        // Returns one row from the buffer.  The row is a vector of generic values.  There will be one generic value
        // in the vector for each entity for one row of data.
        Vector contentsRow = null;

        try {
            contentsRow = (Vector) (contents.get(row));
        } catch (IndexOutOfBoundsException e) {
            Debug.logWarning("[DataBuffer.getContentsRow()]: Row " +
                String.valueOf(row) + " does not exist in the buffer.", module);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return contentsRow;
    }

    /**
     * DOCUMENT ME!
     *
     * @param genericValueVector
     */
    public void addContentsRow(Vector genericValueVector) {
        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(5, "[DataBuffer.addContentsRow] Start");
        }

        contents.add(genericValueVector);

        if (TIMER) {
            utilTimer.timerString(5, "[DataBuffer.addContentsRow] Finish");
        }

        setRowCount(getRowCount() + 1);
    }

  public void setContentsRow(int row, Vector genericValueVector)
  {
    int numRows = getRowCount();
   
    if ( row < numRows )
      contents.set(row,genericValueVector);
    else
      addContentsRow(genericValueVector);
  }
    /**
     * DOCUMENT ME!
     *
     * @param row
     * @param entityNumber
     *
     * @return
     */
    public GenericValue getGenericValue(int row, int entityNumber) {
        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(5,
                "[DataBuffer.getGenericValue(row, entityNumber)] Start");
        }

        GenericValue genericValue = (GenericValue) (getContentsRow(row).get(entityNumber));

        if (TIMER) {
            utilTimer.timerString(5,
                "[DataBuffer.getGenericValue(row, entityNumber)] Finished");
        }

        return genericValue;
    }

    /**
     * DOCUMENT ME!
     *
     * @param row
     * @param entityName
     * @param isMandatory
     *
     * @return
     *
     * @throws GenericEntityException
     */
    public GenericValue getGenericValue(int row, String entityName,
        boolean isMandatory) throws GenericEntityException {
        // THIS IS SLOW.  AVOID USING IT!  Try to use getGenericValue(int row, int entityNumber) instead.
        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(5,
                "[DataBuffer.getGenericValue(row, entityName)] Start");
        }

        Vector contentsRow = getContentsRow(row);

        if (contentsRow == null) {
            throw new GenericEntityException("Row " + String.valueOf(row) +
                " does not exist in the data buffer.");
        }

        Iterator gVI = getContentsRow(row).iterator();

        while (gVI.hasNext()) {
            GenericValue testGV = (GenericValue) gVI.next();

            if (testGV.getEntityName().equals(entityName)) {
                if (TIMER) {
                    utilTimer.timerString(5,
                        "[DataBuffer.getGenericValue(row, entityName)] Finished");
                }

                return testGV;
            }
        }

        if (isMandatory) {
            throw new GenericEntityException(
                "No generic value was found with name " + entityName);
        } else {
            return null;
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param row
     * @param entityName
     *
     * @return
     *
     * @throws GenericEntityException
     */
    public GenericValue getGenericValue(int row, String entityName)
        throws GenericEntityException {
        return getGenericValue(row, entityName, true);
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityName
     * @param contentsRow
     *
     * @return
     *
     * @throws GenericEntityException
     */
    public GenericValue getGenericValue(String entityName, Vector contentsRow)
        throws GenericEntityException {
        // THIS IS SLOW.  AVOID USING IT!  Try to use getGenericValue(int row, int entityNumber) instead.
        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(5,
                "[DataBuffer.getGenericValue(entityName, contentsRow)] Start");
        }

        // Find the correct generic value for the specified entity.
        GenericValue gV = null;
        Iterator contentsRowI = contentsRow.iterator();

        while (contentsRowI.hasNext()) {
            GenericValue testEntity = (GenericValue) contentsRowI.next();

            if (testEntity.getEntityName().equals(entityName)) {
                if (TIMER) {
                    utilTimer.timerString(5,
                        "[DataBuffer.getGenericValue(entityName, contentsRow)] Finished");
                }

                return testEntity;
            }
        }

        throw new GenericEntityException(
            "No generic value found with entity name " + entityName);
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public GenericDelegator getDelegator() {
        return delegator;
    }

    /**
     * DOCUMENT ME!
     *
     * @param delegator_
     */
    public void setDelegator(GenericDelegator delegator_) {
        delegator = delegator_;
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public DataMatrix getParentDataMatrix() {
        return parentDataMatrix;
    }

    /**
     * DOCUMENT ME!
     *
     * @param parentDataMatrix_
     */
    public void setParentDataMatrix(DataMatrix parentDataMatrix_) {
        parentDataMatrix = parentDataMatrix_;
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public int getRowCount() {
        return rowCount;
    }

    /**
     * DOCUMENT ME!
     *
     * @param rowCount_
     */
    public void setRowCount(int rowCount_) {
        rowCount = rowCount_;
    }

    //-------------------------------------------------------------------------
    // Put values from the screen into the data buffer
    //-------------------------------------------------------------------------

    /*
            // This works, but it is not used because it is slow to do the buffers separately.
            public int fillFromHtml(
                            HttpServletRequest request,
                            String htmlNamePrefix,
                            UIScreenSection uiScreenSection)
                    throws GenericEntityException {

                    UtilTimer utilTimer = new UtilTimer();
                    if (TIMER) utilTimer.timerString(5, "[DataBuffer.fillFromHtml] Start");

                    // Get the row count.
                    if (request.getParameter("rowCount")==null)
                            throw new GenericEntityException("rowCount parameter not found in request object.");
                    if (request.getParameter("rowCount").equals(""))
                            throw new GenericEntityException("rowCount parameter is empty in request object.");
                    try {
                            setRowCount(Integer.valueOf(request.getParameter("rowCount")).intValue());
                    } catch (NumberFormatException e) {
                            throw new GenericEntityException("rowCount parameter in request object does not contain a number.");
                    }

                    for (int row = 0; row < getRowCount(); row++) {
                            // Create an array list of empty generic values to hold the values from the screen for one row.
                            addContentsRow(getRowFromHTML(row, request, htmlNamePrefix, uiScreenSection));
                    }
                    if (TIMER) utilTimer.timerString(5, "[DataBuffer.fillFromHtml] Finished");
                    return getRowCount();
            }

            //-------------------------------------------------------------------------
            // Get an array list containing the values from the screen by entity.
            //-------------------------------------------------------------------------

            // This works, but it is not used because it is slow to do the buffers separately.
            public Vector getRowFromHTML(
                            int row,
                            HttpServletRequest request,
                            String htmlNamePrefix,
                            UIScreenSection uiScreenSection)
                    throws GenericEntityException {

                    UtilTimer utilTimer = new UtilTimer();
                    if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start");
                    Vector contentsRow = createEmptyRow();

                    // Loop through the entities and attributes for the current screen section, and get the value
                    // from the request object for each one, and store it in the contents.
                    for (int entityNbr = 0; entityNbr < getParentDataMatrix().getEntityParamVector().size(); entityNbr++) {
                            if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start processing entity " + String.valueOf(entityNbr));
                            String entityName = getParentDataMatrix().getEntityName(entityNbr);
                            GenericValue gV = getGenericValue(entityName, contentsRow);
                            for (int attributeNbr = 0; attributeNbr < getParentDataMatrix().getAttributeVector(entityNbr).size(); attributeNbr++) {
                                    String attributeName = getParentDataMatrix().getAttributeName(entityNbr, attributeNbr);
                                    if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start processing attribute " + attributeName);
                                    String paramName = UIWebUtility.getParamName(htmlNamePrefix, entityName, attributeName, row);

                                    if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start getting UIFieldInfo");
                                    UIFieldInfo uiFieldInfo = uiScreenSection.getUiField(entityName, attributeName);
                                    if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished getting UIFieldInfo");
                                    if (uiFieldInfo != null && uiFieldInfo.getDisplayOrder() > 0) {
                                            // This field was put into the form (as visible or hidden). Get the display object.
                                            if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start getting UIDisplayObject");
    //                                        UIDisplayObject uiDisplayObject = new UIDisplayObject(uiFieldInfo.getDisplayObjectId(), getDelegator());
                                            UIDisplayObject uiDisplayObject = uiFieldInfo.getUiDisplayObject();
                                            if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished getting UIDisplayObject");

                                            // Find out if this field is a check box. If so, need to use special handling.
                                            boolean isCheckbox = false;
                                            if (uiDisplayObject.getDisplayTypeId().equals(uiDisplayObject.DISPLAY_TYPE_CHECKBOX)) {
                                                    isCheckbox = true;
                                                    if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Start getting UIDisplayObject attributes");
                                                    uiDisplayObject.loadAttributes();
                                                    if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished getting UIDisplayObject attributes");
                                            }
                                            if (request.getParameter(paramName)==null) {
                                                    // Attribute does not appear in the parameters.
                                                    if (isCheckbox) {
                                                            // Check box.  Get the unchecked value, and store it in the buffer.
                                                            EventUtility.storeCorrectDataType(gV, attributeName, uiDisplayObject.getAttribUncheckedValue(), getDelegator());
                                                    } else {
                                                            // Skip this attribute.  It is not displayed on the screen section.
                                                    }
                                            } else {
                                                    if (isCheckbox) {
                                                            // Check box.  Get the checked value, and store it in the buffer.
                                                            EventUtility.storeCorrectDataType(gV, attributeName, uiDisplayObject.getAttribCheckedValue(), getDelegator());
                                                    } else {
                                                            // Non-checkbox field. Just store the value.
                                                            String attributeValue = request.getParameter(paramName);
                                                            if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Calling storeCorrectDataType");
                                                            EventUtility.storeCorrectDataType(gV, attributeName, attributeValue, getDelegator());
                                                            if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished storeCorrectDataType");
                                                    }
                                            }
                                    }
                            }
                    }
                    if (TIMER) utilTimer.timerString(6, "[DataBuffer.getRowFromHTML] Finished");
                    return contentsRow;
            }
    */
    public Vector createEmptyRow() throws GenericEntityException {
        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(5, "[DataBuffer.createEmptyRow] Start");
        }

        Vector entityNameVector = getParentDataMatrix().getEntityNameVector();
        Vector contentsRow = new Vector(entityNameVector.size());
        Iterator entityIterator = entityNameVector.iterator();

        while (entityIterator.hasNext()) {
            String entityName = (String) entityIterator.next();

            ModelEntity entityME = getDelegator().getModelEntity(entityName);
            GenericValue blankGV = new GenericValue(entityME);
            blankGV.setDelegator(getDelegator());
            contentsRow.add(blankGV);
        }

        if (TIMER) {
            utilTimer.timerString(5, "[DataBuffer.createEmptyRow] Finished");
        }

        return contentsRow;
    }

    /**
     * DOCUMENT ME!
     *
     * @throws GenericEntityException
     */
    public void addEmptyRow() throws GenericEntityException {
        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(5, "[DataBuffer.addEmptyRow] Start");
        }

        addContentsRow(createEmptyRow());

        if (TIMER) {
            utilTimer.timerString(5, "[DataBuffer.addEmptyRow] Finished");
        }

        return;
    }
}
TOP

Related Classes of com.sourcetap.sfa.event.DataBuffer

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.