KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > tiles > portal > UserPortalAction


1 /*
2  * $Id: UserPortalAction.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.webapp.tiles.portal;
20
21 import java.util.List JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.tiles.ComponentContext;
32 import org.apache.struts.tiles.actions.TilesAction;
33
34 /**
35  * This controller load user portal settings and put them in tile context.
36  * If portal settings are not defined for user, defined them based on tiles
37  * attributes used as default.
38  *
39  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
40  */

41 public final class UserPortalAction extends TilesAction {
42
43     /**
44      * Default name used to store settings in session context.
45      */

46     public static String JavaDoc DEFAULT_USER_SETTINGS_NAME =
47         "tiles.examples.portal.USER_PORTAL_SETTINGS";
48
49     /**
50      * Tile attribute containing number of columns in portal.
51      */

52     public static String JavaDoc NUMCOLS_ATTRIBUTE = "numCols";
53
54     /**
55      * Tile attribute containing list prefix name.
56      */

57     public static String JavaDoc LIST_ATTRIBUTE = "list";
58
59     /**
60      * Tile attribute containing list of labels prefix name.
61      */

62     public static String JavaDoc LIST_LABELS_ATTRIBUTE = "labels";
63
64     /**
65      * Tile attribute containing name used to store user settings in session
66      * context.
67      */

68     public static String JavaDoc USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName";
69
70     /**
71      * Name used to store portal catalog in application scope.
72      */

73     public static String JavaDoc PORTAL_CATALOG_NAME = "tiles.examples.portal.PortalCatalog";
74
75     /**
76      * Process the specified HTTP request, and create the corresponding HTTP
77      * response (or forward to another web component that will create it).
78      * Return an <code>ActionForward</code> instance describing where and how
79      * control should be forwarded, or <code>null</code> if the response has
80      * already been completed.
81      *
82      * @param context The current Tile context, containing Tile attributes.
83      * @param mapping The ActionMapping used to select this instance.
84      * @param form The optional ActionForm bean for this request (if any).
85      * @param request The HTTP request we are processing.
86      * @param response The HTTP response we are creating.
87      *
88      * @exception Exception if the application business logic throws
89      * an exception
90      * @since Struts 1.1
91      */

92     public ActionForward execute(
93         ComponentContext context,
94         ActionMapping mapping,
95         ActionForm form,
96         HttpServletRequest JavaDoc request,
97         HttpServletResponse JavaDoc response)
98         throws Exception JavaDoc {
99
100         // Get user portal list from user context
101
PortalSettings settings = getSettings(request, context);
102
103         // Set parameters for tiles
104
context.putAttribute("numCols", Integer.toString(settings.getNumCols()));
105
106         for (int i = 0; i < settings.getNumCols(); i++) {
107             context.putAttribute("list" + i, settings.getListAt(i));
108         }
109
110         return null;
111     }
112
113     /**
114      * Retrieve user setting from session.
115      * If settings are not found, initialized them.
116      */

117     public static PortalSettings getSettings(
118         HttpServletRequest JavaDoc request,
119         ComponentContext context) {
120
121         // Get current session.
122
HttpSession JavaDoc session = request.getSession();
123
124         // Retrieve user context id used to store settings
125
String JavaDoc userSettingsId =
126             (String JavaDoc) context.getAttribute(USER_SETTINGS_NAME_ATTRIBUTE);
127
128         if (userSettingsId == null) {
129             userSettingsId = DEFAULT_USER_SETTINGS_NAME;
130         }
131
132         // Get user portal list from user context
133
PortalSettings settings =
134             (PortalSettings) session.getAttribute(userSettingsId);
135
136         if (settings == null) {
137             // List doesn't exist, create it and initialize it from Tiles parameters
138
settings = new PortalSettings();
139             settings.setNumCols((String JavaDoc) context.getAttribute(NUMCOLS_ATTRIBUTE));
140
141             for (int i = 0; i < settings.getNumCols(); i++) {
142                 List JavaDoc tiles =
143                     (List JavaDoc) context.getAttribute(((String JavaDoc) LIST_ATTRIBUTE + i));
144
145                 settings.setListAt(i, tiles);
146             }
147
148             // Save user settings in session
149
session.setAttribute(userSettingsId, settings);
150         }
151
152         return settings;
153     }
154
155     /**
156      * Retrieve portal choices object.
157      * Create it from default values if needed.
158      */

159     static public PortalCatalog getPortalCatalog(
160         ComponentContext context,
161         ServletContext JavaDoc servletContext) {
162
163         PortalCatalog catalog =
164             (PortalCatalog) servletContext.getAttribute(PORTAL_CATALOG_NAME);
165
166         if (catalog == null) { // Initialize catalog
167
catalog = new PortalCatalog();
168             int numCols =
169                 Integer.parseInt((String JavaDoc) context.getAttribute(NUMCOLS_ATTRIBUTE));
170
171             for (int i = 0; i < numCols; i++) {
172                 List JavaDoc tiles =
173                     (List JavaDoc) context.getAttribute(((String JavaDoc) LIST_ATTRIBUTE + i));
174
175                 List JavaDoc labels =
176                     (List JavaDoc) context.getAttribute(
177                         ((String JavaDoc) LIST_LABELS_ATTRIBUTE + i));
178
179                 catalog.addTiles(tiles, labels);
180             }
181             servletContext.setAttribute(PORTAL_CATALOG_NAME, catalog);
182         }
183
184         return catalog;
185     }
186
187 }
188
Popular Tags