⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericeventprocessor.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * 
 * 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.Collection;
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 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.GenericModelException;
import org.ofbiz.entity.GenericPK;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;
import org.ofbiz.entity.model.ModelKeyMap;
import org.ofbiz.entity.model.ModelRelation;
import org.ofbiz.entity.util.EntityListIterator;

import com.sourcetap.sfa.replication.GenericReplicator;
import com.sourcetap.sfa.security.SecurityLinkInfo;
import com.sourcetap.sfa.security.SecurityWrapper;
import com.sourcetap.sfa.ui.UIScreenSectionEntity;
import com.sourcetap.sfa.util.EntityHelper;
import com.sourcetap.sfa.util.QueryInfo;
import com.sourcetap.sfa.util.UserInfo;


/**
 * DOCUMENT ME!
 *
 */
public class GenericEventProcessor {
	public static final String module = GenericEventProcessor.class.getName();

    private static final boolean TIMER = false;
    public static final int STATUS_ERROR = -1;
    public static final int STATUS_CANCEL = 0;
    public static final int STATUS_CONTINUE = 1;
    public static final int RETRIEVE_METHOD_ALL = 1;
    public static final int RETRIEVE_METHOD_AND = 2;
    public static final int RETRIEVE_METHOD_CLAUSE = 3;
    public static final int RETRIEVE_METHOD_LIKE = 4;
    public static final int RETRIEVE_METHOD_PK = 5;
    private int rowOffset = 0;
    private int fetchSize = 1;
    private boolean hasMoreRows = false;
    private int totalRows = 0;

    /**
     * DOCUMENT ME!
     *
     * @param iFetchSize 
     */
    public void setFetchSize(int iFetchSize) {
        fetchSize = iFetchSize;
    }

    /**
     * DOCUMENT ME!
     *
     * @param iRowOffset 
     */
    public void setRowOffset(int iRowOffset) {
        rowOffset = iRowOffset;
    }

    /**
     * DOCUMENT ME!
     *
     * @return 
     */
    public int getFetchSize() {
        return fetchSize;
    }

    /**
     * DOCUMENT ME!
     *
     * @return 
     */
    public int getRowOffset() {
        return rowOffset;
    }

    /**
     * DOCUMENT ME!
     *
     * @return 
     */
    public boolean getHasMoreData() {
        return hasMoreRows;
    }

    /**
     * DOCUMENT ME!
     *
     * @return 
     */
    public int getTotalRows() {
        return totalRows;
    }

    /*
            protected String entityName = "";
            public String getEntityName() {
                    return entityName;
            }
            public void setEntityName(String entityName_) {
                    entityName = entityName_;
            }
    */

