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 org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
21  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
22  import org.exoplatform.ecm.webui.component.explorer.control.filter.*;
23  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIWorkingAreaActionListener;
24  import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
25  import org.exoplatform.ecm.utils.lock.LockUtil;
26  import org.exoplatform.ecm.webui.utils.Utils;
27  import org.exoplatform.portal.config.UserACL;
28  import org.exoplatform.services.jcr.core.ManageableRepository;
29  import org.exoplatform.services.log.ExoLogger;
30  import org.exoplatform.services.log.Log;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  import org.exoplatform.web.application.ApplicationMessage;
33  import org.exoplatform.webui.config.annotation.ComponentConfig;
34  import org.exoplatform.webui.config.annotation.EventConfig;
35  import org.exoplatform.webui.core.UIApplication;
36  import org.exoplatform.webui.event.Event;
37  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
38  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
39  import org.exoplatform.webui.ext.manager.UIAbstractManager;
40  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
41  
42  import javax.jcr.Node;
43  import javax.jcr.PathNotFoundException;
44  import javax.jcr.Session;
45  import javax.jcr.lock.LockException;
46  import javax.jcr.version.VersionException;
47  import java.util.Arrays;
48  import java.util.List;
49  import java.util.regex.Matcher;
50  
51  /**
52   * Created by The eXo Platform SARL
53   * Author : Hoang Van Hung
54   *          hunghvit@gmail.com
55   * Aug 6, 2009
56   */
57  
58  @ComponentConfig(
59      events = {
60        @EventConfig(listeners = UnlockManageComponent.UnlockActionListener.class)
61      }
62  )
63  public class UnlockManageComponent extends UIAbstractManagerComponent {
64  
65    private static final List<UIExtensionFilter> FILTERS
66        = Arrays.asList(new UIExtensionFilter[]{new IsNotInTrashFilter(),
67                                                new IsHoldsLockFilter(),
68                                                new IsNotLockedFilter(true, true),
69                                                new CanSetPropertyFilter(),
70                                                new IsCheckedOutFilter(),
71                                                new IsNotTrashHomeNodeFilter() });
72  
73    private static final Log LOG  = ExoLogger.getLogger(UnlockManageComponent.class.getName());
74  
75    @UIExtensionFilters
76    public List<UIExtensionFilter> getFilters() {
77      return FILTERS;
78    }
79  
80    private static Node getNodeByPath(String nodePath, UIJCRExplorer uiExplorer) throws Exception {
81      Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
82      String wsName = null;
83      if (matcher.find()) {
84        wsName = matcher.group(1);
85        nodePath = matcher.group(2);
86      } else {
87        throw new IllegalArgumentException("The ObjectId is invalid '"+ nodePath + "'");
88      }
89      Session session = uiExplorer.getSessionByWorkspace(wsName);
90      return uiExplorer.getNodeByPath(nodePath, session);
91    }
92  
93    private static void processUnlock(String nodePath,
94                                      Event<UnlockManageComponent> event,
95                                      UIJCRExplorer uiExplorer) throws Exception {
96      UIApplication uiApp = uiExplorer.getAncestorOfType(UIApplication.class);
97      Node node;
98      Session session;
99      try {
100       // Use the method getNodeByPath because it is link aware
101       node = getNodeByPath(nodePath, uiExplorer);
102       // Reset the session to manage the links that potentially change of workspace
103       session = node.getSession();
104     } catch(PathNotFoundException path) {
105       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
106           null,ApplicationMessage.WARNING));
107 
108       return;
109     } catch (Exception e) {
110       JCRExceptionManager.process(uiApp, e);
111       return;
112     }
113     String superUser = WCMCoreUtils.getService(UserACL.class).getSuperUser();
114     String remoteUser = WCMCoreUtils.getRemoteUser();
115     if (remoteUser.equalsIgnoreCase(superUser)) {
116       session = WCMCoreUtils.getSystemSessionProvider()
117                             .getSession(node.getSession().getWorkspace().getName(),
118                                         (ManageableRepository) node.getSession().getRepository());
119       node = (Node)session.getItem(node.getPath());
120     }
121     try {
122       if(node.holdsLock()) {
123         String lockToken = LockUtil.getLockToken(node);
124         if(lockToken != null) {
125           session.addLockToken(lockToken);
126         }
127         node.unlock();
128         node.removeMixin(Utils.MIX_LOCKABLE);
129         node.getSession().save();
130         //remove lock from Cache
131         LockUtil.removeLock(node);
132       }
133     } catch(LockException le) {
134       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.can-not-unlock-node", null, ApplicationMessage.WARNING));
135       uiExplorer.updateAjax(event);
136       return;
137     } catch(VersionException versionException) {
138       Object[] args = {node.getName()};
139       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.can-not-unlock-node-is-checked-in",
140                                               args,
141                                               ApplicationMessage.WARNING));
142 
143       uiExplorer.updateAjax(event);
144       return;
145     } catch (Exception e) {
146       if (LOG.isErrorEnabled()) {
147         LOG.error("an unexpected error occurs while unloking the node", e);
148       }
149       JCRExceptionManager.process(uiApp, e);
150 
151       uiExplorer.updateAjax(event);
152     }
153   }
154 
155   public static class UnlockActionListener extends UIWorkingAreaActionListener<UnlockManageComponent> {
156     private void unlockManage(Event<UnlockManageComponent> event, UIJCRExplorer uiExplorer) throws Exception {
157       String nodePath = event.getRequestContext().getRequestParameter(OBJECTID);
158       if(nodePath.indexOf(";") > -1) {
159         processMultiUnlock(nodePath.split(";"), event, uiExplorer);
160       } else {
161         processUnlock(nodePath, event, uiExplorer);
162       }
163     }
164 
165     private void processMultiUnlock(String[] nodePaths,
166                                     Event<UnlockManageComponent> event,
167                                     UIJCRExplorer uiExplorer) throws Exception {
168       for (String nodePath : nodePaths) {
169         if (acceptForMultiNode(event, nodePath))
170           processUnlock(nodePath, event, uiExplorer);
171       }
172       uiExplorer.getSession().save();
173       uiExplorer.updateAjax(event);
174     }
175 
176     public void processEvent(Event<UnlockManageComponent> event) throws Exception {
177       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
178       unlockManage(event, uiExplorer);
179     }
180   }
181 
182   @Override
183   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
184     return null;
185   }
186 
187 }