View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS. This program is free software; you
3    * can redistribute it and/or modify it under the terms of the GNU Affero
4    * General Public License as published by the Free Software Foundation; either
5    * version 3 of the License, or (at your option) any later version. This program
6    * is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7    * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8    * PARTICULAR PURPOSE. See the GNU General Public License for more details. You
9    * should have received a copy of the GNU General Public License along with this
10   * program; if not, see<http://www.gnu.org/licenses/>.
11   */
12  package org.exoplatform.wcm.webui.search;
13  
14  import org.exoplatform.ecm.resolver.JCRResourceResolver;
15  import org.exoplatform.portal.application.PortalRequestContext;
16  import org.exoplatform.portal.mop.SiteType;
17  import org.exoplatform.portal.webui.util.Util;
18  import org.exoplatform.resolver.ResourceResolver;
19  import org.exoplatform.services.cms.impl.DMSConfiguration;
20  import org.exoplatform.web.url.navigation.NavigationResource;
21  import org.exoplatform.web.url.navigation.NodeURL;
22  import org.exoplatform.webui.config.annotation.ComponentConfig;
23  import org.exoplatform.webui.config.annotation.EventConfig;
24  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
25  import org.exoplatform.webui.event.Event;
26  import org.exoplatform.webui.event.EventListener;
27  import org.exoplatform.webui.form.UIForm;
28  import org.exoplatform.webui.form.UIFormStringInput;
29  
30  /*
31   * Created by The eXo Platform SAS Author : Anh Do Ngoc anh.do@exoplatform.com
32   * Oct 31, 2008
33   */
34  @ComponentConfig(
35    lifecycle = UIFormLifecycle.class,
36    events = {
37      @EventConfig(listeners = UISearchBox.SearchActionListener.class)
38    }
39  )
40  public class UISearchBox extends UIForm {
41  
42    /** The template path. */
43    private String             templatePath;
44  
45    /** The Constant KEYWORD_INPUT. */
46    public static final String KEYWORD_INPUT     = "keywordInput";
47  
48    /** The Constant PORTAL_NAME_PARAM. */
49    public static final String PORTAL_NAME_PARAM = "portal";
50  
51    /** The Constant KEYWORD_PARAM. */
52    public static final String KEYWORD_PARAM     = "keyword";
53  
54    /**
55     * Instantiates a new uI search box.
56     *
57     * @throws Exception the exception
58     */
59    public UISearchBox() throws Exception {
60      UIFormStringInput uiKeywordInput = new UIFormStringInput(KEYWORD_INPUT, KEYWORD_INPUT, null);
61      addChild(uiKeywordInput);
62    }
63  
64    /**
65     * Sets the template path.
66     *
67     * @param templatePath the new template path
68     */
69    public void setTemplatePath(String templatePath) {
70      this.templatePath = templatePath;
71    }
72  
73    /*
74     * (non-Javadoc)
75     * @see org.exoplatform.webui.core.UIComponent#getTemplate()
76     */
77    public String getTemplate() {
78      return templatePath;
79    }
80  
81    public ResourceResolver getTemplateResourceResolver() {
82      try {
83        DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
84        String workspace = dmsConfiguration.getConfig().getSystemWorkspace();
85        return new JCRResourceResolver(workspace);
86      } catch (Exception e) {
87        return null;
88      }
89    }
90  
91    /**
92     * The listener interface for receiving searchAction events. The class that is
93     * interested in processing a searchAction event implements this interface,
94     * and the object created with that class is registered with a component using
95     * the component's
96     * <code>addSearchActionListener</code> method. When the searchAction
97     * event occurs, that object's appropriate method is invoked.
98     */
99    public static class SearchActionListener extends EventListener<UISearchBox> {
100 
101     /*
102      * (non-Javadoc)
103      * @see
104      * org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui
105      * .event.Event)
106      */
107     public void execute(Event<UISearchBox> event) throws Exception {
108       UISearchBox uiSearchBox = event.getSource();
109       String keyword = uiSearchBox.getUIStringInput(UISearchBox.KEYWORD_INPUT).getValue();
110       String portalName = Util.getPortalRequestContext().getPortalOwner();
111       PortalRequestContext prContext = Util.getPortalRequestContext();
112       prContext.setResponseComplete(true);
113 
114       NodeURL nodeURL = Util.getPortalRequestContext().createURL(NodeURL.TYPE);
115       NavigationResource resource = new NavigationResource(SiteType.PORTAL, portalName, "searchResult");
116       nodeURL.setResource(resource);
117       nodeURL.setQueryParameterValue(PORTAL_NAME_PARAM, portalName);
118       nodeURL.setQueryParameterValue(KEYWORD_PARAM, keyword);
119 
120       prContext.getResponse().sendRedirect(nodeURL.toString());
121     }
122   }
123 }