KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > tiles > dynPortal > RetrievePortalAction


1 /*
2  * $Id: RetrievePortalAction.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.dynPortal;
20
21 import java.util.List JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26 import org.apache.struts.action.Action;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.tiles.ComponentContext;
31
32
33 /**
34  * Implementation of <strong>Action</strong> that populates an instance of
35  * <code>SubscriptionForm</code> from the currently specified subscription.
36  *
37  */

38
39 public final class RetrievePortalAction extends Action {
40
41       /** Name use to store settings in session context */
42     public static String JavaDoc USER_PORTAL_SETTINGS = "DynamicPortal.USER_PORTAL_SETTINGS";
43       /** Tile parameter name */
44     public static String JavaDoc PARAM_NUMCOLS = "numCols";
45       /** Tile parameter name */
46     public static String JavaDoc PARAM_LIST = "list";
47       /** Tile parameter name */
48     public static String JavaDoc PARAM_LIST_LABELS = "listLabels";
49     // --------------------------------------------------------- Public Methods
50

51
52     /**
53      * Process the specified HTTP request, and create the corresponding HTTP
54      * response (or forward to another web component that will create it).
55      * Return an <code>ActionForward</code> instance describing where and how
56      * control should be forwarded, or <code>null</code> if the response has
57      * already been completed.
58      *
59      * @param mapping The ActionMapping used to select this instance
60      * @param actionForm The optional ActionForm bean for this request (if any)
61      * @param request The HTTP request we are processing
62      * @param response The HTTP response we are creating
63      *
64      * @exception Exception if the application business logic throws
65      * an exception
66      */

67     public ActionForward execute(
68                  ActionMapping mapping,
69                  ActionForm form,
70                  HttpServletRequest JavaDoc request,
71                  HttpServletResponse JavaDoc response)
72     throws Exception JavaDoc {
73     System.out.println("Enter action RetrievePortalAction");
74       // Get current session.
75
HttpSession JavaDoc session = request.getSession();
76
77           // Try to retrieve tile context
78
ComponentContext context = ComponentContext.getContext( request );
79     if( context == null )
80       {
81       throw new ServletException JavaDoc( "This action must be called by a Tile, not directly" );
82       }
83
84       // Get user portal list from user context
85
PortalSettings settings = getSettings( context, session );
86
87       // Set parameters for tiles
88
context.putAttribute( "numCols", Integer.toString(settings.getNumCols()) );
89     for( int i=0; i<settings.getNumCols(); i++ )
90       context.putAttribute( "list"+i, settings.getListAt(i) );
91
92     System.out.println("Exit action RetrievePortalAction");
93       return (mapping.findForward("success"));
94     }
95
96     /**
97      * Retrieve user setting from session.
98      * If settings are not found, initialized them.
99      */

100   public static PortalSettings getSettings( ComponentContext context, HttpSession JavaDoc session )
101   {
102       // Get user portal list from user context
103
PortalSettings settings = (PortalSettings)session.getAttribute( USER_PORTAL_SETTINGS );
104
105     if( settings == null )
106       { // List doesn't exist, create it and initialize it from Tiles parameters
107
settings = new PortalSettings();
108       settings.setNumCols( (String JavaDoc)context.getAttribute( PARAM_NUMCOLS ) );
109       for( int i=0; i<settings.getNumCols(); i++ )
110         {
111         List JavaDoc col = (List JavaDoc)context.getAttribute( ((String JavaDoc)PARAM_LIST+i) );
112         List JavaDoc labels = (List JavaDoc)context.getAttribute( ((String JavaDoc)PARAM_LIST_LABELS+i) );
113         settings.addChoices( col, labels );
114         settings.addList( col );
115         } // end loop
116

117         // Save user settings in session
118
session.setAttribute( USER_PORTAL_SETTINGS, settings);
119       } // end if
120

121   return settings;
122   }
123
124
125 }
126
127
Popular Tags