KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > menuItem > ListMenuItemsController


1 package com.blandware.atleap.webapp.action.core.menuItem;
2
3 import com.blandware.atleap.common.Constants;
4 import com.blandware.atleap.model.core.ActionPage;
5 import com.blandware.atleap.model.core.ContentPage;
6 import com.blandware.atleap.model.core.Layout;
7 import com.blandware.atleap.model.core.Localizable;
8 import com.blandware.atleap.model.core.MenuItem;
9 import com.blandware.atleap.service.core.LookupManager;
10 import com.blandware.atleap.service.core.MenuManager;
11 import com.blandware.atleap.webapp.util.core.MenuUtil;
12 import com.blandware.atleap.webapp.util.core.WebappConstants;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.commons.validator.GenericValidator;
16 import org.apache.struts.tiles.ComponentContext;
17 import org.apache.struts.tiles.ControllerSupport;
18 import org.springframework.context.ApplicationContext;
19 import org.springframework.web.context.support.WebApplicationContextUtils;
20
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27
28
29 /**
30  * <p>ListMenuItemsController class. This class is used to display list of menu items from persistence layer
31  * </p>
32  * <p><a HREF="ListMenuItemsController.java.htm"><i>View Source</i></a></p>
33  *
34  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
35  * @version $Revision: 1.13 $ $Date: 2005/07/02 18:26:18 $
36  */

37 public final class ListMenuItemsController extends ControllerSupport {
38
39     protected transient final Log log = LogFactory.getLog(ListMenuItemsController.class);
40
41     //~ Methods ================================================================
42
/**
43      * Retrieves all menu items and puts them into request
44      *
45      * @param tilesContext Current tile context
46      * @param request Current request
47      * @param response Current response
48      * @param servletContext Current Servlet Context
49      */

50     public void execute(ComponentContext tilesContext,
51                         HttpServletRequest JavaDoc request,
52                         HttpServletResponse JavaDoc response,
53                         ServletContext JavaDoc servletContext) throws Exception JavaDoc {
54
55         Long JavaDoc ownerId = (Long JavaDoc) request.getAttribute("ownerId");
56
57         // get parent item ID from the session
58
Long JavaDoc parentItemId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.MENU_ITEM_PARENT_ID_KEY);
59         List JavaDoc menuItemsList = new ArrayList JavaDoc();
60
61         ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
62
63         if ( ownerId != null ) {
64             LookupManager lookupManager = (LookupManager) applicationContext.getBean(Constants.LOOKUP_MANAGER_BEAN);
65             Localizable owner = lookupManager.retrieveLocalizable(ownerId);
66
67             String JavaDoc pageUri = null;
68             String JavaDoc layoutDefinition = null;
69             if ( owner instanceof Layout ) {
70                 Layout layout = (Layout) owner;
71                 layoutDefinition = layout.getDefinition();
72             } else if ( owner instanceof ContentPage ) {
73                 ContentPage contentPage = (ContentPage) owner;
74                 pageUri = contentPage.getUri();
75                 layoutDefinition = contentPage.getLayout().getDefinition();
76             } else if ( owner instanceof ActionPage ) {
77                 ActionPage actionPage = (ActionPage) owner;
78                 pageUri = actionPage.getUri();
79                 layoutDefinition = (String JavaDoc) request.getSession().getAttribute(WebappConstants.LAYOUT_DEFINITION_KEY);
80             }
81
82             // list of menu items will be shown if and only if layout definition is specified
83
// otherwise, nothing will be displayed
84
if ( !GenericValidator.isBlankOrNull(layoutDefinition) ) {
85                 // check parent's existence, because it might be deleted by another transaction
86
if ( parentItemId != null ) {
87                     MenuManager menuManager = (MenuManager) applicationContext.getBean(Constants.MENU_MANAGER_BEAN);
88                     MenuItem parentItem = menuManager.retrieveMenuItem(parentItemId);
89                     if ( parentItem == null ) {
90                         // item does not exist
91
parentItemId = null;
92                         request.getSession().removeAttribute(WebappConstants.MENU_ITEM_PARENT_ID_KEY);
93                     } else {
94                         // parent item is not null, save list of parents
95
LinkedList JavaDoc parents = new LinkedList JavaDoc();
96                         while ( parentItem != null ) {
97                             parents.addFirst(parentItem);
98                             parentItem = parentItem.getParentItem();
99                         }
100                         request.setAttribute(WebappConstants.MENU_ITEM_PARENTS_LIST_KEY, parents);
101                     }
102                 }
103                 menuItemsList = new MenuUtil(request).getMenuItems(parentItemId, layoutDefinition, pageUri, false);
104             }
105         }
106
107         request.setAttribute("menuItemsList", menuItemsList);
108     }
109 }
110
Popular Tags