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.lead;
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.base.util.UtilMisc;
027: import org.ofbiz.entity.GenericDelegator;
028: import org.ofbiz.entity.GenericEntityException;
029: import org.ofbiz.entity.GenericValue;
030: import org.ofbiz.entity.condition.EntityCondition;
031: import org.ofbiz.entity.condition.EntityConditionList;
032: import org.ofbiz.entity.condition.EntityExpr;
033: import org.ofbiz.entity.condition.EntityOperator;
034: import org.ofbiz.entity.model.DynamicViewEntity;
035: import org.ofbiz.entity.model.ModelKeyMap;
036:
037: import com.sourcetap.sfa.ui.UIDisplayObject;
038: import com.sourcetap.sfa.ui.UIDropDown;
039: import com.sourcetap.sfa.ui.UIFieldInfo;
040: import com.sourcetap.sfa.user.UserHelper;
041: import com.sourcetap.sfa.util.EntityHelper;
042: import com.sourcetap.sfa.util.UserInfo;
043:
044: /**
045: * DOCUMENT ME!
046: *
047: */
048: public class UserLeadQueueDropDown extends UIDropDown {
049: public static final String module = UserLeadQueueDropDown.class
050: .getName();
051:
052: public UserLeadQueueDropDown() {
053: }
054:
055: /**
056: * Return an array of data value/display value pairs to be passed to the getDisplayHtml
057: * method. This overrides the ancestor because 2 types of entities are combined into one
058: * drop list. This prevents the getSelectValues method from being called, which returns
059: * a list of one type of entity only.
060: *
061: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
062: *
063: * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
064: * @param uiDisplayObject Reference to a display object defined in the data base and attached to the field to be displayed by the UI builder
065: * @param orderDef List of fields defining the sort order of the drop down values
066: * @param entityDetailsVector Vector of generic values containing the values to be displayed on the screen for all fields
067: * @param fieldInfo Reference to field info object containing attributes of the current field
068: * @param userInfo Reference to user info object containing information about the currently logged-in user
069: *
070: * @return List of generic values to be displayed in the drop down. This will be null if an error occurs.
071: */
072: public String[][] getValuePairArray(GenericDelegator delegator,
073: UIDisplayObject uiDisplayObject, ArrayList orderDef,
074: Vector entityDetailsVector, UIFieldInfo fieldInfo,
075: UserInfo userInfo) {
076: // Get all users in the current user's company.
077: List userList = UserHelper.getCompanyUsers(userInfo
078: .getAccountId(), delegator);
079:
080: if (userList == null) {
081: Debug.logError("Error retrieving the user list: ", module);
082: userList = new ArrayList();
083: } else {
084: Debug.logVerbose("User count = "
085: + String.valueOf(userList.size()), module);
086: }
087:
088: // Add all lead queues the current user is allowed to see.
089: ArrayList orderBy = new ArrayList();
090: orderBy.add("leadQueueName");
091:
092: // select X from leadQueue lq, leadQueueUser lqu where lq.leadQueueId = lqu.leadQueueId and lqu.contactId = <partyId> and lqu.activeFlag = "Y" order by leadQueueName
093: DynamicViewEntity dve = EntityHelper.createDynamicViewEntity(
094: delegator, "LeadQueue");
095: dve.addMemberEntity("LeadQueueUser", "LeadQueueUser");
096: dve.addViewLink("LeadQueue", "LeadQueueUser", Boolean.FALSE,
097: UtilMisc.toList(new ModelKeyMap("leadQueueId",
098: "leadQueueId")));
099: dve.addAlias("LeadQueueUser", "contactId", null, null, null,
100: null, null);
101: dve.addAlias("LeadQueueUser", "activeFlag", null, null, null,
102: null, null);
103:
104: EntityCondition condition = new EntityConditionList(UtilMisc
105: .toList(new EntityExpr("contactId",
106: EntityOperator.EQUALS, userInfo.getPartyId()),
107: new EntityExpr("activeFlag",
108: EntityOperator.EQUALS, "Y")),
109: EntityOperator.AND);
110:
111: List leadQueueList = new ArrayList();
112:
113: try {
114: leadQueueList = EntityHelper.findByCondition(delegator,
115: dve, condition, orderBy);
116:
117: Debug.logVerbose("userInfo.getPartyId(): "
118: + userInfo.getPartyId(), module);
119:
120: Debug.logVerbose("Lead Queue count = "
121: + String.valueOf(leadQueueList.size()), module);
122: } catch (GenericEntityException e) {
123: Debug.logError("Error retrieving the Lead Queue list: ",
124: module);
125: Debug.logError(e.getLocalizedMessage(), module);
126:
127: return null;
128: }
129:
130: // Create a 2-dimensional array to hold the value/display pairs.
131: String[][] selectPairArray = new String[userList.size()
132: + leadQueueList.size()][2];
133: int itemCount = 0;
134:
135: // Loop through the users and create value/display pairs
136: Iterator userListI = userList.iterator();
137:
138: while (userListI.hasNext()) {
139: GenericValue userGV = (GenericValue) userListI.next();
140: String contactId = (userGV.getString("contactId") == null) ? ""
141: : userGV.getString("contactId");
142: String firstName = (userGV.getString("firstName") == null) ? ""
143: : userGV.getString("firstName");
144: String lastName = (userGV.getString("lastName") == null) ? ""
145: : userGV.getString("lastName");
146: selectPairArray[itemCount][0] = contactId;
147: selectPairArray[itemCount][1] = firstName + " " + lastName;
148: itemCount++;
149: }
150:
151: // Loop through the lead queues and create value/display pairs
152: Iterator leadQueueI = leadQueueList.iterator();
153:
154: while (leadQueueI.hasNext()) {
155: GenericValue leadQueueGV = (GenericValue) leadQueueI.next();
156: String leadQueueId = (leadQueueGV.getString("leadQueueId") == null) ? ""
157: : leadQueueGV.getString("leadQueueId");
158: String leadQueueName = (leadQueueGV
159: .getString("leadQueueName") == null) ? ""
160: : leadQueueGV.getString("leadQueueName");
161: selectPairArray[itemCount][0] = leadQueueId;
162: selectPairArray[itemCount][1] = leadQueueName;
163: itemCount++;
164: }
165:
166: return selectPairArray;
167: }
168:
169: /**
170: * Return a data value/display value pair to be passed to the getSelectHtmlReadOnly
171: * method. This overrides the ancestor because 2 types of entities are combined into one
172: * drop list. This prevents the getReadOnlyValue method from being called, which returns
173: * a list of one type of entity only.
174: *
175: * @see #getReadOnlyValue(String, String, UIDisplayObject, Vector, GenericDelegator)
176: * @see #decodeValue(String, String, String, Object)
177: * @see #getSelectHtmlReadOnly(String, String, String, UIDisplayObject, String[], GenericDelegator)
178: *
179: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
180: *
181: * @param fieldValue Value of field being displayed
182: * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
183: * @param uiDisplayObject Reference to a display object defined in the data base and attached to the field to be displayed by the UI builder
184: * @param entityDetailsVector Vector of generic values containing the values to be displayed on the screen for all fields
185: * @param fieldInfo Reference to field info object containing attributes of the current field
186: * @param userInfo Reference to user info object containing information about the currently logged-in user
187: * @param linkGenericValue Generic value returned to calling method
188: *
189: * @return List of generic values to be displayed in the drop down. This will be null if an error occurs.
190: */
191: public String[] getValuePair(String fieldValue,
192: GenericDelegator delegator,
193: UIDisplayObject uiDisplayObject,
194: Vector entityDetailsVector, UIFieldInfo fieldInfo,
195: UserInfo userInfo, GenericValue linkGenericValue) {
196: String[] selectPair = new String[2];
197:
198: // Look for a contact with the leadOwnerId.
199: HashMap findMap = new HashMap();
200: findMap.put("contactId", fieldValue);
201:
202: GenericValue entityGenericValue = null;
203:
204: try {
205: entityGenericValue = delegator.findByPrimaryKeyCache(
206: "Contact", findMap);
207: } catch (GenericEntityException e) {
208: Debug.logError(
209: "[getSelectHtmlReadonly] Error searching for read-only value for drop down: "
210: + e.getLocalizedMessage(), module);
211: }
212:
213: if (entityGenericValue == null) {
214: // The value was not found in the contact table. Go to the lead queue table
215: // to see if this party ID is for a lead queue.
216: Debug.logVerbose("Did not find contact", module);
217:
218: findMap = new HashMap();
219: findMap.put("leadQueueId", fieldValue);
220:
221: try {
222: entityGenericValue = delegator.findByPrimaryKeyCache(
223: "LeadQueue", findMap);
224: } catch (GenericEntityException e) {
225: Debug.logError(
226: "[getSelectHtmlReadonly] Error searching for read-only value for drop down: "
227: + e.getLocalizedMessage(), module);
228: }
229:
230: if (entityGenericValue == null) {
231: // Item was not found in contact or lead queue table. Don't display anything.
232: Debug.logVerbose("Did not find lead queue", module);
233:
234: Debug
235: .logWarning(
236: "[getSelectHtmlReadonly] Skipping field \""
237: + fieldInfo.getUiAttribute()
238: .getAttributeName()
239: + "\" because no entity was found for value \""
240: + fieldValue
241: + "\" using find definition \""
242: + uiDisplayObject
243: .getAttribEntityPkFindDef()
244: + "\"", module);
245: } else {
246: Debug.logVerbose("Found lead queue", module);
247:
248: selectPair[0] = (entityGenericValue
249: .getString("leadQueueId") == null) ? ""
250: : entityGenericValue.getString("leadQueueId");
251: selectPair[1] = (entityGenericValue
252: .getString("leadQueueName") == null) ? ""
253: : entityGenericValue.getString("leadQueueName");
254: }
255: } else {
256: Debug.logVerbose("Found contact", module);
257:
258: String firstName = (entityGenericValue
259: .getString("firstName") == null) ? ""
260: : entityGenericValue.getString("firstName");
261: String lastName = (entityGenericValue.getString("lastName") == null) ? ""
262: : entityGenericValue.getString("lastName");
263: selectPair[0] = (entityGenericValue.getString("contactId") == null) ? ""
264: : entityGenericValue.getString("contactId");
265: selectPair[1] = firstName + " " + lastName;
266: }
267:
268: linkGenericValue = entityGenericValue;
269:
270: return selectPair;
271: }
272: }
|