View Javadoc
1   /*
2   * Copyright (C) 2003-2010 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  
18  package org.exoplatform.social.plugin.doc;
19  
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.jcr.Node;
25  import javax.jcr.RepositoryException;
26  
27  import org.exoplatform.ecm.resolver.JCRResourceResolver;
28  import org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation;
29  import org.exoplatform.ecm.webui.utils.Utils;
30  import org.exoplatform.portal.webui.util.Util;
31  import org.exoplatform.resolver.ResourceResolver;
32  import org.exoplatform.services.cms.impl.DMSConfiguration;
33  import org.exoplatform.services.cms.templates.TemplateService;
34  import org.exoplatform.services.log.ExoLogger;
35  import org.exoplatform.services.log.Log;
36  import org.exoplatform.services.wcm.core.NodeLocation;
37  import org.exoplatform.social.webui.activity.UIActivitiesContainer;
38  import org.exoplatform.social.webui.composer.PopupContainer;
39  import org.exoplatform.web.application.JavascriptManager;
40  import org.exoplatform.webui.application.WebuiRequestContext;
41  import org.exoplatform.webui.config.annotation.ComponentConfig;
42  import org.exoplatform.webui.config.annotation.EventConfig;
43  import org.exoplatform.webui.core.UIComponent;
44  import org.exoplatform.webui.core.UIContainer;
45  import org.exoplatform.webui.core.UIPopupContainer;
46  import org.exoplatform.webui.core.UIPortletApplication;
47  import org.exoplatform.webui.core.lifecycle.Lifecycle;
48  import org.exoplatform.webui.event.Event;
49  import org.exoplatform.webui.event.EventListener;
50  import org.exoplatform.webui.ext.UIExtension;
51  import org.exoplatform.webui.ext.UIExtensionManager;
52  
53  
54  /**
55   * UIDocViewer <p></p>
56   *
57   * @author Zuanoc
58   * @since Aug 10, 2010
59   */
60  
61  @ComponentConfig(
62    lifecycle = Lifecycle.class,
63    events = {
64      @EventConfig(listeners = UIDocViewer.DownloadActionListener.class),
65      @EventConfig(listeners = UIBaseNodePresentation.OpenDocInDesktopActionListener.class)    
66    }
67  )
68  public class UIDocViewer extends UIBaseNodePresentation {
69  
70    private static final String UIDocViewerPopup = "UIDocViewerPopup";
71    
72    /**
73     * The logger.
74     */
75    private static final Log LOG = ExoLogger.getLogger(UIDocViewer.class);
76    protected Node originalNode;
77    public String docPath;
78    public String repository;
79    public String workspace;
80  
81    /**
82     * Sets the original node.
83     *
84     * @param originalNode
85     */
86    public void setOriginalNode(Node originalNode) {
87      this.originalNode = originalNode;
88    }
89  
90    /**
91     * Gets the original node.
92     *
93     * @return
94     * @throws Exception
95     */
96    public Node getOriginalNode() throws Exception {
97  //    return originalNode;
98      return getDocNode();
99    }
100 
101   /**
102    * Sets the node.
103    *
104    * @param node
105    */
106   public void setNode(Node node) {
107     originalNode = node;
108   }
109 
110   @Override
111   public Node getNode() throws Exception {
112 //    return originalNode;
113     return getDocNode();
114   }
115 
116   public String getTemplate() {
117     Node docNode = getDocNode();
118   	if(docNode == null) return null;
119     TemplateService templateService = getApplicationComponent(TemplateService.class);
120     String userName = Util.getPortalRequestContext().getRemoteUser() ;
121     try {
122       if (docNode.isNodeType("nt:frozenNode")) {
123         String uuid = docNode.getProperty("jcr:frozenUuid").getString();
124         docNode = docNode.getSession().getNodeByUUID(uuid);
125       }
126       String nodeType = docNode.getPrimaryNodeType().getName();
127       if(templateService.isManagedNodeType(nodeType)) {
128         return templateService.getTemplatePathByUser(false, nodeType, userName);
129       }
130     }catch (RepositoryException re){
131       if (LOG.isDebugEnabled() || LOG.isWarnEnabled())
132         LOG.error("Get template catch RepositoryException: ", re);      
133     }
134     catch (Exception e) { //TemplateService
135       LOG.warn(e.getMessage(), e);
136     }
137     
138     return null;
139   }
140 
141   /* (non-Javadoc)
142    * @see org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation#getTemplatePath()
143    */
144   public String getTemplatePath() throws Exception {
145     return getRepository();
146   }
147 
148   public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context, String template) {
149     DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
150     String workspace = dmsConfiguration.getConfig().getSystemWorkspace();
151     return new JCRResourceResolver(workspace);
152   }
153 
154   public String getNodeType() {
155     return null;
156   }
157 
158   public boolean isNodeTypeSupported() {
159     return false;
160   }
161 
162   public UIComponent getCommentComponent() {
163     return null;
164   }
165 
166   public UIComponent getRemoveAttach() {
167     return null;
168   }
169 
170   public UIComponent getRemoveComment() {
171     return null;
172   }
173 
174   public UIComponent getUIComponent(String mimeType) throws Exception {
175     UIExtensionManager manager = getApplicationComponent(UIExtensionManager.class);
176     List<UIExtension> extensions = manager.getUIExtensions(Utils.FILE_VIEWER_EXTENSION_TYPE);
177     Map<String, Object> context = new HashMap<String, Object>();
178     context.put(Utils.MIME_TYPE, mimeType);
179     for (UIExtension extension : extensions) {
180       UIComponent uiComponent = manager.addUIExtension(extension, context, this);
181       if (uiComponent != null) {
182         return uiComponent;
183       }
184     }
185     return null;
186   }
187 
188   public String getRepositoryName() {
189     return UIDocActivityComposer.REPOSITORY;
190   }
191   
192   static  public class DownloadActionListener extends EventListener<UIDocViewer> {
193     public void execute(Event<UIDocViewer> event) throws Exception {
194       UIDocViewer uiComp = event.getSource();
195       String downloadLink = uiComp.getDownloadLink(org.exoplatform.wcm.webui.Utils.getFileLangNode(uiComp.getDocNode()));
196       JavascriptManager jsManager = event.getRequestContext().getJavascriptManager();
197       downloadLink = downloadLink.replaceAll("&amp;", "&") ;           
198       jsManager.addJavascript("window.location.href = " + downloadLink + ";");
199     }
200   }
201   
202   private Node getDocNode() {
203     NodeLocation nodeLocation = new NodeLocation(repository, workspace, docPath);
204     return NodeLocation.getNodeByLocation(nodeLocation);
205   }
206 
207   @Override
208   public UIPopupContainer getPopupContainer() throws Exception {
209     UIPopupContainer pContainer1 = getAncestorOfType(UIPopupContainer.class); 
210     UIPopupContainer pContainer2 = pContainer1.getChildById(UIDocViewerPopup);
211     if (pContainer2 == null) {
212       pContainer2 = pContainer1.addChild(UIPopupContainer.class, null, UIDocViewerPopup);
213     }
214     return pContainer2;
215   }
216   
217 }