This repository has been archived by the owner on Apr 8, 2019. It is now read-only.
/
NodePortlet.java
153 lines (123 loc) · 6.66 KB
/
NodePortlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/******************************************************************************
* JBoss, a division of Red Hat *
* Copyright 2013, Red Hat Middleware, LLC, and individual *
* contributors as indicated by the @authors tag. See the *
* copyright.txt in the distribution for a full listing of *
* individual contributors. *
* *
* This is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2.1 of *
* the License, or (at your option) any later version. *
* *
* This software is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this software; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
******************************************************************************/
package org.gatein.portlet.responsive.header;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import org.gatein.api.PortalRequest;
import org.gatein.api.navigation.Navigation;
import org.gatein.api.navigation.Node;
import org.gatein.api.navigation.NodePath;
import org.gatein.api.navigation.Nodes;
import org.gatein.api.site.SiteId;
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
public abstract class NodePortlet extends GenericPortlet {
private static final Logger log = LoggerFactory.getLogger(NodePortlet.class);
private final String EMPTY_CATEGORY_PROPERTY_NAME = "ShowEmptyCategories";
public static final String NODE_RESOURCE_ID = "node";
/**
* The serveResource method is used for handling AJAX requests. It's used for the rendering of sub-menus. Anytime users
* clicks on the menu item, the URI parameter is passed to the serveResource method. This parameter contains the URI of the
* node which sub-menu is about to be rendered.
*
* @see javax.portlet.GenericPortlet#serveResource(javax.portlet.ResourceRequest, javax.portlet.ResourceResponse)
*
* @param request the resource request
* @param response the resource response
* @throws PortletException if the portlet has problems fulfilling the rendering request
* @throws IOException if the streaming causes an I/O problem
*/
@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
String resourceID = request.getResourceID();
if (resourceID != null && resourceID.equals(NODE_RESOURCE_ID)) {
String siteId = request.getParameter("siteId");
Navigation navigation = PortalRequest.getInstance().getPortal().getNavigation(SiteId.fromString(siteId));
String chosenNodeURI = request.getParameter("uri");
Node chosenNode = navigation.getNode(NodePath.fromString(chosenNodeURI), Nodes.visitNodes(getNodeLevel(request)));
boolean showEmptyCategory = getShowEmptyCategory(request);
NodeBean chosenNodeBean = generateNodeBean(chosenNode, SiteId.fromString(siteId), showEmptyCategory);
request.setAttribute("showEmptyCategory", showEmptyCategory);
request.setAttribute("parentNode", chosenNodeBean);
PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/jsp/node.jsp");
prd.include(request, response);
}
}
protected NodeBean generateNodeBean(Node node, SiteId siteId, boolean showEmptyCategory) {
NodeBean nodeBean = new NodeBean(node, siteId);
List<NodeBean> nodeChildrenList = getChildren(node, siteId, false, showEmptyCategory);
if (!showEmptyCategory) {
nodeBean.setEmptyCategory(isEmptyCategory(node));
}
nodeBean.setChildren(nodeChildrenList);
return nodeBean;
}
private boolean isEmptyCategory(Node n) {
if (n.getPageId() != null) {
return false;
} else {
Navigation navigation = PortalRequest.getInstance().getNavigation();
Iterator<Node> iterator = n.iterator();
while (iterator.hasNext()) {
Node child = navigation.getNode(iterator.next().getNodePath(), Nodes.visitNodes(1));
if (child != null && !isEmptyCategory(child)) {
return false;
}
}
}
return true;
}
private List<NodeBean> getChildren(Node node, SiteId siteId, boolean firstActive, boolean showEmptyCategory) {
List<NodeBean> nodes = new ArrayList<NodeBean>();
boolean firstActiveSet = false;
Iterator<Node> nodeIterator = node.iterator();
while (nodeIterator.hasNext()) {
Node childNode = nodeIterator.next();
NodeBean childNodeBean = new NodeBean(childNode, siteId);
if (firstActive && !firstActiveSet) {
childNodeBean.setActive(true);
firstActiveSet = true;
}
if (!showEmptyCategory) {
childNodeBean.setEmptyCategory(isEmptyCategory(childNode));
}
nodes.add(childNodeBean);
}
return nodes;
}
protected boolean getShowEmptyCategory(PortletRequest request) {
PortletPreferences portletPreferences = request.getPreferences();
String showEmptyCategoryStringValue = portletPreferences.getValue(EMPTY_CATEGORY_PROPERTY_NAME, "false");
return Boolean.valueOf(showEmptyCategoryStringValue);
}
protected abstract int getNodeLevel(PortletRequest request);
}