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.ecm.webui.component.explorer.UIJCRExplorer;
30  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
31  import org.exoplatform.ecm.webui.component.explorer.control.filter.CanCutNodeFilter;
32  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotEditingDocumentFilter;
33  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
34  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotLockedFilter;
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.cms.clipboard.ClipboardService;
40  import org.exoplatform.services.cms.clipboard.jcr.model.ClipboardCommand;
41  import org.exoplatform.services.log.ExoLogger;
42  import org.exoplatform.services.log.Log;
43  import org.exoplatform.services.security.ConversationState;
44  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
45  import org.exoplatform.web.application.ApplicationMessage;
46  import org.exoplatform.webui.config.annotation.ComponentConfig;
47  import org.exoplatform.webui.config.annotation.EventConfig;
48  import org.exoplatform.webui.core.UIApplication;
49  import org.exoplatform.webui.event.Event;
50  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
51  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
52  import org.exoplatform.webui.ext.manager.UIAbstractManager;
53  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
54  
55  /**
56   * Created by The eXo Platform SARL
57   * Author : Hoang Van Hung
58   *          hunghvit@gmail.com
59   * Aug 6, 2009
60   */
61  
62  @ComponentConfig(
63      events = {
64        @EventConfig(listeners = CutManageComponent.CutActionListener.class)
65      }
66  )
67  
68  public class CutManageComponent extends UIAbstractManagerComponent {
69  
70    private final static Log       LOG  = ExoLogger.getLogger(CutManageComponent.class.getName());
71  
72    private static final List<UIExtensionFilter> FILTERS
73        = Arrays.asList(new UIExtensionFilter[] { new IsNotInTrashFilter(),
74                                                  new CanCutNodeFilter(),
75                                                  new IsNotLockedFilter(),
76                                                  new IsNotTrashHomeNodeFilter(),
77                                                  new IsNotEditingDocumentFilter()});
78  
79    @UIExtensionFilters
80    public List<UIExtensionFilter> getFilters() {
81      return FILTERS;
82    }
83  
84    private static void processCut(String nodePath,
85                                   Event<CutManageComponent> event,
86                                   UIJCRExplorer uiExplorer,
87                                   boolean isMultiSelect) throws Exception {
88      UIWorkingArea uiWorkingArea = uiExplorer.getChild(UIWorkingArea.class);
89      UIApplication uiApp = event.getSource().getAncestorOfType(UIApplication.class);
90      Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
91      String wsName = null;
92      if (matcher.find()) {
93        wsName = matcher.group(1);
94        nodePath = matcher.group(2);
95      } else {
96        throw new IllegalArgumentException("The ObjectId is invalid '"+ nodePath + "'");
97      }
98      Session session = uiExplorer.getSessionByWorkspace(wsName);
99      Node selectedNode;
100     try {
101       // Use the method getNodeByPath because it is link aware
102       selectedNode = uiExplorer.getNodeByPath(nodePath, session, false);
103       // Reset the path to manage the links that potentially create virtual path
104       nodePath = selectedNode.getPath();
105       // Reset the session to manage the links that potentially change of workspace
106       session = selectedNode.getSession();
107       // Reset the workspace name to manage the links that potentially change of workspace
108       wsName = session.getWorkspace().getName();
109     } catch(ConstraintViolationException cons) {
110       uiExplorer.getSession().refresh(false);
111       uiExplorer.refreshExplorer();
112       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.constraintviolation-exception",
113           null,ApplicationMessage.WARNING));
114       
115       uiExplorer.updateAjax(event);
116       return;
117     } catch(PathNotFoundException path) {
118       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
119           null,ApplicationMessage.WARNING));
120       
121       return;
122     } catch (Exception e) {
123       if (LOG.isErrorEnabled()) {
124         LOG.error("an unexpected error occurs while cuting the node", e);
125       }
126       JCRExceptionManager.process(uiApp, e);
127       return;
128     }
129     try {
130       ClipboardCommand clipboard = new ClipboardCommand(ClipboardCommand.CUT, nodePath, wsName);
131       ClipboardService clipboardService = WCMCoreUtils.getService(ClipboardService.class);
132       String userId = ConversationState.getCurrent().getIdentity().getUserId();
133       clipboardService.addClipboardCommand(userId, clipboard, false);
134       if (isMultiSelect) {
135         clipboardService.addClipboardCommand(userId, clipboard, true);
136       }
137     } catch (Exception e) {
138       JCRExceptionManager.process(uiApp, e);
139     }
140   }
141 
142   private static void processMultipleCut(String[] nodePaths,
143                                          Event<CutManageComponent> event,
144                                          UIJCRExplorer uiExplorer) throws Exception {
145     for(int i=0; i< nodePaths.length; i++) {
146       processCut(nodePaths[i], event, uiExplorer, true);
147     }
148   }
149 
150   public static void cutManage(Event<CutManageComponent> event, UIJCRExplorer uiExplorer) throws Exception {
151     UIWorkingArea uiWorkingArea = event.getSource().getParent();
152     String nodePath = event.getRequestContext().getRequestParameter(OBJECTID);
153     if(nodePath.indexOf(";") > -1) {
154       ClipboardService clipboardService = WCMCoreUtils.getService(ClipboardService.class);
155       String userId = ConversationState.getCurrent().getIdentity().getUserId();
156       clipboardService.clearClipboardList(userId, true);
157       processMultipleCut(Utils.removeChildNodes(nodePath), event, uiExplorer);
158     } else {
159       processCut(nodePath, event, uiExplorer, false);
160     }
161   }
162 
163   public static class CutActionListener extends UIWorkingAreaActionListener<CutManageComponent> {
164     public void processEvent(Event<CutManageComponent> event) throws Exception {
165       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
166       cutManage(event, uiExplorer);
167     }
168   }
169 
170   @Override
171   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
172     return null;
173   }
174 
175 }