Package com.sourcetap.sfa.account

Source Code of com.sourcetap.sfa.account.AccountEventProcessor

/*
*
* 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.account;

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.activity.ActivityEventProcessor;
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.forecast.ForecastEventProcessor;
import com.sourcetap.sfa.opportunity.OpportunityEventProcessor;
import com.sourcetap.sfa.replication.GenericReplicator;
import com.sourcetap.sfa.security.EntityAccessEventProcessor;
import com.sourcetap.sfa.security.EntityAccessHelper;
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 AccountEventProcessor extends GenericEventProcessor {
  public static final String module = AccountEventProcessor.class.getName();

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

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

        // Just process the first row for now.
        GenericValue accountGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                0);
        GenericValue addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                1);
        GenericValue partyGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                2);

        String accountId = accountGV.getString("accountId");

        // Update the address if it is new.
        String addressId = addressGV.getString("addressId");
        String mailingAddress = AddressHelper.getMailingAddress( addressGV.getString("mailingAddress"), addressGV.getString("address1"), addressGV.getString("address2"), addressGV.getString("address3"));
        addressGV.set("mailingAddress", mailingAddress);

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

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

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

        // Set the account owner ID if it is empty.
        String accountOwnerId = accountGV.getString("accountOwnerId");

        if ((accountOwnerId == null) || accountOwnerId.equals("")) {
            accountGV.set("accountOwnerId", userInfo.getPartyId());
        }

        // Update team, role, and access information.
        SecurityWrapper.updateRoleInformation(dataMatrix, 0, userInfo,
            accountOwnerId, "Account", accountId, delegator);

        Debug.logVerbose("[preUpdate] Account GV: " + accountGV.toString(),module);
        Debug.logVerbose("[preUpdate] Address GV: " + addressGV.toString(),module);
        Debug.logVerbose("[preUpdate] Party GV: " + partyGV.toString(),module);

        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 accountGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                0);
        GenericValue addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                1);
        GenericValue partyGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                2);

        String accountId = GenericReplicator.getNextSeqId("Party", delegator);

        // Account key has been auto-generated. Just read it.
        accountGV.set("accountId", accountId);

        String addressId = GenericReplicator.getNextSeqId("Address", delegator);
        addressGV.set("addressId", addressId);
        addressGV.set("addressOwnerId", accountId);
        addressGV.set("addressOwnerType", "Account");
        addressGV.set("isPrimary", "Y");

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

        // Mark the party ID the same as the account ID.
        partyGV.set("partyId", accountId);

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

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

        // Set the account owner ID if it is empty.
        String accountOwnerId = accountGV.getString("accountOwnerId");

        if ((accountOwnerId == null) || accountOwnerId.equals("")) {
            accountOwnerId = userInfo.getPartyId();
            accountGV.set("accountOwnerId", accountOwnerId);
        }

        // Add team, role, and access information.
        SecurityWrapper.addRoleInformation(dataMatrix, 0, userInfo,
            accountOwnerId, "Account", accountId, delegator);

        Debug.logVerbose("[preInsert] Account GV: " + accountGV.toString(),module);
        Debug.logVerbose("[preInsert] Address GV: " + addressGV.toString(),module);
        Debug.logVerbose("[preInsert] Party GV: " + partyGV.toString(),module);

        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 accountGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                0);
        GenericValue addressGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                1);
        GenericValue partyGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
                2);

        // Set default values on the new record so they will be displayed for the user to see before saving.
        // Account owner ID
        accountGV.set("accountOwnerId", userInfo.getPartyId());
        accountGV.set("accountTypeId", "cust");

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

        // Status
        accountGV.set("statusId", "A");

        return STATUS_CONTINUE;
    }

    /**
     * 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 contacts.
        status = deleteOneRelated(userInfo, delegator, entityGV, "", "Contact",
                originatingEntityName, new ContactEventProcessor());

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

        // Delete related activities.
        status = deleteOneRelated(userInfo, delegator, entityGV, "",
                "Activity", originatingEntityName, new ActivityEventProcessor());

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

        // Delete related opportunities.
        status = deleteOneRelated(userInfo, delegator, entityGV, "", "Deal",
                originatingEntityName, new OpportunityEventProcessor());

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

        // Delete related forecasts.
        status = deleteOneRelated(userInfo, delegator, entityGV, "",
                "Forecast", originatingEntityName, new ForecastEventProcessor());

        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 entity accesses.
        status = deleteOneRelated(userInfo, delegator, entityGV, "",
                "EntityAccess", originatingEntityName,
                new EntityAccessEventProcessor());

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

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

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

        // Delete related AccountFiles.  (Note: This must happen after the FileAttachments are deleted.)
        status = deleteOneRelated(userInfo, delegator, entityGV, "",
                "AccountFile", 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("EntityAccess")) {
            // Finding related EntityAccess records. Need special processing because
            // the relationship cannot be defined in the sfa-config.xml file.
            return EntityAccessHelper.findRelated(userInfo, delegator,
                entityGV, relationTitle);
        } else 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 AccountFile records using the generic version of findOneRelated.
            List accountFileGVL = super.findOneRelated(userInfo, delegator,
                    entityGV, "", "AccountFile");

            // Make a List of FileAttachments tied to the identified AccountFiles.
            List fileAttachmenGVL = new LinkedList();
            Iterator accountFileGVI = accountFileGVL.iterator();

            while (accountFileGVI.hasNext()) {
                GenericValue accountFileGV = (GenericValue) accountFileGVI.next();

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

                    if (fileAttachmentGV != null) {
                        fileAttachmenGVL.add(fileAttachmentGV);
                    }
                } catch (GenericEntityException e) {
                    Debug.logWarning(
                        "[AccountEventProcessor.findOneRelated] Error retrieving FileAttachment record: " +
                        e.getLocalizedMessage(),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 account ID from the account record.
            String accountId = mainGV.getString("accountId");

            HashMap addressFindMap = new HashMap();
            addressFindMap.put("addressOwnerId", accountId);
            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();

                } else {
                    // Address record was not found.  Allow generic event processor to create an empty one.
                }

                return addressGV;
            } catch (GenericEntityException e) {
                Debug.logError(
                    "[AccountEventProcessor.retrieveOneRelatedGV] An error occurred while searching for " +
                    "the address record for the account: " +
                    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 critera 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
           
      queryInfo.addJoin("Account", "Address",  Boolean.valueOf(isOuterJoin), "accountId", "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("Account", "accountId", true);
    }
}
TOP

Related Classes of com.sourcetap.sfa.account.AccountEventProcessor

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.