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

📄 uifielduserdropdown.java

📁 国外的一套开源CRM
💻 JAVA
字号:
/*
 * 
 * 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.ui;

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.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;

import com.sourcetap.sfa.user.UserHelper;
import com.sourcetap.sfa.util.UserInfo;


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

    private static final String GLOBAL_DEFAULT = "Global Default";

    public UIFieldUserDropDown() {
    }

    /**
     * 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.logWarning("Error retrieving the user list: ", module);
            userList = new ArrayList();
        } else {
                Debug.logVerbose("User count = " +
                    String.valueOf(userList.size()), module);
        }

        // Get the account record for the user's company
        int accountCount = 0;
        HashMap findMap = new HashMap();
        findMap.put("accountId", userInfo.getAccountId());

        GenericValue accountGV = null;

        try {
            accountGV = delegator.findByPrimaryKey("Account", findMap);
            accountCount = 1;

        } catch (GenericEntityException e) {
            Debug.logError("Error retrieving the company account: ", module);
            Debug.logError(e.getLocalizedMessage(), module);
        }

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

        // Put the default entry in the list.
        selectPairArray[itemCount][0] = "-1";
        selectPairArray[itemCount][1] = GLOBAL_DEFAULT;
        itemCount++;

        // Put the user's company in the list.
        if (accountCount == 1) {
            String companyName = (accountGV.getString("accountName") == null)
                ? "" : accountGV.getString("accountName");
            selectPairArray[itemCount][0] = userInfo.getAccountId();
            selectPairArray[itemCount][1] = getCompanyDefaultName(companyName);
            itemCount++;
        }

        // 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++;
        }

        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];

        if (fieldValue.equals("-1")) {
            // This is the default entry.
            selectPair[0] = "-1";
            selectPair[1] = GLOBAL_DEFAULT;

            return selectPair;
        }

        // Look for a contact with the given partyId.
        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 account table
            // to see if this party ID is for an account.
            Debug.logVerbose("Did not find contact", module);

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

            try {
                entityGenericValue = delegator.findByPrimaryKeyCache("Account",
                        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 account table. Don't display anything.

                Debug.logWarning("[getSelectHtmlReadonly] Skipping field \"" +
                    fieldInfo.getUiAttribute().getAttributeName() +
                    "\" because no matching contact or account was found.", module);
            } else {

                selectPair[0] = (entityGenericValue.getString("accountId") == null)
                    ? "" : entityGenericValue.getString("accountId");
                selectPair[1] = getCompanyDefaultName((entityGenericValue.getString(
                            "accountName") == null) ? ""
                                                    : entityGenericValue.getString(
                            "accountName"));
            }
        } else {

            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;
    }

    /**
     * DOCUMENT ME!
     *
     * @param companyName 
     *
     * @return 
     */
    protected static String getCompanyDefaultName(String companyName) {
        return companyName + " Default";
    }
}

⌨️ 快捷键说明

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