View Javadoc
1   /*
2    * Copyright (C) 2003-2008 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.wcm.webui.search;
18  
19  import javax.portlet.PortletMode;
20  import javax.portlet.PortletPreferences;
21  
22  import org.exoplatform.ecm.resolver.JCRResourceResolver;
23  import org.exoplatform.portal.webui.container.UIContainer;
24  import org.exoplatform.resolver.ResourceResolver;
25  import org.exoplatform.services.cms.impl.DMSConfiguration;
26  import org.exoplatform.webui.application.WebuiRequestContext;
27  import org.exoplatform.webui.application.portlet.PortletRequestContext;
28  import org.exoplatform.webui.config.annotation.ComponentConfig;
29  import org.exoplatform.webui.config.annotation.EventConfig;
30  import org.exoplatform.webui.core.lifecycle.Lifecycle;
31  import org.exoplatform.webui.event.Event;
32  import org.exoplatform.webui.event.EventListener;
33  
34  /*
35   * Created by The eXo Platform SAS Author : Anh Do Ngoc anh.do@exoplatform.com
36   * Oct 31, 2008
37   */
38  @ComponentConfig(
39    lifecycle = Lifecycle.class,
40    events = {
41      @EventConfig(listeners = UISearchPageLayout.QuickEditActionListener.class)
42    }
43  )
44  public class UISearchPageLayout extends UIContainer {
45  
46    /** The Constant SEARCH_FORM. */
47    public static final String SEARCH_FORM   = "uiSearchForm";
48  
49    /** The Constant SEARCH_RESULT. */
50    public static final String SEARCH_RESULT = "uiSearchResult";
51  
52    /**
53     * Instantiates a new uI search page layout.
54     *
55     * @throws Exception the exception
56     */
57    public UISearchPageLayout() throws Exception {
58      WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();    
59      UISearchForm uiSearchForm = addChild(UISearchForm.class, null, null);
60      UISearchResult uiSearchResult = addChild(UISearchResult.class, null, null);
61      String searchFormTemplatePath = getTemplatePath(UIWCMSearchPortlet.SEARCH_FORM_TEMPLATE_PATH);
62      uiSearchForm.init(searchFormTemplatePath, getTemplateResourceResolver(context,
63                                                                            searchFormTemplatePath));
64      String searchResultTemplatePath = getTemplatePath(UIWCMSearchPortlet.SEARCH_RESULT_TEMPLATE_PATH);
65      uiSearchResult.init(searchResultTemplatePath,
66                          getTemplateResourceResolver(context, searchResultTemplatePath));
67    }
68  
69    /**
70     * Gets the portlet preference.
71     *
72     * @return the portlet preference
73     */
74    private PortletPreferences getPortletPreference() {
75      PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
76      return portletRequestContext.getRequest().getPreferences();
77    }
78  
79    /**
80     * Gets the template path.
81     *
82     * @param templateType the template type
83     *
84     * @return the template path
85     */
86    private String getTemplatePath(String templateType) {
87      return getPortletPreference().getValue(templateType, null);
88    }
89  
90    /*
91     * (non-Javadoc)
92     * @see org.exoplatform.portal.webui.portal.UIPortalComponent#getTemplate()
93     */
94    public String getTemplate() {
95      String template = getPortletPreference().getValue(UIWCMSearchPortlet.SEARCH_PAGE_LAYOUT_TEMPLATE_PATH,
96                                                        null);
97      return template;
98    }
99  
100   /*
101    * (non-Javadoc)
102    * @see
103    * org.exoplatform.webui.core.UIComponent#getTemplateResourceResolver(org.
104    * exoplatform.webui.application.WebuiRequestContext, java.lang.String)
105    */
106   public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context, String template) {
107     try {
108       DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
109       String workspace = dmsConfiguration.getConfig().getSystemWorkspace();
110       return new JCRResourceResolver(workspace);
111     } catch (Exception e) {
112       return null;
113     }
114   }
115 
116   /**
117    * Gets the portlet id.
118    *
119    * @return the portlet id
120    */
121   public String getPortletId() {
122     PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
123     return pContext.getWindowId();
124   }
125 
126   /**
127    * The listener interface for receiving quickEditAction events. The class that
128    * is interested in processing a quickEditAction event implements this
129    * interface, and the object created with that class is registered with a
130    * component using the component's
131    * <code>addQuickEditActionListener</code> method. When
132    * the quickEditAction event occurs, that object's appropriate
133    * method is invoked.
134    */
135   public static class QuickEditActionListener extends EventListener<UISearchPageLayout> {
136 
137     /*
138      * (non-Javadoc)
139      * @see
140      * org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui
141      * .event.Event)
142      */
143     public void execute(Event<UISearchPageLayout> event) throws Exception {
144       PortletRequestContext context = (PortletRequestContext) event.getRequestContext();
145       context.setApplicationMode(PortletMode.EDIT);
146     }
147   }
148 }