View Javadoc
1   /*
2    * Copyright (C) 2003-2007 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.search;
18  
19  import javax.jcr.AccessDeniedException;
20  
21  import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
22  import org.exoplatform.portal.webui.util.Util;
23  import org.exoplatform.services.cms.queries.QueryService;
24  import org.exoplatform.web.application.ApplicationMessage;
25  import org.exoplatform.webui.config.annotation.ComponentConfig;
26  import org.exoplatform.webui.config.annotation.EventConfig;
27  import org.exoplatform.webui.core.UIApplication;
28  import org.exoplatform.webui.core.UIPopupComponent;
29  import org.exoplatform.webui.core.UIPopupContainer;
30  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
31  import org.exoplatform.webui.event.Event;
32  import org.exoplatform.webui.event.Event.Phase;
33  import org.exoplatform.webui.event.EventListener;
34  import org.exoplatform.webui.form.UIForm;
35  import org.exoplatform.webui.form.UIFormStringInput;
36  import org.exoplatform.webui.form.validator.MandatoryValidator;
37  
38  /**
39   * Created by The eXo Platform SARL
40   * Author : Dang Van Minh
41   *          minh.dang@exoplatform.com
42   * Jun 28, 2007 9:43:21 AM
43   */
44  @ComponentConfig(
45      lifecycle = UIFormLifecycle.class,
46      template =  "system:/groovy/webui/form/UIForm.gtmpl",
47      events = {
48        @EventConfig(listeners = UISaveQueryForm.SaveActionListener.class),
49        @EventConfig(listeners = UISaveQueryForm.CancelActionListener.class, phase=Phase.DECODE)
50      }
51  )
52  public class UISaveQueryForm extends UIForm implements UIPopupComponent {
53  
54    final static public String QUERY_NAME = "queryName" ;
55    private String statement_ ;
56    private boolean isSimpleSearch_ = false ;
57    private String queryType_ ;
58  
59    public UISaveQueryForm() throws Exception {
60      addUIFormInput(new UIFormStringInput(QUERY_NAME, QUERY_NAME, null).
61                     addValidator(ECMNameValidator.class).
62                     addValidator(MandatoryValidator.class)) ;
63    }
64  
65    public void activate() {}
66  
67    public void deActivate() {}
68  
69    public void setSimpleSearch(boolean isSimpleSearch) { isSimpleSearch_ = isSimpleSearch ; }
70  
71    public void setStatement(String statement) { statement_ = statement ; }
72  
73    public void setQueryType(String queryType) { queryType_ = queryType ; }
74  
75    static  public class SaveActionListener extends EventListener<UISaveQueryForm> {
76      public void execute(Event<UISaveQueryForm> event) throws Exception {
77        UISaveQueryForm uiSaveQueryForm = event.getSource() ;
78        UIECMSearch uiECMSearch = uiSaveQueryForm.getAncestorOfType(UIECMSearch.class) ;
79        UIApplication uiApp = uiSaveQueryForm.getAncestorOfType(UIApplication.class) ;
80        String userName = Util.getPortalRequestContext().getRemoteUser() ;
81        QueryService queryService = uiSaveQueryForm.getApplicationComponent(QueryService.class) ;
82        String queryName = uiSaveQueryForm.getUIStringInput(QUERY_NAME).getValue() ;
83        if(queryName == null || queryName.trim().length() == 0) {
84          uiApp.addMessage(new ApplicationMessage("UISaveQueryForm.msg.query-name-null", null)) ;
85          event.getRequestContext().addUIComponentToUpdateByAjax(uiECMSearch);
86          return ;
87        }
88        try {
89          queryService.addQuery(queryName, uiSaveQueryForm.statement_, uiSaveQueryForm.queryType_, userName) ;
90        } catch(AccessDeniedException ace) {
91          uiApp.addMessage(new ApplicationMessage("UISaveQueryForm.msg.access-denied", null,
92                                                  ApplicationMessage.WARNING));
93          event.getRequestContext().addUIComponentToUpdateByAjax(uiECMSearch);
94          return ;
95        } catch (Exception e){
96          uiApp.addMessage(new ApplicationMessage("UISaveQueryForm.msg.save-failed", null,
97                                                  ApplicationMessage.WARNING)) ;
98          event.getRequestContext().addUIComponentToUpdateByAjax(uiECMSearch);
99          return ;
100       }
101       uiECMSearch.getChild(UISavedQuery.class).updateGrid(1);
102       if(uiSaveQueryForm.isSimpleSearch_) {
103         UISearchContainer uiSearchContainer = uiSaveQueryForm.getAncestorOfType(UISearchContainer.class) ;
104         UIPopupContainer uiPopup = uiSearchContainer.getChild(UIPopupContainer.class) ;
105         uiPopup.deActivate() ;
106       }
107       uiECMSearch.setSelectedTab(uiECMSearch.getChild(UISavedQuery.class).getId()) ;
108       event.getRequestContext().addUIComponentToUpdateByAjax(uiECMSearch) ;
109     }
110   }
111 
112   static  public class CancelActionListener extends EventListener<UISaveQueryForm> {
113     public void execute(Event<UISaveQueryForm> event) throws Exception {
114       UISearchContainer uiSearchContainer = event.getSource().getAncestorOfType(UISearchContainer.class) ;
115       UIPopupContainer uiPopup = uiSearchContainer.getChild(UIPopupContainer.class) ;
116       uiPopup.deActivate() ;
117       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup) ;
118     }
119   }
120 }