1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.wcm.webui.scv;
18
19 import java.util.Collection;
20 import java.util.Date;
21
22 import javax.jcr.Node;
23 import javax.portlet.MimeResponse;
24 import javax.portlet.PortletMode;
25 import javax.portlet.PortletPreferences;
26 import javax.portlet.RenderResponse;
27 import javax.portlet.ResourceRequest;
28 import javax.portlet.ResourceURL;
29
30 import org.exoplatform.portal.mop.navigation.Scope;
31 import org.exoplatform.portal.mop.Visibility;
32 import org.exoplatform.portal.mop.user.UserNavigation;
33 import org.exoplatform.portal.mop.user.UserNode;
34 import org.exoplatform.portal.mop.user.UserNodeFilterConfig;
35 import org.exoplatform.portal.mop.user.UserPortal;
36 import org.exoplatform.portal.webui.util.Util;
37 import org.exoplatform.services.cms.templates.TemplateService;
38 import org.exoplatform.services.security.ConversationRegistry;
39 import org.exoplatform.services.security.ConversationState;
40 import org.exoplatform.services.wcm.core.WCMService;
41 import org.exoplatform.services.wcm.navigation.NavigationUtils;
42 import org.exoplatform.services.wcm.publication.WCMComposer;
43 import org.exoplatform.wcm.webui.Utils;
44 import org.exoplatform.web.application.RequireJS;
45 import org.exoplatform.webui.application.WebuiApplication;
46 import org.exoplatform.webui.application.WebuiRequestContext;
47 import org.exoplatform.webui.application.portlet.PortletRequestContext;
48 import org.exoplatform.webui.config.annotation.ComponentConfig;
49 import org.exoplatform.webui.core.UIPopupContainer;
50 import org.exoplatform.webui.core.UIPortletApplication;
51 import org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle;
52 import org.gatein.portal.controller.resource.ResourceScope;
53 import org.json.JSONArray;
54 import org.json.JSONObject;
55 import org.w3c.dom.Element;
56
57
58
59
60
61
62
63 @ComponentConfig(
64 lifecycle = UIApplicationLifecycle.class,
65 template = "system:/groovy/SingleContentViewer/UISingleContentView.gtmpl"
66 )
67
68 public class UISingleContentViewerPortlet extends UIPortletApplication {
69
70
71 public static String REPOSITORY = "repository" ;
72
73
74 public static String WORKSPACE = "workspace" ;
75
76
77 public static String IDENTIFIER = "nodeIdentifier" ;
78
79
80 public static String DRIVE = "nodeDrive";
81
82
83 public static String PARAMETER = "ParameterName";
84
85
86 public static String SHOW_DATE = "ShowDate";
87
88
89 public static String SHOW_TITLE = "ShowTitle";
90
91
92 public static String SHOW_OPTIONBAR = "ShowOptionBar";
93
94
95 public static String CONTEXTUAL_MODE= "ContextEnable";
96
97
98 public static String PRINT_PARAMETER= "PrintParameterName";
99
100
101 public static String PRINT_PAGE = "PrintPage";
102
103
104
105 public final static String PREFERENCE_TARGET_PAGE = "basePath";
106
107
108 public final static String PREFERENCE_SHOW_SCV_WITH = "showScvWith";
109
110 public static final String DEFAULT_SHOW_SCV_WITH = "content-id";
111
112
113 public static final String ENABLE_CACHE = "sharedCache";
114
115 public static final String NAVIGATION_SCOPE = "NavigationScope";
116
117 public static final String NAVIGATION_SCOPE_SINGLE = "single";
118 public static final String NAVIGATION_SCOPE_CHILDREN = "children";
119 public static final String NAVIGATION_SCOPE_GRAND_CHILDREN = "grandChildren";
120 public static final String NAVIGATION_SCOPE_ALL = "all";
121
122 private PortletMode mode = null;
123
124 public static final String UIPreferencesPopupID = "UIPreferencesPopupWindows";
125
126 private UISCVPreferences popPreferences;
127 private UIPresentationContainer uiPresentation;
128 PortletPreferences preferences;
129
130
131
132
133
134
135 public UISingleContentViewerPortlet() throws Exception {
136 addChild(UIPopupContainer.class, null, "UIPopupContainer-" + new Date().getTime());
137 PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
138 preferences = portletRequestContext.getRequest().getPreferences();
139 }
140
141
142
143
144
145
146
147
148 public void activateMode(PortletMode newMode) throws Exception{
149 if (getChild(UIPresentationContainer.class) !=null) {
150 removeChild(UIPresentationContainer.class);
151 }
152 if (getChild(UISCVPreferences.class) != null) {
153 removeChild(UISCVPreferences.class);
154 }
155 if(PortletMode.VIEW.equals(newMode)) {
156 PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
157 uiPresentation = addChild(UIPresentationContainer.class, null, UIPresentationContainer.class.getSimpleName()
158 + pContext.getWindowId());
159 } else if (PortletMode.EDIT.equals(newMode)) {
160 popPreferences = addChild(UISCVPreferences.class, null, null);
161 popPreferences.setInternalPreferencesMode(true);
162 }
163 }
164
165
166
167
168
169
170
171
172 public void processRender(WebuiApplication app, WebuiRequestContext context) throws Exception {
173 PortletRequestContext pContext = (PortletRequestContext) context ;
174 PortletMode newMode = pContext.getApplicationMode() ;
175 PortletPreferences preferences = pContext.getRequest().getPreferences();
176 Boolean sharedCache = "true".equals(preferences.getValue(ENABLE_CACHE, "true"));
177
178 if ((context.getRemoteUser() == null && !Boolean.parseBoolean(preferences.getValue(UISingleContentViewerPortlet.CONTEXTUAL_MODE, "false")))
179 || (Utils.isLiveMode() && sharedCache && !Utils.isPortalEditMode() && Utils.isPortletViewMode(pContext))) {
180 WCMService wcmService = getApplicationComponent(WCMService.class);
181 pContext.getResponse().setProperty(MimeResponse.EXPIRATION_CACHE, ""+wcmService.getPortletExpirationCache());
182 if (log.isTraceEnabled())
183 log.trace("SCV rendering : cache set to "+wcmService.getPortletExpirationCache());
184 }
185
186 if(!newMode.equals(mode)) {
187 activateMode(newMode) ;
188 mode = newMode ;
189 }
190
191 Node nodeView = null;
192 if (uiPresentation!=null) {
193 nodeView = uiPresentation.getNodeView();
194 if (nodeView != null) {
195 TemplateService templateService = getApplicationComponent(TemplateService.class);
196 uiPresentation.getChild(UIPresentation.class).setTemplatePath(templateService.getTemplatePath(nodeView, false));
197 }
198 }
199
200
201
202
203
204
205
206
207 if (context.getRemoteUser() != null && WCMComposer.MODE_EDIT.equals(Utils.getCurrentMode())) {
208 pContext.getJavascriptManager().loadScriptResource(ResourceScope.SHARED, "content-selector");
209 pContext.getJavascriptManager().loadScriptResource(ResourceScope.SHARED, "quick-edit");
210 }
211
212 setId(UISingleContentViewerPortlet.class.getSimpleName() + pContext.getWindowId());
213 super.processRender(app, context) ;
214 }
215
216 public void changeToViewMode() throws Exception{
217 PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
218 portletRequestContext.setApplicationMode(PortletMode.VIEW);
219 }
220
221 @Override
222 public void serveResource(WebuiRequestContext context) throws Exception {
223 super.serveResource(context);
224
225 ResourceRequest req = context.getRequest();
226 String nodeURI = req.getResourceID();
227
228 JSONArray jsChilds = getChildrenAsJSON(nodeURI);
229 if (jsChilds == null) {
230 return;
231 }
232
233 MimeResponse res = context.getResponse();
234 res.setContentType("text/json");
235 res.getWriter().write(jsChilds.toString());
236 }
237
238 public JSONArray getChildrenAsJSON(String nodeURI) throws Exception {
239 WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
240 Collection<UserNode> children = null;
241
242 UserPortal userPortal = Util.getPortalRequestContext().getUserPortalConfig().getUserPortal();
243
244
245 UserNodeFilterConfig.Builder filterConfigBuilder = UserNodeFilterConfig.builder();
246 filterConfigBuilder.withReadWriteCheck().withVisibility(Visibility.DISPLAYED, Visibility.TEMPORAL);
247 filterConfigBuilder.withTemporalCheck();
248 UserNodeFilterConfig filterConfig = filterConfigBuilder.build();
249
250
251 UserNavigation userNav = userPortal.getNavigation(Util.getUIPortal().getSiteKey());
252 UserNode userNode = userPortal.resolvePath(userNav, filterConfig, nodeURI);
253
254 if (userNode != null) {
255 userPortal.updateNode(userNode, NavigationUtils.ECMS_NAVIGATION_SCOPE, null);
256 children = userNode.getChildren();
257 }
258
259
260 JSONArray jsChildren = new JSONArray();
261 if (children == null) {
262 return null;
263 }
264 MimeResponse res = context.getResponse();
265 for (UserNode child : children) {
266 jsChildren.put(toJSON(child, res));
267 }
268 return jsChildren;
269 }
270
271 private JSONObject toJSON(UserNode node, MimeResponse res) throws Exception {
272 JSONObject json = new JSONObject();
273 String nodeId = node.getId();
274
275 json.put("label", node.getEncodedResolvedLabel());
276 json.put("hasChild", node.getChildrenCount() > 0);
277
278 UserNode selectedNode = Util.getUIPortal().getNavPath();
279 json.put("isSelected", nodeId.equals(selectedNode.getId()));
280 json.put("icon", node.getIcon());
281 String nodeURI = "";
282 if(node.getPageRef() != null){
283 nodeURI = node.getURI();
284 }
285 json.put("uri", nodeURI);
286
287 ResourceURL rsURL = res.createResourceURL();
288 rsURL.setResourceID(res.encodeURL(node.getURI()));
289 json.put("getNodeURL", rsURL.toString());
290
291 JSONArray jsonChildren = new JSONArray();
292 for (UserNode child : node.getChildren()) {
293 jsonChildren.put(toJSON(child, res));
294 }
295 json.put("childs", jsonChildren);
296 return json;
297 }
298
299 public String getNavigationScope() throws Exception {
300 PortletPreferences preferences = ((PortletRequestContext)WebuiRequestContext.getCurrentInstance()).
301 getRequest().getPreferences();
302 String navigationScope = preferences.getValue(NAVIGATION_SCOPE, NAVIGATION_SCOPE_CHILDREN);
303 return navigationScope;
304 }
305
306 public String getNavigation() throws Exception {
307 String userName = ConversationState.getCurrent().getIdentity().getUserId();
308 String portalName = Util.getPortalRequestContext().getPortalOwner();
309
310 PortletPreferences preferences = ((PortletRequestContext)WebuiRequestContext.getCurrentInstance()).
311 getRequest().getPreferences();
312 String navigationScope = preferences.getValue(NAVIGATION_SCOPE, NAVIGATION_SCOPE_CHILDREN);
313 Scope scope = Scope.CHILDREN;
314 switch (navigationScope) {
315 case NAVIGATION_SCOPE_SINGLE: scope = Scope.SINGLE; break;
316 case NAVIGATION_SCOPE_CHILDREN: scope = Scope.CHILDREN; break;
317 case NAVIGATION_SCOPE_GRAND_CHILDREN: scope = Scope.GRANDCHILDREN; break;
318 case NAVIGATION_SCOPE_ALL: scope = Scope.ALL; break;
319 }
320 return NavigationUtils.getNavigationAsJSON(portalName, userName, scope, navigationScope);
321 }
322 }