1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.exoplatform.ecm.webui.presentation.removeattach;
19
20
21 import java.util.Map;
22
23 import javax.jcr.Node;
24 import javax.jcr.Session;
25
26 import org.exoplatform.services.log.Log;
27 import org.exoplatform.ecm.webui.presentation.AbstractActionComponent;
28 import org.exoplatform.ecm.webui.presentation.action.UIPresentationEventListener;
29 import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
30 import org.exoplatform.ecm.webui.utils.Utils;
31 import org.exoplatform.services.cms.link.NodeFinder;
32 import org.exoplatform.services.log.ExoLogger;
33 import org.exoplatform.webui.application.WebuiRequestContext;
34 import org.exoplatform.webui.config.annotation.ComponentConfig;
35 import org.exoplatform.webui.config.annotation.EventConfig;
36 import org.exoplatform.webui.core.UIApplication;
37
38
39
40
41
42
43
44
45 @ComponentConfig(events = {
46 @EventConfig(listeners = RemoveAttachmentComponent.RemoveAttachActionListener.class,
47 confirm = "RemoveAttachmentComponent.msg.confirm-deleteattachment") }
48 )
49
50 public class RemoveAttachmentComponent extends AbstractActionComponent {
51
52 private static final Log LOG = ExoLogger.getLogger(RemoveAttachmentComponent.class.getName());
53
54
55
56
57 protected String loadConfirmMesssage(org.exoplatform.webui.config.Event event,
58 WebuiRequestContext context,
59 String beanId) {
60 String confirmKey = event.getConfirm();
61 if (confirmKey.length() < 1)
62 return confirmKey;
63 try {
64 String confirm = Utils.getResourceBundle(Utils.LOCALE_WEBUI_DMS,
65 confirmKey,
66 getClass().getClassLoader());
67 return confirm.replaceAll("\\{0\\}", beanId);
68 } catch (Exception e) {
69 return confirmKey;
70 }
71 }
72
73 public static void doDelete(Map<String, Object> variables) throws Exception {
74 AbstractActionComponent uicomponent = (AbstractActionComponent)variables.get(UICOMPONENT);
75 UIApplication uiApp = uicomponent.getAncestorOfType(UIApplication.class);
76 NodeFinder nodefinder = uicomponent.getApplicationComponent(NodeFinder.class);
77 String wsname = String.valueOf(variables.get(Utils.WORKSPACE_PARAM));
78 String nodepath = String.valueOf(variables.get(OBJECTID));
79 WebuiRequestContext requestcontext = (WebuiRequestContext)variables.get(Utils.REQUESTCONTEXT);
80 try {
81 Node node = (Node) nodefinder.getItem(wsname, nodepath);
82
83
84 Session session = node.getSession();
85 Node parentNode = null;
86
87
88 if (nodepath.startsWith("/")) {
89 if (node.hasProperty(Utils.JCR_DATA)) {
90 node.setProperty(Utils.JCR_DATA, Utils.EMPTY);
91 node.save();
92 } else {
93 parentNode = node.getParent();
94 node.remove();
95 parentNode.save();
96 }
97 } else {
98 if (node.hasProperty(nodepath)) {
99 node.setProperty(nodepath, Utils.EMPTY);
100 node.save();
101 }
102 }
103
104 session.save();
105 uicomponent.updateAjax(requestcontext);
106 return;
107 } catch (Exception e) {
108 if (LOG.isErrorEnabled()) {
109 LOG.error("an unexpected error occurs while removing the node", e);
110 }
111 JCRExceptionManager.process(uiApp, e);
112 return;
113 }
114 }
115
116 public static class RemoveAttachActionListener extends UIPresentationEventListener<RemoveAttachmentComponent> {
117 @Override
118 protected void executeAction(Map<String, Object> variables) throws Exception {
119 RemoveAttachmentComponent.doDelete(variables);
120 }
121 }
122 }