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.AccessDeniedException;
25  import javax.jcr.Node;
26  import javax.jcr.PathNotFoundException;
27  import javax.jcr.Session;
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.CanSetPropertyFilter;
32  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsCheckedOutFilter;
33  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotContainBinaryFilter;
34  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
35  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotLockedFilter;
36  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotTrashHomeNodeFilter;
37  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsVersionableFilter;
38  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsVersionableOrAncestorFilter;
39  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIWorkingAreaActionListener;
40  import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
41  import org.exoplatform.web.application.ApplicationMessage;
42  import org.exoplatform.webui.config.annotation.ComponentConfig;
43  import org.exoplatform.webui.config.annotation.EventConfig;
44  import org.exoplatform.webui.core.UIApplication;
45  import org.exoplatform.webui.core.UIComponent;
46  import org.exoplatform.webui.event.Event;
47  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
48  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
49  import org.exoplatform.webui.ext.manager.UIAbstractManager;
50  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
51  
52  /**
53   * Created by The eXo Platform SARL
54   * Author : Hoang Van Hung
55   *          hunghvit@gmail.com
56   * Aug 6, 2009
57   */
58  
59  @ComponentConfig(
60      events = {
61        @EventConfig(listeners = CheckInManageComponent.CheckInActionListener.class)
62      }
63  )
64  
65  public class CheckInManageComponent extends UIAbstractManagerComponent {
66  
67    private static final List<UIExtensionFilter> FILTERS
68          = Arrays.asList(new UIExtensionFilter[]{new IsNotInTrashFilter(),
69                                                  new CanSetPropertyFilter(),
70                                                  new IsNotLockedFilter(),
71                                                  new IsVersionableOrAncestorFilter(),
72                                                  new IsCheckedOutFilter(),
73                                                  new IsVersionableFilter(),
74                                                  new IsNotTrashHomeNodeFilter(),
75                                                  new IsNotContainBinaryFilter()});
76  
77    @UIExtensionFilters
78    public List<UIExtensionFilter> getFilters() {
79      return FILTERS;
80    }
81  
82    public static void checkInManage(Event<? extends UIComponent> event, UIJCRExplorer uiExplorer,
83        UIApplication uiApp) throws Exception {
84      String nodePath = event.getRequestContext().getRequestParameter(OBJECTID);
85      if (nodePath == null) {
86        nodePath = uiExplorer.getCurrentWorkspace() + ':' + uiExplorer.getCurrentPath();
87      }
88      Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
89      String wsName = null;
90      if (matcher.find()) {
91        wsName = matcher.group(1);
92        nodePath = matcher.group(2);
93      } else {
94        throw new IllegalArgumentException("The ObjectId is invalid '"+ nodePath + "'");
95      }
96      Session session = uiExplorer.getSessionByWorkspace(wsName);
97      // Use the method getNodeByPath because it is link aware
98      Node node = uiExplorer.getNodeByPath(nodePath, session);
99      // Reset the path to manage the links that potentially create virtual path
100     nodePath = node.getPath();
101     // Reset the session to manage the links that potentially change of workspace
102     session = node.getSession();
103     // Reset the workspace name to manage the links that potentially change of workspace
104     wsName = session.getWorkspace().getName();
105 
106     try {
107       Node parentNode = node.getParent();
108       uiExplorer.addLockToken(parentNode);
109       node.checkin();
110     } catch(PathNotFoundException path) {
111       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
112           null,ApplicationMessage.WARNING));
113       
114       return;
115     } catch (AccessDeniedException adEx) {
116       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.can-not-checkin-node", null, ApplicationMessage.WARNING));
117       uiExplorer.updateAjax(event);
118       return;
119     } catch (Exception e) {
120       JCRExceptionManager.process(uiApp, e);
121       
122       uiExplorer.updateAjax(event);
123     }
124 
125   }
126 
127   public static class CheckInActionListener extends UIWorkingAreaActionListener<CheckInManageComponent> {
128     public void processEvent(Event<CheckInManageComponent> event) throws Exception {
129       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
130       UIApplication uiApp = event.getSource().getAncestorOfType(UIApplication.class);
131       checkInManage(event, uiExplorer, uiApp);
132     }
133   }
134 
135   @Override
136   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
137     return null;
138   }
139 
140 }