protected void updateTypeSettings(Layout importedLayout, Layout layout) throws PortalException {

    long groupId = layout.getGroupId();

    try {
      LayoutTypePortlet importedLayoutType = (LayoutTypePortlet) importedLayout.getLayoutType();

      List<String> importedPortletIds = importedLayoutType.getPortletIds();

      layout.setGroupId(importedLayout.getGroupId());

      LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

      importedPortletIds.removeAll(layoutTypePortlet.getPortletIds());

      if (!importedPortletIds.isEmpty()) {
        _portletLocalService.deletePortlets(
            importedLayout.getCompanyId(),
            importedPortletIds.toArray(new String[importedPortletIds.size()]),
            importedLayout.getPlid());
      }

      importedLayout.setTypeSettingsProperties(layoutTypePortlet.getTypeSettingsProperties());
    } finally {
      layout.setGroupId(groupId);
    }
  }
  protected String addJournalContentPortletToLayout(
      long userId, Layout layout, JournalArticle journalArticle, String columnId) throws Exception {

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    String journalPortletId =
        layoutTypePortlet.addPortletId(userId, PortletKeys.JOURNAL_CONTENT, columnId, -1);

    LayoutLocalServiceUtil.updateLayout(
        layout.getGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        layout.getTypeSettings());

    javax.portlet.PortletPreferences prefs =
        getPortletPreferences(layout.getCompanyId(), layout.getPlid(), journalPortletId);

    prefs.setValue("articleId", journalArticle.getArticleId());
    prefs.setValue("groupId", String.valueOf(journalArticle.getGroupId()));
    prefs.setValue("showAvailableLocales", Boolean.TRUE.toString());

    updatePortletPreferences(layout.getPlid(), journalPortletId, prefs);

    return journalPortletId;
  }
