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.removecomment;
19  
20  import java.util.Map;
21  
22  import javax.jcr.Node;
23  import javax.jcr.version.VersionException;
24  
25  import org.exoplatform.ecm.utils.text.Text;
26  import org.exoplatform.ecm.webui.presentation.AbstractActionComponent;
27  import org.exoplatform.ecm.webui.presentation.action.UIPresentationEventListener;
28  import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
29  import org.exoplatform.ecm.webui.utils.Utils;
30  import org.exoplatform.services.cms.comments.CommentsService;
31  import org.exoplatform.services.cms.link.NodeFinder;
32  import org.exoplatform.services.log.ExoLogger;
33  import org.exoplatform.services.log.Log;
34  import org.exoplatform.web.application.ApplicationMessage;
35  import org.exoplatform.webui.application.WebuiRequestContext;
36  import org.exoplatform.webui.config.annotation.ComponentConfig;
37  import org.exoplatform.webui.config.annotation.EventConfig;
38  import org.exoplatform.webui.core.UIApplication;
39  
40  /**
41   * Created by The eXo Platform SARL
42   * Author : Hoang Van Hung
43   *          hunghvit@gmail.com
44   * Sep 17, 2009
45   */
46  
47  @ComponentConfig(events = {
48      @EventConfig(listeners = RemoveCommentComponent.RemoveCommentActionListener.class,
49                   confirm = "RemoveCommentComponent.msg.confirm-deletecomment") })
50  public class RemoveCommentComponent extends AbstractActionComponent {
51  
52    private static final Log LOG = ExoLogger.getLogger(RemoveCommentComponent.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) return confirmKey;
62      String confirm = Utils.getResourceBundle(Utils.LOCALE_WEBUI_DMS, confirmKey, getClass().getClassLoader());
63      return confirm.replaceAll("\\{0\\}", beanId);
64    }
65  
66    public static void doDelete(Map<String, Object> variables) throws Exception {
67      AbstractActionComponent uicomponent = (AbstractActionComponent)variables.get(UICOMPONENT);
68      UIApplication uiApp = uicomponent.getAncestorOfType(UIApplication.class);
69      NodeFinder nodefinder = uicomponent.getApplicationComponent(NodeFinder.class);
70      String wsname = String.valueOf(variables.get(Utils.WORKSPACE_PARAM));
71      String nodepath = String.valueOf(variables.get(OBJECTID));
72      WebuiRequestContext requestcontext = (WebuiRequestContext)variables.get(Utils.REQUESTCONTEXT);
73      try {
74        Node commentNode = (Node) nodefinder.getItem(wsname,
75            Text.escapeIllegalJcrChars(nodepath));
76        CommentsService commentService = uicomponent.getApplicationComponent(CommentsService.class);
77        commentService.deleteComment(commentNode);
78        uicomponent.updateAjax(requestcontext);
79      } catch(VersionException e) {
80        if (LOG.isErrorEnabled()) {
81          LOG.error("Version exception");
82        }
83        Object[] args = { nodepath } ;
84        uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.can-not-delete-version", args,
85            ApplicationMessage.WARNING));
86      } catch (Exception e) {
87        if (LOG.isErrorEnabled()) {
88          LOG.error("an unexpected error occurs while removing the node", e);
89        }
90        JCRExceptionManager.process(uiApp, e);
91      }
92    }
93  
94    public static class RemoveCommentActionListener extends UIPresentationEventListener<RemoveCommentComponent> {
95      protected void executeAction(Map<String, Object> variables) throws Exception {
96        RemoveCommentComponent.doDelete(variables);
97      }
98    }
99  }