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

📄 leadeventprocessor.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 
 * 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.lead;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.model.ModelEntity;

import com.sourcetap.sfa.address.AddressHelper;
import com.sourcetap.sfa.attachment.AbstractAttachmentEP;
import com.sourcetap.sfa.contact.ContactEventProcessor;
import com.sourcetap.sfa.event.DataMatrix;
import com.sourcetap.sfa.event.GenericEventProcessor;
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.QueryInfo;
import com.sourcetap.sfa.util.UserInfo;


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

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    protected int postUpdate(UserInfo userInfo, GenericDelegator delegator,
        DataMatrix dataMatrix) {

        try {
            GenericValue originalLeadGV = null;
            GenericValue currentLeadGV = null;
            originalLeadGV = dataMatrix.getOriginalBuffer().getGenericValue(0,
                    "Lead", true);
            currentLeadGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Lead", true);

            String originalOwner = (originalLeadGV.getString("leadOwnerId") == null)
                ? "" : originalLeadGV.getString("leadOwnerId");
            String currentOwner = (currentLeadGV.getString("leadOwnerId") == null)
                ? "" : currentLeadGV.getString("leadOwnerId");
            String updatedBy = (currentLeadGV.getString("modifiedBy") == null)
                ? "" : currentLeadGV.getString("modifiedBy");

            if ((!originalOwner.equals(currentOwner)) &&
                    (!currentOwner.equals(updatedBy))) {
                sendNotification(userInfo, delegator, dataMatrix);
            }
        } catch (GenericEntityException e) {
            Debug.logWarning("postUpdate:Error getting generic value: " +
                e.getLocalizedMessage(), "postUpdate");

            return STATUS_ERROR;
        }

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    protected int postInsert(UserInfo userInfo, GenericDelegator delegator,
        DataMatrix dataMatrix) {

        GenericValue currentLeadGV = null;

        try {
            currentLeadGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Lead", true);

            String currentOwner = (currentLeadGV.getString("leadOwnerId") == null)
                ? "" : currentLeadGV.getString("leadOwnerId");
            String createdBy = (currentLeadGV.getString("modifiedBy") == null)
                ? "" : currentLeadGV.getString("modifiedBy");

            if (!currentOwner.equals(createdBy)) {
                sendNotification(userInfo, delegator, dataMatrix);
            }
        } catch (GenericEntityException e) {
            Debug.logWarning("Error getting generic value: " +
                e.getLocalizedMessage(), "postUpdate");

            return STATUS_ERROR;
        }

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    public int sendNotification(UserInfo userInfo, GenericDelegator delegator,
        DataMatrix dataMatrix) {
        // Find out if the owner changed.
        GenericValue currentLeadGV = null;
        String emailTo = "";
        String smsTo = "";

        try {
            currentLeadGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Lead", true);

            String currentOwner = (currentLeadGV.getString("leadOwnerId") == null)
                ? "" : currentLeadGV.getString("leadOwnerId");

            // The owner was changed. Get email address of new owner.
            HashMap findMap = new HashMap();
            findMap.put("contactId", currentOwner);

            try {
                GenericValue contactGV = delegator.findByPrimaryKey("Contact",
                        findMap);

                if (contactGV != null) {
                    emailTo = (contactGV.getString("email") == null) ? ""
                                                                     : contactGV.getString(
                            "email");
                    smsTo = (contactGV.getString("mobilePhone") == null) ? ""
                                                                         : contactGV.getString(
                            "mobilePhone");

                    if (!smsTo.equals("")) {
                        smsTo += "@mobile.att.net";
                    }
                }
            } catch (Exception e) {
                Debug.logWarning(
                    "Error looking for lead owner contact record: " +
                    e.getLocalizedMessage(), "postUpdate");

                return STATUS_ERROR;
            }

            if (!emailTo.equals("") || !smsTo.equals("")) {
                // Send new owner an email and/or sms message.
                String result = LeadHelper.sendLeadNotifyEmail(emailTo, smsTo,
                        currentLeadGV);

                if (!result.toLowerCase().equals("success")) {
                    Debug.logError("Error sending email: " + result,
                        module);

                    return STATUS_ERROR;
                }
            }
        } catch (GenericEntityException e) {
            Debug.logError("Error getting generic value: " +
                e.getLocalizedMessage(), module);

            return STATUS_ERROR;
        }

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    protected int preUpdate(UserInfo userInfo, GenericDelegator delegator,
        DataMatrix dataMatrix) {
 
        // Just process the first row for now.
        GenericValue leadGV = null;
        GenericValue originalLeadGV = null;
        GenericValue addressGV = null;
        GenericValue partyGV = null;

        try {
            leadGV = dataMatrix.getCurrentBuffer().getGenericValue(0, "Lead",
                    true);
            originalLeadGV = dataMatrix.getOriginalBuffer().getGenericValue(0,
                    "Lead", true);
            addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Address", false);
            partyGV = dataMatrix.getCurrentBuffer().getGenericValue(0, "Party",
                    false);
        } catch (GenericEntityException e) {
            Debug.logError("Error getting generic value: " + e.toString(), module);

            return STATUS_ERROR;
        }

        // Copy the lead ID to the address owner ID in case it is not already filled in.
        String leadId = leadGV.getString("leadId");

        Timestamp now = new Timestamp(Calendar.getInstance().getTime().getTime());

        // call the Name processor to split full name into first, last, etc or to combine first/last to full name
        ContactEventProcessor.setNameFields( leadGV );
        
        // Update the address if it is new.
        String addressId = addressGV.getString("addressId");

        if ((addressId == null) || addressId.equals("")) {
            addressId = GenericReplicator.getNextSeqId("Address", delegator);
            addressGV.set("addressId", addressId);
            addressGV.set("addressOwnerType", "Lead");
            addressGV.set("isPrimary", "Y");
            addressGV.set("createdDate", now);
            addressGV.set("createdBy", userInfo.getPartyId());

            String mailingAddress = AddressHelper.getMailingAddress( addressGV.getString("mailingAddress"), addressGV.getString("address1"), addressGV.getString("address2"), addressGV.getString("address3"));
            addressGV.set("mailingAddress", mailingAddress);
        }

        addressGV.set("addressOwnerId", leadId);

        // Set the time stamps.
        leadGV.set("modifiedDate", now);
        addressGV.set("modifiedDate", now);

        // Store the current user ID in the "modified by" field.
        leadGV.set("modifiedBy", userInfo.getPartyId());
        addressGV.set("modifiedBy", userInfo.getPartyId());

        // Set the lead owner ID if it is empty.
        String leadOwnerId = leadGV.getString("leadOwnerId");

        Debug.logVerbose("leadOwnerId: " + leadOwnerId, module);

        if ((leadOwnerId == null) || leadOwnerId.equals("")) {
            Debug.logVerbose("Setting leadOwnerId to " +
                    userInfo.getPartyId(), module);

            leadGV.set("leadOwnerId", userInfo.getPartyId());
        }

        // Set the account ID if it is empty.
        String accountId = leadGV.getString("accountId");

        Debug.logVerbose("accountId: " + accountId, module);

        if ((accountId == null) || accountId.equals("")) {
            Debug.logVerbose("Setting accountId to " +
                    userInfo.getAccountId(), module);

            leadGV.set("accountId", userInfo.getAccountId());
        }

        String ownerId = leadGV.getString("leadOwnerId");
        String originalOwnerId = originalLeadGV.getString("leadOwnerId");

        if (!ownerId.equals(originalOwnerId)) {
            //add the team access info
            SecurityWrapper.updateRoleInformation(dataMatrix, 0, userInfo,
                ownerId, "Lead", leadId, delegator);

            // Set the assigned date.
            leadGV.set("assignedDate", now);
        }

        String statusId = leadGV.getString("statusId");
        String originalStatusId = originalLeadGV.getString("statusId");

        if (!statusId.equals(originalStatusId) && statusId.equals("50")) {
            // Status changed to "converted". Set the converted date.
            leadGV.set("convertedDate", now);
        }

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo 
     * @param delegator 
     * @param dataMatrix 
     *
     * @return 
     */
    protected int preInsert(UserInfo userInfo, GenericDelegator delegator,
        DataMatrix dataMatrix) {

        // Just process the first row for now.
        GenericValue leadGV = null;
        GenericValue addressGV = null;
        GenericValue partyGV = null;

        try {
            leadGV = dataMatrix.getCurrentBuffer().getGenericValue(0, "Lead",
                    true);
            addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Address", true);
            partyGV = dataMatrix.getCurrentBuffer().getGenericValue(0, "Party",
                    true);
        } catch (GenericEntityException e) {
            Debug.logError("Error getting generic value: " +
                e.getLocalizedMessage(), module);

            return STATUS_ERROR;
        }

        // Generate new keys for the lead and address.
        String leadId = GenericReplicator.getNextSeqId("Party", delegator);
        String addressId = GenericReplicator.getNextSeqId("Address", delegator);
        leadGV.set("leadId", leadId);
        addressGV.set("addressId", addressId);
        addressGV.set("addressOwnerId", leadId);
        addressGV.set("addressOwnerType", "Lead");
        partyGV.set("partyId", leadId);

        
        // Make this address the primary one for the lead.
        addressGV.set("isPrimary", "Y");

        String mailingAddress = AddressHelper.getMailingAddress( addressGV.getString("mailingAddress"), addressGV.getString("address1"), addressGV.getString("address2"), addressGV.getString("address3"));
        addressGV.set("mailingAddress", mailingAddress);

        // call the Name processor to split full name into first, last, etc or to combine first/last to full name
        ContactEventProcessor.setNameFields( leadGV );
        
        // Set the time stamps.
        Timestamp now = new Timestamp(Calendar.getInstance().getTime().getTime());
        leadGV.set("createdDate", now);
        addressGV.set("createdDate", now);
        leadGV.set("modifiedDate", now);
        addressGV.set("modifiedDate", now);

        // Store the current user ID in the "modified by" field.
        leadGV.set("createdBy", userInfo.getPartyId());
        addressGV.set("createdBy", userInfo.getPartyId());
        leadGV.set("modifiedBy", userInfo.getPartyId());
        addressGV.set("modifiedBy", userInfo.getPartyId());

        LeadHelper leadHelper = LeadHelper.getInstance(delegator);

        //get the Vector of Generic Values for the new row
        Vector dataVector = dataMatrix.getCurrentBuffer().getContentsRow(0);

        String assignId = leadHelper.getLeadAssignment(dataVector, userInfo);

        if ((assignId != null) && (assignId.length() > 0)) {
            leadGV.set("leadOwnerId", assignId);
        }

        // Set the lead owner ID if it is empty.
        String leadOwnerId = leadGV.getString("leadOwnerId");

        Debug.logVerbose("leadOwnerId: " + leadOwnerId, module);

        if ((leadOwnerId == null) || leadOwnerId.equals("")) {

⌨️ 快捷键说明

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