~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/tiles-documentation/org/apache/struts/webapp/tiles/portal/UserMenuSettingsAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.6 $
 
4
 * $Date: 2004/03/14 06:23:52 $
 
5
 *
 
6
 * Copyright 1999-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
package org.apache.struts.webapp.tiles.portal;
 
22
 
 
23
import java.util.ArrayList;
 
24
import java.util.Iterator;
 
25
import java.util.List;
 
26
 
 
27
import javax.servlet.http.HttpServletRequest;
 
28
import javax.servlet.http.HttpServletResponse;
 
29
 
 
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;
 
38
 
 
39
/**
 
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 :
 
44
 * <ul>
 
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>
 
48
 * </ul>
 
49
 * Tiles input attributes :
 
50
 * <ul>
 
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>
 
55
 *  <li></li>
 
56
 * </ul>
 
57
 * Tiles output attributes :
 
58
 * <ul>
 
59
 *   <li>choiceItems : List of menu items proposed as a choice</li>
 
60
 *   <li>userItems : List of user actual menu items</li>
 
61
 * </ul>
 
62
 *
 
63
 */
 
64
public class UserMenuSettingsAction extends TilesAction {
 
65
 
 
66
    /** 
 
67
     * Commons Logging instance.
 
68
     */
 
69
    private static Log log = LogFactory.getLog(UserMenuSettingsAction.class);
 
70
 
 
71
    /**
 
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.
 
77
    *
 
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.
 
83
    *
 
84
    * @exception Exception if the application business logic throws
 
85
    *  an exception
 
86
    * @since Struts 1.1
 
87
    */
 
88
    public ActionForward execute(
 
89
        ComponentContext context,
 
90
        ActionMapping mapping,
 
91
        ActionForm form,
 
92
        HttpServletRequest request,
 
93
        HttpServletResponse response)
 
94
        throws Exception {
 
95
 
 
96
        log.debug("Enter action UserMenuSettingsAction");
 
97
 
 
98
        MenuSettingsForm actionForm = (MenuSettingsForm) form;
 
99
 
 
100
        // Load user menu settings and available list of choices
 
101
        MenuSettings settings = UserMenuAction.getUserSettings(request, context);
 
102
        List catalog =
 
103
            UserMenuAction.getCatalog(
 
104
                context,
 
105
                request,
 
106
                getServlet().getServletContext());
 
107
 
 
108
        // Check if form is submitted
 
109
        // If true, read, check and store new values submitted by user.
 
110
        if (actionForm.isSubmitted()) { // read arrays
 
111
 
 
112
            log.debug("form submitted");
 
113
 
 
114
            settings.reset();
 
115
            settings.addItems(getItems(actionForm.getSelected(), catalog));
 
116
 
 
117
            log.debug("settings : " + settings.toString());
 
118
            actionForm.reset();
 
119
 
 
120
        }
 
121
 
 
122
        // Prepare data for view tile
 
123
        context.putAttribute("userItems", settings.getItems());
 
124
        context.putAttribute("catalog", catalog);
 
125
 
 
126
        log.debug("Exit action UserMenuSettingsAction");
 
127
        return null;
 
128
    }
 
129
 
 
130
    /**
 
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.
 
136
     */
 
137
    protected static List getItems(String[] selectedKey, List catalog) {
 
138
        List selectedList = java.util.Arrays.asList(selectedKey);
 
139
        List result = new ArrayList(selectedList.size());
 
140
 
 
141
        Iterator iter = selectedList.iterator();
 
142
        while (iter.hasNext()) {
 
143
            MenuItem item = getItem(iter.next(), catalog);
 
144
            if (item != null) {
 
145
                result.add(item);
 
146
            }
 
147
        }
 
148
        return result;
 
149
    }
 
150
 
 
151
    /**
 
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.
 
156
     */
 
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)) {
 
162
                return item;
 
163
            }
 
164
        }
 
165
 
 
166
        return null;
 
167
    }
 
168
}