Example #3
0
  public static Layout addLayout(
      Group group,
      boolean privateLayout,
      long parentLayoutId,
      String name,
      String friendlyURL,
      String layoutTemplateId)
      throws Exception {

    ServiceContext serviceContext = new ServiceContext();

    Layout layout =
        LayoutLocalServiceUtil.addLayout(
            group.getCreatorUserId(),
            group.getGroupId(),
            privateLayout,
            parentLayoutId,
            name,
            StringPool.BLANK,
            StringPool.BLANK,
            LayoutConstants.TYPE_PORTLET,
            false,
            friendlyURL,
            serviceContext);

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    layoutTypePortlet.setLayoutTemplateId(0, layoutTemplateId, false);

    return LayoutLocalServiceUtil.updateLayout(
        layout.getGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        layout.getTypeSettings());
  }
  private LayoutTypePortlet _getLayoutTypePortletClone(HttpServletRequest request)
      throws IOException {

    LayoutTypePortlet layoutTypePortlet = null;

    LayoutClone layoutClone = LayoutCloneFactory.getInstance();

    if (layoutClone != null) {
      String typeSettings = layoutClone.get(request, getPlid());

      if (typeSettings != null) {
        UnicodeProperties typeSettingsProperties = new UnicodeProperties(true);

        typeSettingsProperties.load(typeSettings);

        String stateMax = typeSettingsProperties.getProperty(LayoutTypePortletConstants.STATE_MAX);
        String stateMin = typeSettingsProperties.getProperty(LayoutTypePortletConstants.STATE_MIN);

        Layout layout = (Layout) this.clone();

        layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

        layoutTypePortlet.setStateMax(stateMax);
        layoutTypePortlet.setStateMin(stateMin);
      }
    }

    if (layoutTypePortlet == null) {
      layoutTypePortlet = (LayoutTypePortlet) getLayoutType();
    }

    return layoutTypePortlet;
  }
  protected void mergePortlets(Layout layout, String newTypeSettings, String portletsMergeMode) {

    try {
      UnicodeProperties previousTypeSettingsProperties = layout.getTypeSettingsProperties();

      LayoutTypePortlet previousLayoutType = (LayoutTypePortlet) layout.getLayoutType();

      LayoutTemplate previousLayoutTemplate = previousLayoutType.getLayoutTemplate();

      List<String> previousColumns = previousLayoutTemplate.getColumns();

      UnicodeProperties newTypeSettingsProperties = new UnicodeProperties(true);

      newTypeSettingsProperties.load(newTypeSettings);

      String layoutTemplateId =
          newTypeSettingsProperties.getProperty(LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID);

      previousTypeSettingsProperties.setProperty(
          LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID, layoutTemplateId);

      LayoutTemplate newLayoutTemplate =
          LayoutTemplateLocalServiceUtil.getLayoutTemplate(layoutTemplateId, false, null);

      String[] newPortletIds = new String[0];

      for (String columnId : newLayoutTemplate.getColumns()) {
        String columnValue = newTypeSettingsProperties.getProperty(columnId);

        String[] portletIds = StringUtil.split(columnValue);

        if (!previousColumns.contains(columnId)) {
          newPortletIds = ArrayUtil.append(newPortletIds, portletIds);
        } else {
          String[] previousPortletIds =
              StringUtil.split(previousTypeSettingsProperties.getProperty(columnId));

          portletIds = appendPortletIds(previousPortletIds, portletIds, portletsMergeMode);

          previousTypeSettingsProperties.setProperty(columnId, StringUtil.merge(portletIds));
        }
      }

      // Add portlets in non-existent column to the first column

      String columnId = previousColumns.get(0);

      String[] portletIds = StringUtil.split(previousTypeSettingsProperties.getProperty(columnId));

      appendPortletIds(portletIds, newPortletIds, portletsMergeMode);

      previousTypeSettingsProperties.setProperty(columnId, StringUtil.merge(portletIds));

      layout.setTypeSettings(previousTypeSettingsProperties.toString());
    } catch (IOException ioe) {
      layout.setTypeSettings(newTypeSettings);
    }
  }
  protected Layout updateLayoutTemplateId(Layout layout, String layoutTemplateId) throws Exception {

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    layoutTypePortlet.setLayoutTemplateId(TestPropsValues.getUserId(), layoutTemplateId);

    return LayoutServiceUtil.updateLayout(
        layout.getGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        layout.getTypeSettings());
  }
  public static List<LayoutTypePortlet> getLayoutTypePortlets(Layout layout)
      throws PortletContainerException {

    if (_PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET) {
      List<Layout> layouts = null;

      try {
        layouts =
            LayoutLocalServiceUtil.getLayouts(
                layout.getGroupId(), layout.isPrivateLayout(), LayoutConstants.TYPE_PORTLET);
      } catch (SystemException se) {
        throw new PortletContainerException(se);
      }

      List<LayoutTypePortlet> layoutTypePortlets = new ArrayList<LayoutTypePortlet>(layouts.size());

      for (Layout curLayout : layouts) {
        LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) curLayout.getLayoutType();

        layoutTypePortlets.add(layoutTypePortlet);
      }

      return layoutTypePortlets;
    }

    if (layout.isTypePortlet()) {
      List<LayoutTypePortlet> layoutTypePortlets = new ArrayList<LayoutTypePortlet>(1);

      LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

      layoutTypePortlets.add(layoutTypePortlet);

      return layoutTypePortlets;
    }

    return Collections.emptyList();
  }
  protected void addLayout(long parentLayoutId, JSONObject layoutJSONObject) throws Exception {

    String name = layoutJSONObject.getString("name");
    String title = layoutJSONObject.getString("title");
    boolean hidden = layoutJSONObject.getBoolean("hidden");

    String friendlyURL = layoutJSONObject.getString("friendlyURL");

    if (Validator.isNotNull(friendlyURL) && !friendlyURL.startsWith(StringPool.SLASH)) {

      friendlyURL = StringPool.SLASH + friendlyURL;
    }

    Layout layout =
        LayoutLocalServiceUtil.addLayout(
            userId,
            groupId,
            privateLayout,
            parentLayoutId,
            name,
            title,
            StringPool.BLANK,
            LayoutConstants.TYPE_PORTLET,
            hidden,
            friendlyURL,
            serviceContext);

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    String layoutTemplateId =
        layoutJSONObject.getString("layoutTemplateId", _defaultLayoutTemplateId);

    if (Validator.isNotNull(layoutTemplateId)) {
      layoutTypePortlet.setLayoutTemplateId(userId, layoutTemplateId, false);
    }

    JSONArray columnsJSONArray = layoutJSONObject.getJSONArray("columns");

    addLayoutColumns(layout, columnsJSONArray);

    LayoutLocalServiceUtil.updateLayout(
        groupId, layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());

    JSONArray layoutsJSONArray = layoutJSONObject.getJSONArray("layouts");

    addLayouts(layout.getLayoutId(), layoutsJSONArray);
  }
  protected void exportPortletPreferences(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      boolean defaultUser,
      Layout layout,
      long plid,
      String portletId,
      Element parentElement)
      throws Exception {

    PortletPreferences portletPreferences = null;

    if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY)
        || (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP)
        || (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED)) {

      plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
    }

    try {
      portletPreferences =
          PortletPreferencesLocalServiceUtil.getPortletPreferences(
              ownerId, ownerType, plid, portletId);
    } catch (NoSuchPortletPreferencesException nsppe) {
      return;
    }

    LayoutTypePortlet layoutTypePortlet = null;

    if (layout != null) {
      layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
    }

    if ((layoutTypePortlet == null) || (layoutTypePortlet.hasPortletId(portletId))) {

      exportPortletPreference(
          portletDataContext,
          ownerId,
          ownerType,
          defaultUser,
          portletPreferences,
          portletId,
          plid,
          parentElement);
    }
  }
  public static void removePortletIds(HttpServletRequest request, Layout layout) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    List<String> portletIds = layoutTypePortlet.getPortletIds();

    for (String portletId : portletIds) {
      layoutTypePortlet.removePortletId(themeDisplay.getUserId(), portletId);
    }

    LayoutServiceUtil.updateLayout(
        layout.getGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        layout.getTypeSettings());
  }
  protected List<LayoutTypePortlet> getLayoutTypePortlets(long groupId, boolean privateLayout)
      throws Exception {

    List<LayoutTypePortlet> layoutTypePortlets = new ArrayList<LayoutTypePortlet>();

    List<Layout> layouts =
        LayoutLocalServiceUtil.getLayouts(groupId, privateLayout, LayoutConstants.TYPE_PORTLET);

    for (Layout layout : layouts) {
      if (!layout.isTypePortlet()) {
        continue;
      }

      LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

      layoutTypePortlets.add(layoutTypePortlet);
    }

    return layoutTypePortlets;
  }
  protected void addLayoutColumnPortlet(
      Layout layout, String columnId, JSONObject portletJSONObject) throws Exception {

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    String rootPortletId = portletJSONObject.getString("portletId");

    if (Validator.isNull(rootPortletId)) {
      throw new ImporterException("portletId is not specified");
    }

    String portletId = layoutTypePortlet.addPortletId(userId, rootPortletId, columnId, -1, false);

    JSONObject portletPreferencesJSONObject = portletJSONObject.getJSONObject("portletPreferences");

    if ((portletPreferencesJSONObject == null) || (portletPreferencesJSONObject.length() == 0)) {

      return;
    }

    PortletPreferences portletSetup =
        PortletPreferencesFactoryUtil.getLayoutPortletSetup(layout, portletId);

    Iterator<String> iterator = portletPreferencesJSONObject.keys();

    while (iterator.hasNext()) {
      String key = iterator.next();

      String value = portletPreferencesJSONObject.getString(key);

      if (rootPortletId.equals(PortletKeys.JOURNAL_CONTENT) && key.equals("articleId")) {

        value = getJournalArticleId(value);
      }

      portletSetup.setValue(key, value);
    }

    portletSetup.store();
  }
  protected void exportPortletPreferences(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      boolean defaultUser,
      Layout layout,
      long plid,
      String portletId,
      Element parentElement)
      throws Exception {

    PortletPreferences portletPreferences = null;

    try {
      portletPreferences = getPortletPreferences(ownerId, ownerType, plid, portletId);
    } catch (NoSuchPortletPreferencesException nsppe) {
      return;
    }

    LayoutTypePortlet layoutTypePortlet = null;

    if (layout != null) {
      layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
    }

    if ((layoutTypePortlet == null) || layoutTypePortlet.hasPortletId(portletId)) {

      exportPortletPreference(
          portletDataContext,
          ownerId,
          ownerType,
          defaultUser,
          portletPreferences,
          portletId,
          plid,
          parentElement);
    }
  }
  public static List<Portlet> getPortletDataHandlerPortlets(List<Layout> layouts) throws Exception {

    List<Portlet> portlets = new ArrayList<Portlet>();
    Set<String> rootPortletIds = new HashSet<String>();

    for (Layout layout : layouts) {
      if (!layout.isTypePortlet()) {
        continue;
      }

      LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

      for (String portletId : layoutTypePortlet.getPortletIds()) {
        Portlet portlet = PortletLocalServiceUtil.getPortletById(layout.getCompanyId(), portletId);

        if ((portlet == null) || rootPortletIds.contains(portlet.getRootPortletId())) {

          continue;
        }

        PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

        PortletDataHandlerControl[] portletDataHandlerControls =
            portletDataHandler.getExportConfigurationControls(
                layout.getCompanyId(), layout.getGroupId(), portlet, layout.getPrivateLayout());

        if (ArrayUtil.isNotEmpty(portletDataHandlerControls)) {
          rootPortletIds.add(portlet.getRootPortletId());

          portlets.add(portlet);
        }
      }
    }

    return portlets;
  }
  protected void setupPage(long userId, long groupId, PortalPage portalPage) throws Exception {
    String portalPageName = portalPage.getName();
    String[] portletIds = portalPage.getPortletIds();
    Layout portalPageLayout = getPortalPageLayout(userId, groupId, portalPageName);
    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) portalPageLayout.getLayoutType();

    layoutTypePortlet.setLayoutTemplateId(userId, "2_columns_i", false);

    int columnNumber = 1;

    for (String portletId : portletIds) {

      if (portletId.endsWith("_INSTANCE_")) {
        portletId = portletId + "ABCD";
      }

      addPortlet(layoutTypePortlet, userId, columnNumber, portletId);
      columnNumber++;
    }

    LayoutLocalServiceUtil.updateLayout(portalPageLayout);

    logger.info("Setup page: " + portalPageName);
  }
  public static void copyPreferences(
      HttpServletRequest request, Layout targetLayout, Layout sourceLayout) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    LayoutTypePortlet sourceLayoutTypePortlet = (LayoutTypePortlet) sourceLayout.getLayoutType();

    List<String> sourcePortletIds = sourceLayoutTypePortlet.getPortletIds();

    for (String sourcePortletId : sourcePortletIds) {

      // Copy preference

      PortletPreferencesIds portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              request, targetLayout, sourcePortletId);

      PortletPreferencesLocalServiceUtil.getPreferences(portletPreferencesIds);

      PortletPreferencesIds sourcePortletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              request, sourceLayout, sourcePortletId);

      PortletPreferences sourcePreferences =
          PortletPreferencesLocalServiceUtil.getPreferences(sourcePortletPreferencesIds);

      PortletPreferencesLocalServiceUtil.updatePreferences(
          portletPreferencesIds.getOwnerId(),
          portletPreferencesIds.getOwnerType(),
          portletPreferencesIds.getPlid(),
          portletPreferencesIds.getPortletId(),
          sourcePreferences);

      // Copy portlet setup

      PortletPreferences targetPreferences =
          PortletPreferencesLocalServiceUtil.getPreferences(
              themeDisplay.getCompanyId(),
              PortletKeys.PREFS_OWNER_ID_DEFAULT,
              PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
              targetLayout.getPlid(),
              sourcePortletId);

      sourcePreferences =
          PortletPreferencesLocalServiceUtil.getPreferences(
              themeDisplay.getCompanyId(),
              PortletKeys.PREFS_OWNER_ID_DEFAULT,
              PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
              sourceLayout.getPlid(),
              sourcePortletId);

      PortletPreferencesLocalServiceUtil.updatePreferences(
          PortletKeys.PREFS_OWNER_ID_DEFAULT,
          PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
          targetLayout.getPlid(),
          sourcePortletId,
          sourcePreferences);

      SitesUtil.updateLayoutScopes(
          themeDisplay.getUserId(),
          sourceLayout,
          targetLayout,
          sourcePreferences,
          targetPreferences,
          sourcePortletId,
          themeDisplay.getLanguageId());

      Portlet sourcePortlet =
          PortletLocalServiceUtil.getPortletById(sourceLayout.getCompanyId(), sourcePortletId);

      PortletLayoutListener sourcePortletLayoutListener =
          sourcePortlet.getPortletLayoutListenerInstance();

      if (sourcePortletLayoutListener != null) {
        sourcePortletLayoutListener.onAddToLayout(sourcePortletId, targetLayout.getPlid());
      }
    }
  }
  protected void exportLayout(
      PortletDataContext portletDataContext,
      List<Portlet> portlets,
      long[] layoutIds,
      Map<String, Object[]> portletIds,
      Layout layout)
      throws Exception {

    if (!ArrayUtil.contains(layoutIds, layout.getLayoutId())
        && (layoutIds != null)
        && (layoutIds.length > 0)) {

      Element layoutElement = portletDataContext.getExportDataElement(layout);

      layoutElement.addAttribute("action", Constants.SKIP);

      return;
    }

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, layout);

    if (!layout.isSupportsEmbeddedPortlets()) {

      // Only portlet type layouts support page scoping

      return;
    }

    if (layout.isTypePortlet()) {
      for (Portlet portlet : portlets) {
        if (portlet.isScopeable() && layout.hasScopeGroup()) {
          String key =
              PortletPermissionUtil.getPrimaryKey(layout.getPlid(), portlet.getPortletId());

          Group scopeGroup = layout.getScopeGroup();

          portletIds.put(
              key,
              new Object[] {
                portlet.getPortletId(),
                layout.getPlid(),
                scopeGroup.getGroupId(),
                StringPool.BLANK,
                layout.getUuid()
              });
        }
      }
    }

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    // The getAllPortlets method returns all effective nonsystem portlets
    // for any layout type, including embedded portlets, or in the case of
    // panel type layout, selected portlets

    for (Portlet portlet : layoutTypePortlet.getAllPortlets(false)) {
      String portletId = portlet.getPortletId();

      javax.portlet.PortletPreferences jxPortletPreferences =
          PortletPreferencesFactoryUtil.getLayoutPortletSetup(layout, portletId);

      String scopeType = GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeType", null));
      String scopeLayoutUuid =
          GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeLayoutUuid", null));

      long scopeGroupId = portletDataContext.getScopeGroupId();

      if (Validator.isNotNull(scopeType)) {
        Group scopeGroup = null;

        if (scopeType.equals("company")) {
          scopeGroup = GroupLocalServiceUtil.getCompanyGroup(layout.getCompanyId());
        } else if (scopeType.equals("layout")) {
          Layout scopeLayout = null;

          scopeLayout =
              LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
                  scopeLayoutUuid,
                  portletDataContext.getGroupId(),
                  portletDataContext.isPrivateLayout());

          if (scopeLayout == null) {
            continue;
          }

          scopeGroup = scopeLayout.getScopeGroup();
        } else {
          throw new IllegalArgumentException("Scope type " + scopeType + " is invalid");
        }

        if (scopeGroup != null) {
          scopeGroupId = scopeGroup.getGroupId();
        }
      }

      String key = PortletPermissionUtil.getPrimaryKey(layout.getPlid(), portletId);

      portletIds.put(
          key,
          new Object[] {portletId, layout.getPlid(), scopeGroupId, scopeType, scopeLayoutUuid});
    }
  }
