@Override
  public String getURLViewInContext(
      LiferayPortletRequest liferayPortletRequest,
      LiferayPortletResponse liferayPortletResponse,
      String noSuchEntryRedirect)
      throws Exception {

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

    Layout layout = themeDisplay.getLayout();

    if (Validator.isNotNull(_article.getLayoutUuid())) {
      layout =
          LayoutLocalServiceUtil.getLayoutByUuidAndCompanyId(
              _article.getLayoutUuid(), _article.getCompanyId());
    }

    String portletId = (String) liferayPortletRequest.getAttribute(WebKeys.PORTLET_ID);

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

    String linkToLayoutUuid =
        GetterUtil.getString(portletSetup.getValue("portletSetupLinkToLayoutUuid", null));

    if (Validator.isNotNull(_article.getLayoutUuid()) && Validator.isNull(linkToLayoutUuid)) {

      Group group = themeDisplay.getScopeGroup();

      if (group.getGroupId() != _article.getGroupId()) {
        group = GroupLocalServiceUtil.getGroup(_article.getGroupId());
      }

      String groupFriendlyURL =
          PortalUtil.getGroupFriendlyURL(group, layout.isPrivateLayout(), themeDisplay);

      return PortalUtil.addPreservedParameters(
          themeDisplay,
          groupFriendlyURL
              .concat(JournalArticleConstants.CANONICAL_URL_SEPARATOR)
              .concat(_article.getUrlTitle()));
    }

    List<Long> hitLayoutIds =
        JournalContentSearchLocalServiceUtil.getLayoutIds(
            _article.getGroupId(), layout.isPrivateLayout(), _article.getArticleId());

    if (!hitLayoutIds.isEmpty()) {
      Long hitLayoutId = hitLayoutIds.get(0);

      Layout hitLayout =
          LayoutLocalServiceUtil.getLayout(
              _article.getGroupId(), layout.isPrivateLayout(), hitLayoutId.longValue());

      return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
    }

    return noSuchEntryRedirect;
  }
  protected String getJournalURL(ThemeDisplay themeDisplay, long groupId, Document result)
      throws Exception {

    Layout layout = themeDisplay.getLayout();

    String articleId = result.get(Field.ENTRY_CLASS_PK);
    String version = result.get("version");

    List<Long> hitLayoutIds =
        JournalContentSearchLocalServiceUtil.getLayoutIds(
            layout.getGroupId(), layout.isPrivateLayout(), articleId);

    if (hitLayoutIds.size() > 0) {
      Long hitLayoutId = hitLayoutIds.get(0);

      Layout hitLayout =
          LayoutLocalServiceUtil.getLayout(
              layout.getGroupId(), layout.isPrivateLayout(), hitLayoutId.longValue());

      return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
    } else {
      StringBundler sb = new StringBundler(7);

      sb.append(themeDisplay.getPathMain());
      sb.append("/journal/view_article_content?groupId=");
      sb.append(groupId);
      sb.append("&articleId=");
      sb.append(articleId);
      sb.append("&version=");
      sb.append(version);

      return sb.toString();
    }
  }
    @Override
    public Void call() throws PortalException {
      Group group = _groupLocalService.getGroup(_groupId);

      Layout layout = _layoutLocalService.fetchLayout(_plid);

      if (group.hasStagingGroup()) {
        group = group.getStagingGroup();

        if (layout != null) {
          layout =
              _layoutLocalService.fetchLayoutByUuidAndGroupId(
                  layout.getUuid(), group.getGroupId(), layout.isPrivateLayout());
        }
      }

      javax.portlet.PortletPreferences jxPortletPreferences = null;

      if (layout == null) {
        jxPortletPreferences =
            PortletPreferencesFactoryUtil.getStrictPortletSetup(
                group.getCompanyId(), group.getGroupId(), _portletId);
      } else {
        jxPortletPreferences =
            PortletPreferencesFactoryUtil.getStrictPortletSetup(layout, _portletId);
      }

      ExportImportDateUtil.updateLastPublishDate(
          _portletId, jxPortletPreferences, _dateRange, _endDate);

      return null;
    }
  protected void assertLinksToLayouts(String content, Layout layout, long groupId) {

    StringBundler sb = new StringBundler();

    sb.append(StringPool.OPEN_BRACKET);
    sb.append(layout.getLayoutId());
    sb.append(CharPool.AT);

    Group group = GroupLocalServiceUtil.fetchGroup(groupId);

    if (layout.isPrivateLayout()) {
      if (group == null) {
        sb.append("private");
      } else if (group.isUser()) {
        sb.append("private-user");
      } else {
        sb.append("private-group");
      }
    } else {
      sb.append("public");
    }

    sb.append(CharPool.AT);
    sb.append(layout.getPlid());

    if (group != null) {
      sb.append(CharPool.AT);
      sb.append(String.valueOf(groupId));
    }

    sb.append(StringPool.CLOSE_BRACKET);

    Assert.assertTrue(content.contains(sb.toString()));
  }
  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;
  }