    //-------------------------------------------------------------------------
    // RETRIEVE - Retrieves an entity and associated entities from the data base.
    //-------------------------------------------------------------------------
    public int processRetrieve(UserInfo userInfo, String mainEntityName,
        int method, Map fields, List orderBy, QueryInfo queryInfo,
        List relatedSearchClauses, GenericDelegator delegator,
        DataMatrix dataMatrix) throws GenericEntityException {

        if ((mainEntityName == null) || mainEntityName.equals("")) {
            throw new GenericEntityException("Main entity name is required.");
        }

        if ((method != RETRIEVE_METHOD_ALL) && (method != RETRIEVE_METHOD_AND) &&
                (method != RETRIEVE_METHOD_CLAUSE) &&
                (method != RETRIEVE_METHOD_LIKE) &&
                (method != RETRIEVE_METHOD_PK)) {
            throw new GenericEntityException("Invalid search method.");
        }

        int status = STATUS_CONTINUE;

        // Trigger the pre-retrieve event.
        status = this.preRetrieve(userInfo, mainEntityName, method, fields,
                orderBy, queryInfo, relatedSearchClauses, delegator,
                dataMatrix);

        if (status != STATUS_CONTINUE) {
            return status;
        }

        // Trigger the do-retrieve event.
        status = this.retrieve(userInfo, mainEntityName, method, fields,
                orderBy, queryInfo, relatedSearchClauses, delegator,
                dataMatrix);

        if (status != STATUS_CONTINUE) {
            return status;
        }

        // Trigger the post-retrieve event.
        status = this.postRetrieve(userInfo, mainEntityName, method, fields,
                orderBy, queryInfo, relatedSearchClauses, delegator,
                dataMatrix);

        return status;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param mainEntityName 
     * @param method 
     * @param fields 
     * @param orderBy 
     * @param queryInfo  criteria to be used in search 
     * @param relatedSearchClauses 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    protected int preRetrieve(UserInfo userInfo, String mainEntityName,
        int method, Map fields, List orderBy, QueryInfo queryInfo,
        List relatedSearchClauses, GenericDelegator delegator,
        DataMatrix dataMatrix) {

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param mainEntityName 
     * @param method 
     * @param fields 
     * @param orderBy 
     * @param queryInfo  criteria to be used in search 
     * @param relatedSearchClauses 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    public int retrieve(UserInfo userInfo, String mainEntityName, int method,
        Map fields, List orderBy, QueryInfo queryInfo,
        List relatedSearchClauses, GenericDelegator delegator,
        DataMatrix dataMatrix) {

        UtilTimer timer = new UtilTimer();

        if (TIMER) {
            timer.timerString(2, "[GenericEventProcessor.retrieve] Start");
        }

        Debug.logVerbose(
                "Getting entity info to see if it has an accountId field = " +
                String.valueOf(method), module);

        SecurityLinkInfo securityInfo = getSecurityLinkInfo(userInfo, delegator);

        if (securityInfo == null) {
            Debug.logVerbose("No securityInfo defined", module);
        } else {
            Debug.logVerbose("securityInfo = " + securityInfo.toString(), module);
        }

        Debug.logVerbose("About to retrieve main entity. Method = " + String.valueOf(method), module);
        Debug.logVerbose("orderBy: " + orderBy.toString(), module);
        Debug.logVerbose("Retrieve Method: " + String.valueOf(method), module);

        // Retrieve the main entity or entities.
        List mainGVL = retrieveMainEntity(securityInfo, userInfo,
                mainEntityName, method, fields, orderBy, queryInfo,
                delegator);

        totalRows = mainGVL.size();

        Debug.logVerbose("GEP:  rowOffset = " + rowOffset + " fetchSize = " + fetchSize, module);

        Debug.logVerbose("Finished retrieving main entity.", module);

        Iterator mainGVI = mainGVL.iterator();
        int rowCount = 0;

        while (mainGVI.hasNext()) {
            rowCount++;

            GenericValue mainGV1 = (GenericValue) mainGVI.next();
            GenericValue mainGV = EntityHelper.getPrimaryGVFromDynamicGV(delegator, mainGV1, mainEntityName);

            if (TIMER) {
                timer.timerString(2,
                    "[GenericEventProcessor.retrieve] Start processing row " +
                    String.valueOf(rowCount));
            }

            // For each instance of the main entity, start a vector of entities, and then
            // retrieve the associated entities and add them also.
            Vector outGVV = new Vector();
            outGVV.add(mainGV);

            Debug.logVerbose("About to retrieve related entities.", module);

            // Loop through the relations to get related entities.
            int entityCount = 0;
            Iterator relatedSearchClauseI = relatedSearchClauses.iterator();

            while (relatedSearchClauseI.hasNext()) {
                UIScreenSectionEntity relatedSearchClause = (UIScreenSectionEntity) relatedSearchClauseI.next();
                entityCount++;

                String relationRelEntityName = relatedSearchClause.getEntityName();

                if (TIMER) {
                    timer.timerString(2,
                        "[GenericEventProcessor.retrieve] Start appending related entity " +
                        String.valueOf(entityCount) + " (" +
                        relationRelEntityName + ") for row " +
                        String.valueOf(rowCount));
                }
//TODO copy mainGV to primaryEntityGV if this is a dynamic view
                GenericValue relatedGV = retrieveOneRelatedGV(mainGV,
                        relatedSearchClause, outGVV, userInfo, delegator);

                if (relatedGV == null) {
                    // Related generic value was not found.  Create a new one.
                    relatedGV = new GenericValue(delegator.getModelEntity(
                                relationRelEntityName));
                    relatedGV.setDelegator(delegator);
                }

                // Add the new related generic value to the entity vector with the main entity.
                Debug.logVerbose("About to append related entity \"" +
                        relationRelEntityName + "\" onto generic value map.", module);

                outGVV.add(relatedGV);

                if (TIMER) {
                    timer.timerString(2,
                        "[GenericEventProcessor.retrieve] End appending related entity " +
                        String.valueOf(entityCount) + " for row " +
                        String.valueOf(rowCount));
                }
            }

            Debug.logVerbose("Generic value vector: " + outGVV.toString(), module);

            // Append the vector of generic entities onto the List.
            Debug.logVerbose("About to append generic value vector onto generic value vector List.", module);

            dataMatrix.getCurrentBuffer().addContentsRow(outGVV);

            if (TIMER) {
                timer.timerString(2,
                    "[GenericEventProcessor.retrieve] Added row to current buffer.");
            }

            Debug.logVerbose("Finished appending row " + String.valueOf(rowCount), module);
        }

        //		if (DEBUG) Debug.logVerbose("Data matrix current buffer: " + dataMatrix.getCurrentBuffer().getContents().toString());
        Debug.logVerbose("Finished retrieve method.", module);

        if (TIMER) {
            timer.timerString(2, "[GenericEventProcessor.retrieve] End");
        }

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param mainEntityName 
     * @param method 
     * @param fields 
     * @param orderBy 
     * @param queryInfo  criteria to be used in search 
     * @param relatedSearchClauses 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    protected int postRetrieve(UserInfo userInfo, String mainEntityName,
        int method, Map fields, List orderBy, QueryInfo queryInfo,
        List relatedSearchClauses, GenericDelegator delegator,
        DataMatrix dataMatrix) {

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param securityInfo 
     * @param userInfo 
     * @param mainEntityName 
     * @param method 
     * @param fields 
     * @param orderBy 
     * @param queryInfo  criteria to be used in search 
     * @param delegator 
     *
     * @return 
     */
    protected List retrieveMainEntity(SecurityLinkInfo securityInfo,
        UserInfo userInfo, String mainEntityName, int method, Map fields,
        List orderBy, QueryInfo queryInfo, GenericDelegator delegator) {
        UtilTimer timer = new UtilTimer();

        GenericValue mainGV = null;
        List mainGVL = null;
        ArrayList queryList = new ArrayList();
        EntityListIterator eli = null;

        switch (method) {
        case RETRIEVE_METHOD_ALL:

			// do nothing, leave condition = null
            break;

        case RETRIEVE_METHOD_AND:

                if (fields != null) {
                    Iterator entries = fields.entrySet().iterator();
                    while (entries.hasNext()) {
					Map.Entry anEntry = (Map.Entry) entries.next();
					queryInfo.addCondition( mainEntityName, (String) anEntry.getKey(), EntityOperator.EQUALS, anEntry.getValue());
                    }
                }
            break;

        case RETRIEVE_METHOD_CLAUSE:

			// nothing needed, as queryInfo is already setup
            break;

        case RETRIEVE_METHOD_LIKE:

			if (fields != null) {
				Iterator entries = fields.entrySet().iterator();
				while ( entries.hasNext() )	{
					Map.Entry anEntry = (Map.Entry) entries.next();
					queryInfo.addCondition( mainEntityName, (String) anEntry.getKey(), EntityOperator.EQUALS,  ((String) anEntry.getValue()).replace('*', '%') + "%");
            }
            }

            break;

        case RETRIEVE_METHOD_PK:

            // Get one main entity using a primary key built from the field map passed in.

            if (securityInfo == null) {
                try {
                    mainGV = delegator.findByPrimaryKey(mainEntityName, fields);
                } catch (GenericEntityException e) {
                    Debug.logError("An error occurred in the " +
                        "GenericDelegator.findByPrimaryKey method: " +
                        e.getLocalizedMessage(), module);
                }

                if (mainGV != null) {
                    // The entity was found. Store it in a List
                    mainGVL = new LinkedList();
                    mainGVL.add(mainGV);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -