1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
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
110 Session session = uiExplorer.getSessionByWorkspace(trashWorkspace);
111
112 Node node = uiExplorer.getNodeByPath(nodePath, session, false);
113
114
115 session = node.getSession();
116
117
118 trashWorkspace = session.getWorkspace().getName();
119
120 node = uiExplorer.getNodeByPath(nodePath, session, false);
121
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 }