View Javadoc
1   /*
2    * Copyright (C) 2003-2007 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.ecm.webui.component.explorer.versions;
18  
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import javax.jcr.Node;
25  import javax.jcr.NodeIterator;
26  import javax.jcr.RepositoryException;
27  import javax.jcr.Value;
28  
29  import org.exoplatform.container.xml.PortalContainerInfo;
30  import org.exoplatform.download.DownloadService;
31  import org.exoplatform.download.InputStreamDownloadResource;
32  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
33  import org.exoplatform.ecm.webui.component.explorer.control.UIActionBar;
34  import org.exoplatform.ecm.webui.presentation.AbstractActionComponent;
35  import org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation;
36  import org.exoplatform.ecm.webui.presentation.removeattach.RemoveAttachmentComponent;
37  import org.exoplatform.ecm.webui.presentation.removecomment.RemoveCommentComponent;
38  import org.exoplatform.ecm.webui.utils.Utils;
39  import org.exoplatform.portal.webui.util.Util;
40  import org.exoplatform.resolver.ResourceResolver;
41  import org.exoplatform.services.cms.comments.CommentsService;
42  import org.exoplatform.services.cms.i18n.MultiLanguageService;
43  import org.exoplatform.services.cms.templates.TemplateService;
44  import org.exoplatform.services.jcr.core.ManageableRepository;
45  import org.exoplatform.services.log.ExoLogger;
46  import org.exoplatform.services.log.Log;
47  import org.exoplatform.services.wcm.core.NodeLocation;
48  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
49  import org.exoplatform.web.application.ApplicationMessage;
50  import org.exoplatform.web.application.Parameter;
51  import org.exoplatform.web.application.RequireJS;
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.UIApplication;
57  import org.exoplatform.webui.core.UIComponent;
58  import org.exoplatform.webui.core.UIPopupWindow;
59  import org.exoplatform.webui.event.Event;
60  import org.exoplatform.webui.event.EventListener;
61  
62  /**
63   * Created by The eXo Platform SARL
64   * Author : lxchiati
65   *          lebienthuy@gmail.com
66   * Oct 19, 2006
67   * 10:07:15 AM
68   */
69  
70  @ComponentConfig(
71      type     = UIViewVersion.class,
72      template = "system:/groovy/webui/core/UITabPane.gtmpl",
73      events = {
74        @EventConfig(listeners = UIViewVersion.ChangeLanguageActionListener.class),
75        @EventConfig(listeners = UIViewVersion.ChangeNodeActionListener.class),
76        @EventConfig(listeners = UIViewVersion.DownloadActionListener.class)
77      }
78  )
79  
80  public class UIViewVersion extends UIBaseNodePresentation {
81    private NodeLocation node_ ;
82    protected NodeLocation originalNode_ ;
83    private String language_ ;
84    private static final Log LOG  = ExoLogger.getLogger(UIViewVersion.class.getName());
85    final private static String COMMENT_COMPONENT = "Comment";
86  
87    public UIViewVersion() throws Exception {
88      addChild(UINodeInfo.class, null, null) ;
89      addChild(UINodeProperty.class, null, null).setRendered(false) ;
90    }
91  
92    public String getTemplate() {
93      TemplateService templateService = getApplicationComponent(TemplateService.class);
94      String userName = Util.getPortalRequestContext().getRemoteUser() ;
95      try {
96        Node node = getAncestorOfType(UIJCRExplorer.class).getCurrentNode() ;
97        originalNode_ = NodeLocation.getNodeLocationByNode(node);
98        String nodeType = node.getPrimaryNodeType().getName();
99        if(isNodeTypeSupported(node)) return templateService.getTemplatePathByUser(false, nodeType, userName) ;
100     } catch (Exception e) {
101       if (LOG.isWarnEnabled()) {
102         LOG.warn(e.getMessage());
103       }
104     }
105     return null ;
106   }
107 
108   public UIComponent getCommentComponent() {
109     UIJCRExplorer uiExplorer = getAncestorOfType(UIJCRExplorer.class);
110     UIActionBar uiActionBar = uiExplorer.findFirstComponentOfType(UIActionBar.class);
111     UIComponent uicomponent = uiActionBar.getUIAction(COMMENT_COMPONENT);
112     return (uicomponent != null ? uicomponent : this);
113   }
114 
115   public UIComponent getRemoveAttach() throws Exception {
116     removeChild(RemoveAttachmentComponent.class);
117     UIComponent uicomponent = addChild(RemoveAttachmentComponent.class, null, "UIViewVersionRemoveAttach");
118     ((AbstractActionComponent) uicomponent).setLstComponentupdate(Arrays.asList(new Class[] {UIPopupWindow.class}));
119     return uicomponent;
120   }
121 
122   public UIComponent getRemoveComment() throws Exception {
123     removeChild(RemoveCommentComponent.class);
124     UIComponent uicomponent = addChild(RemoveCommentComponent.class, null, "UIViewVersionRemoveComment");
125     ((AbstractActionComponent) uicomponent).setLstComponentupdate(Arrays.asList(new Class[] {UIPopupWindow.class}));
126     return uicomponent;
127   }
128 
129 
130   public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context, String template) {
131     return getAncestorOfType(UIJCRExplorer.class).getJCRTemplateResourceResolver() ;
132   }
133 
134   public boolean isNodeTypeSupported(Node node) {
135     try {
136       TemplateService templateService = getApplicationComponent(TemplateService.class) ;
137       String nodeTypeName = node.getPrimaryNodeType().getName();
138       return templateService.isManagedNodeType(nodeTypeName);
139     } catch (Exception e) {
140       return false;
141     }
142   }
143 
144   public Node getNode() throws RepositoryException {
145     return NodeLocation.getNodeByLocation(node_);
146   }
147 
148   public Node getOriginalNode() throws Exception {
149     return  NodeLocation.getNodeByLocation(originalNode_);
150   }
151 
152   public void setNode(Node node) {
153     node_ = NodeLocation.getNodeLocationByNode(node);
154   }
155 
156   public Node getNodeByUUID(String uuid) {
157     ManageableRepository manageRepo = WCMCoreUtils.getRepository();
158     String[] workspaces = manageRepo.getWorkspaceNames() ;
159     for(String ws : workspaces) {
160       try{
161         return WCMCoreUtils.getSystemSessionProvider().getSession(ws, manageRepo).getNodeByUUID(uuid) ;
162       } catch(Exception e) {
163         continue;
164       }
165     }
166     return null;
167   }
168 
169   public List<Node> getRelations() throws Exception {
170     List<Node> relations = new ArrayList<Node>() ;
171     Node node = getNode();
172     if (node.hasProperty(Utils.EXO_RELATION)) {
173       Value[] vals = node.getProperty(Utils.EXO_RELATION).getValues();
174       for (int i = 0; i < vals.length; i++) {
175         String uuid = vals[i].getString();
176         Node nodeToAdd = getNodeByUUID(uuid);
177         relations.add(nodeToAdd);
178       }
179     }
180     return relations;
181   }
182 
183   public List<Node> getAttachments() throws Exception {
184     List<Node> attachments = new ArrayList<Node>() ;
185     Node node = getNode();
186     NodeIterator childrenIterator = node.getNodes();
187     TemplateService templateService = getApplicationComponent(TemplateService.class) ;
188     int attachData = 0 ;
189     while(childrenIterator.hasNext()) {
190       Node childNode = childrenIterator.nextNode();
191       String nodeType = childNode.getPrimaryNodeType().getName();
192       List<String> listCanCreateNodeType =
193         Utils.getListAllowedFileType(node, templateService) ;
194       if(listCanCreateNodeType.contains(nodeType)) {
195 
196         // Case of childNode has jcr:data property
197         if (childNode.hasProperty(Utils.JCR_DATA)) {
198           attachData = childNode.getProperty(Utils.JCR_DATA).getStream().available();
199 
200           // Case of jcr:data has content
201           if (attachData > 0)
202             attachments.add(childNode);
203         } else {
204           attachments.add(childNode);
205         }
206       }
207     }
208     return attachments;
209   }
210 
211   public String getViewableLink(Node attNode, Parameter[] params)
212       throws Exception {
213     return this.event("ChangeNode", Utils.formatNodeName(attNode.getPath()), params);
214   }
215 
216   public String getIcons(Node node, String type) throws Exception {
217     return Utils.getNodeTypeIcon(node, type) ;
218   }
219   public boolean hasPropertyContent(Node node, String property){
220     try {
221       String value = node.getProperty(property).getString() ;
222       if(value.length() > 0) return true ;
223     } catch (Exception e) {
224       if (LOG.isErrorEnabled()) {
225         LOG.error("Unexpected error", e);
226       }
227     }
228     return false ;
229   }
230 
231   public boolean isRssLink() { return false ; }
232   public String getRssLink() { return null ; }
233 
234   public void update() throws Exception {
235     getChild(UINodeInfo.class).update();
236   }
237 
238   public List<Node> getComments() throws Exception {
239     return getApplicationComponent(CommentsService.class).getComments(getNode(), getLanguage()) ;
240   }
241 
242   @SuppressWarnings("unchecked")
243   public Object getComponentInstanceOfType(String className) {
244     Object service = null;
245     try {
246       ClassLoader loader =  Thread.currentThread().getContextClassLoader();
247       Class object = loader.loadClass(className);
248       service = getApplicationComponent(object);
249     } catch (ClassNotFoundException ex) {
250       if (LOG.isErrorEnabled()) {
251         LOG.error("Unexpected error", ex);
252       }
253     }
254     return service;
255   }
256 
257   public String getDownloadLink(Node node) throws Exception {
258     DownloadService dservice = getApplicationComponent(DownloadService.class) ;
259     InputStreamDownloadResource dresource ;
260     if(!node.getPrimaryNodeType().getName().equals(Utils.NT_FILE)) {
261       node = NodeLocation.getNodeByLocation(originalNode_);
262     }
263     Node jcrContentNode = node.getNode(Utils.JCR_CONTENT) ;
264     InputStream input = jcrContentNode.getProperty(Utils.JCR_DATA).getStream() ;
265     dresource = new InputStreamDownloadResource(input, "image") ;
266     dresource.setDownloadName(node.getName()) ;
267     return dservice.getDownloadLink(dservice.addDownloadResource(dresource)) ;
268   }
269 
270   public String getImage(Node node) throws Exception {
271     DownloadService dservice = getApplicationComponent(DownloadService.class) ;
272     InputStreamDownloadResource dresource ;
273     Node imageNode = node.getNode(Utils.EXO_IMAGE) ;
274     InputStream input = imageNode.getProperty(Utils.JCR_DATA).getStream() ;
275     dresource = new InputStreamDownloadResource(input, "image") ;
276     dresource.setDownloadName(node.getName()) ;
277     return dservice.getDownloadLink(dservice.addDownloadResource(dresource)) ;
278   }
279 
280   public void setLanguage(String language) { language_ = language ; }
281   public String getLanguage() { return language_ ; }
282 
283   public String getNodeType() throws Exception {
284     return getNode().getPrimaryNodeType().getName() ;
285   }
286 
287   public String getPortalName() {
288     PortalContainerInfo containerInfo = WCMCoreUtils.getService(PortalContainerInfo.class);
289     return containerInfo.getContainerName();
290   }
291 
292   public List<String> getSupportedLocalise() throws Exception {
293     MultiLanguageService multiLanguageService = getApplicationComponent(MultiLanguageService.class) ;
294     return multiLanguageService.getSupportedLanguages(getNode()) ;
295   }
296 
297   public String getTemplatePath() throws Exception {
298     return null;
299   }
300 
301   public String getViewTemplate(String nodeTypeName, String templateName) throws Exception {
302     TemplateService tempServ = getApplicationComponent(TemplateService.class) ;
303     return tempServ.getTemplatePath(false, nodeTypeName, templateName) ;
304   }
305 
306   public String getTemplateSkin(String nodeTypeName, String skinName) throws Exception {
307     TemplateService tempServ = getApplicationComponent(TemplateService.class) ;
308     return tempServ.getSkinPath(nodeTypeName, skinName, getLanguage()) ;
309   }
310 
311   public String getWebDAVServerPrefix() throws Exception {
312     PortletRequestContext portletRequestContext = PortletRequestContext.getCurrentInstance() ;
313     String prefixWebDAV = portletRequestContext.getRequest().getScheme() + "://" +
314     portletRequestContext.getRequest().getServerName() + ":" +
315     String.format("%s",portletRequestContext.getRequest().getServerPort()) ;
316     return prefixWebDAV ;
317   }
318 
319   public String getWorkspaceName() throws Exception {
320     return getNode().getSession().getWorkspace().getName();
321   }
322 
323   public boolean isNodeTypeSupported() {
324     try {
325       TemplateService templateService = getApplicationComponent(TemplateService.class);
326       return templateService.isManagedNodeType(getNodeType());
327     } catch (Exception e) {
328       return false;
329     }
330   }
331 
332   public String getRepository() throws Exception{
333     return getAncestorOfType(UIJCRExplorer.class).getRepositoryName() ;
334   }
335 
336   public String encodeHTML(String text) throws Exception {
337     return Utils.encodeHTML(text) ;
338   }
339 
340   static public class ChangeLanguageActionListener extends EventListener<UIViewVersion>{
341     public void execute(Event<UIViewVersion> event) throws Exception {
342       UIViewVersion uiViewVersion = event.getSource() ;
343       UIApplication uiApp = uiViewVersion.getAncestorOfType(UIApplication.class) ;
344       uiApp.addMessage(new ApplicationMessage("UIViewVersion.msg.not-supported", null)) ;
345 
346       return ;
347     }
348   }
349 
350   static  public class DownloadActionListener extends EventListener<UIViewVersion> {
351     public void execute(Event<UIViewVersion> event) throws Exception {
352       UIViewVersion uiComp = event.getSource() ;
353       String downloadLink = uiComp.getDownloadLink(org.exoplatform.wcm.webui.Utils.getFileLangNode(uiComp.getNode()));
354       RequireJS requireJS = event.getRequestContext().getJavascriptManager().getRequireJS();
355       requireJS.require("SHARED/ecm-utils", "ecmutil").addScripts("ecmutil.ECMUtils.ajaxRedirect('" + downloadLink + "');");
356     }
357   }
358 
359   static  public class ChangeNodeActionListener extends EventListener<UIViewVersion> {
360     public void execute(Event<UIViewVersion> event) throws Exception {
361       UIViewVersion uiViewVersion =  event.getSource() ;
362       UIApplication uiApp = uiViewVersion.getAncestorOfType(UIApplication.class) ;
363       uiApp.addMessage(new ApplicationMessage("UIViewVersion.msg.not-supported", null)) ;
364 
365       return ;
366     }
367   }
368 
369   public UIComponent getUIComponent(String mimeType) throws Exception {
370     return Utils.getUIComponent(mimeType, this);
371   }
372 
373   public boolean isEnableComment() {
374     return false;
375   }
376 
377   public boolean isEnableVote() {
378     return false;
379   }
380 
381   public void setEnableComment(boolean value) {
382   }
383 
384   public void setEnableVote(boolean value) {
385   }
386 
387   public String getInlineEditingField(Node orgNode, String propertyName, String defaultValue,
388       String inputType, String idGenerator, String cssClass, boolean isGenericProperty, String... arguments) throws Exception {
389     return org.exoplatform.ecm.webui.utils.Utils.getInlineEditingField(orgNode, propertyName, defaultValue, inputType,
390                                                                        idGenerator, cssClass, isGenericProperty, arguments);
391   }
392 
393   public String getInlineEditingField(Node orgNode, String propertyName) throws Exception {
394     return org.exoplatform.ecm.webui.utils.Utils.getInlineEditingField(orgNode, propertyName);
395   }
396 }