View Javadoc
1   /*
2    * Copyright (C) 2003-2008 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  package org.exoplatform.ecm.webui.component.explorer.rightclick.manager;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.regex.Matcher;
22  
23  import javax.jcr.AccessDeniedException;
24  import javax.jcr.Node;
25  import javax.jcr.PathNotFoundException;
26  import javax.jcr.Session;
27  import javax.jcr.lock.LockException;
28  import javax.jcr.version.VersionException;
29  
30  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
31  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
32  import org.exoplatform.ecm.webui.component.explorer.control.filter.HasRemovePermissionFilter;
33  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsCheckedOutFilter;
34  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsFavouriteFilter;
35  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
36  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotLockedFilter;
37  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotTrashHomeNodeFilter;
38  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIWorkingAreaActionListener;
39  import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
40  import org.exoplatform.ecm.webui.utils.PermissionUtil;
41  import org.exoplatform.services.cms.documents.FavoriteService;
42  import org.exoplatform.services.cms.link.LinkManager;
43  import org.exoplatform.services.log.ExoLogger;
44  import org.exoplatform.services.log.Log;
45  import org.exoplatform.services.security.ConversationState;
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  /**
59   * Created by The eXo Platform SARL
60   * Author : Nguyen Anh Vu
61   *          anhvurz90@gmail.com
62   * Oct 14, 2009
63   * 5:23:00 PM
64   */
65  
66  @ComponentConfig(
67        events = {
68          @EventConfig(listeners = RemoveFavouriteManageComponent.RemoveFromFavouriteActionListener.class)
69        }
70    )
71  
72  public class RemoveFavouriteManageComponent extends UIAbstractManagerComponent {
73  
74    private static final List<UIExtensionFilter> FILTERS
75        = Arrays.asList(new UIExtensionFilter[] { new IsNotInTrashFilter(),
76                                                  new IsFavouriteFilter(),
77                                                   new IsNotLockedFilter(),
78                                                   new IsCheckedOutFilter(),
79                                                   new HasRemovePermissionFilter(),
80                                                   new IsNotTrashHomeNodeFilter() });
81  
82    private final static Log       LOG  = ExoLogger.getLogger(RemoveFavouriteManageComponent.class.getName());
83  
84    @UIExtensionFilters
85    public List<UIExtensionFilter> getFilters() {
86      return FILTERS;
87    }
88  
89    private static void multiRemoveFromFavourite(String[] paths, Event<UIComponent> event) throws Exception {
90      for (String path : paths) {
91        removeFromFavourite(path, event);
92      }
93    }
94  
95    private static void removeFromFavourite(String srcPath, Event<UIComponent> event) throws Exception {
96        UIWorkingArea uiWorkingArea = ((UIComponent)event.getSource()).getParent();
97        UIJCRExplorer uiExplorer = uiWorkingArea.getAncestorOfType(UIJCRExplorer.class);
98        FavoriteService favoriteService = WCMCoreUtils.getService(FavoriteService.class);
99  
100       UIApplication uiApp = uiWorkingArea.getAncestorOfType(UIApplication.class);
101       Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(srcPath);
102       String wsName = null;
103       Node node = null;
104       if (matcher.find()) {
105         wsName = matcher.group(1);
106         srcPath = matcher.group(2);
107       } else {
108         throw new IllegalArgumentException("The ObjectId is invalid '"+ srcPath + "'");
109       }
110       Session session = uiExplorer.getSessionByWorkspace(wsName);
111       try {
112         // Use the method getNodeByPath because it is link aware
113         node = uiExplorer.getNodeByPath(srcPath, session, false);
114         //check if node is link
115         LinkManager lnkManager = uiExplorer.getApplicationComponent(LinkManager.class);
116         if (lnkManager.isLink(node) && lnkManager.isTargetReachable(node)) {
117           node = lnkManager.getTarget(node);
118         }
119         // Reset the path to manage the links that potentially create virtual path
120         //srcPath = node.getPath();
121         // Reset the session to manage the links that potentially change of workspace
122         session = node.getSession();
123         // Reset the workspace name to manage the links that potentially change of workspace
124         // wsName = session.getWorkspace().getName();
125       } catch(PathNotFoundException path) {
126         uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
127             null,ApplicationMessage.WARNING));
128 
129         return;
130       }
131 
132       try {
133         uiExplorer.addLockToken(node);
134       } catch (Exception e) {
135         JCRExceptionManager.process(uiApp, e);
136         return;
137       }
138 
139       try {
140         if (!node.isCheckedOut())
141           throw new VersionException("node is locked, can't remove favourite of node :" + node.getPath());
142         if (!PermissionUtil.canRemoveNode(node))
143           throw new AccessDeniedException("access denied, can't remove favourite of node:" + node.getPath());
144         favoriteService.removeFavorite(node, ConversationState.getCurrent().getIdentity().getUserId());
145         uiExplorer.updateAjax(event);
146       } catch (LockException e) {
147         if (LOG.isErrorEnabled()) {
148           LOG.error("node is locked, can't remove favourite of node :" + node.getPath());
149         }
150         JCRExceptionManager.process(uiApp, e);
151 
152         uiExplorer.updateAjax(event);
153       } catch (VersionException e) {
154         if (LOG.isErrorEnabled()) {
155           LOG.error("node is checked in, can't remove favourite of node:" + node.getPath());
156         }
157         JCRExceptionManager.process(uiApp, e);
158 
159         uiExplorer.updateAjax(event);
160       } catch (AccessDeniedException e) {
161         if (LOG.isErrorEnabled()) {
162           LOG.error("access denied, can't remove favourite of node:" + node.getPath());
163         }
164         JCRExceptionManager.process(uiApp, e);
165 
166         uiExplorer.updateAjax(event);
167       } catch (Exception e) {
168         if (LOG.isErrorEnabled()) {
169           LOG.error("an unexpected error occurs", e);
170         }
171         JCRExceptionManager.process(uiApp, e);
172 
173         uiExplorer.updateAjax(event);
174       }
175   }
176 
177   public static void removeFavouriteManage(Event<UIComponent> event) throws Exception {
178     String srcPath = event.getRequestContext().getRequestParameter(OBJECTID);
179     if (srcPath.indexOf(';') > -1) {
180       multiRemoveFromFavourite(srcPath.split(";"), event);
181     } else {
182       removeFromFavourite(srcPath, event);
183     }
184   }
185 
186   public static class RemoveFromFavouriteActionListener extends UIWorkingAreaActionListener<RemoveFavouriteManageComponent> {
187       public void processEvent(Event<RemoveFavouriteManageComponent> event) throws Exception {
188           Event<UIComponent> event_ = new Event<UIComponent>( event.getSource(), event.getName(),event.getRequestContext());
189           removeFavouriteManage(event_);
190         }
191   }
192 
193   @Override
194   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
195     return null;
196   }
197 
198 }
199