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  import java.util.Arrays;
21  import java.util.List;
22  import java.util.regex.Matcher;
23  
24  import javax.jcr.Node;
25  import javax.jcr.PathNotFoundException;
26  import javax.jcr.Session;
27  import javax.jcr.nodetype.ConstraintViolationException;
28  
29  import org.exoplatform.services.cms.clipboard.ClipboardService;
30  import org.exoplatform.services.cms.clipboard.jcr.model.ClipboardCommand;
31  import org.exoplatform.services.log.Log;
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.JCRExceptionManager;
38  import org.exoplatform.ecm.webui.utils.Utils;
39  import org.exoplatform.services.log.ExoLogger;
40  import org.exoplatform.services.security.ConversationState;
41  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
42  import org.exoplatform.web.application.ApplicationMessage;
43  import org.exoplatform.webui.config.annotation.ComponentConfig;
44  import org.exoplatform.webui.config.annotation.EventConfig;
45  import org.exoplatform.webui.core.UIApplication;
46  import org.exoplatform.webui.core.UIComponent;
47  import org.exoplatform.webui.event.Event;
48  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
49  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
50  import org.exoplatform.webui.ext.manager.UIAbstractManager;
51  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
52  
53  /**
54   * Created by The eXo Platform SARL
55   * Author : Hoang Van Hung
56   *          hunghvit@gmail.com
57   * Aug 5, 2009
58   */
59  
60  @ComponentConfig(
61      events = {
62        @EventConfig(listeners = CopyManageComponent.CopyActionListener.class)
63      }
64  )
65  
66  public class CopyManageComponent extends UIAbstractManagerComponent {
67  
68    private static final List<UIExtensionFilter> FILTERS
69        = Arrays.asList(new UIExtensionFilter[]{new IsNotInTrashFilter(),
70                                                new IsNotTrashHomeNodeFilter()});
71  
72    private final static Log       LOG  = ExoLogger.getLogger(CopyManageComponent.class.getName());
73  
74    @UIExtensionFilters
75    public List<UIExtensionFilter> getFilters() {
76      return FILTERS;
77    }
78  
79    public static void multipleCopy(String[] srcPaths, Event<UIComponent> event) throws Exception {
80      for(int i=0; i< srcPaths.length; i++) {
81        processCopy(srcPaths[i], event, true);
82      }
83    }
84  
85    public static void processCopy(String srcPath, Event<?> event, boolean isMultiSelect) throws Exception {
86      UIWorkingArea uiWorkingArea = ((UIComponent)event.getSource()).getParent();
87      UIJCRExplorer uiExplorer = uiWorkingArea.getAncestorOfType(UIJCRExplorer.class);
88      UIApplication uiApp = uiWorkingArea.getAncestorOfType(UIApplication.class);
89      Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(srcPath);
90      String wsName = null;
91      if (matcher.find()) {
92        wsName = matcher.group(1);
93        srcPath = matcher.group(2);
94      } else {
95        throw new IllegalArgumentException("The ObjectId is invalid '"+ srcPath + "'");
96      }
97      Session session = uiExplorer.getSessionByWorkspace(wsName);
98      try {
99        // Use the method getNodeByPath because it is link aware
100       Node node = uiExplorer.getNodeByPath(srcPath, session, false);
101       // Reset the path to manage the links that potentially create virtual path
102       srcPath = node.getPath();
103       // Reset the session to manage the links that potentially change of workspace
104       session = node.getSession();
105       // Reset the workspace name to manage the links that potentially change of workspace
106       wsName = session.getWorkspace().getName();
107     } catch(PathNotFoundException path) {
108       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
109           null,ApplicationMessage.WARNING));
110       
111       return;
112     }
113 
114     ClipboardCommand clipboard = new ClipboardCommand(ClipboardCommand.COPY, srcPath, wsName);
115     ClipboardService clipboardService = WCMCoreUtils.getService(ClipboardService.class);
116     String userId = ConversationState.getCurrent().getIdentity().getUserId();
117     clipboardService.addClipboardCommand(userId, clipboard, false);
118     if (isMultiSelect) {
119       clipboardService.addClipboardCommand(userId, clipboard, true);
120     }
121   }
122 
123   public static void copyManage(Event<UIComponent> event) throws Exception {
124     String srcPath = event.getRequestContext().getRequestParameter(OBJECTID);
125     
126     ClipboardService clipboardService = WCMCoreUtils.getService(ClipboardService.class);
127     String userId = ConversationState.getCurrent().getIdentity().getUserId();
128     clipboardService.clearClipboardList(userId, true);
129     
130     if(srcPath.indexOf(";") > -1) {      
131       multipleCopy(Utils.removeChildNodes(srcPath), event);
132     } else {
133       processCopy(srcPath, event, false);
134     }
135   }
136 
137   public static class CopyActionListener extends UIWorkingAreaActionListener<CopyManageComponent> {
138     public void processEvent(Event<CopyManageComponent> event) throws Exception {
139       Event<UIComponent> event_ = new Event<UIComponent>( event.getSource(), event.getName(),event.getRequestContext());
140       copyManage(event_);
141     }
142   }
143 
144   @Override
145   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
146     return null;
147   }
148 
149 }