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 org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
20  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
21  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsTrashHomeNodeFilter;
22  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIWorkingAreaActionListener;
23  import org.exoplatform.ecm.webui.utils.Utils;
24  import org.exoplatform.services.cms.actions.ActionServiceContainer;
25  import org.exoplatform.services.cms.documents.TrashService;
26  import org.exoplatform.services.cms.taxonomy.TaxonomyService;
27  import org.exoplatform.services.cms.thumbnail.ThumbnailService;
28  import org.exoplatform.services.jcr.core.ManageableRepository;
29  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30  import org.exoplatform.web.application.ApplicationMessage;
31  import org.exoplatform.webui.application.WebuiRequestContext;
32  import org.exoplatform.webui.application.portlet.PortletRequestContext;
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.core.UIComponent;
37  import org.exoplatform.webui.event.Event;
38  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
39  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
40  import org.exoplatform.webui.ext.manager.UIAbstractManager;
41  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
42  
43  import javax.jcr.Node;
44  import javax.jcr.NodeIterator;
45  import javax.jcr.Session;
46  import javax.jcr.nodetype.NodeType;
47  import javax.portlet.PortletPreferences;
48  import java.util.Arrays;
49  import java.util.List;
50  /**
51   * Created by The eXo Platform SARL
52   * Author : Nguyen Anh Vu
53   *          anhvurz90@gmail.com
54   * Nov 13, 2009
55   * 5:19:23 PM
56   */
57  @ComponentConfig(
58                   events = {
59                       @EventConfig(listeners = EmptyTrashManageComponent.EmptyTrashActionListener.class,
60                                           confirm = "EmptyTrashManageComponent.msg.confirm-delete") })
61  public class EmptyTrashManageComponent extends UIAbstractManagerComponent {
62  
63    private static final List<UIExtensionFilter> FILTERS
64        = Arrays.asList(new UIExtensionFilter[] { new IsNotInTrashFilter(),
65                                                  new IsTrashHomeNodeFilter() } );
66  
67    @UIExtensionFilters
68    public List<UIExtensionFilter> getFilters() {
69      return FILTERS;
70    }
71  
72    public static void emptyTrashManage(Event<? extends UIComponent> event, UIJCRExplorer uiExplorer) throws Exception {
73      Node trashHomeNode = getTrashHomeNode(uiExplorer);
74      NodeIterator nodeIter = trashHomeNode.getNodes();
75      UIApplication uiApp = uiExplorer.getAncestorOfType(UIApplication.class);
76      if (nodeIter.getSize() == 0) {
77        return;
78      }
79  
80      String currentUser = WCMCoreUtils.getRemoteUser();
81      boolean error = false;
82      while (nodeIter.hasNext()) {
83        try {
84          Node node = nodeIter.nextNode();
85          if (node.hasProperty(Utils.EXO_LASTMODIFIER))
86            if (currentUser.equals(node.getProperty(Utils.EXO_LASTMODIFIER).getString())) {
87              deleteNode(node, uiExplorer, event);
88            }
89        } catch (Exception ex) {
90          error = true;
91        }
92      }
93      if (error) {
94        uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.error-when-emptying-trash", null,
95            ApplicationMessage.WARNING));
96  
97      }
98    }
99  
100   private static void deleteNode(Node nodeToDelete,
101                                  UIJCRExplorer uiExplorer,
102                                  Event<? extends UIComponent> event) throws Exception {
103     PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
104     PortletPreferences portletPref = pcontext.getRequest().getPreferences();
105     String trashWorkspace = portletPref.getValue(Utils.TRASH_WORKSPACE, "");
106 
107     String nodePath = nodeToDelete.getPath();
108 
109     // Use the method getNodeByPath because it is link aware
110     Session session = uiExplorer.getSessionByWorkspace(trashWorkspace);
111     // Use the method getNodeByPath because it is link aware
112     Node node = uiExplorer.getNodeByPath(nodePath, session, false);
113     // Reset the session to manage the links that potentially change of
114     // workspace
115     session = node.getSession();
116     // Reset the workspace name to manage the links that potentially
117     // change of workspace
118     trashWorkspace = session.getWorkspace().getName();
119     // Use the method getNodeByPath because it is link aware
120     node = uiExplorer.getNodeByPath(nodePath, session, false);
121     // If node has taxonomy
122     TaxonomyService taxonomyService = uiExplorer.getApplicationComponent(TaxonomyService.class);
123     List<Node> listTaxonomyTrees = taxonomyService.getAllTaxonomyTrees();
124     List<Node> listExistedTaxonomy = taxonomyService.getAllCategories(node);
125     for (Node existedTaxonomy : listExistedTaxonomy) {
126       for (Node taxonomyTrees : listTaxonomyTrees) {
127         if(existedTaxonomy.getPath().contains(taxonomyTrees.getPath())) {
128           taxonomyService.removeCategory(node, taxonomyTrees.getName(),
129               existedTaxonomy.getPath().substring(taxonomyTrees.getPath().length()));
130           break;
131         }
132       }
133     }
134 
135     uiExplorer.addLockToken(node);
136     Node parentNode = node.getParent();
137     uiExplorer.addLockToken(parentNode);
138     ActionServiceContainer actionService = uiExplorer.getApplicationComponent(ActionServiceContainer.class);
139     actionService.removeAction(node, uiExplorer.getRepositoryName());
140     ThumbnailService thumbnailService = uiExplorer.getApplicationComponent(ThumbnailService.class);
141     thumbnailService.processRemoveThumbnail(node);
142     TrashService trashService = uiExplorer.getApplicationComponent(TrashService.class);
143     trashService.removeRelations(node, uiExplorer.getSystemProvider());
144     node.remove();
145     parentNode.save();
146     uiExplorer.updateAjax(event);
147   }
148 
149   private static void removeMixins(Node node) throws Exception {
150     NodeType[] mixins = node.getMixinNodeTypes();
151     for (NodeType nodeType : mixins) {
152       node.removeMixin(nodeType.getName());
153     }
154   }
155 
156 
157   private static Node getTrashHomeNode(UIJCRExplorer uiExplorer) throws Exception {
158     PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
159     PortletPreferences portletPref = pcontext.getRequest().getPreferences();
160     String trashHomeNodePath = portletPref.getValue(Utils.TRASH_HOME_NODE_PATH, "");
161     String trashWorkspace = portletPref.getValue(Utils.TRASH_WORKSPACE, "");
162 
163     ManageableRepository manageableRepository = WCMCoreUtils.getRepository();
164     Session trashSession = uiExplorer.getSessionProvider().getSession(trashWorkspace, manageableRepository);
165     return (Node)trashSession.getItem(trashHomeNodePath);
166   }
167 
168   public static class EmptyTrashActionListener extends UIWorkingAreaActionListener<EmptyTrashManageComponent> {
169     public void processEvent(Event<EmptyTrashManageComponent> event) throws Exception {
170       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
171       emptyTrashManage(event, uiExplorer);
172     }
173   }
174 
175   @Override
176   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
177     return null;
178   }
179 
180 }