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

📄 contacteventprocessor.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.contact;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
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.UtilFormatOut;
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.event.DataMatrix;
import com.sourcetap.sfa.event.GenericEventProcessor;
import com.sourcetap.sfa.replication.GenericReplicator;
import com.sourcetap.sfa.ui.UIScreenSectionEntity;
import com.sourcetap.sfa.util.QueryInfo;
import com.sourcetap.sfa.util.UserInfo;
import com.sourcetap.sfa.security.SecurityLinkInfo;

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

    /**
     * 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 contactGV = null;
        GenericValue addressGV = null;
        GenericValue partyGV = null;
        GenericValue userLoginGV = null;

        try {
            contactGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Contact", true);
            addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Address", false);
            partyGV = dataMatrix.getCurrentBuffer().getGenericValue(0, "Party",
                    false);
            userLoginGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "UserLogin", false);
        } catch (GenericEntityException e) {
            Debug.logError(
                "[ContactEventProcessor.preUpdate] Error getting generic value: " +
                e.toString(), module);

            return STATUS_ERROR;
        }

        // Copy the contact ID to the address owner ID in case it is not already filled in.
        String contactId = contactGV.getString("contactId");

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

	setNameFields( contactGV );
	// 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("addressOwnerId", contactId);
		addressGV.set("addressOwnerType", "Contact");
		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);
	
	}

        // Copy the contact ID to the user Login Party ID in case it is not already filled in.
        userLoginGV.set("partyId", contactId);

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

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

        // Set the contact owner ID if it is empty.
        String contactOwnerId = contactGV.getString("contactOwnerId");

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

            contactGV.set("contactOwnerId", userInfo.getPartyId());
        }

        return STATUS_CONTINUE;
    }

    /** event handler for quick insert screen */
    public boolean QuickInsert(UserInfo userInfo, GenericDelegator delegator,
        String firstName, String lastName, String userLogin, String userPassword) {
        GenericValue contactGV = new GenericValue(delegator.getModelEntity(
                    "Contact"));
        contactGV.setDelegator(delegator);
        GenericValue addressGV = new GenericValue(delegator.getModelEntity(
                    "Address"));
        addressGV.setDelegator(delegator);
        GenericValue partyGV = new GenericValue(delegator.getModelEntity(
                    "Party"));
        partyGV.setDelegator(delegator);
		GenericValue userLoginGV = new GenericValue(delegator.getModelEntity(
					"UserLogin"));
		userLoginGV.setDelegator(delegator);

		GenericValue partyRoleGV = new GenericValue(delegator.getModelEntity(
					"PartyRole"));
		partyRoleGV.setDelegator(delegator);

        contactGV.set("firstName", firstName);
        contactGV.set("lastName", lastName);
        contactGV.set("accountId", userInfo.getAccountId());
    	setNameFields( contactGV );
        contactGV.set("contactTypeId", "user");
        userLoginGV.set("userLoginId", userLogin);
        userLoginGV.set("currentPassword", userPassword);
        
        DataMatrix dataMatrix = new DataMatrix(delegator, new Vector());
        dataMatrix.getCurrentBuffer().addContentsRow(new Vector());

        dataMatrix.addEntity("Contact", false, true);
        dataMatrix.getCurrentBuffer().getContentsRow(0).add(contactGV);

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

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

        dataMatrix.addEntity("UserLogin", false, true);
        dataMatrix.getCurrentBuffer().getContentsRow(0).add(userLoginGV);

        if (preInsert(userInfo, delegator, dataMatrix) != STATUS_CONTINUE) {
            return false;
        }
		if ( (userLogin != null) && (userLogin.length() > 0))
		{
			try {
				partyGV =dataMatrix.getCurrentBuffer().getGenericValue(0, "Party", true);
			} catch (GenericEntityException e) {
				Debug.logError(
					"[ContactEventProcessor.QuickInsert] Error getting generic value: " +
					e.getLocalizedMessage(), module);
	
				return false;
			}
			partyRoleGV.set("partyId", partyGV.getString("partyId"));
			partyRoleGV.set("roleTypeId", "SFA_USER");
			dataMatrix.addEntity("PartyRole", false, true);
			dataMatrix.getCurrentBuffer().getContentsRow(0).add(partyRoleGV);
		}

        try {
            delegator.storeAll((List) dataMatrix.getCurrentBuffer()
                                                .getContentsRow(0));
        } catch (GenericEntityException e2) {
            Debug.logError(
                "[ContactEventProcessor.QuickInsert] Error updating: " +
                e2.getLocalizedMessage(), module);

            return false;
        }

        return true;
    }

    /**
     * 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 contactGV = null;
        GenericValue addressGV = null;
        GenericValue partyGV = null;
        GenericValue userLoginGV = null;

        try {
            contactGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Contact", true);
            addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Address", false);
            partyGV = dataMatrix.getCurrentBuffer().getGenericValue(0, "Party",
                    false);
            userLoginGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "UserLogin", false);
        } catch (GenericEntityException e) {
            Debug.logError(
                "[ContactEventProcessor.preInsert] Error getting generic value: " +
                e.getLocalizedMessage(), module);

            return STATUS_ERROR;
        }

        // Generate new keys for the contact and address.
        String contactId = GenericReplicator.getNextSeqId("Party", delegator);
        String addressId = GenericReplicator.getNextSeqId("Address", delegator);
        contactGV.set("contactId", contactId);
        addressGV.set("addressId", addressId);
        addressGV.set("addressOwnerId", contactId);
        addressGV.set("addressOwnerType", "Contact");
        partyGV.set("partyId", contactId);
        userLoginGV.set("partyId", contactId);

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

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


        Date hireDate = contactGV.getDate("hireDate");

        if (hireDate == null) {
            contactGV.set("hireDate",
                new Timestamp(Calendar.getInstance().getTime().getTime()));
        }

        // Set the time stamps.
        Timestamp now = new Timestamp(Calendar.getInstance().getTime().getTime());
        contactGV.set("createdDate", now);
        addressGV.set("createdDate", now);
        contactGV.set("modifiedDate", now);
        addressGV.set("modifiedDate", now);

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

        // Set the contact owner ID if it is empty.
        String contactOwnerId = contactGV.getString("contactOwnerId");


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

            contactGV.set("contactOwnerId", userInfo.getPartyId());
        }

        //Set the account ID if it is empty.  This should only be the case if it is an employee created thru quick create
        String accountId = contactGV.getString("accountId");

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

        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 contactGV = null;
        GenericValue addressGV = null;
        GenericValue partyGV = null;
        GenericValue userLoginGV = null;

        try {
            contactGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Contact", true);
            addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                    "Address", false);

⌨️ 快捷键说明

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