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.popup;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.ResourceBundle;
25  
26  import org.exoplatform.calendar.service.Calendar;
27  import org.exoplatform.calendar.service.CalendarService;
28  import org.exoplatform.calendar.service.CalendarSetting;
29  import org.exoplatform.calendar.util.CalendarUtils;
30  import org.exoplatform.calendar.webui.UIActionBar;
31  import org.exoplatform.calendar.webui.UICalendarContainer;
32  import org.exoplatform.calendar.webui.UICalendarPortlet;
33  import org.exoplatform.calendar.webui.UICalendarView;
34  import org.exoplatform.calendar.webui.UICalendarViewContainer;
35  import org.exoplatform.calendar.webui.UICalendars;
36  import org.exoplatform.web.application.AbstractApplicationMessage;
37  import org.exoplatform.web.application.ApplicationMessage;
38  import org.exoplatform.webui.config.annotation.ComponentConfig;
39  import org.exoplatform.webui.config.annotation.EventConfig;
40  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
41  import org.exoplatform.webui.event.Event;
42  import org.exoplatform.webui.event.Event.Phase;
43  import org.exoplatform.webui.event.EventListener;
44  import org.exoplatform.webui.form.UIFormInputWithActions;
45  import org.exoplatform.webui.form.UIFormTabPane;
46  import org.exoplatform.webui.form.input.UICheckBoxInput;
47  
48  /**
49   * Created by The eXo Platform SARL
50   * Author : Hung Nguyen
51   *          hung.nguyen@exoplatform.com
52   * Aus 01, 2007 2:48:18 PM 
53   */
54  @ComponentConfig(
55      lifecycle = UIFormLifecycle.class,
56      template = "system:/groovy/webui/form/UIFormTabPane.gtmpl",
57      events = {
58        @EventConfig(listeners = UICalendarSettingForm.SaveActionListener.class),
59        @EventConfig(listeners = UICalendarSettingForm.ShowAllTimeZoneActionListener.class, phase = Phase.DECODE),
60        @EventConfig(listeners = UICalendarSettingForm.CancelActionListener.class, phase = Phase.DECODE),
61        @EventConfig(listeners = UICalendarSettingForm.AddActionListener.class, phase = Phase.DECODE),
62        @EventConfig(listeners = UIFormTabPane.SelectTabActionListener.class, phase = Phase.DECODE)
63      }
64  )
65  public class UICalendarSettingForm extends UIFormTabPane implements UIPopupComponent
66  {
67    final private static String SETTING_CALENDAR_TAB = "setting" ;
68    final private static String DEFAULT_CALENDAR_TAB = "defaultCalendarTab" ;
69    final private static String FEED_TAB = "feedTab";
70  
71    private Map<String, String> names_ = new HashMap<String, String>() ;
72    public String[] sharedCalendarColors_  = null ;
73    private CalendarSetting calendarSetting_ = null;
74  
75    public UICalendarSettingForm() throws Exception{
76      super("UICalendarSettingForm") ;
77      UICalendarSettingTab setting = new UICalendarSettingTab(SETTING_CALENDAR_TAB) ;//.setRendered(true) ;
78      addUIFormInput(setting) ;
79      setSelectedTab(setting.getId()) ;
80      UICalendarSettingDisplayTab defaultCalendarsTab  = new UICalendarSettingDisplayTab(DEFAULT_CALENDAR_TAB) ;    
81      addUIFormInput(defaultCalendarsTab) ;
82      UICalendarSettingFeedTab uiFeedTab = new UICalendarSettingFeedTab(FEED_TAB);
83      addUIFormInput(uiFeedTab);
84    }
85  
86    @Override
87    public void activate() throws Exception {}
88    @Override
89    public void deActivate() throws Exception {}
90  
91    public void init(CalendarSetting calendarSetting, CalendarService cservice) throws Exception{
92      names_.clear() ;
93      if(calendarSetting != null) {
94        calendarSetting_ = calendarSetting;
95        sharedCalendarColors_ = calendarSetting.getSharedCalendarsColors() ;
96        UICalendarSettingTab settingTab = getChildById(SETTING_CALENDAR_TAB) ;
97        settingTab.setViewType(calendarSetting.getViewType()) ;
98        settingTab.setWeekStartOn(calendarSetting.getWeekStartOn()) ;
99        settingTab.setDateFormat(calendarSetting.getDateFormat()) ;
100       settingTab.setTimeFormat(calendarSetting.getTimeFormat()) ;
101       settingTab.getUIFormSelectBox(UICalendarSettingTab.WORKINGTIME_BEGIN).setOptions(CalendarUtils.getTimesSelectBoxOptions(calendarSetting.getTimeFormat(), 30)) ;
102       settingTab.getUIFormSelectBox(UICalendarSettingTab.WORKINGTIME_END).setOptions(CalendarUtils.getTimesSelectBoxOptions(calendarSetting.getTimeFormat(), 30)) ;
103       settingTab.setTimeZone(calendarSetting.getTimeZone()) ;
104       settingTab.setShowWorkingTimes(calendarSetting.isShowWorkingTime()) ;
105       if(calendarSetting.isShowWorkingTime()) {
106         settingTab.setWorkingBegin(calendarSetting.getWorkingTimeBegin(), CalendarUtils.DATEFORMAT + " " + calendarSetting.getTimeFormat()) ;
107         settingTab.setWorkingEnd(calendarSetting.getWorkingTimeEnd(), CalendarUtils.DATEFORMAT + " " + calendarSetting.getTimeFormat()) ;
108       }
109       settingTab.setSendOption(calendarSetting.getSendOption()) ;
110       if(calendarSetting.getBaseURL() == null) calendarSetting.setBaseURL(CalendarUtils.getServerBaseUrl() + "calendar/iCalRss") ;
111     }
112 
113     initDisplayedCalendarTab(calendarSetting, cservice);
114   }
115 
116   private void initDisplayedCalendarTab(CalendarSetting calendarSetting, CalendarService cservice) throws Exception
117   {
118     String username = CalendarUtils.getCurrentUser() ;
119     UICalendarSettingDisplayTab defaultCalendarsTab = getChildById(DEFAULT_CALENDAR_TAB) ;
120     List<String> filteredCalendars = new LinkedList<String>() ;
121     if(calendarSetting != null) {
122       filteredCalendars.addAll(calendarSetting.getFilterCalendars());
123     }
124 
125     List<Calendar> privateCals = defaultCalendarsTab.getAllPrivateCalendars();
126     if (privateCals != null && !privateCals.isEmpty()) {
127       for (Calendar calendar : privateCals) {
128         names_.put(calendar.getId(), calendar.getName()) ;
129         UICheckBoxInput checkBox = defaultCalendarsTab.getChildById(calendar.getId()) ;
130         if(checkBox == null) {
131           checkBox = new UICheckBoxInput(calendar.getId(), calendar.getId(), true) ;
132           defaultCalendarsTab.addUIFormInput(checkBox) ;
133         }
134         checkBox.setChecked(true) ;
135       }
136     }
137 
138     List<Calendar> sharedCals = defaultCalendarsTab.getSharedCalendars();
139     if(sharedCals != null && !sharedCals.isEmpty()) {
140       for(Calendar calendar : sharedCals) {
141         names_.put(calendar.getId(), calendar.getName()) ;
142         UICheckBoxInput checkBox = defaultCalendarsTab.getChildById(calendar.getId()) ;
143         if(checkBox == null) {
144           checkBox = new UICheckBoxInput(calendar.getId(), calendar.getId(), true) ;
145           defaultCalendarsTab.addUIFormInput(checkBox) ;
146         }
147         checkBox.setChecked(true) ;
148       }
149     }
150 
151     List<Calendar> publicCals = defaultCalendarsTab.getAllPublicCalendars();
152     if (publicCals != null && !publicCals.isEmpty()) {
153       for(Calendar calendar : publicCals) {
154         String groupName = cservice.getGroupCalendars(calendar.getGroups(), false, username).get(0).getName();
155         names_.put(calendar.getId(), CalendarUtils.getGroupCalendarName(
156         groupName.substring(groupName.lastIndexOf("/") + 1), calendar.getName())) ;
157 
158         UICheckBoxInput checkBox = defaultCalendarsTab.getChildById(calendar.getId()) ;
159 
160         if(checkBox == null) {
161           checkBox = new UICheckBoxInput(calendar.getId(), calendar.getId(), true) ;
162           defaultCalendarsTab.addUIFormInput(checkBox) ;
163         }
164         checkBox.setChecked(true) ;
165       }
166     }
167     
168     List<Calendar> otherCals = defaultCalendarsTab.getAllOtherCalendars();
169     if(otherCals != null && !otherCals.isEmpty()) {
170       for(Calendar calendar : otherCals) {
171         names_.put(calendar.getId(), calendar.getName()) ;
172         UICheckBoxInput checkBox = defaultCalendarsTab.getChildById(calendar.getId()) ;
173         if(checkBox == null) {
174           checkBox = new UICheckBoxInput(calendar.getId(), calendar.getId(), true) ;
175           defaultCalendarsTab.addUIFormInput(checkBox) ;
176         }
177         checkBox.setChecked(true) ;
178       }
179     }
180 
181     for (String calId : filteredCalendars) {
182       UICheckBoxInput input = defaultCalendarsTab.getChildById(calId) ;
183       if (input != null) input.setChecked(false) ;
184     }
185   }
186 
187   @Override
188   public String getLabel(ResourceBundle res, String id) {
189     if(names_.get(id) != null) return names_.get(id) ;
190     String label = getId() + ".label." + id;    
191     return res.getString(label);
192   }
193 
194   @Override
195   public String getLabel(String id) {
196     String label;
197     try {
198       label = super.getLabel(id);
199     } catch (Exception e) {
200       label = id;
201     }
202     return label;
203   }
204 
205   /**
206    * return list of unchecked calendars id
207    *
208    * @param calendars
209    * @return
210    */
211   protected List<String> getUnCheckedList(List<Calendar> calendars) {
212     List<String> list = new ArrayList<String>() ;
213     for (Calendar cal : calendars) {
214       UICheckBoxInput input = ((UIFormInputWithActions)getChildById(DEFAULT_CALENDAR_TAB)).getChildById(cal.getId()) ;
215       if(input != null && !input.isChecked()) list.add(input.getId()) ;
216     }
217     return list;
218   }
219 
220 
221   @Override
222   public String[] getActions(){
223     return new String[]{"Save", "Cancel"} ;
224   }
225 
226 
227   static  public class SaveActionListener extends EventListener<UICalendarSettingForm> {
228     @Override
229     public void execute(Event<UICalendarSettingForm> event) throws Exception {
230       UICalendarSettingForm uiForm = event.getSource() ;
231       CalendarSetting calendarSetting = uiForm.calendarSetting_;
232       UICalendarSettingTab settingTab = uiForm.getChildById(UICalendarSettingForm.SETTING_CALENDAR_TAB) ;
233       calendarSetting.setSharedCalendarsColors(uiForm.sharedCalendarColors_) ;
234       calendarSetting.setViewType(settingTab.getViewType()) ;
235       calendarSetting.setWeekStartOn(settingTab.getWeekStartOn()) ;
236       calendarSetting.setDateFormat(settingTab.getDateFormat()) ;
237       calendarSetting.setTimeFormat(settingTab.getTimeFormat()) ;
238       calendarSetting.setTimeZone(settingTab.getTimeZone()) ;
239       calendarSetting.setBaseURL(CalendarUtils.getServerBaseUrl() + "calendar/iCalRss") ;
240       calendarSetting.setSendOption(settingTab.getSendOption()) ;
241 
242       calendarSetting.setShowWorkingTime(settingTab.getShowWorkingTimes()) ;
243       if(settingTab.getShowWorkingTimes()) {
244         if(settingTab.getWorkingBegin().equals(settingTab.getWorkingEnd()) || settingTab.getWorkingBeginTime().after(settingTab.getWorkingEndTime())) {
245           event.getRequestContext()
246                .getUIApplication()
247                .addMessage(new ApplicationMessage("UICalendarSettingForm.msg.working-time-logic",
248                                                   null,
249                                                   AbstractApplicationMessage.WARNING));
250           return ;
251         }
252         calendarSetting.setWorkingTimeBegin(settingTab.getWorkingBegin()) ;
253         calendarSetting.setWorkingTimeEnd(settingTab.getWorkingEnd()) ;
254       }
255 
256       CalendarService calendarService = CalendarUtils.getCalendarService() ;
257       UICalendarPortlet calendarPortlet = uiForm.getAncestorOfType(UICalendarPortlet.class) ;
258       UICalendars uiCalendars = calendarPortlet.findFirstComponentOfType(UICalendars.class) ;
259       List<String> defaultFilterCalendars = new ArrayList<String>() ;
260       List<String> unCheckList = new ArrayList<String>() ;
261 
262       /* set calendar setting filter for private calendar */
263       UICalendarSettingDisplayTab displayTab = uiForm.getChild(UICalendarSettingDisplayTab.class);
264       defaultFilterCalendars = uiForm.getUnCheckedList( displayTab.getAllPrivateCalendars() ) ;
265       calendarSetting.setFilterPrivateCalendars(defaultFilterCalendars.toArray(new String[] {})) ;
266       if(!defaultFilterCalendars.isEmpty()){
267         unCheckList.addAll(defaultFilterCalendars) ;
268         defaultFilterCalendars.clear() ;
269       }
270 
271       /* set calendar setting filter for group calendar */
272       defaultFilterCalendars = uiForm.getUnCheckedList(displayTab.getAllPublicCalendars()) ;
273       calendarSetting.setFilterPublicCalendars(defaultFilterCalendars.toArray(new String[] {})) ;
274       if(!defaultFilterCalendars.isEmpty()){
275         unCheckList.addAll(defaultFilterCalendars) ;
276         defaultFilterCalendars.clear() ;
277       }
278 
279       /* set calendar setting filter for shared and other calendar */
280       defaultFilterCalendars = uiForm.getUnCheckedList(displayTab.getSharedCalendars());
281       defaultFilterCalendars.addAll(uiForm.getUnCheckedList(displayTab.getAllOtherCalendars()));
282       calendarSetting.setFilterSharedCalendars(defaultFilterCalendars.toArray(new String[] {})) ;
283       if(!defaultFilterCalendars.isEmpty()){
284         unCheckList.addAll(defaultFilterCalendars) ;
285         defaultFilterCalendars.clear() ;
286       }
287       uiCalendars.checkAll() ;
288 
289       calendarService.saveCalendarSetting(CalendarUtils.getCurrentUser(), calendarSetting) ;
290       
291       calendarPortlet.setCalendarSetting(calendarSetting) ;
292       String viewType = UICalendarViewContainer.TYPES[Integer.parseInt(calendarSetting.getViewType())] ;
293       UICalendarViewContainer uiViewContainer = calendarPortlet.findFirstComponentOfType(UICalendarViewContainer.class) ;
294       uiViewContainer.initView(viewType, false) ;
295       uiViewContainer.applySeting() ;
296       uiViewContainer.refresh() ;
297       
298       UICalendarContainer uiCalendarContainer = calendarPortlet.findFirstComponentOfType(UICalendarContainer.class);
299       uiCalendarContainer.applySeting();
300       calendarPortlet.findFirstComponentOfType(UICalendarView.class).setCalendarSetting(calendarSetting);
301       
302       calendarPortlet.findFirstComponentOfType(UIActionBar.class).setCurrentView(viewType) ;
303       calendarPortlet.cancelAction() ;
304       event.getRequestContext().addUIComponentToUpdateByAjax(calendarPortlet) ;
305     }
306   }
307 
308   static  public class ShowAllTimeZoneActionListener extends EventListener<UICalendarSettingForm> {
309     @Override
310     public void execute(Event<UICalendarSettingForm> event) throws Exception {
311       UICalendarSettingForm uiForm = event.getSource() ;
312       UICalendarSettingTab calendarSettingTab = uiForm.getChildById(UICalendarSettingForm.SETTING_CALENDAR_TAB) ;
313       uiForm.getUIFormSelectBox(UICalendarSettingTab.TIMEZONE).setOptions(calendarSettingTab.getTimeZones(null)) ;
314       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getAncestorOfType(UIPopupAction.class)) ;
315     }
316   }
317   static  public class CancelActionListener extends EventListener<UICalendarSettingForm> {
318     @Override
319     public void execute(Event<UICalendarSettingForm> event) throws Exception {
320       UICalendarSettingForm uiForm = event.getSource() ;
321       UICalendarPortlet calendarPortlet = uiForm.getAncestorOfType(UICalendarPortlet.class) ;
322       calendarPortlet.cancelAction() ;
323     }
324   }
325   
326   static  public class AddActionListener extends EventListener<UICalendarSettingForm> {
327     @Override
328     public void execute(Event<UICalendarSettingForm> event) throws Exception {
329       UICalendarSettingForm uiform = event.getSource() ;   
330       UIPopupContainer popupContainer = uiform.getAncestorOfType(UIPopupContainer.class) ;
331       UIPopupAction popupAction = popupContainer.getChild(UIPopupAction.class) ;
332       UIEditFeed uiEditFeed = popupAction.activate(UIEditFeed.class, 500) ;
333       uiEditFeed.setNew(true);
334       event.getRequestContext().addUIComponentToUpdateByAjax(popupAction) ;      
335     }
336   }
337   
338 }