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.calendar.webui;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.exoplatform.calendar.model.Event;
24  import org.exoplatform.calendar.model.query.EventQuery;
25  import org.exoplatform.calendar.service.CalendarEvent;
26  import org.exoplatform.calendar.service.CalendarService;
27  import org.exoplatform.calendar.service.EventPageList;
28  import org.exoplatform.calendar.service.GroupCalendarData;
29  import org.exoplatform.calendar.service.Utils;
30  import org.exoplatform.calendar.util.CalendarUtils;
31  import org.exoplatform.services.log.ExoLogger;
32  import org.exoplatform.services.log.Log;
33  import org.exoplatform.web.application.ApplicationMessage;
34  import org.exoplatform.webui.config.annotation.ComponentConfig;
35  import org.exoplatform.webui.config.annotation.EventConfig;
36  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
37  import org.exoplatform.webui.event.EventListener;
38  import org.exoplatform.webui.form.UIForm;
39  import org.exoplatform.webui.form.UIFormStringInput;
40  import org.exoplatform.webui.form.validator.SpecialCharacterValidator;
41  
42  /**
43   * Created by The eXo Platform SARL
44   * Author : Hung Nguyen
45   *          hung.nguyen@exoplatform.com
46   * Aus 01, 2007 2:48:18 PM 
47   */
48  @ComponentConfig(
49      lifecycle = UIFormLifecycle.class,
50      template = "app:/templates/calendar/webui/UISearchForm.gtmpl",
51      events = {
52        @EventConfig(listeners = UISearchForm.SearchActionListener.class)
53      }
54  )
55  public class UISearchForm extends UIForm {
56    private static final Log log = ExoLogger.getExoLogger(UISearchForm.class);
57    
58    final static  private String FIELD_SEARCHVALUE = "value" ;
59  
60    public UISearchForm() throws Exception {
61      addChild(new UIFormStringInput(FIELD_SEARCHVALUE, FIELD_SEARCHVALUE, null)) ;
62    }
63    public String getSearchValue() {
64      return getUIStringInput(FIELD_SEARCHVALUE).getValue() ;
65    }
66    public String[] getPublicCalendars() throws Exception{
67        String[] groups = CalendarUtils.getUserGroups(CalendarUtils.getCurrentUser()) ;
68        CalendarService calendarService = CalendarUtils.getCalendarService() ;
69        Map<String, String> map = new HashMap<String, String> () ;    
70        for(GroupCalendarData group : calendarService.getGroupCalendars(groups, true, CalendarUtils.getCurrentUser())) {
71          for(org.exoplatform.calendar.service.Calendar calendar : group.getCalendars()) {
72            map.put(calendar.getId(), calendar.getId()) ;          
73          }
74        }
75        return map.values().toArray(new String[map.values().size()] ) ;
76    }
77    static  public class SearchActionListener extends EventListener<UISearchForm> {
78      @Override
79      public void execute(org.exoplatform.webui.event.Event<UISearchForm> event) throws Exception {
80        UISearchForm uiForm = event.getSource() ;
81        String text = uiForm.getSearchValue() ;
82       if(CalendarUtils.isEmpty(text))   {
83          event.getRequestContext()
84               .getUIApplication()
85               .addMessage(new ApplicationMessage("UISearchForm.msg.no-text-to-search", null));
86          return ;
87        }
88       
89        try {
90          EventQuery eventQuery = new EventQuery() ;
91          eventQuery.setText(text) ;
92          UICalendarPortlet calendarPortlet = uiForm.getAncestorOfType(UICalendarPortlet.class) ;
93          UICalendarViewContainer calendarViewContainer = calendarPortlet.findFirstComponentOfType(UICalendarViewContainer.class) ;
94          String currentView = calendarViewContainer.getRenderedChild().getId() ;
95          if(calendarViewContainer.getRenderedChild() instanceof UIWeekView) {
96            if(((UIWeekView)calendarViewContainer.getRenderedChild()).isShowCustomView()) {
97              currentView = UICalendarViewContainer.WORKING_VIEW;
98            }
99          }
100         calendarViewContainer.initView(UICalendarViewContainer.LIST_VIEW, false) ;
101         UIListView uiListView = calendarViewContainer.findFirstComponentOfType(UIListView.class) ;
102         if(!uiListView.isDisplaySearchResult()) {
103           uiListView.setLastViewId(currentView) ;
104         }
105 
106         UICalendars uiCalendars = uiForm.getAncestorOfType(UICalendarPortlet.class).findFirstComponentOfType(UICalendars.class);
107         eventQuery = uiCalendars.getEventQuery(eventQuery);
108         uiListView.setViewType(UICalendarView.TYPE_BOTH);
109         uiListView.setSortedField(UIListView.EVENT_START);
110         uiListView.setIsAscending(false);
111         eventQuery.setOrderBy(new String[] { Utils.EXO_FROM_DATE_TIME });
112         eventQuery.setOrderType(Utils.DESCENDING);
113         uiListView.setEventQuery(eventQuery);
114         uiListView.setDisplaySearchResult(true);
115         List<Event> allEvents = uiListView.getAllEvents(eventQuery);
116         uiListView.update(new EventPageList(allEvents, 10));
117         uiListView.setSelectedEvent(null);
118         uiListView.setLastUpdatedEventId(null);
119         calendarViewContainer.findFirstComponentOfType(UIPreview.class).setEvent(null) ;
120         UIActionBar uiActionBar = calendarPortlet.findFirstComponentOfType(UIActionBar.class) ;
121         uiActionBar.setCurrentView(UICalendarViewContainer.LIST_VIEW) ;
122         event.getRequestContext().addUIComponentToUpdateByAjax(uiActionBar) ;
123         event.getRequestContext().addUIComponentToUpdateByAjax(uiForm) ;
124         event.getRequestContext().addUIComponentToUpdateByAjax(calendarViewContainer) ;
125       } catch (Exception e) {
126         if (log.isDebugEnabled()) {
127           log.debug("Fail to search the calendar", e);
128         }
129         return;
130       }
131     }
132   }
133 }