View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
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   * Created by The eXo Platform SAS
65   * Author : DANG TAN DUNG
66   * dzungdev@gmail.com
67   * Jun 9, 2008
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    /* (non-Javadoc)
90     * @see org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation#getOriginalNode()
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   /* (non-Javadoc)
101    * @see org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation#getNode()
102    */
103   public void setOriginalNode(Node node) throws Exception{
104     originalNodeLocation = NodeLocation.getNodeLocationByNode(node);
105   }
106 
107   /* (non-Javadoc)
108    * @see org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation#getNode()
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   /* (non-Javadoc)
153    * @see org.exoplatform.ecm.webui.presentation.NodePresentation#setNode(javax.jcr.Node)
154    */
155   public void setNode(Node node) {
156     viewNodeLocation = NodeLocation.getNodeLocationByNode(node);
157   }
158 
159   /* (non-Javadoc)
160    * @see org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation#getRepositoryName()
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   /* (non-Javadoc)
171    * @see org.exoplatform.portal.webui.portal.UIPortalComponent#getTemplate()
172    */
173   public String getTemplate() {
174     return templatePath;
175   }
176 
177   /* (non-Javadoc)
178    * @see org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation#getTemplatePath()
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    * (non-Javadoc)
190    * @see
191    * org.exoplatform.webui.core.UIComponent#getTemplateResourceResolver(org.
192    * exoplatform.webui.application.WebuiRequestContext, java.lang.String)
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   /* (non-Javadoc)
207    * @see org.exoplatform.ecm.webui.presentation.NodePresentation#getNodeType()
208    */
209   public String getNodeType() throws Exception {
210     return null;
211   }
212 
213   /* (non-Javadoc)
214    * @see org.exoplatform.ecm.webui.presentation.NodePresentation#isNodeTypeSupported()
215    */
216   public boolean isNodeTypeSupported() {
217     return false;
218   }
219   
220   /**
221    * Checks if allow render fast publish link for the inline editting
222    *
223    * @return true, if need to render fast publish link
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   /* (non-Javadoc)
233    * @see org.exoplatform.ecm.webui.presentation.NodePresentation#getCommentComponent()
234    */
235   public UIComponent getCommentComponent() {
236     return null;
237   }
238 
239   /* (non-Javadoc)
240    * @see org.exoplatform.ecm.webui.presentation.NodePresentation#getCommentComponent()
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   /* (non-Javadoc)
250    * @see org.exoplatform.ecm.webui.presentation.NodePresentation#getCommentComponent()
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    * Gets the viewable link (attachment link, relation document link)
265    *
266    * @param node the node
267    * @return the attachment URL
268    * @throws Exception the exception
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    * Gets the attachment nodes.
310    *
311    * @return the attachment Nodes
312    * @throws Exception the exception
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 }