KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: UserMenuSettingsAction.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.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.tiles.ComponentContext;
34 import org.apache.struts.tiles.actions.TilesAction;
35 import org.apache.struts.tiles.beans.MenuItem;
36
37 /**
38  * Tiles controller as Struts Action.
39  * This controller take a list of lists of MenuItems, and arrange them
40  * to be shown by appropriate jsp view.
41  * Create and set following attribute in Tile context :
42  * <ul>
43  * <li>names : list of names to display</li>
44  * <li>returnedValues : list of corresponding key, or values to return</li>
45  * <li>selecteds : list of boolean indicating whether or not a name is selected</li>
46  * </ul>
47  * Tiles input attributes :
48  * <ul>
49  * <li>title : menu title</li>
50  * <li>items : Menu entries used as default when user settings is created</li>
51  * <li>defaultChoice : Menus or menu entries porposed as choice to user</li>
52  * <li>storeUnderName : Store user settings under provided name in session context [optional]</li>
53  * <li></li>
54  * </ul>
55  * Tiles output attributes :
56  * <ul>
57  * <li>choiceItems : List of menu items proposed as a choice</li>
58  * <li>userItems : List of user actual menu items</li>
59  * </ul>
60  *
61  */

62 public class UserMenuSettingsAction extends TilesAction {
63
64     /**
65      * Commons Logging instance.
66      */

67     private static Log log = LogFactory.getLog(UserMenuSettingsAction.class);
68
69     /**
70     * Process the specified HTTP request, and create the corresponding HTTP
71     * response (or forward to another web component that will create it).
72     * Return an <code>ActionForward</code> instance describing where and how
73     * control should be forwarded, or <code>null</code> if the response has
74     * already been completed.
75     *
76     * @param context The current Tile context, containing Tile attributes.
77     * @param mapping The ActionMapping used to select this instance.
78     * @param form The optional ActionForm bean for this request (if any).
79     * @param request The HTTP request we are processing.
80     * @param response The HTTP response we are creating.
81     *
82     * @exception Exception if the application business logic throws
83     * an exception
84     * @since Struts 1.1
85     */

86     public ActionForward execute(
87         ComponentContext context,
88         ActionMapping mapping,
89         ActionForm form,
90         HttpServletRequest JavaDoc request,
91         HttpServletResponse JavaDoc response)
92         throws Exception JavaDoc {
93
94         log.debug("Enter action UserMenuSettingsAction");
95
96         MenuSettingsForm actionForm = (MenuSettingsForm) form;
97
98         // Load user menu settings and available list of choices
99
MenuSettings settings = UserMenuAction.getUserSettings(request, context);
100         List JavaDoc catalog =
101             UserMenuAction.getCatalog(
102                 context,
103                 request,
104                 getServlet().getServletContext());
105
106         // Check if form is submitted
107
// If true, read, check and store new values submitted by user.
108
if (actionForm.isSubmitted()) { // read arrays
109

110             log.debug("form submitted");
111
112             settings.reset();
113             settings.addItems(getItems(actionForm.getSelected(), catalog));
114
115             log.debug("settings : " + settings.toString());
116             actionForm.reset();
117
118         }
119
120         // Prepare data for view tile
121
context.putAttribute("userItems", settings.getItems());
122         context.putAttribute("catalog", catalog);
123
124         log.debug("Exit action UserMenuSettingsAction");
125         return null;
126     }
127
128     /**
129      * Check selected items, and return apppropriate list of items.
130      * For each item in selected list, check if it exist in catalog.
131      * Also check for double.
132      * @param selectedKey Key of selected items (generally, link url)
133      * @param catalog List of avalaible items to compare against.
134      */

135     protected static List JavaDoc getItems(String JavaDoc[] selectedKey, List JavaDoc catalog) {
136         List JavaDoc selectedList = java.util.Arrays.asList(selectedKey);
137         List JavaDoc result = new ArrayList JavaDoc(selectedList.size());
138
139         Iterator JavaDoc iter = selectedList.iterator();
140         while (iter.hasNext()) {
141             MenuItem item = getItem(iter.next(), catalog);
142             if (item != null) {
143                 result.add(item);
144             }
145         }
146         return result;
147     }
148
149     /**
150      * Get item by its key in list of choices
151      * @param key Key of selected items (generally, link url)
152      * @param catalog List of avalaible items to compare against.
153      * @return corresponding item or null if not found.
154      */

155     protected static MenuItem getItem(Object JavaDoc key, List JavaDoc catalog) {
156         Iterator JavaDoc iter = catalog.iterator();
157         while (iter.hasNext()) {
158             MenuItem item = (MenuItem) iter.next();
159             if (item.getLink().equals(key)) {
160                 return item;
161             }
162         }
163
164         return null;
165     }
166 }
167
Popular Tags