KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > common > CommonEvents


1 /*
2  * $Id: CommonEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.common;
26
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.StringUtil;
39 import org.ofbiz.base.util.UtilHttp;
40 import org.ofbiz.base.util.UtilMisc;
41 import org.ofbiz.base.util.UtilValidate;
42 import org.ofbiz.base.util.cache.UtilCache;
43 import org.ofbiz.entity.GenericDelegator;
44 import org.ofbiz.entity.GenericEntityException;
45 import org.ofbiz.entity.GenericValue;
46 import org.ofbiz.security.Security;
47
48 /**
49  * Common Services
50  *
51  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
52  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
53  * @version $Rev: 5462 $
54  * @since 2.1
55  */

56 public class CommonEvents {
57     
58     public static final String JavaDoc module = CommonEvents.class.getName();
59            
60     public static UtilCache appletSessions = new UtilCache("AppletSessions", 0, 600000, true);
61     
62     public static String JavaDoc checkAppletRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
63         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
64         String JavaDoc sessionId = request.getParameter("sessionId");
65         String JavaDoc visitId = request.getParameter("visitId");
66         sessionId = sessionId.trim();
67         visitId = visitId.trim();
68         
69         String JavaDoc responseString = "";
70         
71         GenericValue visit = null;
72         try {
73             visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap("visitId", visitId));
74         } catch (GenericEntityException e) {
75             Debug.logError(e, "Cannot Visit Object", module);
76         }
77        
78         if (visit != null && visit.getString("sessionId").equals(sessionId) && appletSessions.containsKey(sessionId)) {
79             Map JavaDoc sessionMap = (Map JavaDoc) appletSessions.get(sessionId);
80             if (sessionMap != null && sessionMap.containsKey("followPage"))
81                 responseString = (String JavaDoc) sessionMap.remove("followPage");
82         }
83         
84         try {
85             PrintWriter JavaDoc out = response.getWriter();
86             response.setContentType("text/plain");
87             out.println(responseString);
88             out.close();
89         } catch (IOException JavaDoc e) {
90             Debug.logError(e, "Problems writing servlet output!", module);
91         }
92                                                 
93         return "success";
94     }
95     
96     public static String JavaDoc receiveAppletRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
97         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
98         String JavaDoc sessionId = request.getParameter("sessionId");
99         String JavaDoc visitId = request.getParameter("visitId");
100         sessionId = sessionId.trim();
101         visitId = visitId.trim();
102                 
103         String JavaDoc responseString = "ERROR";
104         
105         GenericValue visit = null;
106         try {
107             visit = delegator.findByPrimaryKey("Visit", UtilMisc.toMap("visitId", visitId));
108         } catch (GenericEntityException e) {
109             Debug.logError(e, "Cannot Visit Object", module);
110         }
111         
112         if (visit.getString("sessionId").equals(sessionId)) {
113             String JavaDoc currentPage = (String JavaDoc) request.getParameter("currentPage");
114             if (appletSessions.containsKey(sessionId)) {
115                 Map JavaDoc sessionMap = (Map JavaDoc) appletSessions.get(sessionId);
116                 String JavaDoc followers = (String JavaDoc) sessionMap.get("followers");
117                 List JavaDoc folList = StringUtil.split(followers, ",");
118                 Iterator JavaDoc i = folList.iterator();
119                 while (i.hasNext()) {
120                     String JavaDoc follower = (String JavaDoc) i.next();
121                     Map JavaDoc folSesMap = UtilMisc.toMap("followPage", currentPage);
122                     appletSessions.put(follower, folSesMap);
123                 }
124             }
125             responseString = "OK";
126         }
127
128         try {
129             PrintWriter JavaDoc out = response.getWriter();
130             response.setContentType("text/plain");
131             out.println(responseString);
132             out.close();
133         } catch (IOException JavaDoc e) {
134             Debug.logError(e, "Problems writing servlet output!", module);
135         }
136         
137         return "success";
138     }
139     
140     public static String JavaDoc setAppletFollower(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
141         Security security = (Security) request.getAttribute("security");
142         GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
143         String JavaDoc visitId = request.getParameter("visitId");
144         if (visitId != null) request.setAttribute("visitId", visitId);
145         if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) {
146             String JavaDoc followerSessionId = request.getParameter("followerSid");
147             String JavaDoc followSessionId = request.getParameter("followSid");
148             Map JavaDoc follow = (Map JavaDoc) appletSessions.get(followSessionId);
149             if (follow == null) follow = new HashMap JavaDoc();
150             String JavaDoc followerListStr = (String JavaDoc) follow.get("followers");
151             if (followerListStr == null) {
152                 followerListStr = followerSessionId;
153             } else {
154                 followerListStr = followerListStr + "," + followerSessionId;
155             }
156             appletSessions.put(followSessionId, follow);
157             appletSessions.put(followerSessionId, null);
158         }
159         return "success";
160     }
161
162     public static String JavaDoc setFollowerPage(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
163         Security security = (Security) request.getAttribute("security");
164         GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
165         String JavaDoc visitId = request.getParameter("visitId");
166         if (visitId != null) request.setAttribute("visitId", visitId);
167         if (security.hasPermission("SEND_CONTROL_APPLET", userLogin)) {
168             String JavaDoc followerSessionId = request.getParameter("followerSid");
169             String JavaDoc pageUrl = request.getParameter("pageUrl");
170             Map JavaDoc follow = (Map JavaDoc) appletSessions.get(followerSessionId);
171             if (follow == null) follow = new HashMap JavaDoc();
172             follow.put("followPage", pageUrl);
173             appletSessions.put(followerSessionId, follow);
174         }
175         return "success";
176     }
177
178     /** Simple event to set the users per-session locale setting */
179     public static String JavaDoc setSessionLocale(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
180         String JavaDoc localeString = request.getParameter("locale");
181         if (UtilValidate.isNotEmpty(localeString)) {
182             UtilHttp.setLocale(request, localeString);
183
184             // update the UserLogin object
185
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
186             if (userLogin == null) {
187                 userLogin = (GenericValue) request.getSession().getAttribute("autoUserLogin");
188             }
189
190             if (userLogin != null) {
191                 GenericValue ulUpdate = GenericValue.create(userLogin);
192                 ulUpdate.set("lastLocale", localeString);
193                 try {
194                     ulUpdate.store();
195                     userLogin.refreshFromCache();
196                 } catch (GenericEntityException e) {
197                     Debug.logWarning(e, module);
198                 }
199             }
200         }
201         return "success";
202     }
203
204     /** Simple event to set the users per-session currency uom value */
205     public static String JavaDoc setSessionCurrencyUom(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
206         String JavaDoc currencyUom = request.getParameter("currencyUom");
207         if (UtilValidate.isNotEmpty(currencyUom)) {
208             // update the session
209
UtilHttp.setCurrencyUom(request.getSession(), currencyUom);
210
211             // update the UserLogin object
212
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
213             if (userLogin == null) {
214                 userLogin = (GenericValue) request.getSession().getAttribute("autoUserLogin");
215             }
216
217             if (userLogin != null) {
218                 GenericValue ulUpdate = GenericValue.create(userLogin);
219                 ulUpdate.set("lastCurrencyUom", currencyUom);
220                 try {
221                     ulUpdate.store();
222                     userLogin.refreshFromCache();
223                 } catch (GenericEntityException e) {
224                     Debug.logWarning(e, module);
225                 }
226             }
227         }
228         return "success";
229     }
230 }
231
232
Popular Tags