1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.navigation;
18
19 import java.io.StringWriter;
20 import java.io.Writer;
21 import java.lang.reflect.Constructor;
22 import java.util.Hashtable;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import javax.portlet.MimeResponse;
27 import javax.portlet.ResourceURL;
28
29 import org.exoplatform.portal.application.PortalRequestContext;
30 import org.exoplatform.portal.config.UserACL;
31 import org.exoplatform.portal.config.UserPortalConfig;
32 import org.exoplatform.portal.config.UserPortalConfigService;
33 import org.exoplatform.portal.mop.SiteKey;
34 import org.exoplatform.portal.mop.SiteType;
35 import org.exoplatform.portal.mop.Visibility;
36 import org.exoplatform.portal.mop.navigation.NavigationContext;
37 import org.exoplatform.portal.mop.navigation.Scope;
38 import org.exoplatform.portal.mop.user.UserNavigation;
39 import org.exoplatform.portal.mop.user.UserNode;
40 import org.exoplatform.portal.mop.user.UserNodeFilterConfig;
41 import org.exoplatform.portal.mop.user.UserPortal;
42 import org.exoplatform.portal.mop.user.UserPortalImpl;
43 import org.exoplatform.portal.webui.util.Util;
44 import org.exoplatform.services.log.ExoLogger;
45 import org.exoplatform.services.log.Log;
46 import org.exoplatform.services.security.ConversationState;
47 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
48 import org.exoplatform.webui.application.WebuiRequestContext;
49
50
51
52
53
54
55
56
57 public class NavigationUtils {
58
59 public static final Scope ECMS_NAVIGATION_SCOPE = Scope.CHILDREN;
60
61 private static ThreadLocal<Map<String, String>> gotNavigationKeeper = new ThreadLocal<Map<String, String>>();
62
63 private static Constructor<UserNavigation> userNavigationCtor = null;
64
65 private static final Log LOG = ExoLogger.getLogger(NavigationUtils.class.getName());
66 static {
67 try {
68
69
70 userNavigationCtor = UserNavigation.class.getDeclaredConstructor(
71 new Class[] {UserPortalImpl.class, NavigationContext.class, boolean.class});
72 userNavigationCtor.setAccessible(true);
73 }catch (Exception e) {
74 if (LOG.isErrorEnabled()) {
75 LOG.error(e);
76 }
77 }
78 }
79
80 public static boolean gotNavigation(String portal, String user) {
81 return gotNavigation(portal, user, "");
82 }
83
84 public static boolean gotNavigation(String portal, String user, String scope) {
85 Map<String, String> navigations = gotNavigationKeeper.get();
86 if (navigations == null) return false;
87 String navigation = navigations.get(portal + " " + user + " " + scope);
88 return (navigation != null);
89 }
90
91 public static UserNavigation getUserNavigationOfPortal(UserPortal userPortal, String portalName) throws Exception {
92 UserACL userACL = WCMCoreUtils.getService(UserACL.class);
93 UserPortalConfigService userPortalConfigService = WCMCoreUtils.getService(UserPortalConfigService.class);
94 NavigationContext portalNav = userPortalConfigService.getNavigationService().
95 loadNavigation(new SiteKey(SiteType.PORTAL, portalName));
96 if (portalNav ==null) {
97 return null;
98 }
99 UserPortalConfig userPortalCfg = userPortalConfigService.getUserPortalConfig(portalName,
100 ConversationState.getCurrent().getIdentity().getUserId(),
101 PortalRequestContext.USER_PORTAL_CONTEXT);
102 return userNavigationCtor.newInstance(
103 userPortal, portalNav,
104 userACL.hasEditPermission(userPortalCfg.getPortalConfig()));
105 }
106
107
108
109
110
111
112
113 public static UserNavigation getUserNavigation(UserPortal userPortal, SiteKey siteKey) throws Exception {
114 if (siteKey.getTypeName().equalsIgnoreCase(SiteType.PORTAL.getName())) {
115 return getUserNavigationOfPortal(userPortal,siteKey.getName());
116 }
117 UserACL userACL = WCMCoreUtils.getService(UserACL.class);
118 UserPortalConfigService userPortalConfigService = WCMCoreUtils.getService(UserPortalConfigService.class);
119
120 NavigationContext portalNav = userPortalConfigService.getNavigationService().
121 loadNavigation(siteKey);
122 if (portalNav == null) {
123 return null;
124 } else {
125 return userNavigationCtor.newInstance(userPortal, portalNav, userACL.hasEditPermissionOnNavigation(siteKey));
126 }
127 }
128
129 public static void removeNavigationAsJson (String portalName, String username) throws Exception {
130 for (String scope : new String[]{"single", "children", "grandchildren", "all"})
131 removeNavigationAsJson(portalName, username, scope);
132 }
133
134 public static void removeNavigationAsJson (String portalName, String username, String scope) throws Exception
135 {
136 String key = portalName + " " + username + " " + scope;
137 Map<String, String> navigations = gotNavigationKeeper.get();
138 if (navigations != null) {
139 navigations.remove(key);
140 gotNavigationKeeper.set(navigations);
141 }
142 }
143
144 public static String getNavigationAsJSON(String portalName, String username) throws Exception {
145 return getNavigationAsJSON(portalName, username, null, "");
146 }
147
148 public static String getNavigationAsJSON(String portalName, String username, Scope scope, String navigationScope) throws Exception {
149
150 String key = portalName + " " + username + " " + navigationScope;
151 Map<String, String> navigations = gotNavigationKeeper.get();
152 if (navigations == null) {
153 navigations = new Hashtable<String, String>();
154 } else {
155 String navigationData = navigations.get(key);
156 if (navigationData != null) {
157 return navigationData;
158 }
159 }
160 UserPortalConfigService userPortalConfigService = WCMCoreUtils.getService(UserPortalConfigService.class);
161 UserPortalConfig userPortalCfg = userPortalConfigService.getUserPortalConfig(portalName,
162 username,
163 PortalRequestContext.USER_PORTAL_CONTEXT);
164 UserPortal userPortal = userPortalCfg.getUserPortal();
165
166
167 UserNodeFilterConfig.Builder filterConfigBuilder = UserNodeFilterConfig.builder();
168 filterConfigBuilder.withReadWriteCheck().withVisibility(Visibility.DISPLAYED, Visibility.TEMPORAL);
169 filterConfigBuilder.withTemporalCheck();
170 UserNodeFilterConfig filterConfig = filterConfigBuilder.build();
171
172
173 UserNavigation navigation = getUserNavigationOfPortal(userPortal, portalName);
174 UserNode root = userPortal.getNode(navigation, scope == null ? ECMS_NAVIGATION_SCOPE : scope, filterConfig, null);
175
176 String ret = createJsonTree(navigation, root);
177 navigations.put(key, ret);
178 gotNavigationKeeper.set(navigations);
179 return ret;
180 }
181
182 private static String createJsonTree(UserNavigation navigation, UserNode rootNode) throws Exception {
183 StringBuffer sbJsonTree = new StringBuffer();
184 sbJsonTree.append("[");
185 sbJsonTree.append("{");
186 sbJsonTree.append("\"ownerId\":\"").append(navigation.getKey().getName()).append("\",");
187 sbJsonTree.append("\"ownerType\":\"").append(navigation.getKey().getTypeName()).append("\",");
188 sbJsonTree.append("\"priority\":\"").append(navigation.getPriority()).append("\",");
189 sbJsonTree.append("\"nodes\":").append(addJsonNodes(rootNode.getChildren().iterator()));
190 sbJsonTree.append("}");
191 sbJsonTree.append("]");
192 return sbJsonTree.toString();
193 }
194
195 private static StringBuffer addJsonNodes(Iterator<UserNode> children) throws Exception {
196 StringBuffer sbJsonTree = new StringBuffer();
197 sbJsonTree.append("[");
198 boolean first = true;
199
200 while (children.hasNext()) {
201 UserNode child = children.next();
202 if (!first) {
203 sbJsonTree.append(",");
204 }
205 first = false;
206 sbJsonTree.append("{");
207 sbJsonTree.append("\"icon\":").append(child.getIcon() != null ? "\"" + child.getIcon() + "\""
208 : "null").append(",");
209 sbJsonTree.append("\"label\":\"").append(child.getLabel()).append("\",");
210 sbJsonTree.append("\"name\":\"").append(child.getName()).append("\",");
211 sbJsonTree.append("\"resolvedLabel\":\"").append(child.getResolvedLabel()).append("\",");
212 String childURI = "";
213 if (child.getPageRef() != null){
214 childURI = child.getURI();
215 }
216 sbJsonTree.append("\"uri\":\"").append(childURI).append("\",");
217
218 WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
219 MimeResponse res = context.getResponse();
220 ResourceURL resourceURL = res.createResourceURL();
221 resourceURL.setResourceID(res.encodeURL(child.getURI()));
222 Writer w = new StringWriter();
223 resourceURL.write(w, true);
224 sbJsonTree.append("\"getNodeURL\":\"").append(w.toString()).append("\",");
225 sbJsonTree.append("\"nodes\":").append(addJsonNodes(child.getChildren().iterator()));
226 sbJsonTree.append("}");
227 }
228 sbJsonTree.append("]");
229 return sbJsonTree;
230 }
231 }