Example #18
0
  public static void addPortlets(Group group, Layout layout, String name, String keyPrefix)
      throws Exception {

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate();

    List<String> columns = layoutTemplate.getColumns();

    for (String column : columns) {
      String[] portletIds = null;

      if (Validator.isNull(name)) {
        portletIds = PortletProps.getArray(keyPrefix + column);
      } else {
        Filter filter = new Filter(name);

        portletIds = PortletProps.getArray(keyPrefix + column, filter);
      }

      layoutTypePortlet.addPortletIds(0, portletIds, column, false);
    }

    LayoutLocalServiceUtil.updateLayout(
        layout.getGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        layout.getTypeSettings());

    List<String> portletIds = layoutTypePortlet.getPortletIds();

    for (String portletId : portletIds) {
      addResources(layout, portletId);

      if (portletId.startsWith("1_WAR_eventsdisplayportlet")) {
        updatePortletTitle(layout, portletId, "Events");
      } else if (portletId.startsWith("1_WAR_soannouncementsportlet")) {
        updatePortletTitle(layout, portletId, "Announcements");
      } else if (portletId.startsWith("1_WAR_wysiwygportlet")) {
        updatePortletTitle(layout, portletId, "Welcome");
      } else if (portletId.contains("_WAR_contactsportlet")) {
        configureProfile(layout, portletId);
        removePortletBorder(layout, portletId);
      } else if (portletId.startsWith(PortletKeys.ASSET_PUBLISHER)) {
        configureAssetPublisher(layout);
        updatePortletTitle(layout, portletId, "Related Content");
      } else if (portletId.startsWith(PortletKeys.BLOGS_AGGREGATOR)) {
        configureBlogsAggregator(layout);
        updatePortletTitle(layout, portletId, "Recent Blogs");
      } else if (portletId.startsWith(PortletKeys.BREADCRUMB)) {
        removePortletBorder(layout, portletId);
      } else if (portletId.startsWith(PortletKeys.MESSAGE_BOARDS)) {
        configureMessageBoards(layout);
        removePortletBorder(layout, portletId);
      } else if (portletId.equals(PortletKeys.DOCUMENT_LIBRARY)
          || portletId.equals(PortletKeys.BLOGS)
          || portletId.equals(PortletKeys.WIKI)
          || portletId.equals("1_WAR_calendarportlet")
          || portletId.contains("_WAR_microblogsportlet")
          || portletId.equals("1_WAR_privatemessagingportlet")
          || portletId.contains("1_WAR_tasksportlet")) {

        removePortletBorder(layout, portletId);
      }
    }
  }
  protected PortletURL getKBArticleURL(
      long plid, boolean privateLayout, KBArticle kbArticle, HttpServletRequest request)
      throws Exception {

    PortletURL firstMatchPortletURL = null;

    List<Layout> layouts = getCandidateLayouts(plid, privateLayout, kbArticle);

    for (Layout layout : layouts) {
      LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

      List<Portlet> portlets = layoutTypePortlet.getAllPortlets();

      for (Portlet portlet : portlets) {
        String rootPortletId = PortletConstants.getRootPortletId(portlet.getPortletId());

        if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_DISPLAY)) {
          PortletPreferences portletPreferences =
              PortletPreferencesFactoryUtil.getPortletSetup(
                  layout, portlet.getPortletId(), StringPool.BLANK);

          long kbFolderClassNameId = PortalUtil.getClassNameId(KBFolderConstants.getClassName());

          long resourceClassNameId =
              GetterUtil.getLong(
                  portletPreferences.getValue("resourceClassNameId", null), kbFolderClassNameId);
          long resourcePrimKey =
              GetterUtil.getLong(
                  portletPreferences.getValue("resourcePrimKey", null),
                  KBFolderConstants.DEFAULT_PARENT_FOLDER_ID);

          if (resourceClassNameId == kbFolderClassNameId) {
            if (isParentFolder(resourcePrimKey, kbArticle.getKbFolderId())) {

              return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request);
            }
          } else if (resourcePrimKey == kbArticle.getResourcePrimKey()) {

            return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request);
          }

          if (firstMatchPortletURL == null) {
            firstMatchPortletURL =
                getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request);
          }
        }

        if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_SECTION)) {
          PortletPreferences portletPreferences =
              PortletPreferencesFactoryUtil.getPortletSetup(
                  layout, portlet.getPortletId(), StringPool.BLANK);

          String[] kbArticlesSections =
              portletPreferences.getValues("kbArticlesSections", new String[0]);

          KBArticle rootKBArticle =
              KBArticleLocalServiceUtil.fetchLatestKBArticle(
                  kbArticle.getRootResourcePrimKey(), WorkflowConstants.STATUS_APPROVED);

          if (rootKBArticle == null) {
            continue;
          }

          String[] sections = AdminUtil.unescapeSections(rootKBArticle.getSections());

          for (String section : sections) {
            if (!ArrayUtil.contains(kbArticlesSections, section)) {
              continue;
            }

            return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request);
          }
        }

        if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_ARTICLE)) {
          PortletPreferences portletPreferences =
              PortletPreferencesFactoryUtil.getPortletSetup(
                  layout, portlet.getPortletId(), StringPool.BLANK);

          long resourcePrimKey =
              GetterUtil.getLong(portletPreferences.getValue("resourcePrimKey", null));

          KBArticle selKBArticle =
              KBArticleLocalServiceUtil.fetchLatestKBArticle(
                  resourcePrimKey, WorkflowConstants.STATUS_APPROVED);

          if (selKBArticle == null) {
            continue;
          }

          long rootResourcePrimKey = kbArticle.getRootResourcePrimKey();
          long selRootResourcePrimKey = selKBArticle.getRootResourcePrimKey();

          if (rootResourcePrimKey == selRootResourcePrimKey) {
            return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request);
          }

          if (firstMatchPortletURL == null) {
            firstMatchPortletURL =
                getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request);
          }
        }
      }
    }

    return firstMatchPortletURL;
  }
  protected void exportLayout(
      PortletDataContext portletDataContext,
      Portlet layoutConfigurationPortlet,
      LayoutCache layoutCache,
      List<Portlet> portlets,
      Map<String, Object[]> portletIds,
      boolean exportPermissions,
      boolean exportUserPermissions,
      Layout layout,
      Element layoutsElement)
      throws Exception {

    String path = portletDataContext.getLayoutPath(layout.getLayoutId()) + "/layout.xml";

    if (!portletDataContext.isPathNotProcessed(path)) {
      return;
    }

    LayoutRevision layoutRevision = null;

    if (LayoutStagingUtil.isBranchingLayout(layout)) {
      ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

      long layoutSetBranchId = ParamUtil.getLong(serviceContext, "layoutSetBranchId");

      if (layoutSetBranchId <= 0) {
        return;
      }

      layoutRevision = LayoutRevisionUtil.fetchByL_H_P(layoutSetBranchId, true, layout.getPlid());

      if (layoutRevision == null) {
        return;
      }

      LayoutStagingHandler layoutStagingHandler = LayoutStagingUtil.getLayoutStagingHandler(layout);

      layoutStagingHandler.setLayoutRevision(layoutRevision);
    }

    Element layoutElement = layoutsElement.addElement("layout");

    if (layoutRevision != null) {
      layoutElement.addAttribute(
          "layout-revision-id", String.valueOf(layoutRevision.getLayoutRevisionId()));
      layoutElement.addAttribute(
          "layout-branch-id", String.valueOf(layoutRevision.getLayoutBranchId()));
      layoutElement.addAttribute(
          "layout-branch-name", String.valueOf(layoutRevision.getLayoutBranch().getName()));
    }

    layoutElement.addAttribute("layout-uuid", layout.getUuid());
    layoutElement.addAttribute("layout-id", String.valueOf(layout.getLayoutId()));

    long parentLayoutId = layout.getParentLayoutId();

    if (parentLayoutId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
      Layout parentLayout =
          LayoutLocalServiceUtil.getLayout(
              layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);

      if (parentLayout != null) {
        layoutElement.addAttribute("parent-layout-uuid", parentLayout.getUuid());
      }
    }

    boolean deleteLayout =
        MapUtil.getBoolean(portletDataContext.getParameterMap(), "delete_" + layout.getPlid());

    if (deleteLayout) {
      layoutElement.addAttribute("delete", String.valueOf(true));

      return;
    }

    portletDataContext.setPlid(layout.getPlid());

    if (layout.isIconImage()) {
      Image image = ImageLocalServiceUtil.getImage(layout.getIconImageId());

      if (image != null) {
        String iconPath = getLayoutIconPath(portletDataContext, layout, image);

        layoutElement.addElement("icon-image-path").addText(iconPath);

        portletDataContext.addZipEntry(iconPath, image.getTextObj());
      }
    }

    _portletExporter.exportPortletData(
        portletDataContext, layoutConfigurationPortlet, layout, null, layoutElement);

    // Layout permissions

    if (exportPermissions) {
      _permissionExporter.exportLayoutPermissions(
          portletDataContext,
          layoutCache,
          portletDataContext.getCompanyId(),
          portletDataContext.getScopeGroupId(),
          layout,
          layoutElement,
          exportUserPermissions);
    }

    if (layout.isTypeArticle()) {
      exportJournalArticle(portletDataContext, layout, layoutElement);
    } else if (layout.isTypeLinkToLayout()) {
      UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

      long linkToLayoutId =
          GetterUtil.getLong(
              typeSettingsProperties.getProperty("linkToLayoutId", StringPool.BLANK));

      if (linkToLayoutId > 0) {
        try {
          Layout linkedToLayout =
              LayoutLocalServiceUtil.getLayout(
                  portletDataContext.getScopeGroupId(), layout.isPrivateLayout(), linkToLayoutId);

          exportLayout(
              portletDataContext,
              layoutConfigurationPortlet,
              layoutCache,
              portlets,
              portletIds,
              exportPermissions,
              exportUserPermissions,
              linkedToLayout,
              layoutsElement);
        } catch (NoSuchLayoutException nsle) {
        }
      }
    } else if (layout.isTypePortlet()) {
      for (Portlet portlet : portlets) {
        if (portlet.isScopeable() && layout.hasScopeGroup()) {
          String key =
              PortletPermissionUtil.getPrimaryKey(layout.getPlid(), portlet.getPortletId());

          portletIds.put(
              key,
              new Object[] {
                portlet.getPortletId(),
                layout.getPlid(),
                layout.getScopeGroup().getGroupId(),
                StringPool.BLANK,
                layout.getUuid()
              });
        }
      }

      LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

      for (String portletId : layoutTypePortlet.getPortletIds()) {
        javax.portlet.PortletPreferences jxPreferences =
            PortletPreferencesFactoryUtil.getLayoutPortletSetup(layout, portletId);

        String scopeType = GetterUtil.getString(jxPreferences.getValue("lfrScopeType", null));
        String scopeLayoutUuid =
            GetterUtil.getString(jxPreferences.getValue("lfrScopeLayoutUuid", null));

        long scopeGroupId = portletDataContext.getScopeGroupId();

        if (Validator.isNotNull(scopeType)) {
          Group scopeGroup = null;

          if (scopeType.equals("company")) {
            scopeGroup = GroupLocalServiceUtil.getCompanyGroup(layout.getCompanyId());
          } else if (scopeType.equals("layout")) {
            Layout scopeLayout = null;

            scopeLayout =
                LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
                    scopeLayoutUuid, portletDataContext.getGroupId());

            if (scopeLayout == null) {
              continue;
            }

            scopeGroup = scopeLayout.getScopeGroup();
          } else {
            throw new IllegalArgumentException("Scope type " + scopeType + " is invalid");
          }

          if (scopeGroup != null) {
            scopeGroupId = scopeGroup.getGroupId();
          }
        }

        String key = PortletPermissionUtil.getPrimaryKey(layout.getPlid(), portletId);

        portletIds.put(
            key,
            new Object[] {portletId, layout.getPlid(), scopeGroupId, scopeType, scopeLayoutUuid});
      }
    }

    fixTypeSettings(layout);

    layoutElement.addAttribute("path", path);

    portletDataContext.addExpando(layoutElement, path, layout);

    portletDataContext.addZipEntry(path, layout);
  }
  protected Portlet processPortletRequest(
      HttpServletRequest request, HttpServletResponse response, String lifecycle) throws Exception {

    HttpSession session = request.getSession();

    long companyId = PortalUtil.getCompanyId(request);
    User user = PortalUtil.getUser(request);
    Layout layout = (Layout) request.getAttribute(WebKeys.LAYOUT);

    String portletId = ParamUtil.getString(request, "p_p_id");

    if (Validator.isNull(portletId)) {
      return null;
    }

    Portlet portlet = PortletLocalServiceUtil.getPortletById(companyId, portletId);

    if (portlet == null) {
      return null;
    }

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    themeDisplay.setScopeGroupId(PortalUtil.getScopeGroupId(request, portletId));

    ServletContext servletContext = (ServletContext) request.getAttribute(WebKeys.CTX);

    InvokerPortlet invokerPortlet = PortletInstanceFactoryUtil.create(portlet, servletContext);

    if (user != null) {
      InvokerPortletImpl.clearResponse(
          session, layout.getPrimaryKey(), portletId, LanguageUtil.getLanguageId(request));
    }

    PortletConfig portletConfig = PortletConfigFactoryUtil.create(portlet, servletContext);
    PortletContext portletContext = portletConfig.getPortletContext();

    WindowState windowState =
        WindowStateFactory.getWindowState(ParamUtil.getString(request, "p_p_state"));

    if (layout.isTypeControlPanel()
        && ((windowState == null)
            || windowState.equals(WindowState.NORMAL)
            || (Validator.isNull(windowState.toString())))) {

      windowState = WindowState.MAXIMIZED;
    }

    PortletMode portletMode =
        PortletModeFactory.getPortletMode(ParamUtil.getString(request, "p_p_mode"));

    PortletPreferencesIds portletPreferencesIds =
        PortletPreferencesFactoryUtil.getPortletPreferencesIds(request, portletId);

    PortletPreferences portletPreferences =
        PortletPreferencesLocalServiceUtil.getPreferences(portletPreferencesIds);

    processPublicRenderParameters(request, layout, portlet);

    if (lifecycle.equals(PortletRequest.ACTION_PHASE)) {
      String contentType = request.getHeader(HttpHeaders.CONTENT_TYPE);

      if (_log.isDebugEnabled()) {
        _log.debug("Content type " + contentType);
      }

      UploadServletRequest uploadRequest = null;

      try {
        if ((contentType != null) && (contentType.startsWith(ContentTypes.MULTIPART_FORM_DATA))) {

          PortletConfigImpl invokerPortletConfigImpl =
              (PortletConfigImpl) invokerPortlet.getPortletConfig();

          if (invokerPortlet.isStrutsPortlet()
              || ((invokerPortletConfigImpl != null) && (!invokerPortletConfigImpl.isWARFile()))) {

            uploadRequest = new UploadServletRequestImpl(request);

            request = uploadRequest;
          }
        }

        if (PropsValues.AUTH_TOKEN_CHECK_ENABLED && invokerPortlet.isCheckAuthToken()) {

          AuthTokenUtil.check(request);
        }

        ActionRequestImpl actionRequestImpl =
            ActionRequestFactory.create(
                request,
                portlet,
                invokerPortlet,
                portletContext,
                windowState,
                portletMode,
                portletPreferences,
                layout.getPlid());

        ActionResponseImpl actionResponseImpl =
            ActionResponseFactory.create(
                actionRequestImpl, response, portletId, user, layout, windowState, portletMode);

        actionRequestImpl.defineObjects(portletConfig, actionResponseImpl);

        ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequestImpl);

        ServiceContextThreadLocal.pushServiceContext(serviceContext);

        invokerPortlet.processAction(actionRequestImpl, actionResponseImpl);

        actionResponseImpl.transferHeaders(response);

        RenderParametersPool.put(
            request, layout.getPlid(), portletId, actionResponseImpl.getRenderParameterMap());

        List<LayoutTypePortlet> layoutTypePortlets = null;

        if (!actionResponseImpl.getEvents().isEmpty()) {
          if (PropsValues.PORTLET_EVENT_DISTRIBUTION_LAYOUT_SET) {
            layoutTypePortlets =
                getLayoutTypePortlets(layout.getGroupId(), layout.isPrivateLayout());
          } else {
            if (layout.isTypePortlet()) {
              LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

              layoutTypePortlets = new ArrayList<LayoutTypePortlet>();

              layoutTypePortlets.add(layoutTypePortlet);
            }
          }

          processEvents(actionRequestImpl, actionResponseImpl, layoutTypePortlets);

          actionRequestImpl.defineObjects(portletConfig, actionResponseImpl);
        }
      } finally {
        if (uploadRequest != null) {
          uploadRequest.cleanUp();
        }

        ServiceContextThreadLocal.popServiceContext();
      }
    } else if (lifecycle.equals(PortletRequest.RENDER_PHASE)
        || lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {

      PortalUtil.updateWindowState(portletId, user, layout, windowState, request);

      PortalUtil.updatePortletMode(portletId, user, layout, portletMode, request);
    }

    if (lifecycle.equals(PortletRequest.RESOURCE_PHASE)) {
      PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

      String portletPrimaryKey = PortletPermissionUtil.getPrimaryKey(layout.getPlid(), portletId);

      portletDisplay.setId(portletId);
      portletDisplay.setRootPortletId(portlet.getRootPortletId());
      portletDisplay.setInstanceId(portlet.getInstanceId());
      portletDisplay.setResourcePK(portletPrimaryKey);
      portletDisplay.setPortletName(portletConfig.getPortletName());
      portletDisplay.setNamespace(PortalUtil.getPortletNamespace(portletId));

      ResourceRequestImpl resourceRequestImpl =
          ResourceRequestFactory.create(
              request,
              portlet,
              invokerPortlet,
              portletContext,
              windowState,
              portletMode,
              portletPreferences,
              layout.getPlid());

      ResourceResponseImpl resourceResponseImpl =
          ResourceResponseFactory.create(resourceRequestImpl, response, portletId, companyId);

      resourceRequestImpl.defineObjects(portletConfig, resourceResponseImpl);

      try {
        ServiceContext serviceContext = ServiceContextFactory.getInstance(resourceRequestImpl);

        ServiceContextThreadLocal.pushServiceContext(serviceContext);

        invokerPortlet.serveResource(resourceRequestImpl, resourceResponseImpl);
      } finally {
        ServiceContextThreadLocal.popServiceContext();
      }
    }

    return portlet;
  }
  private List<Event> _doProcessEvent(
      HttpServletRequest request,
      HttpServletResponse response,
      Portlet portlet,
      Layout layout,
      Event event)
      throws Exception {

    ServletContext servletContext = (ServletContext) request.getAttribute(WebKeys.CTX);

    InvokerPortlet invokerPortlet = PortletInstanceFactoryUtil.create(portlet, servletContext);

    PortletConfig portletConfig = PortletConfigFactoryUtil.create(portlet, servletContext);
    PortletContext portletContext = portletConfig.getPortletContext();

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    WindowState windowState = null;

    if (layoutTypePortlet.hasStateMaxPortletId(portlet.getPortletId())) {
      windowState = WindowState.MAXIMIZED;
    } else if (layoutTypePortlet.hasStateMinPortletId(portlet.getPortletId())) {

      windowState = WindowState.MINIMIZED;
    } else {
      windowState = WindowState.NORMAL;
    }

    PortletMode portletMode = null;

    if (layoutTypePortlet.hasModeAboutPortletId(portlet.getPortletId())) {
      portletMode = LiferayPortletMode.ABOUT;
    } else if (layoutTypePortlet.hasModeConfigPortletId(portlet.getPortletId())) {

      portletMode = LiferayPortletMode.CONFIG;
    } else if (layoutTypePortlet.hasModeEditPortletId(portlet.getPortletId())) {

      portletMode = PortletMode.EDIT;
    } else if (layoutTypePortlet.hasModeEditDefaultsPortletId(portlet.getPortletId())) {

      portletMode = LiferayPortletMode.EDIT_DEFAULTS;
    } else if (layoutTypePortlet.hasModeEditGuestPortletId(portlet.getPortletId())) {

      portletMode = LiferayPortletMode.EDIT_GUEST;
    } else if (layoutTypePortlet.hasModeHelpPortletId(portlet.getPortletId())) {

      portletMode = PortletMode.HELP;
    } else if (layoutTypePortlet.hasModePreviewPortletId(portlet.getPortletId())) {

      portletMode = LiferayPortletMode.PREVIEW;
    } else if (layoutTypePortlet.hasModePrintPortletId(portlet.getPortletId())) {

      portletMode = LiferayPortletMode.PRINT;
    } else {
      portletMode = PortletMode.VIEW;
    }

    long scopeGroupId = getScopeGroupId(request, layout, portlet.getPortletId());

    PortletPreferences portletPreferences =
        PortletPreferencesFactoryUtil.getPortletSetup(
            scopeGroupId, layout, portlet.getPortletId(), null);

    EventRequestImpl eventRequestImpl =
        EventRequestFactory.create(
            request,
            portlet,
            invokerPortlet,
            portletContext,
            windowState,
            portletMode,
            portletPreferences,
            layout.getPlid());

    eventRequestImpl.setEvent(serializeEvent(event, invokerPortlet.getPortletClassLoader()));

    User user = PortalUtil.getUser(request);
    Layout requestLayout = (Layout) request.getAttribute(WebKeys.LAYOUT);

    EventResponseImpl eventResponseImpl =
        EventResponseFactory.create(
            eventRequestImpl, response, portlet.getPortletId(), user, requestLayout);

    eventRequestImpl.defineObjects(portletConfig, eventResponseImpl);

    try {
      invokerPortlet.processEvent(eventRequestImpl, eventResponseImpl);

      if (eventResponseImpl.isCalledSetRenderParameter()) {
        Map<String, String[]> renderParameterMap = new HashMap<String, String[]>();

        renderParameterMap.putAll(eventResponseImpl.getRenderParameterMap());

        RenderParametersPool.put(
            request, requestLayout.getPlid(), portlet.getPortletId(), renderParameterMap);
      }

      return eventResponseImpl.getEvents();
    } finally {
      eventRequestImpl.cleanUp();
    }
  }
  protected String getLayoutTemplateId(Layout layout) {
    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    return layoutTypePortlet.getLayoutTemplateId();
  }