2
* $Header: /home/cvs/jakarta-struts/src/tiles-documentation/org/apache/struts/webapp/tiles/portal/UserMenuSettingsAction.java,v 1.6 2004/03/14 06:23:52 sraeburn Exp $
4
* $Date: 2004/03/14 06:23:52 $
6
* Copyright 1999-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.webapp.tiles.portal;
23
import java.util.ArrayList;
24
import java.util.Iterator;
25
import java.util.List;
27
import javax.servlet.http.HttpServletRequest;
28
import javax.servlet.http.HttpServletResponse;
30
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
32
import org.apache.struts.action.ActionForm;
33
import org.apache.struts.action.ActionForward;
34
import org.apache.struts.action.ActionMapping;
35
import org.apache.struts.tiles.ComponentContext;
36
import org.apache.struts.tiles.actions.TilesAction;
37
import org.apache.struts.tiles.beans.MenuItem;
40
* Tiles controller as Struts Action.
41
* This controller take a list of lists of MenuItems, and arrange them
42
* to be shown by appropriate jsp view.
43
* Create and set following attribute in Tile context :
45
* <li>names : list of names to display</li>
46
* <li>returnedValues : list of corresponding key, or values to return</li>
47
* <li>selecteds : list of boolean indicating whether or not a name is selected</li>
49
* Tiles input attributes :
51
* <li>title : menu title</li>
52
* <li>items : Menu entries used as default when user settings is created</li>
53
* <li>defaultChoice : Menus or menu entries porposed as choice to user</li>
54
* <li>storeUnderName : Store user settings under provided name in session context [optional]</li>
57
* Tiles output attributes :
59
* <li>choiceItems : List of menu items proposed as a choice</li>
60
* <li>userItems : List of user actual menu items</li>
64
public class UserMenuSettingsAction extends TilesAction {
67
* Commons Logging instance.
69
private static Log log = LogFactory.getLog(UserMenuSettingsAction.class);
72
* Process the specified HTTP request, and create the corresponding HTTP
73
* response (or forward to another web component that will create it).
74
* Return an <code>ActionForward</code> instance describing where and how
75
* control should be forwarded, or <code>null</code> if the response has
76
* already been completed.
78
* @param context The current Tile context, containing Tile attributes.
79
* @param mapping The ActionMapping used to select this instance.
80
* @param form The optional ActionForm bean for this request (if any).
81
* @param request The HTTP request we are processing.
82
* @param response The HTTP response we are creating.
84
* @exception Exception if the application business logic throws
88
public ActionForward execute(
89
ComponentContext context,
90
ActionMapping mapping,
92
HttpServletRequest request,
93
HttpServletResponse response)
96
log.debug("Enter action UserMenuSettingsAction");
98
MenuSettingsForm actionForm = (MenuSettingsForm) form;
100
// Load user menu settings and available list of choices
101
MenuSettings settings = UserMenuAction.getUserSettings(request, context);
103
UserMenuAction.getCatalog(
106
getServlet().getServletContext());
108
// Check if form is submitted
109
// If true, read, check and store new values submitted by user.
110
if (actionForm.isSubmitted()) { // read arrays
112
log.debug("form submitted");
115
settings.addItems(getItems(actionForm.getSelected(), catalog));
117
log.debug("settings : " + settings.toString());
122
// Prepare data for view tile
123
context.putAttribute("userItems", settings.getItems());
124
context.putAttribute("catalog", catalog);
126
log.debug("Exit action UserMenuSettingsAction");
131
* Check selected items, and return apppropriate list of items.
132
* For each item in selected list, check if it exist in catalog.
133
* Also check for double.
134
* @param selectedKey Key of selected items (generally, link url)
135
* @param catalog List of avalaible items to compare against.
137
protected static List getItems(String[] selectedKey, List catalog) {
138
List selectedList = java.util.Arrays.asList(selectedKey);
139
List result = new ArrayList(selectedList.size());
141
Iterator iter = selectedList.iterator();
142
while (iter.hasNext()) {
143
MenuItem item = getItem(iter.next(), catalog);
152
* Get item by its key in list of choices
153
* @param key Key of selected items (generally, link url)
154
* @param catalog List of avalaible items to compare against.
155
* @return corresponding item or null if not found.
157
protected static MenuItem getItem(Object key, List catalog) {
158
Iterator iter = catalog.iterator();
159
while (iter.hasNext()) {
160
MenuItem item = (MenuItem) iter.next();
161
if (item.getLink().equals(key)) {