Esempio n. 6
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());
  }
  /** @see com.liferay.portal.lar.ExportImportHelperImpl#getMissingParentLayouts( Layout, long) */
  protected List<Layout> getMissingRemoteParentLayouts(
      HttpPrincipal httpPrincipal, Layout layout, long remoteGroupId) throws PortalException {

    List<Layout> missingRemoteParentLayouts = new ArrayList<>();

    long parentLayoutId = layout.getParentLayoutId();

    while (parentLayoutId > 0) {
      Layout parentLayout =
          LayoutLocalServiceUtil.getLayout(
              layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);

      try {
        LayoutServiceHttp.getLayoutByUuidAndGroupId(
            httpPrincipal, parentLayout.getUuid(), remoteGroupId, parentLayout.getPrivateLayout());

        // If one parent is found, all others are assumed to exist

        break;
      } catch (NoSuchLayoutException nsle) {
        missingRemoteParentLayouts.add(parentLayout);

        parentLayoutId = parentLayout.getParentLayoutId();
      }
    }

    return missingRemoteParentLayouts;
  }
  protected PortletDataContext getPortletDataContext(
      ExportImportConfiguration exportImportConfiguration, File file) throws PortalException {

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");
    String portletId = MapUtil.getString(settingsMap, "portletId");
    long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
    long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
    long userId = MapUtil.getLong(settingsMap, "userId");

    Layout layout = _layoutLocalService.getLayout(targetPlid);

    String userIdStrategyString =
        MapUtil.getString(parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY);

    UserIdStrategy userIdStrategy =
        ExportImportHelperUtil.getUserIdStrategy(userId, userIdStrategyString);

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);

    PortletDataContext portletDataContext =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            layout.getCompanyId(), targetGroupId, parameterMap, userIdStrategy, zipReader);

    portletDataContext.setOldPlid(targetPlid);
    portletDataContext.setPlid(targetPlid);
    portletDataContext.setPortletId(portletId);
    portletDataContext.setPrivateLayout(layout.isPrivateLayout());

    return portletDataContext;
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Layout layout)
      throws Exception {

    if (layout.isTypeSharedPortlet()) {
      return;
    }

    Element layoutElement = portletDataContext.getExportDataElement(layout);

    populateElementLayoutMetadata(layoutElement, layout);

    layoutElement.addAttribute(Constants.ACTION, Constants.ADD);

    portletDataContext.setPlid(layout.getPlid());

    long parentLayoutId = layout.getParentLayoutId();

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

      if (parentLayout != null) {
        StagedModelDataHandlerUtil.exportReferenceStagedModel(
            portletDataContext, layout, parentLayout, PortletDataContext.REFERENCE_TYPE_PARENT);

        layoutElement.addAttribute("parent-layout-uuid", parentLayout.getUuid());
      }
    }

    List<LayoutFriendlyURL> layoutFriendlyURLs =
        _layoutFriendlyURLLocalService.getLayoutFriendlyURLs(layout.getPlid());

    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext,
          layout,
          layoutFriendlyURL,
          PortletDataContext.REFERENCE_TYPE_DEPENDENCY);
    }

    if (layout.isIconImage()) {
      exportLayoutIconImage(portletDataContext, layout, layoutElement);
    }

    if (layout.isTypeLinkToLayout()) {
      exportLinkedLayout(portletDataContext, layout, layoutElement);
    }

    fixExportTypeSettings(layout);

    exportTheme(portletDataContext, layout);

    portletDataContext.addClassedModel(
        layoutElement, ExportImportPathUtil.getModelPath(layout), layout);
  }
  protected void addLayoutIds(
      List<Long> layoutIds, long groupId, boolean privateLayout, long layoutId) throws Exception {

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

    for (Layout layout : layouts) {
      layoutIds.add(layout.getLayoutId());

      addLayoutIds(layoutIds, layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId());
    }
  }
  @Override
  public Map<String, String> getReferenceAttributes(
      PortletDataContext portletDataContext, Layout layout) {

    Map<String, String> referenceAttributes = new HashMap<>();

    referenceAttributes.put("private-layout", String.valueOf(layout.isPrivateLayout()));
    referenceAttributes.put("layout-id", String.valueOf(layout.getLayoutId()));

    return referenceAttributes;
  }
  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());
  }
