KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > wstore > JSPEnv


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.wstore;
15
16 import java.util.*;
17 import java.sql.*;
18
19 import javax.servlet.*;
20 import javax.servlet.http.*;
21 import org.apache.log4j.Logger;
22
23 import org.compiere.*;
24 import org.compiere.util.*;
25 import org.compiere.model.*;
26
27 /**
28  * JSP Environment Utilities
29  *
30  * @author Jorg Janke
31  * @version $Id: JSPEnv.java,v 1.17 2003/09/07 06:15:16 jjanke Exp $
32  */

33 public class JSPEnv
34 {
35     /** Logger */
36     static private Logger s_log = Logger.getLogger (JSPEnv.class);
37
38     public final static String JavaDoc CONTEXT_NAME = "ctx";
39     public final static String JavaDoc CTX_SERVER_CONTEXT = "context";
40     public final static String JavaDoc CTX_DOCUMENT_DIR = "documentDir";
41     /** Header (Error) Message */
42     public final static String JavaDoc HDR_MESSAGE = "hdrMessage";
43     /** Header Info Message */
44     public final static String JavaDoc HDR_INFO = "hdrInfo";
45     /** Next Refresh */
46     public static long s_refresh;
47     public static final int REFRESH_MS = 360000; // 1 min = 60,000 - 1 Std = 360,000
48
/** Context */
49     private static CCache s_cacheCtx = new CCache("JSPEnvCtx", 2);
50
51
52     /**
53      * Get Context from Session
54      * @param request request
55      * @return properties
56      */

57     public static Properties getCtx (HttpServletRequest request)
58     {
59         // Session
60
HttpSession session = request.getSession(true);
61         Properties ctx = (Properties)session.getAttribute(CONTEXT_NAME);
62         if (ctx != null && System.currentTimeMillis() < s_refresh)
63         {
64             session.setMaxInactiveInterval(1800); // 30 Min
65
String JavaDoc info = (String JavaDoc)ctx.get(HDR_INFO);
66             if (info != null)
67                 session.setAttribute(HDR_INFO, info);
68             return ctx;
69         }
70
71         //
72
s_log.info("getCtx - refresh");
73         s_refresh = System.currentTimeMillis() + REFRESH_MS;
74         ctx = new Properties();
75         // Create New
76
ServletContext sc = session.getServletContext();
77         Enumeration en = sc.getInitParameterNames();
78         while (en.hasMoreElements())
79         {
80             String JavaDoc key = (String JavaDoc)en.nextElement();
81             String JavaDoc value = sc.getInitParameter(key);
82             ctx.setProperty(key, value);
83         }
84         // Default Client
85
int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID");
86         if (AD_Client_ID == 0)
87         {
88             AD_Client_ID = DB.getSQLValue("SELECT AD_Client_ID FROM AD_Client WHERE AD_Client_ID > 11 AND IsActive='Y'");
89             if (AD_Client_ID < 0)
90                 AD_Client_ID = 11; // GardenWorld
91
Env.setContext (ctx, "#AD_Client_ID", AD_Client_ID);
92         }
93
94         ctx = getDefaults(ctx, AD_Client_ID);
95         // ServerContext - dev2/wstore
96
ctx.put(CTX_SERVER_CONTEXT, request.getServerName() + request.getContextPath());
97
98         // save it
99
s_log.debug("getCtx #" + ctx.size());
100         session.setAttribute(CONTEXT_NAME, ctx);
101         return ctx;
102     } // getCtx
103

104     /**
105      * Get Defaults
106      * @param ctx context
107      * @param AD_Client_ID client
108      * @return context
109      */

110     private static Properties getDefaults (Properties ctx, int AD_Client_ID)
111     {
112         Integer JavaDoc key = new Integer JavaDoc (AD_Client_ID);
113         Properties pp = (Properties)s_cacheCtx.get(key);
114         if (pp != null)
115         {
116         // return new Properties (pp); seems not to work with JSP
117
Enumeration e = pp.keys();
118             while (e.hasMoreElements())
119             {
120                 String JavaDoc pKey = (String JavaDoc)e.nextElement();
121                 ctx.setProperty(pKey, pp.getProperty(pKey));
122             }
123             return ctx;
124         }
125
126         /** Create New Context */
127         s_log.info("getDefaults - AD_Client_ID=" + AD_Client_ID);
128
129         // Default Trx! Org
130
if (Env.getContextAsInt(ctx, "#AD_Org_ID") == 0)
131         {
132             int AD_Org_ID = DB.getSQLValue("SELECT AD_Org_ID FROM AD_Org WHERE AD_Client_ID=? AND IsActive='Y' AND IsSummary='N' ORDER BY 1", AD_Client_ID);
133             Env.setContext(ctx, "#AD_Org_ID", AD_Org_ID);
134         }
135         // Default User
136
if (Env.getContextAsInt(ctx, "#AD_User_ID") == 0)
137             ctx.setProperty("#AD_User_ID", "0"); // System
138

139         // Warehouse
140
if (Env.getContextAsInt(ctx, "#M_Warehouse_ID") == 0)
141         {
142             int M_Warehouse_ID = DB.getSQLValue("SELECT M_Warehouse_ID FROM M_Warehouse WHERE AD_Client_ID=? AND IsActive='Y' ORDER BY 1", AD_Client_ID);
143             Env.setContext(ctx, "#M_Warehouse_ID", M_Warehouse_ID);
144         }
145         // Sales Rep
146
if (Env.getContextAsInt(ctx, "#SalesRep_ID") == 0)
147         {
148             int SalesRep_ID = 0;
149             Env.setContext(ctx, "#SalesRep_ID", SalesRep_ID);
150         }
151         // Payment Term
152
if (Env.getContextAsInt(ctx, "#C_PaymentTerm_ID") == 0)
153         {
154             int C_PaymentTerm_ID = DB.getSQLValue("SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y' ORDER BY NetDays", AD_Client_ID);
155             Env.setContext(ctx, "#C_PaymentTerm_ID", C_PaymentTerm_ID);
156         }
157
158         /****************************************/
159
160         // Read from disk
161
MClient client = MClient.get (ctx, AD_Client_ID);
162         // Name,Description, SMTPHost,RequestEMail,RequestUser, RequestUserPw
163
Env.setContext(ctx, "name", client.getName());
164         Env.setContext(ctx, "description", client.getDescription());
165         Env.setContext(ctx, "SMTPHost", client.getSMTPHost());
166         Env.setContext(ctx, "RequestEMail", client.getRequestEMail());
167         Env.setContext(ctx, "RequestUser", client.getRequestUser());
168         Env.setContext(ctx, "RequestUserPw", client.getRequestUserPW());
169         // AD_Language, WebDir, WebParam1,WebParam2,WebParam3,WebParam4, WebOrderEMail
170
if (ctx.getProperty("#AD_Language") == null && client.getAD_Language() != null)
171             Env.setContext(ctx, "#AD_Language", client.getAD_Language());
172         Env.setContext(ctx, "webDir", client.getWebDir());
173         String JavaDoc s = client.getWebParam1();
174         Env.setContext(ctx, "webParam1", s == null ? "" : s);
175         s = client.getWebParam2();
176         Env.setContext(ctx, "webParam2", s == null ? "" : s);
177         s = client.getWebParam3();
178         Env.setContext(ctx, "webParam3", s == null ? "" : s);
179         s = client.getWebParam4();
180         Env.setContext(ctx, "webParam4", s == null ? "" : s);
181         s = client.getWebParam5();
182         Env.setContext(ctx, "webParam5", s == null ? "" : s);
183         s = client.getWebParam6();
184         Env.setContext(ctx, "webParam6", s == null ? "" : s);
185         s = client.getWebOrderEMail();
186         Env.setContext(ctx, "webOrderEMail", s == null ? "" : s);
187         s = client.getWebInfo();
188         if (s != null && s.length() > 0)
189             Env.setContext(ctx, HDR_INFO, s);
190         // M_PriceList_ID, DocumentDir
191
Env.setContext(ctx, "#M_PriceList_ID", client.getMClientInfo().getM_PriceList_ID());
192         s = client.getDocumentDir();
193         Env.setContext(ctx, CTX_DOCUMENT_DIR, s == null ? "" : s);
194
195         // Default Language
196
if (ctx.getProperty("#AD_Language") == null)
197             Env.setContext(ctx, "#AD_Language", "en_US");
198
199         s_cacheCtx.put(key, ctx); // Key is AD_Client_ID
200
return ctx;
201     } // getDefaults
202

203     /*************************************************************************/
204
205     private final static String JavaDoc COOKIE_NAME = "CompiereWebUser";
206
207     /**
208      * Get Web User from Cookie
209      * @param request request with cookie
210      * @return web user or null
211      */

212     public static String JavaDoc getCookieWebUser (HttpServletRequest request)
213     {
214         Cookie[] cookies = request.getCookies();
215         if (cookies == null)
216             return null;
217         for (int i = 0; i < cookies.length; i++)
218         {
219             if (COOKIE_NAME.equals(cookies[i].getName()))
220                 return cookies[i].getValue();
221         }
222         return null;
223     }
224
225     /**
226      * Add Cookie with web user
227      * @param request request (for context path)
228      * @param response response to add cookie
229      * @param webUser email address
230      */

231     public static void addCookieWebUser (HttpServletRequest request, HttpServletResponse response, String JavaDoc webUser)
232     {
233         Cookie cookie = new Cookie(COOKIE_NAME, webUser);
234         cookie.setComment("Compiere Web User");
235         cookie.setPath(request.getContextPath());
236         cookie.setMaxAge(2592000); // 30 days in seconds 60*60*24*30
237
response.addCookie(cookie);
238     } // setCookieWebUser
239

240     /**
241      * Remove Cookie with web user by setting user to _
242      * @param request request (for context path)
243      * @param response response to add cookie
244      */

245     public static void deleteCookieWebUser (HttpServletRequest request, HttpServletResponse response)
246     {
247         Cookie cookie = new Cookie(COOKIE_NAME, " ");
248         cookie.setComment("Compiere Web User");
249         cookie.setPath(request.getContextPath());
250         cookie.setMaxAge(1); // second
251
response.addCookie(cookie);
252     } // deleteCookieWebUser
253

254 } // JSPEnv
255
Popular Tags