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