Esempio n. 13
0
  protected String getEntryURL(
      ResourceRequest resourceRequest,
      JournalFeed feed,
      JournalArticle article,
      Layout layout,
      ThemeDisplay themeDisplay)
      throws Exception {

    List<Long> hitLayoutIds =
        JournalContentSearchLocalServiceUtil.getLayoutIds(
            layout.getGroupId(), layout.isPrivateLayout(), article.getArticleId());

    if (hitLayoutIds.size() > 0) {
      Long hitLayoutId = hitLayoutIds.get(0);

      Layout hitLayout =
          LayoutLocalServiceUtil.getLayout(
              layout.getGroupId(), layout.isPrivateLayout(), hitLayoutId.longValue());

      return PortalUtil.getLayoutFriendlyURL(hitLayout, themeDisplay);
    } else {
      long plid =
          PortalUtil.getPlidFromFriendlyURL(feed.getCompanyId(), feed.getTargetLayoutFriendlyUrl());

      String portletId = PortletKeys.JOURNAL_CONTENT;

      if (Validator.isNotNull(feed.getTargetPortletId())) {
        portletId = feed.getTargetPortletId();
      }

      PortletURL entryURL =
          new PortletURLImpl(resourceRequest, portletId, plid, PortletRequest.RENDER_PHASE);

      entryURL.setParameter("struts_action", "/journal_content/view");
      entryURL.setParameter("groupId", String.valueOf(article.getGroupId()));
      entryURL.setParameter("articleId", article.getArticleId());

      return entryURL.toString();
    }
  }
  protected String getLinkToLayoutFieldValue(Layout layout, boolean includeGroupId) {

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    if (includeGroupId) {
      jsonObject.put("groupId", String.valueOf(layout.getGroupId()));
    }

    jsonObject.put("layoutId", String.valueOf(layout.getLayoutId()));
    jsonObject.put("privateLayout", layout.isPrivateLayout());

    return jsonObject.toString();
  }
  protected void updateContentSearch(
      ActionRequest actionRequest, String portletResource, String articleId) throws Exception {

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

    Layout layout = themeDisplay.getLayout();

    _journalContentSearchLocalService.updateContentSearch(
        layout.getGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        portletResource,
        articleId,
        true);
  }
  protected void importJournalArticle(
      PortletDataContext portletDataContext, Layout layout, Element layoutElement)
      throws Exception {

    UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

    String articleId = typeSettingsProperties.getProperty("article-id", StringPool.BLANK);

    if (Validator.isNull(articleId)) {
      return;
    }

    JournalPortletDataHandlerImpl.importReferencedData(portletDataContext, layoutElement);

    Element structureElement = layoutElement.element("structure");

    if (structureElement != null) {
      JournalPortletDataHandlerImpl.importStructure(portletDataContext, structureElement);
    }

    Element templateElement = layoutElement.element("template");

    if (templateElement != null) {
      JournalPortletDataHandlerImpl.importTemplate(portletDataContext, templateElement);
    }

    Element articleElement = layoutElement.element("article");

    if (articleElement != null) {
      JournalPortletDataHandlerImpl.importArticle(portletDataContext, articleElement);
    }

    Map<String, String> articleIds =
        (Map<String, String>)
            portletDataContext.getNewPrimaryKeysMap(JournalArticle.class + ".articleId");

    articleId = MapUtil.getString(articleIds, articleId, articleId);

    typeSettingsProperties.setProperty("article-id", articleId);

    JournalContentSearchLocalServiceUtil.updateContentSearch(
        portletDataContext.getScopeGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        StringPool.BLANK,
        articleId,
        true);
  }
  @Override
  public List<Layout> getAncestors() throws PortalException, SystemException {
    List<Layout> layouts = new ArrayList<Layout>();

    Layout layout = this;

    while (!layout.isRootLayout()) {
      layout =
          LayoutLocalServiceUtil.getLayout(
              layout.getGroupId(), layout.isPrivateLayout(), layout.getParentLayoutId());

      layouts.add(layout);
    }

    return layouts;
  }
  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);
  }
  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());
  }
  @Override
  public void setAddToPagePreferences(
      PortletPreferences preferences, String portletId, ThemeDisplay themeDisplay)
      throws Exception {

    preferences.setValue("articleId", _article.getArticleId());
    preferences.setValue("groupId", String.valueOf(_article.getGroupId()));

    Layout layout = themeDisplay.getLayout();

    JournalContentSearchLocalServiceUtil.updateContentSearch(
        layout.getGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        portletId,
        _article.getArticleId(),
        true);
  }
  public long getAncestorPlid() throws PortalException, SystemException {
    long plid = 0;

    Layout layout = this;

    while (true) {
      if (!layout.isRootLayout()) {
        layout =
            LayoutLocalServiceUtil.getLayout(
                layout.getGroupId(), layout.isPrivateLayout(), layout.getParentLayoutId());
      } else {
        plid = layout.getPlid();

        break;
      }
    }

    return plid;
  }
  private void _buildLayoutBreadcrumb(
      Layout selLayout,
      String selLayoutParam,
      PortletURL portletURL,
      ThemeDisplay themeDisplay,
      boolean selectedLayout,
      StringBundler sb)
      throws Exception {
    String layoutURL = _getBreadcrumbLayoutURL(selLayout, selLayoutParam, portletURL, themeDisplay);
    String target = PortalUtil.getLayoutTarget(selLayout);

    StringBundler breadCrumbSB = new StringBundler(7);

    breadCrumbSB.append("<li><span><a href=\"");
    breadCrumbSB.append(layoutURL);
    breadCrumbSB.append("\" ");
    breadCrumbSB.append(target);
    breadCrumbSB.append(">");

    breadCrumbSB.append(HtmlUtil.escape(selLayout.getName(themeDisplay.getLocale())));

    breadCrumbSB.append("</a></span></li>");

    Layout layoutParent = null;
    long layoutParentId = selLayout.getParentLayoutId();

    if (layoutParentId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
      layoutParent =
          LayoutLocalServiceUtil.getLayout(
              selLayout.getGroupId(), selLayout.isPrivateLayout(), layoutParentId);

      _buildLayoutBreadcrumb(layoutParent, selLayoutParam, portletURL, themeDisplay, false, sb);

      sb.append(breadCrumbSB.toString());
    } else {
      sb.append(breadCrumbSB.toString());
    }
  }
  protected void exportLinkedLayout(
      PortletDataContext portletDataContext, Layout layout, Element layoutElement)
      throws Exception {

    UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

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

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

        StagedModelDataHandlerUtil.exportReferenceStagedModel(
            portletDataContext, layout, linkedToLayout, PortletDataContext.REFERENCE_TYPE_STRONG);

        layoutElement.addAttribute("linked-to-layout-uuid", linkedToLayout.getUuid());
      } catch (NoSuchLayoutException nsle) {
      }
    }
  }
  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();
  }
  @Override
  public boolean isScopeIdSelectable(
      PermissionChecker permissionChecker, String scopeId, long companyGroupId, Layout layout)
      throws PortalException, SystemException {

    long groupId = getGroupIdFromScopeId(scopeId, layout.getGroupId(), layout.isPrivateLayout());

    if (scopeId.startsWith(SCOPE_ID_CHILD_GROUP_PREFIX)) {
      Group group = GroupLocalServiceUtil.getGroup(groupId);

      if (!group.hasAncestor(layout.getGroupId())) {
        return false;
      }
    } else if (scopeId.startsWith(SCOPE_ID_PARENT_GROUP_PREFIX)) {
      Group siteGroup = layout.getGroup();

      if (!siteGroup.hasAncestor(groupId)) {
        return false;
      }

      if (SitesUtil.isContentSharingWithChildrenEnabled(siteGroup)) {
        return true;
      }

      if (!PrefsPropsUtil.getBoolean(
          layout.getCompanyId(), PropsKeys.SITES_CONTENT_SHARING_THROUGH_ADMINISTRATORS_ENABLED)) {

        return false;
      }

      return GroupPermissionUtil.contains(permissionChecker, groupId, ActionKeys.UPDATE);
    } else if (groupId != companyGroupId) {
      return GroupPermissionUtil.contains(permissionChecker, groupId, ActionKeys.UPDATE);
    }

    return true;
  }
  @Override
  public long[] getGroupIds(
      PortletPreferences portletPreferences, long scopeGroupId, Layout layout) {

    String[] scopeIds =
        portletPreferences.getValues(
            "scopeIds", new String[] {SCOPE_ID_GROUP_PREFIX + scopeGroupId});

    long[] groupIds = new long[scopeIds.length];

    int i = 0;

    for (String scopeId : scopeIds) {
      try {
        groupIds[i] = getGroupIdFromScopeId(scopeId, scopeGroupId, layout.isPrivateLayout());

        i++;
      } catch (Exception e) {
        continue;
      }
    }

    return groupIds;
  }
  protected void exportPortlet(
      PortletDataContext portletDataContext,
      Layout layout,
      Element parentElement,
      boolean exportPermissions,
      boolean exportPortletArchivedSetups,
      boolean exportPortletData,
      boolean exportPortletSetup,
      boolean exportPortletUserPreferences)
      throws Exception {

    long plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
    long layoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;

    if (layout != null) {
      plid = layout.getPlid();
      layoutId = layout.getLayoutId();
    }

    Portlet portlet =
        _portletLocalService.getPortletById(
            portletDataContext.getCompanyId(), portletDataContext.getPortletId());

    if ((portlet == null) || portlet.isUndeployedPortlet()) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not export portlet "
                + portletDataContext.getPortletId()
                + " because the portlet is not deployed");
      }

      return;
    }

    if (!portlet.isInstanceable()
        && !portlet.isPreferencesUniquePerLayout()
        && portletDataContext.hasNotUniquePerLayout(portletDataContext.getPortletId())) {

      return;
    }

    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
      PortletDataContext clonedPortletDataContext =
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext);

      ManifestSummary manifestSummary = clonedPortletDataContext.getManifestSummary();

      manifestSummary.resetCounters();

      PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

      portletDataHandler.prepareManifestSummary(clonedPortletDataContext);

      PortletDataHandlerStatusMessageSenderUtil.sendStatusMessage(
          "portlet", portletDataContext.getPortletId(), manifestSummary);
    }

    Document document = SAXReaderUtil.createDocument();

    Element portletElement = document.addElement("portlet");

    portletElement.addAttribute("portlet-id", portletDataContext.getPortletId());
    portletElement.addAttribute("root-portlet-id", portletDataContext.getRootPortletId());
    portletElement.addAttribute("old-plid", String.valueOf(plid));
    portletElement.addAttribute(
        "scope-group-id", String.valueOf(portletDataContext.getScopeGroupId()));
    portletElement.addAttribute("scope-layout-type", portletDataContext.getScopeType());
    portletElement.addAttribute("scope-layout-uuid", portletDataContext.getScopeLayoutUuid());
    portletElement.addAttribute("private-layout", String.valueOf(layout.isPrivateLayout()));

    // Data

    if (exportPortletData) {
      javax.portlet.PortletPreferences jxPortletPreferences = null;

      if (ExportImportThreadLocal.isInitialLayoutStagingInProcess()) {
        Group liveGroup = layout.getGroup();

        Group stagingGroup = liveGroup.getStagingGroup();

        layout.setGroupId(stagingGroup.getGroupId());

        jxPortletPreferences =
            PortletPreferencesFactoryUtil.getStrictPortletSetup(
                layout, portletDataContext.getPortletId());

        layout.setGroupId(liveGroup.getGroupId());
      } else {
        jxPortletPreferences =
            PortletPreferencesFactoryUtil.getStrictPortletSetup(
                layout, portletDataContext.getPortletId());
      }

      if (!portlet.isPreferencesUniquePerLayout()) {
        StringBundler sb = new StringBundler(5);

        sb.append(portletDataContext.getPortletId());
        sb.append(StringPool.AT);
        sb.append(portletDataContext.getScopeType());
        sb.append(StringPool.AT);
        sb.append(portletDataContext.getScopeLayoutUuid());

        String dataKey = sb.toString();

        if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
          portletDataContext.putNotUniquePerLayout(dataKey);

          exportPortletData(
              portletDataContext, portlet, layout, jxPortletPreferences, portletElement);
        }
      } else {
        exportPortletData(
            portletDataContext, portlet, layout, jxPortletPreferences, portletElement);
      }
    }

    // Portlet preferences

    if (exportPortletSetup) {

      // Company

      exportPortletPreferences(
          portletDataContext,
          portletDataContext.getCompanyId(),
          PortletKeys.PREFS_OWNER_TYPE_COMPANY,
          false,
          layout,
          plid,
          portlet.getRootPortletId(),
          portletElement);

      // Group

      exportPortletPreferences(
          portletDataContext,
          portletDataContext.getScopeGroupId(),
          PortletKeys.PREFS_OWNER_TYPE_GROUP,
          false,
          layout,
          PortletKeys.PREFS_PLID_SHARED,
          portlet.getRootPortletId(),
          portletElement);

      // Layout

      exportPortletPreferences(
          portletDataContext,
          PortletKeys.PREFS_OWNER_ID_DEFAULT,
          PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
          false,
          layout,
          plid,
          portletDataContext.getPortletId(),
          portletElement);
    }

    // Portlet user preferences

    if (exportPortletUserPreferences) {
      List<PortletPreferences> portletPreferencesList =
          _portletPreferencesLocalService.getPortletPreferences(
              PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletDataContext.getPortletId());

      for (PortletPreferences portletPreferences : portletPreferencesList) {

        boolean defaultUser = false;

        if (portletPreferences.getOwnerId() == PortletKeys.PREFS_OWNER_ID_DEFAULT) {

          defaultUser = true;
        }

        exportPortletPreferences(
            portletDataContext,
            portletPreferences.getOwnerId(),
            PortletKeys.PREFS_OWNER_TYPE_USER,
            defaultUser,
            layout,
            plid,
            portletDataContext.getPortletId(),
            portletElement);
      }

      try {
        PortletPreferences groupPortletPreferences =
            _portletPreferencesLocalService.getPortletPreferences(
                portletDataContext.getScopeGroupId(),
                PortletKeys.PREFS_OWNER_TYPE_GROUP,
                PortletKeys.PREFS_PLID_SHARED,
                portlet.getRootPortletId());

        exportPortletPreference(
            portletDataContext,
            portletDataContext.getScopeGroupId(),
            PortletKeys.PREFS_OWNER_TYPE_GROUP,
            false,
            groupPortletPreferences,
            portlet.getRootPortletId(),
            PortletKeys.PREFS_PLID_SHARED,
            portletElement);
      } catch (NoSuchPortletPreferencesException nsppe) {
      }
    }

    // Archived setups

    if (exportPortletArchivedSetups) {
      List<PortletItem> portletItems =
          _portletItemLocalService.getPortletItems(
              portletDataContext.getGroupId(),
              portletDataContext.getRootPortletId(),
              PortletPreferences.class.getName());

      for (PortletItem portletItem : portletItems) {
        exportPortletPreferences(
            portletDataContext,
            portletItem.getPortletItemId(),
            PortletKeys.PREFS_OWNER_TYPE_ARCHIVED,
            false,
            null,
            plid,
            portletItem.getPortletId(),
            portletElement);
      }
    }

    // Permissions

    if (exportPermissions) {
      _permissionExporter.exportPortletPermissions(
          portletDataContext, portletDataContext.getPortletId(), layout, portletElement);
    }

    // Zip

    StringBundler pathSB = new StringBundler(4);

    pathSB.append(ExportImportPathUtil.getPortletPath(portletDataContext));
    pathSB.append(StringPool.SLASH);
    pathSB.append(plid);
    pathSB.append("/portlet.xml");

    String path = pathSB.toString();

    Element element = parentElement.addElement("portlet");

    element.addAttribute("portlet-id", portletDataContext.getPortletId());
    element.addAttribute("layout-id", String.valueOf(layoutId));
    element.addAttribute("path", path);
    element.addAttribute("portlet-data", String.valueOf(exportPortletData));

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    element.addAttribute("schema-version", portletDataHandler.getSchemaVersion());

    StringBundler configurationOptionsSB = new StringBundler(6);

    if (exportPortletSetup) {
      configurationOptionsSB.append("setup");
      configurationOptionsSB.append(StringPool.COMMA);
    }

    if (exportPortletArchivedSetups) {
      configurationOptionsSB.append("archived-setups");
      configurationOptionsSB.append(StringPool.COMMA);
    }

    if (exportPortletUserPreferences) {
      configurationOptionsSB.append("user-preferences");
      configurationOptionsSB.append(StringPool.COMMA);
    }

    if (configurationOptionsSB.index() > 0) {
      configurationOptionsSB.setIndex(configurationOptionsSB.index() - 1);
    }

    element.addAttribute("portlet-configuration", configurationOptionsSB.toString());

    try {
      portletDataContext.addZipEntry(path, document.formattedString());
    } catch (IOException ioe) {
      if (_log.isWarnEnabled()) {
        _log.warn(ioe.getMessage());
      }
    }
  }
