public String getPortalOwner() {
   UserPortalConfig userPortalConfig = getUserPortalConfig();
   if (userPortalConfig != null) {
     return userPortalConfig.getPortalName();
   } else {
     return null;
   }
 }
 public UserPortal getUserPortal() {
   UserPortalConfig upc = getUserPortalConfig();
   if (upc != null) {
     return upc.getUserPortal();
   } else {
     return null;
   }
 }
    public void execute(Event<UIAdminToolbarContainer> event) throws Exception {
      UIAdminToolbarContainer uicomp = event.getSource();
      UserNavigation edittedNavigation = Utils.getSelectedNavigation();

      WebuiRequestContext context = event.getRequestContext();
      UIApplication uiApplication = context.getUIApplication();

      if (edittedNavigation == null) {
        uiApplication.addMessage(
            new ApplicationMessage("UISiteManagement.msg.Invalid-editPermission", null));
        return;
      }

      UserACL userACL = uicomp.getApplicationComponent(UserACL.class);
      if (edittedNavigation.getKey().getType().equals(SiteType.PORTAL)) {
        String portalName = Util.getPortalRequestContext().getPortalOwner();
        UserPortalConfigService configService =
            uicomp.getApplicationComponent(UserPortalConfigService.class);
        UserPortalConfig userPortalConfig =
            configService.getUserPortalConfig(
                portalName, context.getRemoteUser(), PortalRequestContext.USER_PORTAL_CONTEXT);
        if (userPortalConfig == null) {
          uiApplication.addMessage(
              new ApplicationMessage(
                  "UISiteManagement.msg.portal-not-exist", new String[] {portalName}));
          return;
        }
        if (!userACL.hasEditPermission(userPortalConfig.getPortalConfig())) {
          uiApplication.addMessage(
              new ApplicationMessage("UISiteManagement.msg.Invalid-editPermission", null));
          return;
        }
      } else if (edittedNavigation.getKey().getType().equals(PortalConfig.GROUP_TYPE)) {
        if (!userACL.hasEditPermissionOnNavigation(
            SiteKey.group(edittedNavigation.getKey().getTypeName()))) {
          uiApplication.addMessage(
              new ApplicationMessage("UISiteManagement.msg.Invalid-editPermission", null));
          return;
        }
      }

      if (uicomp.naviManager == null) {
        uicomp.naviManager = uicomp.createUIComponent(UINavigationManagement.class, null, null);
      }
      Utils.createPopupWindow(
          uicomp, uicomp.naviManager, EDIT_NAVIGATION_POPUP_CONTAINER_ID, 400, -1, -1);

      uicomp.naviManager.setSiteKey(edittedNavigation.getKey());
      UserPortal userPortal = getUserPortal();
      UINavigationNodeSelector selector =
          uicomp.naviManager.getChild(UINavigationNodeSelector.class);
      selector.setEdittedNavigation(edittedNavigation);
      selector.setUserPortal(userPortal);
      selector.initTreeData();

      context.addUIComponentToUpdateByAjax(uicomp);
    }
    public void execute(Event<UISpaceNavigationManagement> event) throws Exception {
      PortalRequestContext prContext = Util.getPortalRequestContext();
      UISpaceNavigationManagement uiManagement = event.getSource();
      UISpaceNavigationNodeSelector uiNodeSelector =
          uiManagement.getChild(UISpaceNavigationNodeSelector.class);
      UserPortalConfigService portalConfigService =
          uiManagement.getApplicationComponent(UserPortalConfigService.class);

      UIPortalApplication uiPortalApp = (UIPortalApplication) prContext.getUIApplication();
      UIWorkingWorkspace uiWorkingWS =
          uiPortalApp.getChildById(UIPortalApplication.UI_WORKING_WS_ID);
      prContext.addUIComponentToUpdateByAjax(uiWorkingWS);
      prContext.ignoreAJAXUpdateOnPortlets(true);

      UserNavigation navigation = uiNodeSelector.getEdittedNavigation();
      SiteKey siteKey = navigation.getKey();
      String editedOwnerId = siteKey.getName();

      // Check existed
      UserPortalConfig userPortalConfig;
      if (SiteType.PORTAL.equals(siteKey.getType())) {
        userPortalConfig =
            portalConfigService.getUserPortalConfig(
                editedOwnerId, event.getRequestContext().getRemoteUser());
        if (userPortalConfig == null) {
          prContext
              .getUIApplication()
              .addMessage(
                  new ApplicationMessage(
                      "UIPortalForm.msg.notExistAnymore", null, ApplicationMessage.ERROR));
          return;
        }
      } else {
        userPortalConfig =
            portalConfigService.getUserPortalConfig(
                prContext.getPortalOwner(), event.getRequestContext().getRemoteUser());
      }

      UserNavigation persistNavigation = userPortalConfig.getUserPortal().getNavigation(siteKey);
      if (persistNavigation == null) {
        prContext
            .getUIApplication()
            .addMessage(
                new ApplicationMessage(
                    "UINavigationManagement.msg.NavigationNotExistAnymore",
                    null,
                    ApplicationMessage.ERROR));
        return;
      }

      uiNodeSelector.save();
    }
  /**
   * The constructor of this class is used to build the tree of UI components that will be
   * aggregated in the portal page. 1) The component is stored in the current PortalRequestContext
   * ThreadLocal 2) The configuration for the portal associated with the current user request is
   * extracted from the PortalRequestContext 3) Then according to the context path, either a public
   * or private portal is initiated. Usually a public portal does not contain the left column and
   * only the private one has it. 4) The skin to use is setup 5) Finally, the current component is
   * associated with the current portal owner
   *
   * @throws Exception
   */
  public UIPortalApplication() throws Exception {
    log = ExoLogger.getLogger("portal:UIPortalApplication");
    PortalRequestContext context = PortalRequestContext.getCurrentInstance();
    userPortalConfig_ = (UserPortalConfig) context.getAttribute(UserPortalConfig.class);
    if (userPortalConfig_ == null) throw new Exception("Can't load user portal config");

    // dang.tung - set portal language by user preference -> browser -> default
    // ------------------------------------------------------------------------------
    String portalLanguage = null;
    LocaleConfigService localeConfigService = getApplicationComponent(LocaleConfigService.class);
    OrganizationService orgService = getApplicationComponent(OrganizationService.class);
    LocaleConfig localeConfig =
        localeConfigService.getLocaleConfig(userPortalConfig_.getPortalConfig().getLocale());
    String user = context.getRemoteUser();
    if (user != null) {
      UserProfile userProfile = orgService.getUserProfileHandler().findUserProfileByName(user);
      if (userProfile != null) {
        portalLanguage = userProfile.getUserInfoMap().get("user.language");
      } else {
        if (log.isWarnEnabled())
          log.warn("Could not load user profile for " + user + ". Using default portal locale.");
      }
    }
    localeConfig = localeConfigService.getLocaleConfig(portalLanguage);
    if (portalLanguage == null || !portalLanguage.equals(localeConfig.getLanguage())) {
      // if user language no support by portal -> get browser language if no ->
      // get portal
      portalLanguage = context.getRequest().getLocale().getLanguage();
      localeConfig = localeConfigService.getLocaleConfig(portalLanguage);
      if (!portalLanguage.equals(localeConfig.getLanguage())) {
        localeConfig =
            localeConfigService.getLocaleConfig(userPortalConfig_.getPortalConfig().getLocale());
      }
    }
    setLocale(localeConfig.getLocale());
    setOrientation(localeConfig.getOrientation());
    // -------------------------------------------------------------------------------
    context.setUIApplication(this);

    addWorkingWorkspace();

    String currentSkin = userPortalConfig_.getPortalConfig().getSkin();
    if (currentSkin != null && currentSkin.trim().length() > 0) skin_ = currentSkin;
    setOwner(context.getPortalOwner());
  }
 public static UserPortal getUserPortal() {
   UserPortalConfig portalConfig = Util.getPortalRequestContext().getUserPortalConfig();
   return portalConfig.getUserPortal();
 }
  @SuppressWarnings("unchecked")
  public UIPageForm(InitParams initParams) throws Exception {
    super("UIPageForm");
    PortalRequestContext pcontext = Util.getPortalRequestContext();
    UserPortalConfigService configService = getApplicationComponent(UserPortalConfigService.class);
    List<SelectItemOption<String>> ownerTypes = new ArrayList<SelectItemOption<String>>();
    ownerTypes.add(new SelectItemOption<String>(PortalConfig.USER_TYPE));

    UserPortalConfig userPortalConfig =
        configService.getUserPortalConfig(pcontext.getPortalOwner(), pcontext.getRemoteUser());
    ExoContainer container = ExoContainerContext.getCurrentContainer();
    UserACL acl = (UserACL) container.getComponentInstanceOfType(UserACL.class);
    if (acl.hasEditPermission(userPortalConfig.getPortalConfig())) {
      ownerTypes.add(new SelectItemOption<String>(PortalConfig.PORTAL_TYPE));
    }
    ownerIdInput = new UIFormStringInput(OWNER_ID, OWNER_ID, null);
    ownerIdInput.setEditable(false).setValue(pcontext.getRemoteUser());

    UIFormSelectBox uiSelectBoxOwnerType = new UIFormSelectBox(OWNER_TYPE, OWNER_TYPE, ownerTypes);
    uiSelectBoxOwnerType.setOnChange("ChangeOwnerType");

    UIFormInputSet uiSettingSet = new UIFormInputSet("PageSetting");
    uiSettingSet
        .addUIFormInput(new UIFormStringInput("pageId", "pageId", null).setEditable(false))
        .addUIFormInput(uiSelectBoxOwnerType)
        .addUIFormInput(ownerIdInput)
        .addUIFormInput(
            new UIFormStringInput("name", "name", null)
                .addValidator(StringLengthValidator.class, 3, 30)
                .addValidator(IdentifierValidator.class)
                .addValidator(MandatoryValidator.class))
        .addUIFormInput(
            new UIFormStringInput("title", "title", null)
                .addValidator(StringLengthValidator.class, 3, 120))
        .addUIFormInput(new UIFormCheckBoxInput("showMaxWindow", "showMaxWindow", false));
    addUIFormInput(uiSettingSet);
    setSelectedTab(uiSettingSet.getId());

    // WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
    // Param param = initParams.getParam("PageTemplate");
    // List<SelectItemCategory> itemCategories =
    // (List<SelectItemCategory>)param.getMapGroovyObject(context);
    // UIFormInputItemSelector uiTemplate = new UIFormInputItemSelector("Template", "template");
    // uiTemplate.setItemCategories(itemCategories);
    // addUIFormInput(uiTemplate);

    uiPermissionSetting = createUIComponent(UIFormInputSet.class, "PermissionSetting", null);
    UIListPermissionSelector uiListPermissionSelector =
        createUIComponent(UIListPermissionSelector.class, null, null);
    uiListPermissionSelector.configure("UIListPermissionSelector", "accessPermissions");
    uiListPermissionSelector.addValidator(EmptyIteratorValidator.class);
    uiPermissionSetting.addChild(uiListPermissionSelector);
    uiPermissionSetting.setSelectedComponent(uiListPermissionSelector.getId());
    UIPermissionSelector uiEditPermission =
        createUIComponent(UIPermissionSelector.class, null, null);
    uiEditPermission.setRendered(false);
    uiEditPermission.addValidator(
        org.exoplatform.webui.organization.UIPermissionSelector.MandatoryValidator.class);
    uiEditPermission.setEditable(false);
    uiEditPermission.configure("UIPermissionSelector", "editPermission");
    uiPermissionSetting.addChild(uiEditPermission);

    // TODO: This following line is fixed for bug PORTAL-2127
    uiListPermissionSelector
        .getChild(UIFormPopupWindow.class)
        .setId("UIPageFormPopupGroupMembershipSelector");

    List<String> groups = configService.getMakableNavigations(pcontext.getRemoteUser(), true);
    if (groups.size() > 0) {
      Collections.sort(groups);
      ownerTypes.add(new SelectItemOption<String>(PortalConfig.GROUP_TYPE));
      List<SelectItemOption<String>> groupsItem = new ArrayList<SelectItemOption<String>>();
      for (String group : groups) {
        groupsItem.add(new SelectItemOption<String>(group));
      }
      groupIdSelectBox = new UIFormSelectBox(OWNER_ID, OWNER_ID, groupsItem);
      groupIdSelectBox.setOnChange("ChangeOwnerId");
      groupIdSelectBox.setParent(uiSettingSet);
    }
    setActions(new String[] {"Save", "Close"});
  }