Package com.sourcetap.sfa.lead

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

/*
*
* 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.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
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.EntityCondition;
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.model.DynamicViewEntity;
import org.ofbiz.entity.model.ModelKeyMap;

import com.sourcetap.sfa.ui.UIDisplayObject;
import com.sourcetap.sfa.ui.UIDropDown;
import com.sourcetap.sfa.ui.UIFieldInfo;
import com.sourcetap.sfa.user.UserHelper;
import com.sourcetap.sfa.util.EntityHelper;
import com.sourcetap.sfa.util.UserInfo;


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

    public UserLeadQueueDropDown() {
    }

    /**
     * Return an array of data value/display value pairs to be passed to the getDisplayHtml
     * method.  This overrides the ancestor because 2 types of entities are combined into one
     * drop list.  This prevents the getSelectValues method from being called, which returns
     * a list of one type of entity only.
     *
     * @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 uiDisplayObject Reference to a display object defined in the data base and attached to the field to be displayed by the UI builder
     * @param orderDef List of fields defining the sort order of the drop down values
     * @param entityDetailsVector Vector of generic values containing the values to be displayed on the screen for all fields
     * @param fieldInfo Reference to field info object containing attributes of the current field
     * @param userInfo Reference to user info object containing information about the currently logged-in user
     *
     * @return List of generic values to be displayed in the drop down.  This will be null if an error occurs.
     */
    public String[][] getValuePairArray(GenericDelegator delegator,
        UIDisplayObject uiDisplayObject, ArrayList orderDef,
        Vector entityDetailsVector, UIFieldInfo fieldInfo, UserInfo userInfo) {
        // Get all users in the current user's company.
        List userList = UserHelper.getCompanyUsers(userInfo.getAccountId(),
                delegator);

        if (userList == null) {
            Debug.logError("Error retrieving the user list: ", module);
            userList = new ArrayList();
        } else {
                Debug.logVerbose("User count = " +
                    String.valueOf(userList.size()), module);
        }

        // Add all lead queues the current user is allowed to see.
        ArrayList orderBy = new ArrayList();
        orderBy.add("leadQueueName");
       
    // select X from leadQueue lq, leadQueueUser lqu where lq.leadQueueId = lqu.leadQueueId and lqu.contactId = <partyId> and lqu.activeFlag = "Y" order by leadQueueName
    DynamicViewEntity dve = EntityHelper.createDynamicViewEntity( delegator, "LeadQueue");
    dve.addMemberEntity("LeadQueueUser", "LeadQueueUser");
    dve.addViewLink("LeadQueue", "LeadQueueUser", Boolean.FALSE, UtilMisc.toList(new ModelKeyMap("leadQueueId", "leadQueueId")));
    dve.addAlias("LeadQueueUser", "contactId", null, null, null, null, null);
    dve.addAlias("LeadQueueUser", "activeFlag", null, null, null, null, null);
    
    EntityCondition condition = new EntityConditionList(UtilMisc.toList(
        new EntityExpr("contactId", EntityOperator.EQUALS, userInfo.getPartyId()),
        new EntityExpr("activeFlag", EntityOperator.EQUALS, "Y")),
        EntityOperator.AND);

        List leadQueueList = new ArrayList();

        try {
            leadQueueList = EntityHelper.findByCondition( delegator, dve, condition, orderBy );

            Debug.logVerbose("userInfo.getPartyId(): " +
                    userInfo.getPartyId(), module);

            Debug.logVerbose("Lead Queue count = " +
                    String.valueOf(leadQueueList.size()), module);
        } catch (GenericEntityException e) {
            Debug.logError("Error retrieving the Lead Queue list: ", module);
            Debug.logError(e.getLocalizedMessage(), module);

            return null;
        }

        // Create a 2-dimensional array to hold the value/display pairs.
        String[][] selectPairArray = new String[userList.size() +
            leadQueueList.size()][2];
        int itemCount = 0;

        // Loop through the users and create value/display pairs
        Iterator userListI = userList.iterator();

        while (userListI.hasNext()) {
            GenericValue userGV = (GenericValue) userListI.next();
            String contactId = (userGV.getString("contactId") == null) ? ""
                                                                       : userGV.getString(
                    "contactId");
            String firstName = (userGV.getString("firstName") == null) ? ""
                                                                       : userGV.getString(
                    "firstName");
            String lastName = (userGV.getString("lastName") == null) ? ""
                                                                     : userGV.getString(
                    "lastName");
            selectPairArray[itemCount][0] = contactId;
            selectPairArray[itemCount][1] = firstName + " " + lastName;
            itemCount++;
        }

        // Loop through the lead queues and create value/display pairs
        Iterator leadQueueI = leadQueueList.iterator();

        while (leadQueueI.hasNext()) {
            GenericValue leadQueueGV = (GenericValue) leadQueueI.next();
            String leadQueueId = (leadQueueGV.getString("leadQueueId") == null)
                ? "" : leadQueueGV.getString("leadQueueId");
            String leadQueueName = (leadQueueGV.getString("leadQueueName") == null)
                ? "" : leadQueueGV.getString("leadQueueName");
            selectPairArray[itemCount][0] = leadQueueId;
            selectPairArray[itemCount][1] = leadQueueName;
            itemCount++;
        }

        return selectPairArray;
    }

    /**
     * Return a data value/display value pair to be passed to the getSelectHtmlReadOnly
     * method.  This overrides the ancestor because 2 types of entities are combined into one
     * drop list.  This prevents the getReadOnlyValue method from being called, which returns
     * a list of one type of entity only.
     *
     * @see #getReadOnlyValue(String, String, UIDisplayObject, Vector, GenericDelegator)
     * @see #decodeValue(String, String, String, Object)
     * @see #getSelectHtmlReadOnly(String, String, String, UIDisplayObject, String[], GenericDelegator)
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param fieldValue Value of field being displayed
     * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
     * @param uiDisplayObject Reference to a display object defined in the data base and attached to the field to be displayed by the UI builder
     * @param entityDetailsVector Vector of generic values containing the values to be displayed on the screen for all fields
     * @param fieldInfo Reference to field info object containing attributes of the current field
     * @param userInfo Reference to user info object containing information about the currently logged-in user
     * @param linkGenericValue Generic value returned to calling method
     *
     * @return List of generic values to be displayed in the drop down.  This will be null if an error occurs.
     */
    public String[] getValuePair(String fieldValue, GenericDelegator delegator,
        UIDisplayObject uiDisplayObject, Vector entityDetailsVector,
        UIFieldInfo fieldInfo, UserInfo userInfo, GenericValue linkGenericValue) {
        String[] selectPair = new String[2];

        // Look for a contact with the leadOwnerId.
        HashMap findMap = new HashMap();
        findMap.put("contactId", fieldValue);

        GenericValue entityGenericValue = null;

        try {
            entityGenericValue = delegator.findByPrimaryKeyCache("Contact",
                    findMap);
        } catch (GenericEntityException e) {
            Debug.logError(
                "[getSelectHtmlReadonly] Error searching for read-only value for drop down: " +
                e.getLocalizedMessage(), module);
        }

        if (entityGenericValue == null) {
            // The value was not found in the contact table. Go to the lead queue table
            // to see if this party ID is for a lead queue.
            Debug.logVerbose("Did not find contact", module);

            findMap = new HashMap();
            findMap.put("leadQueueId", fieldValue);

            try {
                entityGenericValue = delegator.findByPrimaryKeyCache("LeadQueue",
                        findMap);
            } catch (GenericEntityException e) {
                Debug.logError(
                    "[getSelectHtmlReadonly] Error searching for read-only value for drop down: " +
                    e.getLocalizedMessage(),module);
            }

            if (entityGenericValue == null) {
                // Item was not found in contact or lead queue table. Don't display anything.
                Debug.logVerbose("Did not find lead queue", module);

                Debug.logWarning("[getSelectHtmlReadonly] Skipping field \"" +
                    fieldInfo.getUiAttribute().getAttributeName() +
                    "\" because no entity was found for value \"" + fieldValue +
                    "\" using find definition \"" +
                    uiDisplayObject.getAttribEntityPkFindDef() + "\"", module);
            } else {
                Debug.logVerbose("Found lead queue", module);

                selectPair[0] = (entityGenericValue.getString("leadQueueId") == null)
                    ? "" : entityGenericValue.getString("leadQueueId");
                selectPair[1] = (entityGenericValue.getString("leadQueueName") == null)
                    ? "" : entityGenericValue.getString("leadQueueName");
            }
        } else {
            Debug.logVerbose("Found contact", module);

            String firstName = (entityGenericValue.getString("firstName") == null)
                ? "" : entityGenericValue.getString("firstName");
            String lastName = (entityGenericValue.getString("lastName") == null)
                ? "" : entityGenericValue.getString("lastName");
            selectPair[0] = (entityGenericValue.getString("contactId") == null)
                ? "" : entityGenericValue.getString("contactId");
            selectPair[1] = firstName + " " + lastName;
        }

        linkGenericValue = entityGenericValue;

        return selectPair;
    }
}
TOP

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

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.