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.config;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.Node;
23  import javax.portlet.PortletMode;
24  import javax.portlet.PortletPreferences;
25  
26  import org.exoplatform.ecm.webui.form.UIFormInputSetWithAction;
27  import org.exoplatform.ecm.webui.selector.UISelectable;
28  import org.exoplatform.services.cms.views.ApplicationTemplateManagerService;
29  import org.exoplatform.services.jcr.RepositoryService;
30  import org.exoplatform.services.jcr.core.ManageableRepository;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  import org.exoplatform.wcm.webui.Utils;
33  import org.exoplatform.wcm.webui.search.UIWCMSearchPortlet;
34  import org.exoplatform.wcm.webui.selector.page.UIPageSelector;
35  import org.exoplatform.web.application.ApplicationMessage;
36  import org.exoplatform.webui.application.WebuiRequestContext;
37  import org.exoplatform.webui.application.portlet.PortletRequestContext;
38  import org.exoplatform.webui.config.annotation.ComponentConfig;
39  import org.exoplatform.webui.config.annotation.EventConfig;
40  import org.exoplatform.webui.core.UIApplication;
41  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
42  import org.exoplatform.webui.core.model.SelectItemOption;
43  import org.exoplatform.webui.event.Event;
44  import org.exoplatform.webui.event.EventListener;
45  import org.exoplatform.webui.form.UIForm;
46  import org.exoplatform.webui.form.UIFormSelectBox;
47  import org.exoplatform.webui.form.UIFormStringInput;
48  
49  /*
50   * Created by The eXo Platform SAS Author : Anh Do Ngoc anh.do@exoplatform.com
51   * Oct 31, 2008
52   */
53  @ComponentConfig(
54    lifecycle = UIFormLifecycle.class,
55    template = "app:/groovy/webui/search/config/UISearchPageLayoutManager.gtmpl",
56    events = {
57      @EventConfig(listeners = UISearchPageLayoutManager.SaveActionListener.class),
58      @EventConfig(listeners = UISearchPageLayoutManager.SelectBasePathActionListener.class),
59      @EventConfig(listeners = UISearchPageLayoutManager.CancelActionListener.class)
60    }
61  )
62  public class UISearchPageLayoutManager extends UIForm  implements UISelectable {
63  
64    /** The Constant PORTLET_NAME. */
65    public static final String PORTLET_NAME                       = "search";
66  
67    /** The Constant SEARCH_PAGE_LAYOUT_CATEGORY. */
68    public static final String SEARCH_PAGE_LAYOUT_CATEGORY        = "search-page-layout";
69  
70    /** The Constant SEARCH_PAGE_LAYOUT_SELECTOR. */
71    public static final String SEARCH_PAGE_LAYOUT_SELECTOR        = "searchPageLayoutSelector";
72  
73    /** The Constant SEARCH_FORM_TEMPLATE_CATEGORY. */
74    public static final String SEARCH_FORM_TEMPLATE_CATEGORY      = "search-form";
75  
76  //  /** The Constant SEARCH_PAGINATOR_TEMPLATE_CATEGORY. */
77  //  public static final String SEARCH_PAGINATOR_TEMPLATE_CATEGORY = "search-paginator";
78  
79    /** The Constant SEARCH_RESULT_TEMPLATE_CATEGORY. */
80    public static final String SEARCH_RESULT_TEMPLATE_CATEGORY    = "search-result";
81  
82    /** The Constant SEARCH_FORM_TEMPLATE_SELECTOR. */
83    public static final String SEARCH_FORM_TEMPLATE_SELECTOR      = "searchFormSelector";
84  
85    /** The Constant SEARCH_PAGINATOR_TEMPLATE_SELECTOR. */
86    public static final String SEARCH_PAGINATOR_TEMPLATE_SELECTOR = "searchPaginatorSelector";
87  
88    /** The Constant SEARCH_RESULT_TEMPLATE_SELECTOR. */
89    public static final String SEARCH_RESULT_TEMPLATE_SELECTOR    = "searchResultSelector";
90  
91    /** The Constant ITEMS_PER_PAGE_SELECTOR. */
92    public final static String ITEMS_PER_PAGE_SELECTOR            = "itemsPerPageSelector";
93  
94    /** The Constant PAGE_MODE_SELECTOR. */
95    public final static String PAGE_MODE_SELECTOR                 = "pageMode";
96  
97    /** The Constant BASE_PATH_INPUT. */
98    public final static String BASE_PATH_INPUT                    = "searchResultBasePathInput";
99  
100   /** The Constant BASE_PATH_SELECTOR_POPUP_WINDOW. */
101   public final static String BASE_PATH_SELECTOR_POPUP_WINDOW    = "searchResultBasePathPopupWindow";
102 
103   /** The Constant BASE_PATH_INPUT_SET_ACTION. */
104   public final static String BASE_PATH_INPUT_SET_ACTION         = "searchResultBasePathInputSetAction";
105 
106   /** The popup id. */
107   private String popupId;
108   /**
109    * @return the popupId
110    */
111   public String getPopupId() {
112     return popupId;
113   }
114 
115   /**
116    * @param popupId the popupId to set
117    */
118   public void setPopupId(String popupId) {
119     this.popupId = popupId;
120   }
121 
122   /**
123    * Instantiates a new uI search page layout manager.
124    *
125    * @throws Exception the exception
126    */
127   public UISearchPageLayoutManager() throws Exception {
128     PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
129     PortletPreferences portletPreferences = portletRequestContext.getRequest().getPreferences();
130 
131     String itemsPerpage = portletPreferences.getValue(UIWCMSearchPortlet.ITEMS_PER_PAGE, null);
132     String pageMode = portletPreferences.getValue(UIWCMSearchPortlet.PAGE_MODE, null);
133     String searchFormTemplate = portletPreferences.getValue(UIWCMSearchPortlet.SEARCH_FORM_TEMPLATE_PATH,
134                                                             null);
135     String searchResultTemplate = portletPreferences.getValue(UIWCMSearchPortlet.SEARCH_RESULT_TEMPLATE_PATH,
136                                                               null);
137 //    String searchPaginatorTemplate = portletPreferences.getValue(UIWCMSearchPortlet.SEARCH_PAGINATOR_TEMPLATE_PATH,
138 //                                                                 null);
139     String searchPageLayoutTemplate = portletPreferences.getValue(UIWCMSearchPortlet.SEARCH_PAGE_LAYOUT_TEMPLATE_PATH,
140                                                                   null);
141     List<SelectItemOption<String>> searchFormTemplateList = createTemplateList(PORTLET_NAME,
142                                                                                SEARCH_FORM_TEMPLATE_CATEGORY);
143     List<SelectItemOption<String>> searchResultTemplateList = createTemplateList(PORTLET_NAME,
144                                                                                  SEARCH_RESULT_TEMPLATE_CATEGORY);
145 //    List<SelectItemOption<String>> searchPaginatorTemplateList = createTemplateList(PORTLET_NAME,
146 //                                                                                    SEARCH_PAGINATOR_TEMPLATE_CATEGORY);
147     List<SelectItemOption<String>> searchPageLayoutTemplateList = createTemplateList(PORTLET_NAME,
148                                                                                      SEARCH_PAGE_LAYOUT_CATEGORY);
149     List<SelectItemOption<String>> itemsPerPageList = new ArrayList<SelectItemOption<String>>();
150     itemsPerPageList.add(new SelectItemOption<String>("5", "5"));
151     itemsPerPageList.add(new SelectItemOption<String>("10", "10"));
152     itemsPerPageList.add(new SelectItemOption<String>("20", "20"));
153 
154     List<SelectItemOption<String>> pageModeList = new ArrayList<SelectItemOption<String>>();
155     pageModeList.add(new SelectItemOption<String>("none", "none"));
156     pageModeList.add(new SelectItemOption<String>("more", "more"));
157     pageModeList.add(new SelectItemOption<String>("pagination", "pagination"));
158 
159     UIFormSelectBox pageModeSelector = new UIFormSelectBox(PAGE_MODE_SELECTOR,
160             PAGE_MODE_SELECTOR,
161             pageModeList);
162 
163     UIFormSelectBox itemsPerPageSelector = new UIFormSelectBox(ITEMS_PER_PAGE_SELECTOR,
164                                                                ITEMS_PER_PAGE_SELECTOR,
165                                                                itemsPerPageList);
166     UIFormSelectBox searchFormTemplateSelector = new UIFormSelectBox(SEARCH_FORM_TEMPLATE_SELECTOR,
167                                                                      SEARCH_FORM_TEMPLATE_SELECTOR,
168                                                                      searchFormTemplateList);
169     UIFormSelectBox searchResultTemplateSelector = new UIFormSelectBox(SEARCH_RESULT_TEMPLATE_SELECTOR,
170                                                                        SEARCH_RESULT_TEMPLATE_SELECTOR,
171                                                                        searchResultTemplateList);
172 //    UIFormSelectBox searchPaginatorTemplateSelector = new UIFormSelectBox(SEARCH_PAGINATOR_TEMPLATE_SELECTOR,
173 //                                                                          SEARCH_PAGINATOR_TEMPLATE_SELECTOR,
174 //                                                                          searchPaginatorTemplateList);
175     UIFormSelectBox searchPageLayoutTemplateSelector = new UIFormSelectBox(SEARCH_PAGE_LAYOUT_SELECTOR,
176                                                                            SEARCH_PAGE_LAYOUT_SELECTOR,
177                                                                            searchPageLayoutTemplateList);
178 
179     String preferenceBasePath = portletPreferences.getValue(UIWCMSearchPortlet.BASE_PATH, null);
180     UIFormInputSetWithAction targetPathFormInputSet = new UIFormInputSetWithAction(BASE_PATH_INPUT_SET_ACTION);
181     UIFormStringInput targetPathFormStringInput = new UIFormStringInput(BASE_PATH_INPUT, BASE_PATH_INPUT, preferenceBasePath);
182     targetPathFormStringInput.setValue(preferenceBasePath);
183     targetPathFormStringInput.setReadOnly(true);
184     targetPathFormInputSet.setActionInfo(BASE_PATH_INPUT, new String[] {"SelectBasePath"}) ;
185     targetPathFormInputSet.addUIFormInput(targetPathFormStringInput);
186 
187     pageModeSelector.setValue(pageMode);
188     itemsPerPageSelector.setValue(itemsPerpage);
189     searchFormTemplateSelector.setValue(searchFormTemplate);
190     searchResultTemplateSelector.setValue(searchResultTemplate);
191 //    searchPaginatorTemplateSelector.setValue(searchPaginatorTemplate);
192     searchPageLayoutTemplateSelector.setValue(searchPageLayoutTemplate);
193 
194     addChild(pageModeSelector);
195     addChild(itemsPerPageSelector);
196     addChild(searchFormTemplateSelector);
197     addChild(searchResultTemplateSelector);
198 //    addChild(searchPaginatorTemplateSelector);
199     addChild(searchPageLayoutTemplateSelector);
200     addChild(targetPathFormInputSet);
201 
202     setActions(new String[] { "Save", "Cancel" });
203   }
204 
205   /**
206    * Creates the template list.
207    *
208    * @param portletName the portlet name
209    * @param category the category
210    *
211    * @return the list< select item option< string>>
212    *
213    * @throws Exception the exception
214    */
215   private List<SelectItemOption<String>> createTemplateList(String portletName, String category) throws Exception {
216     List<SelectItemOption<String>> templateList = new ArrayList<SelectItemOption<String>>();
217     ApplicationTemplateManagerService templateManagerService = getApplicationComponent(ApplicationTemplateManagerService.class);
218     List<Node> templateNodeList = templateManagerService.getTemplatesByCategory(portletName,
219                                                                                 category,
220                                                                                 WCMCoreUtils.getUserSessionProvider());
221     for (Node templateNode : templateNodeList) {
222       String templateName = templateNode.getName();
223       String templatePath = templateNode.getPath();
224       templateList.add(new SelectItemOption<String>(templateName, templatePath));
225     }
226     return templateList;
227   }
228 
229   /**
230    * The listener interface for receiving saveAction events. The class that is
231    * interested in processing a saveAction event implements this interface, and
232    * the object created with that class is registered with a component using the
233    * component's <code>addSaveActionListener</code> method. When
234    * the saveAction event occurs, that object's appropriate
235    * method is invoked.
236    *
237    */
238   public static class SaveActionListener extends EventListener<UISearchPageLayoutManager> {
239 
240     /*
241      * (non-Javadoc)
242      * @see
243      * org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui
244      * .event.Event)
245      */
246     public void execute(Event<UISearchPageLayoutManager> event) throws Exception {
247       UISearchPageLayoutManager uiSearchLayoutManager = event.getSource();
248       UIApplication uiApp = uiSearchLayoutManager.getAncestorOfType(UIApplication.class);
249       RepositoryService repositoryService = uiSearchLayoutManager.getApplicationComponent(RepositoryService.class);
250       ManageableRepository manageableRepository = repositoryService.getCurrentRepository();
251       String repository = manageableRepository.getConfiguration().getName();
252       String workspace = manageableRepository.getConfiguration().getDefaultWorkspaceName();
253       PortletRequestContext portletRequestContext = (PortletRequestContext) event.getRequestContext();
254       PortletPreferences portletPreferences = portletRequestContext.getRequest().getPreferences();
255 
256       String searchResultTemplatePath = uiSearchLayoutManager.
257           getUIFormSelectBox(UISearchPageLayoutManager.SEARCH_RESULT_TEMPLATE_SELECTOR).getValue();
258       String searchFormTemplatePath = uiSearchLayoutManager.
259           getUIFormSelectBox(UISearchPageLayoutManager.SEARCH_FORM_TEMPLATE_SELECTOR).getValue();
260 //      String searchPaginatorTemplatePath = uiSearchLayoutManager.
261 //          getUIFormSelectBox(UISearchPageLayoutManager.SEARCH_PAGINATOR_TEMPLATE_SELECTOR).getValue();
262       String searchPageLayoutTemplatePath = uiSearchLayoutManager.
263           getUIFormSelectBox(UISearchPageLayoutManager.SEARCH_PAGE_LAYOUT_SELECTOR).getValue();
264       String itemsPerPage = uiSearchLayoutManager.
265           getUIFormSelectBox(UISearchPageLayoutManager.ITEMS_PER_PAGE_SELECTOR).getValue();
266 
267       String pageMode = uiSearchLayoutManager.getUIFormSelectBox(UISearchPageLayoutManager.PAGE_MODE_SELECTOR).getValue();
268 
269       String basePath = uiSearchLayoutManager.getUIStringInput(UISearchPageLayoutManager.BASE_PATH_INPUT)
270                                              .getValue();
271 
272       portletPreferences.setValue(UIWCMSearchPortlet.REPOSITORY, repository);
273       portletPreferences.setValue(UIWCMSearchPortlet.WORKSPACE, workspace);
274       portletPreferences.setValue(UIWCMSearchPortlet.SEARCH_RESULT_TEMPLATE_PATH,
275                                   searchResultTemplatePath);
276       portletPreferences.setValue(UIWCMSearchPortlet.SEARCH_FORM_TEMPLATE_PATH,
277                                   searchFormTemplatePath);
278 //      portletPreferences.setValue(UIWCMSearchPortlet.SEARCH_PAGINATOR_TEMPLATE_PATH,
279 //                                  searchPaginatorTemplatePath);
280       portletPreferences.setValue(UIWCMSearchPortlet.SEARCH_PAGE_LAYOUT_TEMPLATE_PATH,
281                                   searchPageLayoutTemplatePath);
282       portletPreferences.setValue(UIWCMSearchPortlet.ITEMS_PER_PAGE, itemsPerPage);
283       portletPreferences.setValue(UIWCMSearchPortlet.PAGE_MODE, pageMode);
284       portletPreferences.setValue(UIWCMSearchPortlet.BASE_PATH, basePath);
285       portletPreferences.store();
286 
287       if (Utils.isEditPortletInCreatePageWizard()) {
288         uiApp.addMessage(new ApplicationMessage("UISearchConfig.msg.saving-success",
289                                                 null,
290                                                 ApplicationMessage.INFO));
291       } else {
292         portletRequestContext.setApplicationMode(PortletMode.VIEW);
293       }
294     }
295   }
296 
297   /**
298    * The listener interface for receiving cancelAction events. The class that is
299    * interested in processing a cancelAction event implements this interface,
300    * and the object created with that class is registered with a component using
301    * the component's <code>addCancelActionListener</code> method. When
302    * the cancelAction event occurs, that object's appropriate
303    * method is invoked.
304    *
305    */
306   public static class CancelActionListener extends EventListener<UISearchPageLayoutManager> {
307 
308     /*
309      * (non-Javadoc)
310      * @see
311      * org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui
312      * .event.Event)
313      */
314     public void execute(Event<UISearchPageLayoutManager> event) throws Exception {
315       PortletRequestContext context = (PortletRequestContext) event.getRequestContext();
316       context.setApplicationMode(PortletMode.VIEW);
317     }
318   }
319 
320   /*
321    * (non-Javadoc)
322    *
323    * @see org.exoplatform.ecm.webui.selector.UISelectable#doSelect(java.lang.String,
324    *      java.lang.Object)
325    */
326   public void doSelect(String selectField, Object value) throws Exception {
327     getUIStringInput(selectField).setValue((String) value);
328     Utils.closePopupWindow(this, popupId);
329   }
330 
331   /**
332    * The listener interface for receiving selectTargetPageAction events.
333    * The class that is interested in processing a selectTargetPageAction
334    * event implements this interface, and the object created
335    * with that class is registered with a component using the
336    * component's <code>addSelectTargetPageActionListener</code> method. When
337    * the selectTargetPageAction event occurs, that object's appropriate
338    * method is invoked.
339    *
340    */
341   public static class SelectBasePathActionListener extends EventListener<UISearchPageLayoutManager> {
342 
343     /* (non-Javadoc)
344      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
345      */
346     public void execute(Event<UISearchPageLayoutManager> event) throws Exception {
347       UISearchPageLayoutManager viewerManagementForm = event.getSource();
348       UIPageSelector pageSelector = viewerManagementForm.createUIComponent(UIPageSelector.class, null, null);
349       pageSelector.setSourceComponent(viewerManagementForm, new String[] {BASE_PATH_INPUT});
350       Utils.createPopupWindow(viewerManagementForm, pageSelector, BASE_PATH_SELECTOR_POPUP_WINDOW, 800);
351       viewerManagementForm.setPopupId(BASE_PATH_SELECTOR_POPUP_WINDOW);
352     }
353   }
354 }