View Javadoc
1   /***************************************************************************
2    * Copyright (C) 2003-2009 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.ecm.webui.component.explorer.rightclick.manager;
19  
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.regex.Matcher;
27  
28  import javax.jcr.Node;
29  import javax.jcr.PathNotFoundException;
30  
31  import org.exoplatform.ecm.webui.component.explorer.UIDocumentWorkspace;
32  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
33  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
34  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
35  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotTrashHomeNodeFilter;
36  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIWorkingAreaActionListener;
37  import org.exoplatform.ecm.webui.utils.Utils;
38  import org.exoplatform.ecm.webui.viewer.VideoAudioViewer;
39  import org.exoplatform.web.application.ApplicationMessage;
40  import org.exoplatform.webui.config.annotation.ComponentConfig;
41  import org.exoplatform.webui.config.annotation.EventConfig;
42  import org.exoplatform.webui.core.UIApplication;
43  import org.exoplatform.webui.event.Event;
44  import org.exoplatform.webui.ext.UIExtensionManager;
45  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
46  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
47  import org.exoplatform.webui.ext.manager.UIAbstractManager;
48  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
49  
50  /**
51   * Created by The eXo Platform SARL
52   * Author : Hoang Van Hung
53   *          hunghvit@gmail.com
54   * Oct 30, 2009
55   */
56  @ComponentConfig(
57      events = {
58        @EventConfig(listeners = PlayMediaComponent.PlayMediaActionListener.class)
59      }
60  )
61  public class PlayMediaComponent  extends UIAbstractManagerComponent {
62  
63    private static final List<UIExtensionFilter> FILTERS
64        = Arrays.asList(new UIExtensionFilter[]{ new IsNotInTrashFilter(),
65                                                 new IsNotTrashHomeNodeFilter() });
66    @UIExtensionFilters
67    public List<UIExtensionFilter> getFilters() {
68      return FILTERS;
69    }
70  
71    private boolean accept(Node node) throws Exception {
72      if (!node.isNodeType(Utils.NT_FILE)) return false;
73      String mimeType = node.getNode(Utils.JCR_CONTENT).getProperty(Utils.JCR_MIMETYPE).getString();
74      UIExtensionManager manager = getApplicationComponent(UIExtensionManager.class);
75      Map<String, Object> context = new HashMap<String, Object>();
76      context.put(Utils.MIME_TYPE, mimeType);
77      if (manager.accept(Utils.FILE_VIEWER_EXTENSION_TYPE, "VideoAudio", context)) {
78          return true;
79      }
80      return false;
81    }
82  
83    public static class PlayMediaActionListener extends UIWorkingAreaActionListener<PlayMediaComponent> {
84      public void processEvent(Event<PlayMediaComponent> event) throws Exception {
85        PlayMediaComponent playMedia = event.getSource();
86        UIJCRExplorer uiExplorer = playMedia.getAncestorOfType(UIJCRExplorer.class);
87        UIWorkingArea uiWorkingArea = playMedia.getParent();
88        UIDocumentWorkspace uiDocumentWorkspace = uiWorkingArea.getChild(UIDocumentWorkspace.class);
89        UIApplication uiApp = uiWorkingArea.getAncestorOfType(UIApplication.class);
90        uiDocumentWorkspace.removeChildById("PlayMedia");
91        String srcPath = event.getRequestContext().getRequestParameter(OBJECTID);
92        Matcher matcher = null;
93        String wsName = null;
94        Node tempNode = null;
95        if(srcPath.indexOf(";") > -1) {
96          String[] paths = srcPath.split(";");
97          List<Node> nodes = new ArrayList<Node>();
98          for(String path : paths) {
99            matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(path);
100           if (matcher.find()) {
101             wsName = matcher.group(1);
102             srcPath = matcher.group(2);
103           } else {
104             throw new IllegalArgumentException("The ObjectId is invalid '"+ srcPath + "'");
105           }
106           tempNode = uiExplorer.getNodeByPath(srcPath, uiExplorer.getSessionByWorkspace(wsName));
107           if (playMedia.accept(tempNode)) nodes.add(tempNode);
108           if (nodes.size() == 0) {
109             uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.unavaiable-supported-media-file",
110                 null,ApplicationMessage.WARNING));
111             
112             return;
113           }
114         }
115         VideoAudioViewer uiViewer = uiDocumentWorkspace.addChild(VideoAudioViewer.class, null, "PlayMedia");
116         uiViewer.setPresentNodes(nodes);
117         uiViewer.setRepository(uiExplorer.getRepositoryName());
118         uiDocumentWorkspace.setRenderedChild(VideoAudioViewer.class);
119       } else {
120         matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(srcPath);
121         if (matcher.find()) {
122           wsName = matcher.group(1);
123           srcPath = matcher.group(2);
124         } else {
125           throw new IllegalArgumentException("The ObjectId is invalid '"+ srcPath + "'");
126         }
127         try {
128           // Use the method getNodeByPath because it is link aware
129           uiExplorer.setSelectNode(wsName, srcPath);
130         } catch(PathNotFoundException path) {
131           uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
132               null,ApplicationMessage.WARNING));
133           
134           return;
135         }
136       }
137 
138       event.getRequestContext().addUIComponentToUpdateByAjax(uiDocumentWorkspace);
139     }
140   }
141 
142 
143   @Override
144   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
145     return null;
146   }
147 
148 }