KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > crmsfa > ajax > AjaxEvents


1 /*
2 Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the Honest Public License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 Honest Public License for more details.
11
12 You should have received a copy of the Honest Public License
13 along with this program; if not, write to Funambol,
14 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USApackage com.opensourcestrategies.crmsfa.ajax;
15 */

16 package com.opensourcestrategies.crmsfa.ajax;
17
18 import java.util.*;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import javax.servlet.http.*;
21
22 import org.ofbiz.base.util.*;
23 import org.ofbiz.entity.*;
24 import org.ofbiz.entity.condition.*;
25 import org.ofbiz.entity.util.*;
26 import org.ofbiz.entity.transaction.*;
27 import org.ofbiz.service.*;
28
29 /**
30  * Ajax events to be invoked by the controller.
31  *
32  * @author Leon Torres (leon@opensourcestrategies.com)
33  */

34 public class AjaxEvents {
35
36     public static final String JavaDoc module = AjaxEvents.class.getName();
37
38     /** How many results to show in the autocomplete list. */
39     public static final int DEFAULT_AUTOCOMPLETE_RESULTS_SIZE = 10;
40
41     /**
42      * Obtains candidate accounts for an autocomplete element. The input
43      * parameter is accoutName, which will be matched against
44      * PartyGroup.groupName.
45      */

46     public static String JavaDoc autocompleteAccounts(HttpServletRequest request, HttpServletResponse response) {
47         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
48
49         // get the accountName parameter and make sure it has some content
50
String JavaDoc accountName = null;
51         try {
52             accountName = new String JavaDoc(request.getParameter("accountName").getBytes("iso-8859-1"), "UTF-8");
53         } catch (UnsupportedEncodingException JavaDoc e) {
54         }
55         if (accountName == null || accountName.trim().length() == 0) return "success";
56
57         try {
58             // select parties with ACCOUNT role and lowercase(groupName) like lowercase(accountName)
59
List conditions = UtilMisc.toList(
60                     new EntityExpr("roleTypeId", EntityOperator.EQUALS, "ACCOUNT"),
61                     new EntityExpr("groupName", true, EntityOperator.LIKE, "%" + accountName.trim() + "%", true));
62             EntityConditionList conditionList = new EntityConditionList(conditions, EntityOperator.AND);
63             List accounts = delegator.findByCondition("PartyRoleAndPartyDetail", conditionList, UtilMisc.toList("partyId", "groupName"), UtilMisc.toList("groupName"));
64
65             // put the results in the request as an attribute, which will be retrieved by the ftl
66
request.setAttribute("accounts", accounts);
67             return "success";
68         } catch (GenericEntityException e) {
69             Debug.logError(e, e.getMessage(), module);
70             return "error";
71         }
72     }
73
74     // static data so these don't get created over and over again each request
75
private static EntityCondition accountOrProspectRoleCondition = new EntityConditionList( UtilMisc.toList(
76                 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "ACCOUNT"),
77                 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "PROSPECT")
78                 ), EntityOperator.OR);
79     private static EntityFindOptions entityFindOptions = new EntityFindOptions(true, EntityFindOptions.TYPE_FORWARD_ONLY, EntityFindOptions.CONCUR_READ_ONLY, true);
80     private static List partyNameFields = UtilMisc.toList("partyId", "displayName");
81     private static List partyNameOrderBy = UtilMisc.toList("displayName");
82
83     /**
84      * Search for Accounts or Leads using a given search string partyName. Constrains results to Accounts
85      * or Leads that have any kind of active relationship with the userLogin.
86      * The search space is defined by UPPERCASE(groupName + firstName + lastName).
87      */

88     public static String JavaDoc autocompleteAccountsOrLeads(HttpServletRequest request, HttpServletResponse response) {
89         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
90         HttpSession session = request.getSession();
91         GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
92         if (delegator == null || userLogin == null) return "error";
93
94         // convert input to UTF-8 and make sure it has content
95
String JavaDoc partyName = null;
96         try {
97             partyName = new String JavaDoc(request.getParameter("partyName").getBytes("iso-8859-1"), "UTF-8");
98         } catch (UnsupportedEncodingException JavaDoc e) {
99             Debug.logError(e, e.getMessage());
100         }
101         if (partyName == null || partyName.trim().length() == 0) return "success";
102
103         // format partyName for search
104
partyName = partyName.trim().toUpperCase();
105
106         try {
107             // find active ACCOUNT or PROSPECT parties that the userLogin has a relationship to
108
EntityCondition conditions = new EntityConditionList( UtilMisc.toList(
109                         new EntityExpr("partyIdTo", EntityOperator.EQUALS, userLogin.get("partyId")),
110                         EntityUtil.getFilterByDateExpr(),
111                         accountOrProspectRoleCondition
112                         ), EntityOperator.AND);
113
114             // get result as a list iterator (transaction block is to work around a bug in entity engine)
115
TransactionUtil.begin();
116             EntityListIterator iterator = delegator.findListIteratorByCondition("PartyFromSummaryByRelationship", conditions, null,
117                     partyNameFields, partyNameOrderBy, entityFindOptions);
118
119             // scan the parties by hand (prevents SQL injection)
120
ArrayList parties = new ArrayList();
121             GenericValue party = null;
122             String JavaDoc searchString = null;
123             int results = 0;
124             while ((party = (GenericValue) iterator.next()) != null) {
125                 searchString = party.getString("displayName").toUpperCase();
126                 if (searchString.indexOf(partyName) > -1) {
127                     parties.add(party);
128                     results += 1;
129                 }
130                 if (results >= DEFAULT_AUTOCOMPLETE_RESULTS_SIZE)
131                     break;
132             }
133
134             // clean up
135
iterator.close();
136             TransactionUtil.commit();
137
138             // put the results in the request as an attribute, which will be retrieved by the ftl
139
request.setAttribute("parties", parties);
140             return "success";
141         } catch (GenericEntityException e) {
142             Debug.logError(e, module);
143             return "error";
144         }
145     }
146 }
147
Popular Tags