Esempio n. 28
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 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 void importLinkedLayout(
      PortletDataContext portletDataContext,
      Layout layout,
      Layout importedLayout,
      Element layoutElement,
      Map<Long, Layout> layouts)
      throws Exception {

    UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

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

    String linkedToLayoutUuid = layoutElement.attributeValue("linked-to-layout-uuid");

    if (Validator.isNull(linkedToLayoutUuid)) {
      return;
    }

    if (linkToLayoutId <= 0) {
      updateTypeSettings(importedLayout, layout);

      return;
    }

    Element linkedToLayoutElement =
        portletDataContext.getReferenceDataElement(
            layout, Layout.class, layout.getGroupId(), linkedToLayoutUuid);

    if (linkedToLayoutElement != null) {
      String linkedToLayoutPath = linkedToLayoutElement.attributeValue("path");

      Layout linkedToLayout = (Layout) portletDataContext.getZipEntryAsObject(linkedToLayoutPath);

      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, linkedToLayout);

      Layout importedLinkedLayout = layouts.get(linkToLayoutId);

      if (importedLinkedLayout == null) {
        throw new NoSuchLayoutException(
            "Layout with layout id "
                + linkToLayoutId
                + " that is linked from layout with layout id "
                + layout.getLayoutId()
                + " does not exist");
      }

      typeSettingsProperties.setProperty(
          "privateLayout", String.valueOf(importedLinkedLayout.isPrivateLayout()));
      typeSettingsProperties.setProperty(
          "linkToLayoutId", String.valueOf(importedLinkedLayout.getLayoutId()));
    } else {
      if (_log.isWarnEnabled()) {
        StringBundler sb = new StringBundler(6);

        sb.append("Unable to link layout with friendly URL ");
        sb.append(layout.getFriendlyURL());
        sb.append(" and layout id ");
        sb.append(layout.getLayoutId());
        sb.append(" to layout with layout id ");
        sb.append(linkToLayoutId);

        _log.warn(sb.toString());
      }
    }

    updateTypeSettings(importedLayout, layout);
  }