Package com.sourcetap.sfa.event

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

/*
*
* 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.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

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.GenericPK;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityComparisonOperator;

import com.sourcetap.sfa.ui.UIDisplayObject;
import com.sourcetap.sfa.ui.UIFieldInfo;
import com.sourcetap.sfa.ui.UIScreenSection;
import com.sourcetap.sfa.ui.UIScreenSectionEntity;
import com.sourcetap.sfa.ui.UIWebUtility;
import com.sourcetap.sfa.util.DelimitedPairDecoder;
import com.sourcetap.sfa.util.DelimitedValueDecoder;
import com.sourcetap.sfa.util.QueryInfo;
import com.sourcetap.sfa.util.StringHelper;
import com.sourcetap.sfa.util.UserInfo;


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

    public DataMatrix(GenericDelegator delegator, Vector entityParamVector) {
        setDelegator(delegator);
        currentBuffer = new DataBuffer(delegator, this);
        originalBuffer = new DataBuffer(delegator, this);
        setEntityParamVector(entityParamVector);
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public DataBuffer getCurrentBuffer() {
        return currentBuffer;
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public DataBuffer getOriginalBuffer() {
        return originalBuffer;
    }

    /**
     * DOCUMENT ME!
     *
     * @param isDeleted
     */
    public void addDeleteFlag(boolean isDeleted) {
        deleteFlags.add(new Boolean(isDeleted));
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public Vector getEntityParamVector() {
        return entityParamVector;
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityParamVector_
     */
    public void setEntityParamVector(Vector entityParamVector_) {
        entityParamVector = entityParamVector_;
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityName
     * @param hasSequenceKey
     * @param isUpdateable
     */
    public void addEntity(String entityName, boolean hasSequenceKey,
        boolean isUpdateable) {
        //    Vector attributeVector = new Vector();
        //    for (int attributeNbr = 0; attributeNbr < uiEntity.getUiAttributeList().size(); attributeNbr++) {
        //      String attributeName = uiEntity.getUiAttribute(attributeNbr).getAttributeName();
        //      attributeVector.add(attributeName);
        //    }
        HashMap parameterMap = new HashMap();
        parameterMap.put("entityName", entityName);
        parameterMap.put("attributeVector", new Vector());
        parameterMap.put("hasSequenceKey", new Boolean(hasSequenceKey));
        parameterMap.put("isUpdateable", new Boolean(isUpdateable));

        Debug.logVerbose("[DataMatrix.addEntity] parameterMap: " +
                parameterMap.toString(), module);
        Debug.logVerbose("[DataMatrix.addEntity] entityParamVector: " +
                entityParamVector.toString(), module);

        getEntityParamVector().add(parameterMap);
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityNbr
     *
     * @return
     */
    public String getEntityName(int entityNbr) {
        HashMap parameterMap = (HashMap) getEntityParamVector().get(entityNbr);
        String entityName = (String) parameterMap.get("entityName");

        return entityName;
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityNbr
     *
     * @return
     */
    public boolean getHasSequenceKey(int entityNbr) {
        HashMap parameterMap = (HashMap) getEntityParamVector().get(entityNbr);
        Boolean hasSequenceKey = (Boolean) parameterMap.get("hasSequenceKey");

        return hasSequenceKey.booleanValue();
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityNbr
     *
     * @return
     */
    public boolean getIsUpdateable(int entityNbr) {

        HashMap parameterMap = (HashMap) getEntityParamVector().get(entityNbr);
        Boolean isUpdateable = (Boolean) parameterMap.get("isUpdateable");

        return isUpdateable.booleanValue();
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityNbr
     *
     * @return
     */
    public Vector getAttributeVector(int entityNbr) {
        HashMap parameterMap = (HashMap) getEntityParamVector().get(entityNbr);
        Vector attributeVector = (Vector) parameterMap.get("attributeVector");

        return attributeVector;
    }

    /**
     * DOCUMENT ME!
     *
     * @param entityNbr
     * @param attributeNbr
     *
     * @return
     */
    public String getAttributeName(int entityNbr, int attributeNbr) {
        HashMap parameterMap = (HashMap) getEntityParamVector().get(entityNbr);
        Vector attributeVector = (Vector) parameterMap.get("attributeVector");
        String attributeName = (String) attributeVector.get(attributeNbr);

        return attributeName;
    }

    /**
     * DOCUMENT ME!
     *
     * @return
     */
    public Vector getEntityNameVector() {
        Vector entityNameVector = new Vector();

        for (int entityNbr = 0; entityNbr < getEntityParamVector().size();
                entityNbr++) {
            entityNameVector.add(getEntityName(entityNbr));
        }

        return entityNameVector;
    }

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

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

    /**
     * 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 value matrix
    //-------------------------------------------------------------------------
    public int fillFromHtml(HttpServletRequest request,
        UIScreenSection uiScreenSection) throws GenericEntityException {

        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(3, "[DataMatrix.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.
            addRowFromHTML(row, request, uiScreenSection);
        }

        if (TIMER) {
            utilTimer.timerString(3, "[DataMatrix.fillFromHTML] End");
        }

        return getRowCount();
    }

    /**
     * DOCUMENT ME!
     *
     * @param row
     * @param request
     * @param uiScreenSection
     *
     * @throws GenericEntityException
     */
    public void addRowFromHTML(int row, HttpServletRequest request,
        UIScreenSection uiScreenSection) throws GenericEntityException {
        UtilTimer utilTimer = new UtilTimer();

        if (TIMER) {
            utilTimer.timerString(4, "[DataMatrix.addRowFromHTML] Start");
        }

        // Create an empty row to be updated and stored in the current and original buffers.
        Vector currentRow = getCurrentBuffer().createEmptyRow();

        //    Vector originalRow = (Vector)currentRow.clone(); -- This didn't really create a new object. It caused
        //      all operations on originalRow to be done on the currentRow also.
        Vector originalRow = getOriginalBuffer().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 < getEntityParamVector().size();
                entityNbr++) {
            String entityName = getEntityName(entityNbr);

            if (TIMER) {
                utilTimer.timerString(4,
                    "[DataMatrix.addRowFromHTML] Start processing entity " +
                    String.valueOf(entityNbr) + " (" + entityName + ")");
            }

            // Get references to the orginal and current generic values for this entity.
            GenericValue currentGV = (GenericValue) (currentRow.get(entityNbr));
            GenericValue originalGV = (GenericValue) (originalRow.get(entityNbr));

            // Process each attribute for this entity.
            for (int attributeNbr = 0;
                    attributeNbr < getAttributeVector(entityNbr).size();
                    attributeNbr++) {
                String attributeName = getAttributeName(entityNbr, attributeNbr);
                String rowDotEntityDotAttribName = String.valueOf(row) + "." +
                    entityName + "." + attributeName;

                if (TIMER) {
                    utilTimer.timerString(4,
                        "[DataMatrix.addRowFromHTML] Start processing attribute " +
                        rowDotEntityDotAttribName);
                }

                String originalParamName = UIWebUtility.getParamName(UIWebUtility.HTML_NAME_PREFIX_ORIGINAL,
                        uiScreenSection.getSectionName(), entityName,
                        attributeName, row);
                String currentParamName = UIWebUtility.getParamName(UIWebUtility.HTML_NAME_PREFIX_CURRENT,
                        uiScreenSection.getSectionName(), entityName,
                        attributeName, row);

                if (request.getParameter(originalParamName) != null) {
                    // This attribute exists in "original" hidden fields in the HTML data.  Continue to process it.
                    boolean currentFound = false;

                    if (request.getParameter(currentParamName) == null) {
                        // Attribute does not appear in the "current" parameters. It could be excluded, or it could
                        // be a check box, which would not appear if it is not checked.  Continue, but don't set flag.
                    } else {
                        // Attribute appears in the "current" parameters.
                        currentFound = true;
                    }

                    // We will need to determine whether this field is a check box.  Get the field info.
                    if (TIMER) {
                        utilTimer.timerString(4,
                            "[DataMatrix.addRowFromHTML] Start getting UIFieldInfo for " +
                            rowDotEntityDotAttribName);
                    }

                    UIFieldInfo uiFieldInfo = uiScreenSection.getUiField(entityName,
                            attributeName);

                    if (TIMER) {
                        utilTimer.timerString(4,
                            "[DataMatrix.addRowFromHTML] Finished getting UIFieldInfo for " +
                            rowDotEntityDotAttribName);
                    }

                    if ((uiFieldInfo != null) &&
                            (uiFieldInfo.getDisplayOrder() > 0)) {
                        // The field info was found. Get the display object.
                        if (TIMER) {
                            utilTimer.timerString(4,
                                "[DataMatrix.addRowFromHTML] Start getting UIDisplayObject for " +
                                rowDotEntityDotAttribName);
                        }

                        UIDisplayObject uiDisplayObject = uiFieldInfo.getUiDisplayObject();

                        if (TIMER) {
                            utilTimer.timerString(4,
                                "[DataMatrix.addRowFromHTML] Finished getting UIDisplayObject for " +
                                rowDotEntityDotAttribName);
                        }

                        // 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;
                        }

                        // Make sure the current value was found in the HTML, or that this is a checkbox field,
                        // which would not appear in the HTML if its box is not checked.
                        if (currentFound || isCheckbox) {
                            if (isCheckbox) {
                                // Need to load the display object attributes when there is a check box attribute.
                                if (TIMER) {
                                    utilTimer.timerString(4,
                                        "[DataMatrix.addRowFromHTML] Start getting UIDisplayObject attributes for " +
                                        rowDotEntityDotAttribName);
                                }

                                uiDisplayObject.loadAttributes();

                                if (TIMER) {
                                    utilTimer.timerString(4,
                                        "[DataMatrix.addRowFromHTML] Finished getting UIDisplayObject attributes for " +
                                        rowDotEntityDotAttribName);
                                }
                            }

                            // Get the original value and store it in the "original" buffer.
                            String originalAttributeValue = request.getParameter(originalParamName);

                            if (TIMER) {
                                utilTimer.timerString(4,
                                    "[DataMatrix.addRowFromHTML] Start getDataType for " +
                                    rowDotEntityDotAttribName);
                            }

                            String fieldType = EventUtility.getDataType(originalGV,
                                    attributeName, getDelegator());

                            if (TIMER) {
                                utilTimer.timerString(4,
                                    "[DataMatrix.addRowFromHTML] End getDataType for " +
                                    rowDotEntityDotAttribName);
                            }

                            if (TIMER) {
                                utilTimer.timerString(4,
                                    "[DataMatrix.addRowFromHTML] Start storeValue original for " +
                                    rowDotEntityDotAttribName);
                            }

                            EventUtility.storeValue(originalGV, attributeName,
                                originalAttributeValue, getDelegator(),
                                fieldType);

                            if (TIMER) {
                                utilTimer.timerString(4,
                                    "[DataMatrix.addRowFromHTML] End storeValue original for " +
                                    rowDotEntityDotAttribName);
                            }

                            if (!currentFound) {
                                // Attribute does not appear in the "current" HTML parameters.  It must be a checkbox, or this code
                                // would not be executing. Get the unchecked value, and store it in the buffer.
                                EventUtility.storeValue(currentGV,
                                    attributeName,
                                    uiDisplayObject.getAttribUncheckedValue(),
                                    getDelegator(), fieldType);

                            } else {
                                // Attribute does appear in the "current" HTML parameters.
                                if (isCheckbox) {
                                    // This is a checked checkbox.  Get the checked value, and store it in the current buffer.
                                    EventUtility.storeValue(currentGV,
                                        attributeName,
                                        uiDisplayObject.getAttribCheckedValue(),
                                        getDelegator(), fieldType);

                                } else {
                                    // Non-checkbox field. Just store the value.
                                    String currentAttributeValue = request.getParameter(currentParamName);

                                    if (TIMER) {
                                        utilTimer.timerString(4,
                                            "[DataMatrix.addRowFromHTML] Calling storeValue for " +
                                            rowDotEntityDotAttribName);
                                    }

                                    EventUtility.storeValue(currentGV,
                                        attributeName, currentAttributeValue,
                                        getDelegator(), fieldType);

                                    if (TIMER) {
                                        utilTimer.timerString(4,
                                            "[DataMatrix.addRowFromHTML] Finished storeValue for " +
                                            rowDotEntityDotAttribName);
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }

        // Store the new row in each buffer.
        getCurrentBuffer().addContentsRow(currentRow);
        getOriginalBuffer().addContentsRow(originalRow);

        // Check for delete flag for the new row.
        if (request.getParameter("deleteFlag" + String.valueOf(row)) != null) {
            // The user specified for this row to be deleted.  Set the delete flag to true for this row.
            addDeleteFlag(true);
        } else {
            // Either there was no delete check box, or it was not checked.  Set delete flag to false for this row.
            addDeleteFlag(false);
        }

        if (TIMER) {
            utilTimer.timerString(4, "[DataMatrix.addRowFromHTML] End");
        }

        return;
    }

  public void addRowFromArray( int row, Vector dataValues, Vector importFields, UIScreenSection uiScreenSection)
    throws GenericEntityException
  {
    UtilTimer utilTimer = new UtilTimer();

    if (TIMER) {
      utilTimer.timerString(4, "[DataMatrix.addRowFromArray] Start");
    }

    // Create an empty row to be updated and stored in the current and original buffers.
    Vector currentRow = getCurrentBuffer().getContentsRow(0);
    Vector originalRow = getOriginalBuffer().getContentsRow(0);
    if ( currentRow == null)
    {
      currentRow = getCurrentBuffer().createEmptyRow();
    }
    if ( originalRow == null )
    {
      originalRow = getOriginalBuffer().createEmptyRow();
    }
 
    for (int fieldNum = 0; fieldNum < dataValues.size(); fieldNum++) {
      String paramName = (String) importFields.get(fieldNum);

      if ((paramName == null) || (paramName.length() == 0)) {
        continue;
      }

      String paramEntityName = UIWebUtility.getEntityFromParamName(paramName);
      String paramAttributeName = UIWebUtility.getAttribFromParamName(paramName);

      String paramDataValue = (String) dataValues.get(fieldNum);

      // 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 < getEntityParamVector().size();
          entityNbr++) {
        String entityName = getEntityName(entityNbr);

        if (TIMER) {
          utilTimer.timerString(4,
            "[DataMatrix.addRowFromArray] Start processing entity " +
            String.valueOf(entityNbr) + " (" + entityName + ")");
        }

        if (entityName.equals(paramEntityName)) {
          // Get references to the orginal and current generic values for this entity.
          GenericValue currentGV = (GenericValue) (currentRow.get(entityNbr));
          String fieldType = EventUtility.getDataType(currentGV,
              paramAttributeName, getDelegator());
          EventUtility.storeValue(currentGV, paramAttributeName,
            paramDataValue, getDelegator(), fieldType);
        }
      }
    }

    // Store the new row in each buffer.
    getCurrentBuffer().setContentsRow(0,currentRow);
    getOriginalBuffer().setContentsRow(0,originalRow);

    if (TIMER) {
      utilTimer.timerString(4, "[DataMatrix.addRowFromArray] End");
    }

    return;
  }
    /**
     * DOCUMENT ME!
     *
     * @param row
     * @param request
     * @param dataValues
     * @param importFields
     * @param uiScreenSection
     *
     * @throws GenericEntityException
     */
    public void addRowFromArray(int row, String[] dataValues, Vector importFields,
        UIScreenSection uiScreenSection) throws GenericEntityException {
         
         Vector values = new Vector( Arrays.asList( dataValues ) );
         addRowFromArray(row, values, importFields, uiScreenSection);
        return;
    }

    /**
     * DOCUMENT ME!
     *
     * @param row
     *
     * @return
     */
    public boolean getRowChanged(int row) {
        // Compare all fields in the current generic value with the ones in the original generic value
        // to see if the user changed any values in the current row.
        Vector currentRow = getCurrentBuffer().getContentsRow(row);
        Vector originalRow = getOriginalBuffer().getContentsRow(row);
        boolean rowChanged = false;

        for (int entityNumber = 0; entityNumber < currentRow.size();
                entityNumber++) {
            if (getIsUpdateable(entityNumber)) {
                // This entity is updateable.  See if any values changed in its generic value.
                GenericValue currentGV = (GenericValue) currentRow.get(entityNumber);
                GenericValue originalGV = (GenericValue) originalRow.get(entityNumber);

                if (!currentGV.equals(originalGV)) {
                    rowChanged = true;

                    break;
                }
            }
        }

        return rowChanged;
    }
   
    public static DataMatrix fillFromDB(GenericDelegator delegator, UserInfo userInfo, UIScreenSection uiScreenSection,
        GenericEventProcessor eventProcessor, GenericPK primaryKey)
    {
      try {
      DataMatrix dataMatrix = new DataMatrix(delegator,
          uiScreenSection.getEntityParamVector());
         
      String primaryEntityName = uiScreenSection.getUiScreenSectionEntity(0).getUiEntity().getEntityName();
      // Specify how the primary entity will be retrieved.
      ArrayList orderBy = new ArrayList();
      QueryInfo queryInfo = new QueryInfo(delegator, primaryEntityName);
      Enumeration params = null;
      String searchAttribValue = "";
      String searchEntityName = "";
      String searchAttribName = "";
      String queryName = "";
      EntityComparisonOperator entityOperator = null;
      List relatedSearchClauses = new LinkedList();
      List primaryPkFieldNames = uiScreenSection.getUiScreenSectionEntity(0).getUiEntity().getPrimaryKeyFieldNames();
     
      Map fields = primaryKey.getAllFields();
     
      // Specify how associated entities will be retrieved.
      Iterator uiScreenSectionEntityI = uiScreenSection.getUiScreenSectionEntityList()
                                .iterator();
      uiScreenSectionEntityI.next(); // Pass up the primary entity.
 
      while (uiScreenSectionEntityI.hasNext()) {
        UIScreenSectionEntity uiScreenSectionEntity = (UIScreenSectionEntity) uiScreenSectionEntityI.next();
        relatedSearchClauses.add(uiScreenSectionEntity);
      }
 
      uiScreenSectionEntityI = null; // Reset the iterator.
 
      // Specify the sort order.
      orderBy = new DelimitedValueDecoder(uiScreenSection.getSortDef()).decode();

      eventProcessor.processRetrieve(userInfo, primaryEntityName,
          eventProcessor.RETRIEVE_METHOD_PK, fields, orderBy, queryInfo,
          relatedSearchClauses, delegator, dataMatrix);
         
      return dataMatrix;
      }
      catch (Exception e)
      {
        Debug.logError(e,module);
     
      return null;
    }
}
TOP

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

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.