001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049: package org.jaffa.components.navigation;
050:
051: import java.util.List;
052: import java.util.Iterator;
053: import java.util.Map;
054: import java.util.ArrayList;
055: import java.util.HashMap;
056: import org.jaffa.components.navigation.domain.GlobalMenu;
057: import org.jaffa.components.navigation.domain.MenuOption;
058: import org.apache.log4j.Logger;
059: import org.jaffa.util.LoggerHelper;
060: import org.jaffa.presentation.portlet.component.ComponentDefinition;
061: import org.jaffa.presentation.portlet.component.ComponentManager;
062: import javax.servlet.http.HttpSession;
063:
064: /** This class allows access to a navigation system, based on a users access.
065: * When this object is constructed, it looks at the full navigation system, and
066: * filters out any options the current user does not have access to.
067: *
068: * @author JonnyR
069: * @author PaulE
070: * @version 1.0
071: */
072: public class NavAccessor {
073: private static final Logger log = Logger
074: .getLogger(NavAccessor.class);
075:
076: List m_globalNavOptions = new ArrayList();
077: Map m_desktopNavOptions = new HashMap();
078: int m_desktopCounter = 1;
079:
080: /**This is a static method to allow a synchronized Access to the NavAccessor object
081: *caching it on creation
082: */
083: public static synchronized NavAccessor getNavAccessor(
084: HttpSession session) {
085: NavAccessor na = (NavAccessor) session
086: .getAttribute(NavAccessor.class.getName());
087: if (na == null) {
088: na = new NavAccessor();
089: session.setAttribute(NavAccessor.class.getName(), na);
090: }
091: return na;
092: }
093:
094: /** Creates a new instance of NavAccessor
095: */
096: NavAccessor() {
097: GlobalMenu gmenu = NavCache.getInstance().getGlobalMenu();
098: parseMenuOptions(gmenu.getMenuOption(), m_globalNavOptions);
099: }
100:
101: /** @return the number of menu items added under this option */
102: int parseMenuOptions(List menuOptions, List navOptions) {
103: int counter = 0;
104: for (Iterator it = menuOptions.iterator(); it.hasNext();) {
105: MenuOption mOpt = (MenuOption) it.next();
106: try {
107: NavOption nOpt = new NavOption(this , mOpt);
108: navOptions.add(nOpt);
109: counter++;
110: if (log.isDebugEnabled())
111: log.debug("Added Option - " + nOpt.getLabel());
112: } catch (SecurityException se) {
113: if (log.isDebugEnabled())
114: log.debug("Access Denied to Menu Option "
115: + mOpt.getLabel());
116: }
117: }
118: return counter;
119: }
120:
121: /** Holder a counter for assigning unique ids to options within this NavAccessor
122: */
123: int getNextDesktopId() {
124: return m_desktopCounter++;
125: }
126:
127: /** Based on the UserId this method will return the list of variables available to the user.
128: * @return This returns a List of every menu NavOption available to the user based on their UserId
129: */
130: public List getGlobalNavOptions() {
131: return m_globalNavOptions;
132: }
133:
134: /** Based on the DesktopId passed a list will be build of child nodes based on security and parent node ID.
135: * @param desktopId Desktop Id that will one verified be used to return a list of NavOptions that are children that have this desktop Id
136: * @return List of NavOptions based on a DektopId is passed back , for use in building desktop navs
137: */
138: public NavOption getDesktopNavOptions(String desktopId) {
139: return (NavOption) m_desktopNavOptions.get(desktopId);
140: }
141:
142: void addDesktopNavOption(String desktopId, NavOption option) {
143: m_desktopNavOptions.put(desktopId, option);
144: }
145:
146: /** Clear the NavAccessor attribute for this session.
147: */
148: public void clearSession(HttpSession session) {
149: session.setAttribute(NavAccessor.class.getName(), null);
150: }
151:
152: //-----------------------------------------------------------------------------------------------
153: // For Testing
154: //-----------------------------------------------------------------------------------------------
155:
156: public static void main(String[] args) {
157: LoggerHelper.init();
158: NavAccessor n = new NavAccessor();
159:
160: log.info("Display Global Menu...");
161: NavAccessor.printMenuOptions(n.getGlobalNavOptions(), "");
162: String compdef = ComponentManager.find("XX").getComponentType();
163: for (int i = 1; i < n.m_desktopCounter; i++) {
164: NavOption o = n.getDesktopNavOptions("" + i);
165: log.info("Display DeskTop... [" + o.getDesktopId() + "] - "
166: + o.getLabel());
167: NavAccessor.printMenuOptions(o.getChildren(), " ");
168: }
169: }
170:
171: public static void printMenuOptions(List l, String prefix) {
172: for (Iterator it = l.iterator(); it.hasNext();) {
173: NavOption n = (NavOption) it.next();
174: log.info(prefix + n.getType() + " - " + n.getLabel() + " "
175: + (n.isDesktop() ? n.getDesktopId() : ""));
176: if (n.isSubMenu())
177: NavAccessor.printMenuOptions(n.getChildren(), prefix
178: + " ");
179: else if (n.isComponent()) {
180: log.info(prefix + "...... Component:"
181: + n.getComponent());
182: log.info(prefix + "...... Params:" + n.getParameters()
183: + " / " + n.getURL());
184: }
185: }
186: }
187: }
|