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.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import javax.jcr.Node;
24 import javax.jcr.NodeIterator;
25 import javax.jcr.RepositoryException;
26 import javax.portlet.PortletPreferences;
27 import javax.portlet.PortletRequest;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.exoplatform.ecm.resolver.JCRResourceResolver;
31 import org.exoplatform.ecm.webui.presentation.AbstractActionComponent;
32 import org.exoplatform.ecm.webui.presentation.NodePresentation;
33 import org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation;
34 import org.exoplatform.ecm.webui.presentation.removeattach.RemoveAttachmentComponent;
35 import org.exoplatform.ecm.webui.presentation.removecomment.RemoveCommentComponent;
36 import org.exoplatform.portal.mop.SiteType;
37 import org.exoplatform.portal.webui.util.Util;
38 import org.exoplatform.resolver.ResourceResolver;
39 import org.exoplatform.services.cms.impl.DMSConfiguration;
40 import org.exoplatform.services.cms.templates.TemplateService;
41 import org.exoplatform.services.log.ExoLogger;
42 import org.exoplatform.services.log.Log;
43 import org.exoplatform.services.wcm.core.NodeLocation;
44 import org.exoplatform.services.wcm.core.NodetypeConstant;
45 import org.exoplatform.services.wcm.friendly.FriendlyService;
46 import org.exoplatform.services.wcm.publication.WCMComposer;
47 import org.exoplatform.wcm.webui.Utils;
48 import org.exoplatform.web.application.Parameter;
49 import org.exoplatform.web.application.RequireJS;
50 import org.exoplatform.web.url.navigation.NavigationResource;
51 import org.exoplatform.web.url.navigation.NodeURL;
52 import org.exoplatform.webui.application.WebuiRequestContext;
53 import org.exoplatform.webui.application.portlet.PortletRequestContext;
54 import org.exoplatform.webui.config.annotation.ComponentConfig;
55 import org.exoplatform.webui.config.annotation.EventConfig;
56 import org.exoplatform.webui.core.UIComponent;
57 import org.exoplatform.webui.core.UIPopupContainer;
58 import org.exoplatform.webui.core.UIPortletApplication;
59 import org.exoplatform.webui.core.lifecycle.Lifecycle;
60 import org.exoplatform.webui.event.Event;
61 import org.exoplatform.webui.event.EventListener;
62
63
64
65
66
67
68
69 @ComponentConfig(
70 lifecycle = Lifecycle.class,
71 events = {
72 @EventConfig(listeners = UIPresentation.DownloadActionListener.class),
73 @EventConfig(listeners = UIPresentation.SwitchToAudioDescriptionActionListener.class),
74 @EventConfig(listeners = UIPresentation.SwitchToOriginalActionListener.class),
75 @EventConfig(listeners = UIBaseNodePresentation.OpenDocInDesktopActionListener.class)
76 }
77 )
78
79 public class UIPresentation extends UIBaseNodePresentation {
80
81 private static final Log LOG = ExoLogger.getLogger(UIPresentation.class.getName());
82
83 private NodeLocation originalNodeLocation;
84
85 private NodeLocation viewNodeLocation;
86
87 String templatePath = null;
88
89
90
91
92 public Node getOriginalNode() throws Exception {
93 return originalNodeLocation == null ? null :
94 Utils.getViewableNodeByComposer(originalNodeLocation.getRepository(),
95 originalNodeLocation.getWorkspace(),
96 originalNodeLocation.getPath(),
97 WCMComposer.BASE_VERSION);
98 }
99
100
101
102
103 public void setOriginalNode(Node node) throws Exception{
104 originalNodeLocation = NodeLocation.getNodeLocationByNode(node);
105 }
106
107
108
109
110 public Node getNode() throws Exception {
111 Node ret = getDisplayNode();
112 if (NodePresentation.MEDIA_STATE_DISPLAY.equals(getMediaState()) &&
113 (ret.isNodeType(NodetypeConstant.EXO_ACCESSIBLE_MEDIA) ||
114 (ret.isNodeType(NodetypeConstant.NT_FROZEN_NODE) &&
115 NodetypeConstant.EXO_ACCESSIBLE_MEDIA.equals(ret.getProperty("jcr:frozenPrimaryType").getString())))) {
116 Node audioDescription = org.exoplatform.services.cms.impl.Utils.getChildOfType(ret, NodetypeConstant.EXO_AUDIO_DESCRIPTION);
117 if (audioDescription != null) {
118 return audioDescription;
119 }
120 }
121 return ret;
122 }
123
124 public String getFastPublicLink(Node viewNode) {
125 String fastPublishLink = null;
126 try {
127 UIPresentationContainer container = (UIPresentationContainer)this.getParent();
128 fastPublishLink = container.event("FastPublish", NodeLocation.getExpressionByNode(viewNode));
129 } catch (Exception e) {
130 if (LOG.isWarnEnabled()) {
131 LOG.warn(e.getMessage());
132 }
133 }
134 return fastPublishLink;
135 }
136
137 public Node getDisplayNode() throws Exception {
138 if (viewNodeLocation == null) return null;
139 PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
140 PortletPreferences preferences = portletRequestContext.getRequest().getPreferences();
141 String sharedCache = preferences.getValue(UISingleContentViewerPortlet.ENABLE_CACHE, "true");
142 sharedCache = "true".equals(sharedCache) ? WCMComposer.VISIBILITY_PUBLIC:WCMComposer.VISIBILITY_USER;
143 Node ret = Utils.getViewableNodeByComposer(viewNodeLocation.getRepository(),
144 viewNodeLocation.getWorkspace(),
145 viewNodeLocation.getPath(),
146 null,
147 sharedCache);
148 return ret;
149 }
150
151
152
153
154
155 public void setNode(Node node) {
156 viewNodeLocation = NodeLocation.getNodeLocationByNode(node);
157 }
158
159
160
161
162 public String getRepositoryName() {
163 PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
164 PortletPreferences portletPreferences = portletRequestContext.getRequest().getPreferences();
165 return originalNodeLocation != null ? originalNodeLocation.getRepository()
166 : portletPreferences.getValue(UISingleContentViewerPortlet.REPOSITORY,
167 "repository");
168 }
169
170
171
172
173 public String getTemplate() {
174 return templatePath;
175 }
176
177
178
179
180 public String getTemplatePath() throws Exception {
181 return templatePath;
182 }
183
184 public void setTemplatePath(String templatePath) {
185 this.templatePath = templatePath;
186 }
187
188
189
190
191
192
193
194 public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context, String template) {
195 DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
196 String workspace = dmsConfiguration.getConfig().getSystemWorkspace();
197 return new JCRResourceResolver(workspace);
198 }
199
200 public ResourceResolver getTemplateResourceResolver() {
201 DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
202 String workspace = dmsConfiguration.getConfig().getSystemWorkspace();
203 return new JCRResourceResolver(workspace);
204 }
205
206
207
208
209 public String getNodeType() throws Exception {
210 return null;
211 }
212
213
214
215
216 public boolean isNodeTypeSupported() {
217 return false;
218 }
219
220
221
222
223
224
225 public boolean isFastPublishLink() { return true ; }
226
227 public String getFastPublishLink() throws Exception {
228 UIPresentationContainer container = (UIPresentationContainer)getParent();
229 return container.event("FastPublish");
230 }
231
232
233
234
235 public UIComponent getCommentComponent() {
236 return null;
237 }
238
239
240
241
242 public UIComponent getRemoveAttach() throws Exception {
243 removeChild(RemoveAttachmentComponent.class);
244 UIComponent uicomponent = addChild(RemoveAttachmentComponent.class, null, "PresentationRemoveAttach");
245 ((AbstractActionComponent) uicomponent).setLstComponentupdate(Arrays.asList(new Class[] { UIPresentationContainer.class }));
246 return uicomponent;
247 }
248
249
250
251
252 public UIComponent getRemoveComment() throws Exception {
253 removeChild(RemoveCommentComponent.class);
254 UIComponent uicomponent = addChild(RemoveCommentComponent.class, null, "PresentationRemoveComment");
255 ((AbstractActionComponent)uicomponent).setLstComponentupdate(Arrays.asList(new Class[] {UIPresentationContainer.class}));
256 return uicomponent;
257 }
258
259 public UIComponent getUIComponent(String mimeType) throws Exception {
260 return org.exoplatform.ecm.webui.utils.Utils.getUIComponent(mimeType, this);
261 }
262
263
264
265
266
267
268
269
270 public String getViewableLink(Node node, Parameter[] params) throws Exception {
271 PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
272 PortletRequest portletRequest = portletRequestContext.getRequest();
273 NodeLocation nodeLocation = NodeLocation.getNodeLocationByNode(node);
274 String baseURI = portletRequest.getScheme() + "://" + portletRequest.getServerName() + ":"
275 + String.format("%s", portletRequest.getServerPort());
276 String basePath = Utils.getPortletPreference(UISingleContentViewerPortlet.PREFERENCE_TARGET_PAGE);
277 String scvWith = Utils.getPortletPreference(UISingleContentViewerPortlet.PREFERENCE_SHOW_SCV_WITH);
278 if (scvWith == null || scvWith.length() == 0)
279 scvWith = UISingleContentViewerPortlet.DEFAULT_SHOW_SCV_WITH;
280
281 StringBuffer param = new StringBuffer();
282 param.append("/")
283 .append(nodeLocation.getRepository())
284 .append("/")
285 .append(nodeLocation.getWorkspace());
286
287 if (node.isNodeType("nt:frozenNode")) {
288 String uuid = node.getProperty("jcr:frozenUuid").getString();
289 Node originalNode = node.getSession().getNodeByUUID(uuid);
290 param.append(originalNode.getPath());
291 } else {
292 param.append(node.getPath());
293 }
294
295 NodeURL nodeURL = Util.getPortalRequestContext().createURL(NodeURL.TYPE);
296 NavigationResource resource = new NavigationResource(SiteType.PORTAL,
297 Util.getPortalRequestContext()
298 .getPortalOwner(), basePath);
299 nodeURL.setResource(resource).setQueryParameterValue(scvWith, param.toString());
300 String link = baseURI + nodeURL.toString();
301
302 FriendlyService friendlyService = getApplicationComponent(FriendlyService.class);
303 link = friendlyService.getFriendlyUri(link);
304
305 return link;
306 }
307
308
309
310
311
312
313
314 public List<Node> getAttachments() throws Exception {
315 List<Node> attachments = new ArrayList<Node>() ;
316 Node parent = getOriginalNode();
317 NodeIterator childrenIterator = parent.getNodes();;
318 TemplateService templateService = getApplicationComponent(TemplateService.class) ;
319 while (childrenIterator.hasNext()) {
320 Node childNode = childrenIterator.nextNode();
321 String nodeType = childNode.getPrimaryNodeType().getName();
322 List<String> listCanCreateNodeType = org.exoplatform.ecm.webui.utils.Utils.getListAllowedFileType(parent,
323 templateService);
324 if (listCanCreateNodeType.contains(nodeType)) attachments.add(childNode);
325 }
326 return attachments;
327 }
328
329 @Override
330 public boolean isDisplayAlternativeText() {
331 try {
332 Node node = this.getNode();
333 return ( node.isNodeType(NodetypeConstant.EXO_ACCESSIBLE_MEDIA) ||
334 (node.isNodeType(NodetypeConstant.NT_FROZEN_NODE) &&
335 NodetypeConstant.EXO_ACCESSIBLE_MEDIA.equals(node.getProperty("jcr:frozenPrimaryType").getString()))) &&
336 node.hasProperty(NodetypeConstant.EXO_ALTERNATIVE_TEXT) &&
337 StringUtils.isNotEmpty(node.getProperty(NodetypeConstant.EXO_ALTERNATIVE_TEXT).getString());
338 } catch (Exception e) { return false; }
339 }
340
341 @Override
342 public boolean playAudioDescription() {
343 try {
344 Node node = this.getNode();
345 return ( node.isNodeType(NodetypeConstant.EXO_ACCESSIBLE_MEDIA) ||
346 (node.isNodeType(NodetypeConstant.NT_FROZEN_NODE) &&
347 NodetypeConstant.EXO_ACCESSIBLE_MEDIA.equals(node.getProperty("jcr:frozenPrimaryType").getString()))) &&
348 org.exoplatform.services.cms.impl.Utils.hasChild(node, NodetypeConstant.EXO_AUDIO_DESCRIPTION);
349 } catch (Exception e) { return false; }
350 }
351
352 @Override
353 public boolean switchBackAudioDescription() {
354 try {
355 Node node = this.getNode();
356 Node parent = node.getParent();
357 return node.isNodeType(NodetypeConstant.EXO_AUDIO_DESCRIPTION) &&
358 ( parent.isNodeType(NodetypeConstant.EXO_ACCESSIBLE_MEDIA) ||
359 (parent.isNodeType(NodetypeConstant.NT_FROZEN_NODE) &&
360 NodetypeConstant.EXO_ACCESSIBLE_MEDIA.equals(parent.getProperty("jcr:frozenPrimaryType").getString())));
361 } catch (Exception e) { return false; }
362 }
363
364 @Override
365 public UIPopupContainer getPopupContainer() throws Exception {
366 return this.getAncestorOfType(UIPortletApplication.class).getChild(UIPopupContainer.class);
367 }
368
369 static public class DownloadActionListener extends EventListener<UIPresentation> {
370 public void execute(Event<UIPresentation> event) throws Exception {
371 UIPresentation uiComp = event.getSource();
372 try {
373 String downloadLink = Utils.getDownloadLink(Utils.getFileLangNode(uiComp.getNode()));
374 RequireJS requireJS = event.getRequestContext().getJavascriptManager().getRequireJS();
375 requireJS.require("SHARED/ecm-utils", "ecmutil").addScripts("ecmutil.ECMUtils.ajaxRedirect('" + downloadLink + "');");
376 } catch(RepositoryException e) {
377 if (LOG.isErrorEnabled()) {
378 LOG.error("Repository cannot be found", e);
379 }
380 }
381 }
382 }
383
384 static public class SwitchToAudioDescriptionActionListener extends EventListener<UIPresentation> {
385 public void execute(Event<UIPresentation> event) throws Exception {
386 UIPresentation uiPresentation = event.getSource();
387 UIPresentationContainer uiContainer = uiPresentation.getAncestorOfType(UIPresentationContainer.class);
388 uiPresentation.switchMediaState();
389 event.getRequestContext().addUIComponentToUpdateByAjax(uiContainer);
390 }
391 }
392
393 static public class SwitchToOriginalActionListener extends EventListener<UIPresentation> {
394 public void execute(Event<UIPresentation> event) throws Exception {
395 UIPresentation uiPresentation = event.getSource();
396 UIPresentationContainer uiContainer = uiPresentation.getAncestorOfType(UIPresentationContainer.class);
397 uiPresentation.switchMediaState();
398 event.getRequestContext().addUIComponentToUpdateByAjax(uiContainer);
399 }
400 }
401
402 }