View Javadoc
1   package org.exoplatform.wcm.ext.component.activity;
2   
3   
4   import org.exoplatform.commons.utils.CommonsUtils;
5   import org.exoplatform.ecm.webui.utils.Utils;
6   import org.exoplatform.services.cms.documents.DocumentService;
7   import org.exoplatform.social.plugin.doc.UIDocViewer;
8   import org.exoplatform.social.webui.activity.BaseUIActivity;
9   import org.exoplatform.webui.application.WebuiRequestContext;
10  import org.exoplatform.webui.config.annotation.ComponentConfig;
11  import org.exoplatform.webui.config.annotation.EventConfig;
12  import org.exoplatform.webui.core.UIContainer;
13  import org.exoplatform.webui.core.UIPopupContainer;
14  import org.exoplatform.webui.core.UIPopupWindow;
15  import org.exoplatform.webui.event.Event;
16  import org.exoplatform.webui.event.EventListener;
17  import org.exoplatform.webui.ext.UIExtension;
18  import org.exoplatform.webui.ext.UIExtensionManager;
19  
20  import javax.jcr.Node;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  @ComponentConfig(
26          template = "classpath:groovy/ecm/social-integration/UIDocumentPreview.gtmpl",
27          events = {
28                  @EventConfig(listeners = UIDocumentPreview.CloseActionListener.class)
29          }
30  )
31  public class UIDocumentPreview extends UIContainer {
32  
33    private DocumentService documentService;
34  
35    private BaseUIActivity baseUIActivity;
36  
37    public UIDocumentPreview() throws Exception {
38      this.documentService = CommonsUtils.getService(DocumentService.class);
39  
40      this.addChild(UIDocViewer.class, null, "UIDocViewer");
41      this.addChild(UIPreviewCommentArea.class, null, "UIPreviewCommentArea");
42    }
43  
44    public void setContentInfo(String docPath, String repository, String workspace, Node docNode) {
45      UIDocViewer uiDocViewer = this.getChild(UIDocViewer.class);
46      uiDocViewer.docPath = docPath;
47      uiDocViewer.repository = repository;
48      uiDocViewer.workspace = workspace;
49      uiDocViewer.setOriginalNode(docNode);
50      uiDocViewer.setNode(docNode);
51    }
52  
53    public Node getOriginalNode() throws Exception {
54      UIDocViewer uiDocViewer = findFirstComponentOfType(UIDocViewer.class);
55      return uiDocViewer.getOriginalNode();
56    }
57  
58    /**
59     * Return the link of the document in the Documents application
60     * @return the link of the document in the Documents application
61     */
62    public String getLinkInDocumentsApp() throws Exception {
63      return documentService.getLinkInDocumentsApp(getOriginalNode().getPath());
64    }
65  
66    private boolean isWebContent() throws Exception {
67      UIDocViewer uiDocViewer = findFirstComponentOfType(UIDocViewer.class);
68      Node previewNode = uiDocViewer.getNode();
69      if (previewNode != null) {
70        return previewNode.isNodeType(org.exoplatform.ecm.webui.utils.Utils.EXO_WEBCONTENT);
71      }
72  
73      return false;
74    }
75  
76    /**
77     * Check if a node is media/image
78     * @param data
79     * @return
80     * @throws Exception
81     */
82    private boolean isMediaFile(Node data) throws Exception {
83      if (data.isNodeType(Utils.NT_FILE)) {
84        UIExtensionManager manager = getApplicationComponent(UIExtensionManager.class);
85        List<UIExtension> extensions = manager.getUIExtensions(Utils.FILE_VIEWER_EXTENSION_TYPE);
86  
87        Map<String, Object> context = new HashMap<String, Object>();
88        context.put(Utils.MIME_TYPE, data.getNode(Utils.JCR_CONTENT).getProperty(Utils.JCR_MIMETYPE).getString());
89  
90        for (UIExtension extension : extensions) {
91          if (manager.accept(Utils.FILE_VIEWER_EXTENSION_TYPE, extension.getName(), context)
92                  && !"Text".equals(extension.getName())
93                  && !"PDF".equals(extension.getName())) {
94            return true;
95          }
96        }
97      }
98  
99      return false;
100   }
101 
102   public BaseUIActivity getBaseUIActivity() {
103     return baseUIActivity;
104   }
105 
106   public void setBaseUIActivity(BaseUIActivity baseUIActivity) {
107     this.baseUIActivity = baseUIActivity;
108   }
109 
110   public String getEmbedHtml() {
111     BaseUIActivity baseUIActivity = this.getBaseUIActivity();
112     if (baseUIActivity instanceof UILinkActivity) {
113       String embedHtml = ((UILinkActivity) baseUIActivity).getEmbedHtml();
114       if (embedHtml != null) {
115         embedHtml = embedHtml.replaceFirst("width=\\\"[0-9]*\\\"","width=\"100%\"")
116                 .replaceFirst("height=\\\"[0-9]*\\\"","height=\"100%\"");
117       }
118       return embedHtml;
119     }
120 
121     return null;
122   }
123 
124   public static class CloseActionListener extends EventListener<UIDocumentPreview> {
125     public void execute(Event<UIDocumentPreview> event) throws Exception {
126       UIDocumentPreview uiDocumentPreview = event.getSource();
127       UIPopupWindow uiPopupWindow = uiDocumentPreview.getAncestorOfType(UIPopupWindow.class);
128       if (!uiPopupWindow.isShow())
129         return;
130       uiPopupWindow.setShow(false);
131       uiPopupWindow.setUIComponent(null);
132       UIPopupContainer popupContainer = uiPopupWindow.getAncestorOfType(UIPopupContainer.class);
133       WebuiRequestContext requestContext = event.getRequestContext();
134       if(uiDocumentPreview.getBaseUIActivity() != null
135               && requestContext.getUIApplication().findComponentById(uiDocumentPreview.getBaseUIActivity().getId()) != null) {
136         requestContext.addUIComponentToUpdateByAjax(uiDocumentPreview.getBaseUIActivity());
137       }
138       requestContext.addUIComponentToUpdateByAjax(popupContainer);
139     }
140   }
141 }