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.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   * Created by The eXo Platform SARL
40   * Author : Hoang Van Hung
41   *          hunghvit@gmail.com
42   * Sep 16, 2009
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     * Overide method UIComponent.loadConfirmMesssage() to get resource bundle in jar file
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          // begin of lampt's modification.
84          Session session = node.getSession();
85          Node parentNode = null;
86  
87          // In case of node path begin with slash sign.
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         } // end of modification.
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 }