LockManageComponent.java

  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. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.regex.Matcher;

  22. import javax.jcr.AccessDeniedException;
  23. import javax.jcr.Node;
  24. import javax.jcr.PathNotFoundException;
  25. import javax.jcr.Session;
  26. import javax.jcr.lock.Lock;
  27. import javax.jcr.lock.LockException;

  28. import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
  29. import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
  30. import org.exoplatform.ecm.webui.component.explorer.control.filter.CanSetPropertyFilter;
  31. import org.exoplatform.ecm.webui.component.explorer.control.filter.IsCheckedOutFilter;
  32. import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotHoldsLockFilter;
  33. import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
  34. import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotSimpleLockedFilter;
  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.utils.lock.LockUtil;
  39. import org.exoplatform.ecm.webui.utils.PermissionUtil;
  40. import org.exoplatform.ecm.webui.utils.Utils;
  41. import org.exoplatform.services.cms.lock.LockService;
  42. import org.exoplatform.services.log.ExoLogger;
  43. import org.exoplatform.services.log.Log;
  44. import org.exoplatform.services.organization.MembershipType;
  45. import org.exoplatform.services.organization.OrganizationService;
  46. import org.exoplatform.services.wcm.utils.WCMCoreUtils;
  47. import org.exoplatform.web.application.ApplicationMessage;
  48. import org.exoplatform.webui.config.annotation.ComponentConfig;
  49. import org.exoplatform.webui.config.annotation.EventConfig;
  50. import org.exoplatform.webui.core.UIApplication;
  51. import org.exoplatform.webui.core.UIComponent;
  52. import org.exoplatform.webui.event.Event;
  53. import org.exoplatform.webui.ext.filter.UIExtensionFilter;
  54. import org.exoplatform.webui.ext.filter.UIExtensionFilters;
  55. import org.exoplatform.webui.ext.manager.UIAbstractManager;
  56. import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;

  57. /**
  58.  * Created by The eXo Platform SARL
  59.  * Author : Hoang Van Hung
  60.  *          hunghvit@gmail.com
  61.  * Aug 6, 2009
  62.  */

  63. @ComponentConfig(
  64.     events = {
  65.       @EventConfig(listeners = LockManageComponent.LockActionListener.class)
  66.     }
  67. )

  68. public class LockManageComponent extends UIAbstractManagerComponent {

  69.   private static final List<UIExtensionFilter> FILTERS
  70.       = Arrays.asList(new UIExtensionFilter[]{new IsNotInTrashFilter(),
  71.                                               new IsNotHoldsLockFilter(),
  72.                                               new IsNotSimpleLockedFilter(),
  73.                                               new CanSetPropertyFilter(),
  74.                                               new IsCheckedOutFilter(),
  75.                                               new IsNotTrashHomeNodeFilter() });

  76.   private static final Log LOG  = ExoLogger.getLogger(LockManageComponent.class.getName());

  77.   @UIExtensionFilters
  78.   public List<UIExtensionFilter> getFilters() {
  79.     return FILTERS;
  80.   }


  81.   private static void processMultiLock(String[] nodePaths, Event<?> event, UIJCRExplorer uiExplorer) throws Exception {
  82.     for(int i=0; i< nodePaths.length; i++) {
  83.       processLock(nodePaths[i], event, uiExplorer);
  84.     }
  85.     uiExplorer.updateAjax(event);
  86.   }

  87.   private static void processLock(String nodePath, Event<?> event,  UIJCRExplorer uiExplorer) throws Exception {
  88.     UIApplication uiApp = uiExplorer.getAncestorOfType(UIApplication.class);
  89.     Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
  90.     String wsName = null;
  91.     if (matcher.find()) {
  92.       wsName = matcher.group(1);
  93.       nodePath = matcher.group(2);
  94.     } else {
  95.       throw new IllegalArgumentException("The ObjectId is invalid '"+ nodePath + "'");
  96.     }
  97.     Session session = uiExplorer.getSessionByWorkspace(wsName);
  98.     Node node;
  99.     try {
  100.       // Use the method getNodeByPath because it is link aware
  101.       node = uiExplorer.getNodeByPath(nodePath, session);
  102.       // Reset the path to manage the links that potentially create virtual path
  103.       nodePath = node.getPath();
  104.       // Reset the session to manage the links that potentially change of workspace
  105.       session = node.getSession();
  106.       // Reset the workspace name to manage the links that potentially change of workspace
  107.       wsName = session.getWorkspace().getName();
  108.     } catch(PathNotFoundException path) {
  109.       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
  110.           null,ApplicationMessage.WARNING));
  111.      
  112.       return;
  113.     } catch (Exception e) {
  114.       JCRExceptionManager.process(uiApp, e);
  115.       return;
  116.     }
  117.    
  118.     try {
  119.       if (!PermissionUtil.canSetProperty(node))
  120.         throw new AccessDeniedException("access denied, can't lock node:" + node.getPath());
  121.      
  122.       if(node.canAddMixin(Utils.MIX_LOCKABLE)){
  123.         node.addMixin(Utils.MIX_LOCKABLE);
  124.         node.save();
  125.       }
  126.       Lock lock = node.lock(false, false);
  127.       LockUtil.keepLock(lock);
  128.       LockService lockService = uiExplorer.getApplicationComponent(LockService.class);
  129.       List<String> settingLockList = lockService.getAllGroupsOrUsersForLock();
  130.       OrganizationService service = WCMCoreUtils.getService(OrganizationService.class);
  131.       List<MembershipType> memberships = (List<MembershipType>) service.getMembershipTypeHandler().findMembershipTypes();
  132.       for (String settingLock : settingLockList) {
  133.         LockUtil.keepLock(lock, settingLock);
  134.         if (!settingLock.startsWith("*"))
  135.           continue;
  136.         String lockTokenString = settingLock;
  137.         for (MembershipType membership : memberships) {
  138.           lockTokenString = settingLock.replace("*", membership.getName());
  139.           LockUtil.keepLock(lock, lockTokenString);
  140.         }
  141.       }
  142.       session.save();
  143.     } catch(LockException le) {
  144.       ApplicationMessage appMessage =
  145.         new ApplicationMessage("UIPopupMenu.msg.cant-lock",
  146.                                new String[] {node.getPath()}, ApplicationMessage.ERROR);
  147.       appMessage.setArgsLocalized(false);
  148.       uiApp.addMessage(appMessage);
  149.      
  150.       uiExplorer.updateAjax(event);
  151.       return;
  152.     } catch (AccessDeniedException adEx) {
  153.       ApplicationMessage appMessage =
  154.         new ApplicationMessage("UIPopupMenu.msg.can-not-lock-node",
  155.                                new String[] {node.getPath()}, ApplicationMessage.ERROR);
  156.       appMessage.setArgsLocalized(false);
  157.       uiApp.addMessage(appMessage);
  158.      
  159.       uiExplorer.updateAjax(event);
  160.       return;
  161.     }
  162.     catch (Exception e) {
  163.       if (LOG.isErrorEnabled()) {
  164.         LOG.error("an unexpected error occurs while locking the node", e);
  165.       }
  166.       JCRExceptionManager.process(uiApp, e);
  167.      
  168.       uiExplorer.updateAjax(event);
  169.     }
  170.   }

  171.   public static void lockManage(Event<? extends UIComponent> event, UIJCRExplorer uiExplorer) throws Exception {
  172.     String nodePath = event.getRequestContext().getRequestParameter(OBJECTID);
  173.     if(nodePath.indexOf(";") > -1) {
  174.       processMultiLock(nodePath.split(";"), event, uiExplorer);
  175.     } else {
  176.       processLock(nodePath, event, uiExplorer);
  177.     }
  178.   }

  179.   public static class LockActionListener extends UIWorkingAreaActionListener<LockManageComponent> {
  180.     public void processEvent(Event<LockManageComponent> event) throws Exception {
  181.       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
  182.       lockManage(event, uiExplorer);
  183.     }
  184.   }

  185.   @Override
  186.   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
  187.     return null;
  188.   }

  189. }