View Javadoc
1   /*
2    * Copyright (C) 2003-2011 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (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 Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package org.exoplatform.social.webui.space;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Calendar;
23  import java.util.Collections;
24  import java.util.Comparator;
25  import java.util.Date;
26  import java.util.EnumSet;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  import java.util.MissingResourceException;
33  import java.util.ResourceBundle;
34  
35  import org.exoplatform.container.ExoContainer;
36  import org.exoplatform.container.ExoContainerContext;
37  import org.exoplatform.portal.application.PortalRequestContext;
38  import org.exoplatform.portal.config.DataStorage;
39  import org.exoplatform.portal.config.UserACL;
40  import org.exoplatform.portal.config.model.ModelObject;
41  import org.exoplatform.portal.config.model.Page;
42  import org.exoplatform.portal.mop.Described;
43  import org.exoplatform.portal.mop.SiteType;
44  import org.exoplatform.portal.mop.Visibility;
45  import org.exoplatform.portal.mop.page.PageContext;
46  import org.exoplatform.portal.mop.page.PageKey;
47  import org.exoplatform.portal.mop.page.PageService;
48  import org.exoplatform.portal.mop.page.PageState;
49  import org.exoplatform.portal.mop.user.UserNavigation;
50  import org.exoplatform.portal.webui.page.UIPageSelector;
51  import org.exoplatform.portal.webui.page.UIWizardPageSetInfo;
52  import org.exoplatform.portal.webui.portal.UIPortal;
53  import org.exoplatform.portal.webui.util.Util;
54  import org.exoplatform.portal.webui.workspace.UIPortalApplication;
55  import org.exoplatform.services.log.ExoLogger;
56  import org.exoplatform.services.log.Log;
57  import org.exoplatform.services.resources.LocaleConfig;
58  import org.exoplatform.services.resources.LocaleConfigService;
59  import org.exoplatform.services.resources.ResourceBundleService;
60  import org.exoplatform.social.core.space.SpaceUtils;
61  import org.exoplatform.web.application.ApplicationMessage;
62  import org.exoplatform.webui.application.WebuiRequestContext;
63  import org.exoplatform.webui.application.portlet.PortletRequestContext;
64  import org.exoplatform.webui.core.UIApplication;
65  import org.exoplatform.webui.core.UIComponent;
66  import org.exoplatform.webui.core.UIPopupWindow;
67  import org.exoplatform.webui.core.model.SelectItemOption;
68  import org.exoplatform.webui.event.Event;
69  import org.exoplatform.webui.event.EventListener;
70  import org.exoplatform.webui.exception.MessageException;
71  import org.exoplatform.webui.form.UIFormDateTimeInput;
72  import org.exoplatform.webui.form.UIFormInputBase;
73  import org.exoplatform.webui.form.UIFormInputIconSelector;
74  import org.exoplatform.webui.form.UIFormInputSet;
75  import org.exoplatform.webui.form.UIFormSelectBox;
76  import org.exoplatform.webui.form.UIFormStringInput;
77  import org.exoplatform.webui.form.UIFormTabPane;
78  import org.exoplatform.webui.form.input.UICheckBoxInput;
79  import org.exoplatform.webui.form.validator.DateTimeValidator;
80  import org.exoplatform.webui.form.validator.IdentifierValidator;
81  import org.exoplatform.webui.form.validator.MandatoryValidator;
82  import org.exoplatform.webui.form.validator.StringLengthValidator;
83  import org.exoplatform.webui.form.validator.Validator;
84  
85  public class UIPageNodeForm extends UIFormTabPane {
86  
87    private TreeNode                     pageNode_;
88  
89    private Object                       selectedParent;
90  
91    /**
92     * PageNavigation to which editted PageNode belongs
93     */
94    private UserNavigation               contextPageNavigation;
95  
96    final private static String          SHOW_PUBLICATION_DATE  = "showPublicationDate";
97  
98    final private static String          START_PUBLICATION_DATE = "startPublicationDate";
99  
100   final private static String          END_PUBLICATION_DATE   = "endPublicationDate";
101 
102   final private static String          VISIBLE                = "visible";
103 
104   private Map<String, Described.State> cachedLabels;
105 
106   private String                       selectedLocale;
107 
108   private static final String          I18N_LABEL             = "i18nizedLabel";
109 
110   private static final String          LANGUAGES              = "languages";
111 
112   private static final String          LANGUAGES_ONCHANGE     = "ChangeLanguage";
113 
114   private static final String          SWITCH_MODE            = "switchmode";
115 
116   private static final String          SWITCH_MODE_ONCHANGE   = "SwitchLabelMode";
117 
118   private static final String          LABEL                  = "label";
119   
120   private static final Log LOG = ExoLogger.getLogger(UIPageNodeForm.class);
121   
122   public UIPageNodeForm() throws Exception {
123     super("UIPageNodeForm");
124 
125     UIFormInputSet uiSettingSet = new UIFormInputSet("PageNodeSetting");
126 
127     UICheckBoxInput uiDateInputCheck = new UICheckBoxInput(SHOW_PUBLICATION_DATE, null, false);
128     UICheckBoxInput uiVisibleCheck = new UICheckBoxInput(VISIBLE, null, true);
129     UICheckBoxInput uiSwitchLabelMode = new UICheckBoxInput(SWITCH_MODE, null, true);
130     
131     uiDateInputCheck.setOnChange("SwitchPublicationDate");
132     uiVisibleCheck.setOnChange("SwitchVisible");
133     uiSwitchLabelMode.setOnChange(SWITCH_MODE_ONCHANGE);
134 
135     UIFormSelectBox uiFormLanguagesSelectBox = new UIFormSelectBox(LANGUAGES, null, null);
136     initLanguageSelectBox(uiFormLanguagesSelectBox);
137     uiFormLanguagesSelectBox.setOnChange(LANGUAGES_ONCHANGE);
138 
139     uiSettingSet.addUIFormInput(new UIFormStringInput("URI", "URI", null).setDisabled(true))
140                 .addUIFormInput(new UIFormStringInput("name", "name", null).addValidator(MandatoryValidator.class)
141                                                                            .addValidator(StringLengthValidator.class,
142                                                                                          3,
143                                                                                          30)
144                                                                            .addValidator(IdentifierValidator.class))
145                 .addUIFormInput(uiSwitchLabelMode)
146                 .addUIFormInput(new UIFormStringInput(LABEL, LABEL, null).addValidator(StringLengthValidator.class, 
147                                                                                        3,
148                                                                                        120))
149                 .addUIFormInput(uiFormLanguagesSelectBox)
150                 .addUIFormInput(new UIFormStringInput(I18N_LABEL, null, null).setMaxLength(255)
151                                                                              .addValidator(StringLengthValidator.class, 
152                                                                                            3,
153                                                                                            120))
154                 .addUIFormInput(uiVisibleCheck.setChecked(true))
155                 .addUIFormInput(uiDateInputCheck)
156                 .addUIFormInput(new UIFormDateTimeInput(START_PUBLICATION_DATE, null, null).addValidator(DateTimeValidator.class))
157                 .addUIFormInput(new UIFormDateTimeInput(END_PUBLICATION_DATE, null, null).addValidator(DateTimeValidator.class));
158 
159     addUIFormInput(uiSettingSet);
160     setSelectedTab(uiSettingSet.getId());
161 
162     UIPageSelector uiPageSelector = createUIComponent(UIPageSelector.class, null, null);
163     uiPageSelector.configure("UIPageSelector", "pageRef");
164     addUIFormInput(uiPageSelector);
165 
166     UIFormInputIconSelector uiIconSelector = new UIFormInputIconSelector("Icon", "icon");
167     addUIFormInput(uiIconSelector);
168     setActions(new String[] { "Save", "Back" });
169   }
170 
171   public TreeNode getPageNode() {
172     return pageNode_;
173   }
174 
175   public void setValues(TreeNode pageNode) throws Exception {
176     pageNode_ = pageNode;
177     selectedLocale = getUIFormSelectBox(LANGUAGES).getValue();
178     cachedLabels = new HashMap<String, Described.State>();
179     if (pageNode == null) {
180       getUIStringInput("name").setReadOnly(false);
181       getChild(UIFormInputIconSelector.class).setSelectedIcon("Default");
182       setShowPublicationDate(false);
183       switchLabelMode(true);
184       return;
185     }
186     getUIStringInput("name").setReadOnly(true);
187     invokeGetBindingBean(pageNode_);
188   }
189 
190   private void initLanguageSelectBox(UIFormSelectBox langSelectBox) {
191     List<SelectItemOption<String>> lang = new ArrayList<SelectItemOption<String>>();
192     LocaleConfigService localeService = getApplicationComponent(LocaleConfigService.class);
193     Locale currentLocale = ((PortletRequestContext) WebuiRequestContext.getCurrentInstance()).getLocale();
194     Iterator<LocaleConfig> i = localeService.getLocalConfigs().iterator();
195     String displayName = null;
196     String language = null;
197     String country = null;
198     String defaultValue = null;
199     SelectItemOption<String> option;
200     while (i.hasNext()) {
201       LocaleConfig config = i.next();
202       Locale locale = config.getLocale();
203 
204       language = locale.getLanguage();
205       country = locale.getCountry();
206       if (country != null && country.length() > 0) {
207         language = language + "_" + country;
208       }
209 
210       ResourceBundle localeResourceBundle;
211 
212       displayName = null;
213       try {
214         localeResourceBundle = getResourceBundle(currentLocale);
215         String key = "Locale." + language;
216         String translation = localeResourceBundle.getString(key);
217         displayName = translation;
218       } catch (MissingResourceException e) {
219         displayName = capitalizeFirstLetter(locale.getDisplayName(currentLocale));
220       } catch (Exception e) {
221         LOG.debug("Could not get resource bundle.");
222       }
223 
224       option = new SelectItemOption<String>(displayName, language);
225       if (locale.getDisplayName().equals(currentLocale.getDisplayName())) {
226         option.setSelected(true);
227         defaultValue = language;
228       }
229 
230       lang.add(option);
231     }
232 
233     Collections.sort(lang, new LanguagesComparator());
234     langSelectBox.setOptions(lang);
235     langSelectBox.setValue(defaultValue);
236   }
237 
238   private ResourceBundle getResourceBundle(Locale locale) throws Exception {
239     ExoContainer appContainer = ExoContainerContext.getCurrentContainer();
240     ResourceBundleService service = (ResourceBundleService) appContainer.getComponentInstanceOfType(ResourceBundleService.class);
241     ResourceBundle res = service.getResourceBundle("locale.portal.webui", locale);
242     return res;
243   }
244 
245   private String capitalizeFirstLetter(String word) {
246     if (word == null) {
247       return null;
248     }
249     if (word.length() == 0) {
250       return word;
251     }
252     StringBuilder result = new StringBuilder(word);
253     result.replace(0, 1, result.substring(0, 1).toUpperCase());
254     return result.toString();
255   }
256 
257   private class LanguagesComparator implements Comparator<SelectItemOption<String>> {
258     public int compare(SelectItemOption<String> o1, SelectItemOption<String> o2) {
259       return o1.getLabel().compareToIgnoreCase(o2.getLabel());
260     }
261   }
262 
263   public void invokeGetBindingBean(Object bean) throws Exception {
264     super.invokeGetBindingBean(bean);
265 
266     TreeNode pageNode = (TreeNode) bean;
267 
268     String icon = pageNode.getIcon();
269     if (icon == null || icon.length() < 0)
270       icon = "Default";
271     getChild(UIFormInputIconSelector.class).setSelectedIcon(icon);
272     getUIStringInput(LABEL).setValue(pageNode.getLabel());
273     Map<Locale, Described.State> i18nizedLabels = pageNode.getI18nizedLabels();
274     if (i18nizedLabels != null) {
275       for (Locale key : i18nizedLabels.keySet()) {
276         String locale = !("".equals(key.getCountry())) ? key.getLanguage() + "_" + key.getCountry()
277                                                : key.getLanguage(); 
278         cachedLabels.put(locale, i18nizedLabels.get(key));
279       }
280     }
281 
282     if (cachedLabels.get(selectedLocale) != null) {
283       getUIStringInput(I18N_LABEL).setValue(cachedLabels.get(selectedLocale).getName());
284     }
285 
286     if (pageNode.getVisibility() == Visibility.SYSTEM) {
287       UIFormInputSet uiSettingSet = getChildById("PageNodeSetting");
288       uiSettingSet.removeChildById(VISIBLE);
289       uiSettingSet.removeChildById(SHOW_PUBLICATION_DATE);
290       uiSettingSet.removeChildById(START_PUBLICATION_DATE);
291       uiSettingSet.removeChildById(END_PUBLICATION_DATE);
292     } else {
293       Visibility visibility = pageNode.getVisibility();
294       boolean isVisible = visibility == null
295           || EnumSet.of(Visibility.DISPLAYED, Visibility.TEMPORAL).contains(visibility);
296       getUICheckBoxInput(VISIBLE).setChecked(isVisible);
297       getUICheckBoxInput(SHOW_PUBLICATION_DATE).setChecked(Visibility.TEMPORAL.equals(visibility));
298       setShowCheckPublicationDate(isVisible);
299       Calendar cal = Calendar.getInstance();
300       if (pageNode.getStartPublicationTime() != -1) {
301         cal.setTime(new Date(pageNode.getStartPublicationTime()));
302         getUIFormDateTimeInput(START_PUBLICATION_DATE).setCalendar(cal);
303       } else
304         getUIFormDateTimeInput(START_PUBLICATION_DATE).setValue(null);
305       if (pageNode.getEndPublicationTime() != -1) {
306         cal.setTime(new Date(pageNode.getEndPublicationTime()));
307         getUIFormDateTimeInput(END_PUBLICATION_DATE).setCalendar(cal);
308       } else
309         getUIFormDateTimeInput(END_PUBLICATION_DATE).setValue(null);
310     }
311 
312     boolean isExtendedMode = true;
313     if (pageNode.getNode().getLabel() != null && pageNode.getNode().getLabel().trim().length() > 0) {
314       isExtendedMode = false;
315     }
316 
317     getUICheckBoxInput(SWITCH_MODE).setChecked(isExtendedMode);
318     this.switchLabelMode(isExtendedMode);
319   }
320 
321   public void invokeSetBindingBean(Object bean) throws Exception {
322     super.invokeSetBindingBean(bean);
323     TreeNode node = (TreeNode) bean;
324 
325     if (node.getVisibility() != Visibility.SYSTEM) {
326       Visibility visibility;
327       if (getUICheckBoxInput(VISIBLE).isChecked()) {
328         UICheckBoxInput showPubDate = getUICheckBoxInput(SHOW_PUBLICATION_DATE); 
329         visibility = showPubDate.isChecked() ? Visibility.TEMPORAL : Visibility.DISPLAYED;
330       } else {
331         visibility = Visibility.HIDDEN;
332       }
333       node.setVisibility(visibility);
334 
335       Calendar cal = getUIFormDateTimeInput(START_PUBLICATION_DATE).getCalendar();
336       Date date = (cal != null) ? cal.getTime() : null;
337       node.setStartPublicationTime(date == null ? -1 : date.getTime());
338       cal = getUIFormDateTimeInput(END_PUBLICATION_DATE).getCalendar();
339       date = (cal != null) ? cal.getTime() : null;
340       node.setEndPublicationTime(date == null ? -1 : date.getTime());
341     }
342 
343     cachedLabels.put(getUIFormSelectBox(LANGUAGES).getValue(),
344                      new Described.State(getUIStringInput(I18N_LABEL).getValue(), null));
345     Map<Locale, Described.State> labels = new HashMap<Locale, Described.State>(cachedLabels.size());
346     getUIFormSelectBox(LANGUAGES).getValue();
347     for (String strLocale : cachedLabels.keySet()) {
348       Locale locale;
349       if (strLocale.contains("_")) {
350         String[] arr = strLocale.split("_");
351         if (arr.length > 2) {
352           locale = new Locale(arr[0], arr[1], arr[2]);
353         } else {
354           locale = new Locale(arr[0], arr[1]);
355         }
356       } else {
357         locale = new Locale(strLocale);
358       }
359 
360       labels.put(locale, cachedLabels.get(strLocale));
361     }
362 
363     node.setI18nizedLabels(labels);
364 
365     if (getUICheckBoxInput(SWITCH_MODE).getValue().toString().equals("true"))
366       node.setLabel(null);
367     else if (node.getLabel() == null)
368       node.setLabel(node.getName());
369   }
370 
371   public void setShowCheckPublicationDate(boolean show) {
372     getUICheckBoxInput(VISIBLE).setChecked(show);
373     UICheckBoxInput uiForm = getUICheckBoxInput(SHOW_PUBLICATION_DATE);
374     uiForm.setRendered(show);
375     setShowPublicationDate(show && uiForm.isChecked());
376   }
377 
378   public void setShowPublicationDate(boolean show) {
379     getUIFormDateTimeInput(START_PUBLICATION_DATE).setRendered(show);
380     getUIFormDateTimeInput(END_PUBLICATION_DATE).setRendered(show);
381   }
382 
383   public Object getSelectedParent() {
384     return selectedParent;
385   }
386 
387   public void setSelectedParent(Object obj) {
388     this.selectedParent = obj;
389   }
390 
391   public void processRender(WebuiRequestContext context) throws Exception {
392     super.processRender(context);
393 
394     UIPageSelector uiPageSelector = getChild(UIPageSelector.class);
395     if (uiPageSelector == null)
396       return;
397     UIPopupWindow uiPopupWindowPage = uiPageSelector.getChild(UIPopupWindow.class);
398     if (uiPopupWindowPage == null)
399       return;
400     uiPopupWindowPage.processRender(context);
401   }
402 
403   public String getOwner() {
404     return contextPageNavigation.getKey().getName();
405   }
406 
407   public SiteType getOwnerType() {
408     return contextPageNavigation.getKey().getType();
409   }
410 
411   public void setContextPageNavigation(UserNavigation _contextPageNav) {
412     this.contextPageNavigation = _contextPageNav;
413   }
414 
415   public UserNavigation getContextPageNavigation() {
416     return this.contextPageNavigation;
417   }
418 
419   static public class SaveActionListener extends EventListener<UIPageNodeForm> {
420     public void execute(Event<UIPageNodeForm> event) throws Exception {
421       WebuiRequestContext ctx = event.getRequestContext();
422       UIPageNodeForm uiPageNodeForm = event.getSource();
423       UserNavigation contextNavigation = uiPageNodeForm.getContextPageNavigation();
424       UIApplication uiPortalApp = ctx.getUIApplication();
425       TreeNode pageNode = uiPageNodeForm.getPageNode();
426 
427       if (pageNode == null
428           || (pageNode.getVisibility() != Visibility.SYSTEM && uiPageNodeForm.getUICheckBoxInput(SHOW_PUBLICATION_DATE)
429                                                                              .isChecked())) {
430         Calendar currentCalendar = Calendar.getInstance();
431         currentCalendar.set(currentCalendar.get(Calendar.YEAR),
432                             currentCalendar.get(Calendar.MONTH),
433                             currentCalendar.get(Calendar.DAY_OF_MONTH),
434                             0,
435                             0,
436                             0);
437         Date currentDate = currentCalendar.getTime();
438 
439         Calendar startCalendar = uiPageNodeForm.getUIFormDateTimeInput(UIWizardPageSetInfo.START_PUBLICATION_DATE)
440                                                .getCalendar();
441         Date startDate = startCalendar != null ? startCalendar.getTime() : currentDate;
442         Calendar endCalendar = uiPageNodeForm.getUIFormDateTimeInput(UIWizardPageSetInfo.END_PUBLICATION_DATE)
443                                              .getCalendar();
444         Date endDate = endCalendar != null ? endCalendar.getTime() : null;
445 
446         // Case 1: current date after start date
447         if (currentDate.after(startDate)) {
448           Object[] args = {};
449           uiPortalApp.addMessage(new ApplicationMessage("UIPageNodeForm.msg.currentDateBeforeStartDate",
450                                                         args,
451                                                         ApplicationMessage.WARNING));
452           return;
453         }
454         // Case 2: start date after end date
455         else if ((endCalendar != null) && (startCalendar != null) && (startDate.after(endDate))) {
456           Object[] args = {};
457           uiPortalApp.addMessage(new ApplicationMessage("UIPageNodeForm.msg.startDateBeforeEndDate",
458                                                         args,
459                                                         ApplicationMessage.WARNING));
460           return;
461         }
462         // Case 3: start date is null and current date after end date
463         else if ((endCalendar != null) && (currentDate.after(endDate))) {
464           Object[] args = {};
465           uiPortalApp.addMessage(new ApplicationMessage("UIPageNodeForm.msg.currentDateBeforeEndDate",
466                                                         args,
467                                                         ApplicationMessage.WARNING));
468           return;
469         }
470 
471       }
472 
473       UIFormStringInput nameInput = uiPageNodeForm.getUIStringInput("name");
474       String nodeName = nameInput.getValue();
475 
476       TreeNode selectedParent = (TreeNode) uiPageNodeForm.getSelectedParent();
477       if (pageNode == null && selectedParent.getChild(nodeName) != null) {
478         uiPortalApp.addMessage(new ApplicationMessage("UIPageNodeForm.msg.SameName", null));
479         return;
480       }
481 
482       // Add node that need to be rebased to context
483       if (pageNode == null) {
484         pageNode = selectedParent.addChild(nodeName);
485         uiPageNodeForm.pageNode_ = pageNode;
486       }
487 
488       UIPageSelector pageSelector = uiPageNodeForm.getChild(UIPageSelector.class);
489       if (pageSelector.getPage() == null) {
490         pageSelector.setValue(null);
491       } else {
492         PageContext pageContext = pageSelector.getPage();
493         DataStorage storage = uiPageNodeForm.getApplicationComponent(DataStorage.class);
494         PageService pageService = uiPageNodeForm.getApplicationComponent(PageService.class);
495         if (pageService.loadPage(pageContext.getKey()) == null) {
496             pageService.savePage(pageContext);
497 
498             //
499             Page page = new Page();
500             page.setOwnerType(pageContext.getKey().getSite().getTypeName());
501             page.setOwnerId(pageContext.getKey().getSite().getName());
502             page.setName(pageContext.getKey().getName());
503             String title = pageContext.getState().getDisplayName();
504             String[] accessPermission = pageContext.getState().getAccessPermissions() == null ? null : pageContext
505                     .getState().getAccessPermissions()
506                     .toArray(new String[pageContext.getState().getAccessPermissions().size()]);
507             if (title == null || title.trim().length() < 1) {
508                 title = page.getName();
509             }
510             page.setTitle(title);
511             page.setShowMaxWindow(false);
512             page.setAccessPermissions(accessPermission);
513             page.setEditPermission(pageContext.getState().getEditPermission());
514             page.setModifiable(true);
515             if (page.getChildren() == null) {
516                 page.setChildren(new ArrayList<ModelObject>());
517             }
518 
519             storage.save(page);
520             pageSelector.setValue(page.getPageId());
521         }
522       }
523 
524       if (pageNode.getLabel() == null)
525         pageNode.setLabel(pageNode.getName());
526       uiPageNodeForm.invokeSetBindingBean(pageNode);
527 
528       UIFormInputIconSelector uiIconSelector = uiPageNodeForm.getChild(UIFormInputIconSelector.class);
529       if (uiIconSelector.getSelectedIcon().equals("Default"))
530         pageNode.setIcon(null);
531       else
532         pageNode.setIcon(uiIconSelector.getSelectedIcon());
533 
534       UIPopupWindow uiPopup = uiPageNodeForm.getParent();
535       UISpaceNavigationManagement uiSpaceNav = uiPopup.getParent();
536       UISpaceNavigationNodeSelector uiNodeSelector = uiSpaceNav.getChild(UISpaceNavigationNodeSelector.class);
537       if (pageNode != null) {
538         uiNodeSelector.getUserNodeLabels().put(pageNode.getId(), pageNode.getI18nizedLabels());
539       } 
540       uiNodeSelector.save();
541       uiSpaceNav.setOwner(contextNavigation.getKey().getName());
542       uiSpaceNav.setOwnerType(contextNavigation.getKey().getTypeName());
543       uiNodeSelector.setEdittedNavigation(contextNavigation);
544       uiNodeSelector.initTreeData();
545       uiPopup.setShow(false);
546       SpaceUtils.updateWorkingWorkSpace();
547     }
548   }
549 
550   static public class BackActionListener extends EventListener<UIPageNodeForm> {
551 
552     public void execute(Event<UIPageNodeForm> event) throws Exception {
553 
554     }
555   }
556 
557   public static class ChangeLanguageActionListener extends EventListener<UIPageNodeForm> {
558     @Override
559     public void execute(Event<UIPageNodeForm> event) throws Exception {
560       UIPageNodeForm uiForm = event.getSource();
561       UIFormSelectBox languageSelection = uiForm.getUIFormSelectBox(LANGUAGES);
562       UIFormStringInput label = uiForm.getUIStringInput(I18N_LABEL);
563       uiForm.updateCachedLabels(uiForm.getSelectedLocale(), label.getValue());
564 
565       uiForm.setSelectedLocale(languageSelection.getValue());
566       label.setValue(uiForm.getLabelOnLocale(uiForm.getSelectedLocale()));
567       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm); 
568     }
569   }
570 
571   private String getLabelOnLocale(String locale) {
572     if (cachedLabels.get(locale) != null) {
573       return cachedLabels.get(locale).getName();
574     }
575 
576     return null;
577   }
578 
579   private void updateCachedLabels(String locale, String label) {
580     cachedLabels.put(locale, new Described.State(label, null));
581   }
582 
583   public void setSelectedLocale(String selectedLocale) {
584     this.selectedLocale = selectedLocale;
585   }
586 
587   public String getSelectedLocale() {
588     return selectedLocale;
589   }
590 
591   static public class SwitchPublicationDateActionListener extends EventListener<UIPageNodeForm> {
592     public void execute(Event<UIPageNodeForm> event) throws Exception {
593       UIPageNodeForm uiForm = event.getSource();
594       boolean isCheck = uiForm.getUICheckBoxInput(SHOW_PUBLICATION_DATE).isChecked();
595       uiForm.setShowPublicationDate(isCheck);
596       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
597     }
598   }
599 
600   static public class SwitchVisibleActionListener extends EventListener<UIPageNodeForm> {
601     @Override
602     public void execute(Event<UIPageNodeForm> event) throws Exception {
603       UIPageNodeForm uiForm = event.getSource();
604       boolean isCheck = uiForm.getUICheckBoxInput(VISIBLE).isChecked();
605       uiForm.setShowCheckPublicationDate(isCheck);
606       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
607     }
608   }
609 
610   static public class SwitchLabelModeActionListener extends EventListener<UIPageNodeForm> {
611     @Override
612     public void execute(Event<UIPageNodeForm> event) throws Exception {
613       UIPageNodeForm uiForm = event.getSource();
614       boolean isExtendedMode = uiForm.getUICheckBoxInput(SWITCH_MODE).isChecked();
615       uiForm.switchLabelMode(isExtendedMode);
616       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
617     }
618   }
619 
620   static public class ClearPageActionListener extends EventListener<UIPageNodeForm> {
621     public void execute(Event<UIPageNodeForm> event) throws Exception {
622       UIPageNodeForm uiForm = event.getSource();
623       UIPageSelector pageSelector = uiForm.findFirstComponentOfType(UIPageSelector.class);
624       pageSelector.setPage(null);
625       event.getRequestContext().addUIComponentToUpdateByAjax(pageSelector);
626     }
627   }
628 
629   static public class CreatePageActionListener extends EventListener<UIPageNodeForm> {
630     public void execute(Event<UIPageNodeForm> event) throws Exception {
631       UIPageNodeForm uiForm = event.getSource();
632       UIPageSelector pageSelector = uiForm.findFirstComponentOfType(UIPageSelector.class);
633 
634       PortalRequestContext pcontext = Util.getPortalRequestContext();
635       UIPortalApplication uiPortalApp = Util.getUIPortalApplication();
636 
637       UIFormInputSet uiInputSet = pageSelector.getChild(UIFormInputSet.class);
638       List<UIComponent> children = uiInputSet.getChildren();
639       /*********************************************************************/
640       for (UIComponent uiChild : children) {
641         if (uiChild instanceof UIFormInputBase) {
642           UIFormInputBase uiInput = (UIFormInputBase) uiChild;
643           if (!uiInput.isValid())
644             continue;
645           List<Validator> validators = uiInput.getValidators();
646           if (validators == null)
647             continue;
648           try {
649             for (Validator validator : validators)
650               validator.validate(uiInput);
651           } catch (MessageException ex) {
652             uiPortalApp.addMessage(ex.getDetailMessage());
653             return;
654           } catch (Exception ex) {
655             // TODO: This is a critical exception and should be handle in the
656             // UIApplication
657             uiPortalApp.addMessage(new ApplicationMessage(ex.getMessage(), null));
658             return;
659           }
660         }
661       }
662 
663       UserACL userACL = uiForm.getApplicationComponent(UserACL.class);
664 
665       String ownerId = uiForm.getOwner();
666       String[] accessPermission = new String[1];
667       accessPermission[0] = "*:" + ownerId;
668       String editPermission = userACL.getMakableMT() + ":" + ownerId;
669 
670       if (SiteType.PORTAL.equals(uiForm.getOwnerType())) {
671         UIPortal uiPortal = Util.getUIPortal();
672         accessPermission = uiPortal.getAccessPermissions();
673         editPermission = uiPortal.getEditPermission();
674       }
675 
676       UIFormStringInput uiPageName = uiInputSet.getChildById("pageName");
677       UIFormStringInput uiPageTitle = uiInputSet.getChildById("pageTitle");
678 
679       Page page = new Page();
680       page.setOwnerType(uiForm.getOwnerType().getName());
681       page.setOwnerId(ownerId);
682       page.setName(uiPageName.getValue());
683       String title = uiPageTitle.getValue();
684       if (title == null || title.trim().length() < 1)
685         title = page.getName();
686       page.setTitle(title);
687 
688       page.setShowMaxWindow(false);
689 
690       page.setAccessPermissions(accessPermission);
691       page.setEditPermission(editPermission);
692 
693       userACL.hasPermission(page);
694 
695       page.setModifiable(true);
696       if (page.getChildren() == null)
697         page.setChildren(new ArrayList<ModelObject>());
698 
699       PageState pageState = new PageState(uiPageTitle.getValue(), null, false, null,
700                                           accessPermission != null ? Arrays.asList(accessPermission) : null, editPermission,
701                                           Arrays.asList(page.getMoveAppsPermissions()), Arrays.asList(page.getMoveContainersPermissions()));
702       
703       // check page is exist
704       PageKey pageKey = PageKey.parse(uiForm.getOwnerType().getName() + "::" + ownerId + "::" + uiPageName.getValue());
705       PageService pageService = uiForm.getApplicationComponent(PageService.class);
706       PageContext existPage = pageService.loadPage(pageKey);
707       if (existPage != null) {
708         uiPortalApp.addMessage(new ApplicationMessage("UIPageForm.msg.sameName", null));
709         pcontext.addUIComponentToUpdateByAjax(uiPortalApp.getUIPopupMessages());
710         return;
711       }
712 
713       pageSelector.setPage(new PageContext(pageKey, pageState));
714       event.getRequestContext().addUIComponentToUpdateByAjax(pageSelector); 
715     }
716   }
717 
718   private void switchLabelMode(boolean isExtendedMode) {
719     getUIStringInput(LABEL).setRendered(!isExtendedMode);
720     getUIStringInput(I18N_LABEL).setRendered(isExtendedMode);
721     getUIFormSelectBox(LANGUAGES).setRendered(isExtendedMode);
722   }
723   
724   static public class SelectTabActionListener extends UIFormTabPane.SelectTabActionListener {
725     public void execute(Event<UIFormTabPane> event) throws Exception {
726       super.execute(event);
727       PortalRequestContext pcontext = Util.getPortalRequestContext();
728       pcontext.setResponseComplete(true);
729     }
730   } 
731 }