Package com.sourcetap.sfa.lead

Source Code of com.sourcetap.sfa.lead.LeadEventProcessor

/*
*
* 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("")) {
            Debug.logVerbose("Setting leadOwnerId to " +
                    userInfo.getPartyId(), module);

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

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

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

        if ((accountId == null) || accountId.equals("")) {
            leadGV.set("accountId", userInfo.getAccountId());
        }

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

        //add the team access info
        SecurityWrapper.addRoleInformation(dataMatrix, 0, userInfo, ownerId,
            "Lead", leadId, delegator);

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

        if (statusId == null) {
            statusId = "10";
            leadGV.set("statusId", statusId);
        }

        if (leadGV.get("activeFlag") == null) {
            leadGV.set("activeFlag", new Boolean(true));
        }

        if (leadGV.get("validatedFlag") == null) {
            leadGV.set("validatedFlag", new Boolean(true));
        }

        if (addressGV.get("country") == null) {
            addressGV.set("country", "USA");
        }

        if (statusId.equals("50")) {
            // Status is "converted". Set the converted date.
            leadGV.set("convertedDate", now);
        }

        return STATUS_CONTINUE;
    }

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

        // Get the empty generic values.
        GenericValue leadGV = null;
        GenericValue addressGV = null;
        GenericValue partyGV = null;

        try {
            leadGV = dataMatrix.getCurrentBuffer().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;
        }

        // Set default values on the new record so they will be displayed for the user to see before saving.
        // Lead owner ID
        Debug.logVerbose("Setting leadOwnerId to " + userInfo.getPartyId(), module);

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

        leadGV.set("statusId", "10");

        // Active Flag
        leadGV.set("activeFlag", new Boolean(true));

        leadGV.set("validatedFlag", new Boolean(true));

        // Country
        addressGV.set("country", "USA");

        // Refresh the cache in the delegator so that dropdowns will show the new lead
        delegator.getAndCache().clear();

        return STATUS_CONTINUE;
    }

    /** event handler for quick insert screen */
    public boolean QuickInsert(UserInfo userInfo, GenericDelegator delegator,
        String firstName, String lastName, String title, String companyName,
        String businessPhone, String mailingAddress, String city, String state,
        String zip, String country, String statusId, String leadTypeId,
        String activeFlag, String validatedFlag) {
        GenericValue leadGV = new GenericValue(delegator.getModelEntity("Lead"));
        leadGV.setDelegator(delegator);

        GenericValue addressGV = new GenericValue(delegator.getModelEntity(
                    "Address"));
        addressGV.setDelegator(delegator);

        GenericValue partyGV = new GenericValue(delegator.getModelEntity(
                    "Party"));
        partyGV.setDelegator(delegator);

        leadGV.set("firstName", firstName);
        leadGV.set("lastName", lastName);
        leadGV.set("title", title);
        leadGV.set("companyName", companyName);
        leadGV.set("businessPhone", businessPhone);
        addressGV.set("mailingAddress", mailingAddress);
        addressGV.set("isPrimary", "Y");
        addressGV.set("city", city);
        addressGV.set("state", state);
        addressGV.set("zip", zip);
        addressGV.set("country", country);
        leadGV.set("statusId", statusId);
        leadGV.set("leadTypeId", leadTypeId);
        leadGV.set("activeFlag",
            activeFlag.toUpperCase().equals("Y") ? new Boolean(true)
                                                 : new Boolean(false));
        leadGV.set("validatedFlag",
            validatedFlag.toUpperCase().equals("Y") ? new Boolean(true)
                                                    : new Boolean(false));

        // call the Name processor to split full name into first, last, etc or to combine first/last to full name
        ContactEventProcessor.setNameFields( leadGV );
       
       
        DataMatrix dataMatrix = new DataMatrix(delegator, new Vector());
        dataMatrix.getCurrentBuffer().addContentsRow(new Vector());

        dataMatrix.addEntity("Lead", false, true);
        dataMatrix.getCurrentBuffer().getContentsRow(0).add(leadGV);

        dataMatrix.addEntity("Address", false, true);
        dataMatrix.getCurrentBuffer().getContentsRow(0).add(addressGV);

        dataMatrix.addEntity("Party", false, true);
        dataMatrix.getCurrentBuffer().getContentsRow(0).add(partyGV);

        if (preInsert(userInfo, delegator, dataMatrix) != STATUS_CONTINUE) {
            return false;
        }

        try {
            delegator.storeAll((List) dataMatrix.getCurrentBuffer()
                                                .getContentsRow(0));
        } catch (GenericEntityException e2) {
            Debug.logError("Error saving new lead: " +
                e2.getLocalizedMessage(), module);

            return false;
        }

        if (postInsert(userInfo, delegator, dataMatrix) != STATUS_CONTINUE) {
            return false;
        }

        return true;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo
     * @param delegator
     * @param originatingEntityName
     * @param entityGV
     *
     * @return
     */
    public int deleteAllRelated(UserInfo userInfo, GenericDelegator delegator,
        String originatingEntityName, GenericValue entityGV) {

        int status = STATUS_CONTINUE;

        // Delete related Addresses.
        status = deleteOneRelated(userInfo, delegator, entityGV, "", "Address",
                originatingEntityName, new GenericEventProcessor());

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

        // Delete related Parties.
        status = deleteOneRelated(userInfo, delegator, entityGV, "", "Party",
                originatingEntityName, new GenericEventProcessor());

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

        // Delete related FileAttachments.  (Note: This must happen before the LeadFiles are deleted.)
        status = deleteOneRelated(userInfo, delegator, entityGV, "",
                "FileAttachment", originatingEntityName,
                new AbstractAttachmentEP());

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

        // Delete related LeadFiles.  (Note: This must happen after the FileAttachments are deleted.)
        status = deleteOneRelated(userInfo, delegator, entityGV, "",
                "LeadFile", originatingEntityName, new GenericEventProcessor());

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

        return STATUS_CONTINUE;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo
     * @param delegator
     * @param entityGV
     * @param relationTitle
     * @param relatedEntityName
     *
     * @return
     */
    public List findOneRelated(UserInfo userInfo, GenericDelegator delegator,
        GenericValue entityGV, String relationTitle, String relatedEntityName) {
        // Find instances of an entity related to the current entity so the instances can be deleted.

        if (relatedEntityName.equals("FileAttachment")) {
            // Finding related FileAttachment records. Need special processing because the relationship cannot be
            // defined in the sfa-config.xml file.
            // Get all the related LeadFile records using the generic version of findOneRelated.
            List leadFileGVL = super.findOneRelated(userInfo, delegator,
                    entityGV, "", "LeadFile");

            // Make a List of FileAttachments tied to the identified LeadFiles.
            List fileAttachmenGVL = new LinkedList();
            Iterator leadFileGVI = leadFileGVL.iterator();

            while (leadFileGVI.hasNext()) {
                GenericValue leadFileGV = (GenericValue) leadFileGVI.next();

                try {
                    GenericValue fileAttachmentGV = delegator.getRelatedOne("FileAttachment",
                            leadFileGV);

                    if (fileAttachmentGV != null) {
                        fileAttachmenGVL.add(fileAttachmentGV);
                    }
                } catch (GenericEntityException e) {
                    Debug.logError("Error retrieving FileAttachment record: " +
                        e.getLocalizedMessage(), module);
                }
            }

            Debug.logVerbose("End - FileAttachment", module);

            return fileAttachmenGVL;
        } else {
            // Not finding EntityAccess or FileAttachment records.  Just use the standard processing using relations.
            return super.findOneRelated(userInfo, delegator, entityGV,
                relationTitle, relatedEntityName);
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param mainGV
     * @param relatedSearchClause
     * @param outGVV
     * @param userInfo
     * @param delegator
     *
     * @return
     */
    protected GenericValue retrieveOneRelatedGV(GenericValue mainGV,
        UIScreenSectionEntity relatedSearchClause, Vector outGVV, UserInfo userInfo,
        GenericDelegator delegator) {

        String relationRelEntityName = (String) relatedSearchClause.getEntityName();

        if (relationRelEntityName.equals("Address")) {
            // Special processing for the Address record since it can't be retrieved with a relationship
            // defined in the sfa-config.xml file.

            // Get lead ID from the Lead record.
            String leadId = mainGV.getString("leadId");

            HashMap addressFindMap = new HashMap();
            addressFindMap.put("addressOwnerId", leadId);
            addressFindMap.put("isPrimary", "Y");

            GenericValue addressGV = null;

            try {
                List addressGVL = delegator.findByAnd("Address", addressFindMap);
                Iterator addressGVI = addressGVL.iterator();

                if (addressGVI.hasNext()) {
                    // Address record was found.
                    addressGV = (GenericValue) addressGVI.next();

                    Debug.logVerbose("Address was found:" +
                            addressGV.toString(), module);
                } else {
                    // Address record was not found.  Allow generic event processor to create an empty one.
                    Debug.logVerbose("Address was not found.", module);
                }

                return addressGV;
            } catch (GenericEntityException e) {
                Debug.logError("An error occurred while searching for " +
                    "the address record for the Lead: " +
                    e.getLocalizedMessage(), module);

                return addressGV;
            }
        } else {
            // Retrieve all other related entities the regular way.

            return super.retrieveOneRelatedGV(mainGV, relatedSearchClause,
                outGVV, userInfo, delegator);
        }
    }

    /**
     * Add entity clauses for one related entity.  This will join related tables to the query
     * during a retrieve so query values can be entered that are in related entities.
     * <P>
     * This version overrides the ancestor to handle the Account entity, which
     * has no relation defined.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
     * @param relationTitle Relation title
     * @param relatedEntityName Name of related entity
     * @param primaryEntityName Name of the primary entity
     * @param primaryME ModelEntity object for the primary entity
     * @param queryInfo  criteria to be used in search
     */
    public void addOneRelationClause(GenericDelegator delegator,
        String relationTitle, String relatedAndFields, String relatedEntityName,
        String primaryEntityName, ModelEntity primaryME, boolean isOuterJoin, QueryInfo queryInfo)
        throws GenericEntityException {
        if (relatedEntityName.equals("Address")) {
            // Adding entity clauses for the Address entity.  Need to build it manually.
            // Join the address owner ID field
            Debug.logVerbose("[addOneRelationClause] Added to entityClauses: " +
                    "Lead" + ", " + "Address" + ", " + "leadId" + ", " +
                    "addressOwnerId", module);

      queryInfo.addJoin("Lead", "Address", Boolean.valueOf(isOuterJoin), "leadId", "addressOwnerId");
     
      if ( isOuterJoin )
      {
        queryInfo.addAlias("Address", "isPrimary", "isPrimary");
        queryInfo.addAlias("Address", "addressId", "addressId");
        queryInfo.addCondition( new EntityConditionList( UtilMisc.toList( new EntityExpr("isPrimary", EntityOperator.EQUALS, "Y"),
                      new EntityExpr("addressId", EntityOperator.EQUALS, null)), EntityOperator.OR));
      }
      else
        queryInfo.addCondition("Address", "isPrimary", EntityOperator.EQUALS, "Y");
        } else {
            // Use the parent script for all other related entities.
            super.addOneRelationClause(delegator, relationTitle, relatedAndFields,
                relatedEntityName, primaryEntityName, primaryME, isOuterJoin, queryInfo);
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param userInfo
     * @param delegator
     *
     * @return
     */
    public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo,
        GenericDelegator delegator) {
        return new SecurityLinkInfo("Lead", "leadId", false);
    }
}
TOP

Related Classes of com.sourcetap.sfa.lead.LeadEventProcessor

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.