001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.ui;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Vector;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.entity.GenericEntityException;
028: import org.ofbiz.entity.GenericValue;
029:
030: import com.sourcetap.sfa.user.UserHelper;
031: import com.sourcetap.sfa.util.UserInfo;
032:
033: /**
034: * DOCUMENT ME!
035: *
036: */
037: public class UIFieldUserDropDown extends UIDropDown {
038: public static final String module = UIFieldUserDropDown.class
039: .getName();
040:
041: private static final String GLOBAL_DEFAULT = "Global Default";
042:
043: public UIFieldUserDropDown() {
044: }
045:
046: /**
047: * Return an array of data value/display value pairs to be passed to the getDisplayHtml
048: * method. This overrides the ancestor because 2 types of entities are combined into one
049: * drop list. This prevents the getSelectValues method from being called, which returns
050: * a list of one type of entity only.
051: *
052: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
053: *
054: * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
055: * @param uiDisplayObject Reference to a display object defined in the data base and attached to the field to be displayed by the UI builder
056: * @param orderDef List of fields defining the sort order of the drop down values
057: * @param entityDetailsVector Vector of generic values containing the values to be displayed on the screen for all fields
058: * @param fieldInfo Reference to field info object containing attributes of the current field
059: * @param userInfo Reference to user info object containing information about the currently logged-in user
060: *
061: * @return List of generic values to be displayed in the drop down. This will be null if an error occurs.
062: */
063: public String[][] getValuePairArray(GenericDelegator delegator,
064: UIDisplayObject uiDisplayObject, ArrayList orderDef,
065: Vector entityDetailsVector, UIFieldInfo fieldInfo,
066: UserInfo userInfo) {
067: // Get all users in the current user's company.
068: List userList = UserHelper.getCompanyUsers(userInfo
069: .getAccountId(), delegator);
070:
071: if (userList == null) {
072: Debug
073: .logWarning("Error retrieving the user list: ",
074: module);
075: userList = new ArrayList();
076: } else {
077: Debug.logVerbose("User count = "
078: + String.valueOf(userList.size()), module);
079: }
080:
081: // Get the account record for the user's company
082: int accountCount = 0;
083: HashMap findMap = new HashMap();
084: findMap.put("accountId", userInfo.getAccountId());
085:
086: GenericValue accountGV = null;
087:
088: try {
089: accountGV = delegator.findByPrimaryKey("Account", findMap);
090: accountCount = 1;
091:
092: } catch (GenericEntityException e) {
093: Debug.logError("Error retrieving the company account: ",
094: module);
095: Debug.logError(e.getLocalizedMessage(), module);
096: }
097:
098: // Create a 2-dimensional array to hold the value/display pairs.
099: String[][] selectPairArray = new String[1 + accountCount
100: + userList.size()][2];
101: int itemCount = 0;
102:
103: // Put the default entry in the list.
104: selectPairArray[itemCount][0] = "-1";
105: selectPairArray[itemCount][1] = GLOBAL_DEFAULT;
106: itemCount++;
107:
108: // Put the user's company in the list.
109: if (accountCount == 1) {
110: String companyName = (accountGV.getString("accountName") == null) ? ""
111: : accountGV.getString("accountName");
112: selectPairArray[itemCount][0] = userInfo.getAccountId();
113: selectPairArray[itemCount][1] = getCompanyDefaultName(companyName);
114: itemCount++;
115: }
116:
117: // Loop through the users and create value/display pairs
118: Iterator userListI = userList.iterator();
119:
120: while (userListI.hasNext()) {
121: GenericValue userGV = (GenericValue) userListI.next();
122: String contactId = (userGV.getString("contactId") == null) ? ""
123: : userGV.getString("contactId");
124: String firstName = (userGV.getString("firstName") == null) ? ""
125: : userGV.getString("firstName");
126: String lastName = (userGV.getString("lastName") == null) ? ""
127: : userGV.getString("lastName");
128: selectPairArray[itemCount][0] = contactId;
129: selectPairArray[itemCount][1] = firstName + " " + lastName;
130: itemCount++;
131: }
132:
133: return selectPairArray;
134: }
135:
136: /**
137: * Return a data value/display value pair to be passed to the getSelectHtmlReadOnly
138: * method. This overrides the ancestor because 2 types of entities are combined into one
139: * drop list. This prevents the getReadOnlyValue method from being called, which returns
140: * a list of one type of entity only.
141: *
142: * @see #getReadOnlyValue(String, String, UIDisplayObject, Vector, GenericDelegator)
143: * @see #decodeValue(String, String, String, Object)
144: * @see #getSelectHtmlReadOnly(String, String, String, UIDisplayObject, String[], GenericDelegator)
145: *
146: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
147: *
148: * @param fieldValue Value of field being displayed
149: * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
150: * @param uiDisplayObject Reference to a display object defined in the data base and attached to the field to be displayed by the UI builder
151: * @param entityDetailsVector Vector of generic values containing the values to be displayed on the screen for all fields
152: * @param fieldInfo Reference to field info object containing attributes of the current field
153: * @param userInfo Reference to user info object containing information about the currently logged-in user
154: * @param linkGenericValue Generic value returned to calling method
155: *
156: * @return List of generic values to be displayed in the drop down. This will be null if an error occurs.
157: */
158: public String[] getValuePair(String fieldValue,
159: GenericDelegator delegator,
160: UIDisplayObject uiDisplayObject,
161: Vector entityDetailsVector, UIFieldInfo fieldInfo,
162: UserInfo userInfo, GenericValue linkGenericValue) {
163: String[] selectPair = new String[2];
164:
165: if (fieldValue.equals("-1")) {
166: // This is the default entry.
167: selectPair[0] = "-1";
168: selectPair[1] = GLOBAL_DEFAULT;
169:
170: return selectPair;
171: }
172:
173: // Look for a contact with the given partyId.
174: HashMap findMap = new HashMap();
175: findMap.put("contactId", fieldValue);
176:
177: GenericValue entityGenericValue = null;
178:
179: try {
180: entityGenericValue = delegator.findByPrimaryKeyCache(
181: "Contact", findMap);
182: } catch (GenericEntityException e) {
183: Debug.logError(
184: "[getSelectHtmlReadonly] Error searching for read-only value for drop down: "
185: + e.getLocalizedMessage(), module);
186: }
187:
188: if (entityGenericValue == null) {
189: // The value was not found in the contact table. Go to the account table
190: // to see if this party ID is for an account.
191: Debug.logVerbose("Did not find contact", module);
192:
193: findMap = new HashMap();
194: findMap.put("accountId", fieldValue);
195:
196: try {
197: entityGenericValue = delegator.findByPrimaryKeyCache(
198: "Account", findMap);
199: } catch (GenericEntityException e) {
200: Debug.logError(
201: "[getSelectHtmlReadonly] Error searching for read-only value for drop down: "
202: + e.getLocalizedMessage(), module);
203: }
204:
205: if (entityGenericValue == null) {
206: // Item was not found in contact or account table. Don't display anything.
207:
208: Debug
209: .logWarning(
210: "[getSelectHtmlReadonly] Skipping field \""
211: + fieldInfo.getUiAttribute()
212: .getAttributeName()
213: + "\" because no matching contact or account was found.",
214: module);
215: } else {
216:
217: selectPair[0] = (entityGenericValue
218: .getString("accountId") == null) ? ""
219: : entityGenericValue.getString("accountId");
220: selectPair[1] = getCompanyDefaultName((entityGenericValue
221: .getString("accountName") == null) ? ""
222: : entityGenericValue.getString("accountName"));
223: }
224: } else {
225:
226: String firstName = (entityGenericValue
227: .getString("firstName") == null) ? ""
228: : entityGenericValue.getString("firstName");
229: String lastName = (entityGenericValue.getString("lastName") == null) ? ""
230: : entityGenericValue.getString("lastName");
231: selectPair[0] = (entityGenericValue.getString("contactId") == null) ? ""
232: : entityGenericValue.getString("contactId");
233: selectPair[1] = firstName + " " + lastName;
234: }
235:
236: linkGenericValue = entityGenericValue;
237:
238: return selectPair;
239: }
240:
241: /**
242: * DOCUMENT ME!
243: *
244: * @param companyName
245: *
246: * @return
247: */
248: protected static String getCompanyDefaultName(String companyName) {
249: return companyName + " Default";
250: }
251: }
|