protected String replaceLinksToLayoutsParameters(String content, Map<String, Layout> layoutsMap)
      throws Exception {

    Layout privateLayout = layoutsMap.get(_PRIVATE_LAYOUT);
    Layout privateUserLayout = layoutsMap.get(_PRIVATE_USER_LAYOUT);
    Layout publicLayout = layoutsMap.get(_PUBLIC_LAYOUT);
    Layout publicUserLayout = layoutsMap.get(_PUBLIC_USER_LAYOUT);

    return StringUtil.replace(
        content,
        new String[] {
          "[$GROUP_ID$]",
          "[$GROUP_ID_USER$]",
          "[$LAYOUT_ID_PRIVATE$]",
          "[$LAYOUT_ID_PRIVATE_USER$]",
          "[$LAYOUT_ID_PUBLIC$]",
          "[$LAYOUT_ID_PUBLIC_USER$]"
        },
        new String[] {
          String.valueOf(privateLayout.getGroupId()),
          String.valueOf(privateUserLayout.getGroupId()),
          String.valueOf(privateLayout.getLayoutId()),
          String.valueOf(privateUserLayout.getLayoutId()),
          String.valueOf(publicLayout.getLayoutId()),
          String.valueOf(publicUserLayout.getLayoutId()),
        });
  }
  @Test
  public void testFriendlyURLCollision() throws Exception {
    String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault());

    Layout layoutA = LayoutTestUtil.addLayout(group.getGroupId(), "layoutA");

    layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutA-de", "de");

    Layout layoutB = LayoutTestUtil.addLayout(group.getGroupId(), "layoutB");

    layoutB = LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutB-de", "de");

    long[] layoutIds = {layoutA.getLayoutId(), layoutB.getLayoutId()};

    exportImportLayouts(layoutIds, getImportParameterMap());

    layoutA =
        LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/temp", defaultLanguageId);

    layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/temp-de", "de");

    layoutB =
        LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutA", defaultLanguageId);

    LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutA-de", "de");

    layoutA =
        LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutB", defaultLanguageId);

    LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutB-de", "de");

    exportImportLayouts(layoutIds, getImportParameterMap());
  }
  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());
    }
  }
  @Test
  public void testExportImportLayoutsPriorities() throws Exception {
    Layout layout1 = LayoutTestUtil.addLayout(group);
    Layout layout2 = LayoutTestUtil.addLayout(group);
    Layout layout3 = LayoutTestUtil.addLayout(group);

    int priority = layout1.getPriority();

    layout1.setPriority(layout3.getPriority());
    layout3.setPriority(priority);

    layout1 = LayoutLocalServiceUtil.updateLayout(layout1);
    layout3 = LayoutLocalServiceUtil.updateLayout(layout3);

    long[] layoutIds = new long[] {layout1.getLayoutId(), layout2.getLayoutId()};

    exportImportLayouts(layoutIds, getImportParameterMap());

    Layout importedLayout1 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout1.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotEquals(layout1.getPriority(), importedLayout1.getPriority());

    Layout importedLayout2 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout2.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotEquals(layout2.getPriority(), importedLayout2.getPriority());

    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(group.getGroupId(), false);

    layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);

    exportImportLayouts(layoutIds, getImportParameterMap());

    importedLayout1 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout1.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertEquals(layout1.getPriority(), importedLayout1.getPriority());

    importedLayout2 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout2.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertEquals(layout2.getPriority(), importedLayout2.getPriority());

    Layout importedLayout3 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout3.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertEquals(layout3.getPriority(), importedLayout3.getPriority());
  }
  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;
  }
  protected String replaceLinksToLayoutsParameters(
      String content, Layout privateLayout, Layout publicLayout) {

    return StringUtil.replace(
        content,
        new String[] {
          "[$GROUP_ID_PRIVATE$]", "[$GROUP_ID_PUBLIC$]",
          "[$LAYOUT_ID_PRIVATE$]", "[$LAYOUT_ID_PUBLIC$]"
        },
        new String[] {
          String.valueOf(privateLayout.getGroupId()),
          String.valueOf(publicLayout.getGroupId()),
          String.valueOf(privateLayout.getLayoutId()),
          String.valueOf(publicLayout.getLayoutId())
        });
  }
  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()));
  }
Example #8
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());
  }
  protected void deleteMissingLayouts(
      long groupId,
      boolean privateLayout,
      Set<Long> newLayoutIds,
      List<Layout> previousLayouts,
      ServiceContext serviceContext)
      throws Exception {

    // Layouts

    if (_log.isDebugEnabled()) {
      if (newLayoutIds.size() > 0) {
        _log.debug("Delete missing layouts");
      }
    }

    for (Layout layout : previousLayouts) {
      if (!newLayoutIds.contains(layout.getLayoutId())) {
        try {
          LayoutLocalServiceUtil.deleteLayout(layout, false, serviceContext);
        } catch (NoSuchLayoutException nsle) {
        }
      }
    }

    // Layout set

    LayoutSetLocalServiceUtil.updatePageCount(groupId, privateLayout);
  }
  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);
  }
Example #11
0
  @Override
  public PortletURL getViewContentURL(HttpServletRequest request, String className, long classPK)
      throws PortalException {

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

    if (!themeDisplay.isSignedIn()
        || !isTrashEnabled(themeDisplay.getScopeGroupId())
        || !PortletPermissionUtil.hasControlPanelAccessPermission(
            themeDisplay.getPermissionChecker(),
            themeDisplay.getScopeGroupId(),
            PortletKeys.TRASH)) {

      return null;
    }

    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);

    if (trashHandler.isInTrashContainer(classPK)) {
      TrashEntry trashEntry = trashHandler.getTrashEntry(classPK);

      className = trashEntry.getClassName();
      classPK = trashEntry.getClassPK();

      trashHandler = TrashHandlerRegistryUtil.getTrashHandler(className);
    }

    TrashRenderer trashRenderer = trashHandler.getTrashRenderer(classPK);

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

    Layout layout = themeDisplay.getLayout();

    PortletURL portletURL =
        PortalUtil.getControlPanelPortletURL(
            request, PortletKeys.TRASH, layout.getLayoutId(), PortletRequest.RENDER_PHASE);

    portletURL.setParameter("struts_action", "/trash/view_content");
    portletURL.setParameter("redirect", themeDisplay.getURLCurrent());

    TrashEntry trashEntry = TrashEntryLocalServiceUtil.getEntry(className, classPK);

    if (trashEntry.getRootEntry() != null) {
      portletURL.setParameter("className", className);
      portletURL.setParameter("classPK", String.valueOf(classPK));
    } else {
      portletURL.setParameter("trashEntryId", String.valueOf(trashEntry.getEntryId()));
    }

    portletURL.setParameter("type", trashRenderer.getType());
    portletURL.setParameter("showActions", Boolean.FALSE.toString());
    portletURL.setParameter("showAssetMetadata", Boolean.TRUE.toString());
    portletURL.setParameter("showEditURL", Boolean.FALSE.toString());

    return portletURL;
  }
  protected static List<String> getLayoutIds(
      HttpServletRequest request,
      boolean privateLayout,
      long parentLayoutId,
      List<String> layoutIds)
      throws Exception {

    long groupId = ParamUtil.getLong(request, "groupId");

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

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

      getLayoutIds(request, privateLayout, layout.getLayoutId(), layoutIds);
    }

    return layoutIds;
  }
  @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;
  }
  @Test
  public void testGetSelectedLayoutsJSONSelectAllLayouts() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(_stagingGroup);

    Layout childLayout = LayoutTestUtil.addLayout(_stagingGroup, layout.getPlid());

    long[] selectedLayoutIds = new long[] {layout.getLayoutId(), childLayout.getLayoutId()};

    String selectedLayoutsJSON =
        ExportImportHelperUtil.getSelectedLayoutsJSON(
            _stagingGroup.getGroupId(), false, StringUtil.merge(selectedLayoutIds));

    JSONArray selectedLayoutsJSONArray = JSONFactoryUtil.createJSONArray(selectedLayoutsJSON);

    Assert.assertEquals(1, selectedLayoutsJSONArray.length());

    JSONObject layoutJSONObject = selectedLayoutsJSONArray.getJSONObject(0);

    Assert.assertTrue(layoutJSONObject.getBoolean("includeChildren"));
    Assert.assertEquals(layout.getPlid(), layoutJSONObject.getLong("plid"));
  }
  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());
  }
  protected String getLayoutIconPath(
      PortletDataContext portletDataContext, Layout layout, Image image) {

    StringBundler sb = new StringBundler(5);

    sb.append(portletDataContext.getLayoutPath(layout.getLayoutId()));
    sb.append("/icons/");
    sb.append(image.getImageId());
    sb.append(StringPool.PERIOD);
    sb.append(image.getType());

    return sb.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();
  }
  @Test
  public void testTypeLinkToLayout() throws Exception {
    initExport();

    Map<String, List<StagedModel>> dependentStagedModelsMap = new HashMap<>();

    Layout linkedLayout = LayoutTestUtil.addLayout(stagingGroup);

    List<LayoutFriendlyURL> linkedLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(linkedLayout.getPlid());

    addDependentStagedModel(dependentStagedModelsMap, Layout.class, linkedLayout);

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, linkedLayout);

    Layout layout =
        LayoutTestUtil.addTypeLinkToLayoutLayout(
            stagingGroup.getGroupId(), linkedLayout.getLayoutId());

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

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, layout);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, layout);

    validateExport(portletDataContext, layout, dependentStagedModelsMap);

    initImport();

    Layout exportedLayout = (Layout) readExportedStagedModel(layout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLayout);

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        linkedLayout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL linkedLayoutFriendlyURL = linkedLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        linkedLayoutFriendlyURL.getUuid(), liveGroup.getGroupId());

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        layout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL layoutFriendlyURL = layoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        layoutFriendlyURL.getUuid(), liveGroup.getGroupId());
  }
  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);
  }
  @Test
  public void testExportImportSelectedLayouts() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(group);

    long[] layoutIds = new long[] {layout.getLayoutId()};

    exportImportLayouts(layoutIds, getImportParameterMap());

    Assert.assertEquals(
        layoutIds.length, LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));

    importedLayout =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotNull(importedLayout);
  }
  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 getAncestorLayoutId() throws PortalException, SystemException {
    long layoutId = 0;

    Layout layout = this;

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

        break;
      }
    }

    return layoutId;
  }
  @Test
  public void testDeleteMissingLayouts() throws Exception {
    Layout layout1 = LayoutTestUtil.addLayout(group);
    Layout layout2 = LayoutTestUtil.addLayout(group);

    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(group.getGroupId(), false);

    long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);

    exportImportLayouts(layoutIds, getImportParameterMap());

    Assert.assertEquals(
        LayoutLocalServiceUtil.getLayoutsCount(group, false),
        LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));

    LayoutTestUtil.addLayout(importedGroup);

    Map<String, String[]> parameterMap = getImportParameterMap();

    parameterMap.put(
        PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS, new String[] {Boolean.TRUE.toString()});

    layoutIds = new long[] {layout1.getLayoutId()};

    exportImportLayouts(layoutIds, getImportParameterMap());

    Assert.assertEquals(
        LayoutLocalServiceUtil.getLayoutsCount(group, false),
        LayoutLocalServiceUtil.getLayoutsCount(importedGroup, false));

    Layout importedLayout1 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout1.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotNull(importedLayout1);

    Layout importedLayout2 =
        LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
            layout2.getUuid(), importedGroup.getGroupId(), false);

    Assert.assertNotNull(importedLayout2);
  }
  protected void populateElementLayoutMetadata(Element layoutElement, Layout layout)
      throws Exception {

    LayoutStagingHandler layoutStagingHandler = LayoutStagingUtil.getLayoutStagingHandler(layout);

    if (layoutStagingHandler != null) {
      LayoutRevision layoutRevision = layoutStagingHandler.getLayoutRevision();

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

        LayoutBranch layoutBranch = layoutRevision.getLayoutBranch();

        layoutElement.addAttribute("layout-branch-name", String.valueOf(layoutBranch.getName()));
      }
    }

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

    String layoutPrototypeUuid = layout.getLayoutPrototypeUuid();

    if (Validator.isNotNull(layoutPrototypeUuid)) {
      LayoutPrototype layoutPrototype =
          _layoutPrototypeLocalService.getLayoutPrototypeByUuidAndCompanyId(
              layoutPrototypeUuid, layout.getCompanyId());

      layoutElement.addAttribute("layout-prototype-uuid", layoutPrototypeUuid);
      layoutElement.addAttribute(
          "layout-prototype-name", layoutPrototype.getName(LocaleUtil.getDefault()));
    }
  }
Example #27
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 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);
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Layout layout)
      throws Exception {

    long groupId = portletDataContext.getGroupId();
    long userId = portletDataContext.getUserId(layout.getUserUuid());

    Element layoutElement = portletDataContext.getImportDataStagedModelElement(layout);

    String layoutUuid = GetterUtil.getString(layoutElement.attributeValue("layout-uuid"));

    long layoutId = GetterUtil.getInteger(layoutElement.attributeValue("layout-id"));

    long oldLayoutId = layoutId;

    boolean privateLayout = portletDataContext.isPrivateLayout();

    String action = layoutElement.attributeValue(Constants.ACTION);

    if (action.equals(Constants.DELETE)) {
      Layout deletingLayout =
          _layoutLocalService.fetchLayoutByUuidAndGroupId(layoutUuid, groupId, privateLayout);

      _layoutLocalService.deleteLayout(
          deletingLayout, false, ServiceContextThreadLocal.getServiceContext());

      return;
    }

    Map<Long, Layout> layouts =
        (Map<Long, Layout>) portletDataContext.getNewPrimaryKeysMap(Layout.class + ".layout");

    Layout existingLayout = null;
    Layout importedLayout = null;

    String friendlyURL = layout.getFriendlyURL();

    String layoutsImportMode =
        MapUtil.getString(
            portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_UUID);

    if (layoutsImportMode.equals(PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_ADD_AS_NEW)) {

      layoutId = _layoutLocalService.getNextLayoutId(groupId, privateLayout);
      friendlyURL = StringPool.SLASH + layoutId;
    } else if (layoutsImportMode.equals(
        PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_NAME)) {

      Locale locale = LocaleUtil.getSiteDefault();

      String localizedName = layout.getName(locale);

      List<Layout> previousLayouts = _layoutLocalService.getLayouts(groupId, privateLayout);

      for (Layout curLayout : previousLayouts) {
        if (localizedName.equals(curLayout.getName(locale))
            || friendlyURL.equals(curLayout.getFriendlyURL())) {

          existingLayout = curLayout;

          break;
        }
      }

      if (existingLayout == null) {
        layoutId = _layoutLocalService.getNextLayoutId(groupId, privateLayout);

        friendlyURL = getFriendlyURL(friendlyURL, layoutId);
      }
    } else if (layoutsImportMode.equals(
        PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

      existingLayout =
          _layoutLocalService.fetchLayoutByUuidAndGroupId(layout.getUuid(), groupId, privateLayout);

      if (SitesUtil.isLayoutModifiedSinceLastMerge(existingLayout)) {
        layouts.put(oldLayoutId, existingLayout);

        return;
      }

      LayoutFriendlyURL layoutFriendlyURL =
          _layoutFriendlyURLLocalService.fetchFirstLayoutFriendlyURL(
              groupId, privateLayout, friendlyURL);

      if ((layoutFriendlyURL != null) && (existingLayout == null)) {
        Layout mergeFailFriendlyURLLayout =
            _layoutLocalService.getLayout(layoutFriendlyURL.getPlid());

        SitesUtil.addMergeFailFriendlyURLLayout(mergeFailFriendlyURLLayout);

        if (!_log.isWarnEnabled()) {
          return;
        }

        StringBundler sb = new StringBundler(6);

        sb.append("Layout with layout ID ");
        sb.append(layout.getLayoutId());
        sb.append(" cannot be propagated because the friendly URL ");
        sb.append("conflicts with the friendly URL of layout with ");
        sb.append("layout ID ");
        sb.append(mergeFailFriendlyURLLayout.getLayoutId());

        _log.warn(sb.toString());

        return;
      }
    } else {

      // The default behavior of import mode is
      // PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_UUID

      existingLayout =
          _layoutLocalService.fetchLayoutByUuidAndGroupId(layout.getUuid(), groupId, privateLayout);

      if (existingLayout == null) {
        existingLayout =
            _layoutLocalService.fetchLayoutByFriendlyURL(groupId, privateLayout, friendlyURL);
      }

      if (existingLayout == null) {
        layoutId = _layoutLocalService.getNextLayoutId(groupId, privateLayout);

        friendlyURL = getFriendlyURL(friendlyURL, layoutId);
      }
    }

    if (_log.isDebugEnabled()) {
      StringBundler sb = new StringBundler(7);

      sb.append("Layout with {groupId=");
      sb.append(groupId);
      sb.append(",privateLayout=");
      sb.append(privateLayout);
      sb.append(",layoutId=");
      sb.append(layoutId);

      if (existingLayout == null) {
        sb.append("} does not exist");

        _log.debug(sb.toString());
      } else {
        sb.append("} exists");

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

    if (existingLayout == null) {
      long plid = _counterLocalService.increment();

      importedLayout = _layoutLocalService.createLayout(plid);

      if (layoutsImportMode.equals(
          PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

        importedLayout.setSourcePrototypeLayoutUuid(layout.getUuid());

        layoutId = _layoutLocalService.getNextLayoutId(groupId, privateLayout);

        friendlyURL = getFriendlyURL(friendlyURL, layoutId);
      } else {
        importedLayout.setCreateDate(layout.getCreateDate());
        importedLayout.setModifiedDate(layout.getModifiedDate());
        importedLayout.setLayoutPrototypeUuid(layout.getLayoutPrototypeUuid());
        importedLayout.setLayoutPrototypeLinkEnabled(layout.isLayoutPrototypeLinkEnabled());
        importedLayout.setSourcePrototypeLayoutUuid(layout.getSourcePrototypeLayoutUuid());
      }

      importedLayout.setUuid(layout.getUuid());
      importedLayout.setGroupId(groupId);
      importedLayout.setUserId(userId);
      importedLayout.setPrivateLayout(privateLayout);
      importedLayout.setLayoutId(layoutId);

      initNewLayoutPermissions(
          portletDataContext.getCompanyId(),
          groupId,
          userId,
          layout,
          importedLayout,
          privateLayout);

      LayoutSet layoutSet = _layoutSetLocalService.getLayoutSet(groupId, privateLayout);

      importedLayout.setLayoutSet(layoutSet);
    } else {
      importedLayout = existingLayout;
    }

    portletDataContext.setPlid(importedLayout.getPlid());
    portletDataContext.setOldPlid(layout.getPlid());

    long parentLayoutId = layout.getParentLayoutId();

    String parentLayoutUuid =
        GetterUtil.getString(layoutElement.attributeValue("parent-layout-uuid"));

    Element parentLayoutElement =
        portletDataContext.getReferenceDataElement(
            layout, Layout.class, layout.getGroupId(), parentLayoutUuid);

    if ((parentLayoutId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID)
        && (parentLayoutElement != null)) {

      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, parentLayoutElement);

      Layout importedParentLayout = layouts.get(parentLayoutId);

      parentLayoutId = importedParentLayout.getLayoutId();
    }

    if (_log.isDebugEnabled()) {
      StringBundler sb = new StringBundler(4);

      sb.append("Importing layout with layout id ");
      sb.append(layoutId);
      sb.append(" and parent layout id ");
      sb.append(parentLayoutId);

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

    importedLayout.setCompanyId(portletDataContext.getCompanyId());

    if (layout.getLayoutPrototypeUuid() != null) {
      importedLayout.setModifiedDate(new Date());
    }

    importedLayout.setParentLayoutId(parentLayoutId);
    importedLayout.setName(layout.getName());
    importedLayout.setTitle(layout.getTitle());
    importedLayout.setDescription(layout.getDescription());
    importedLayout.setKeywords(layout.getKeywords());
    importedLayout.setRobots(layout.getRobots());
    importedLayout.setType(layout.getType());

    String portletsMergeMode =
        MapUtil.getString(
            portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
            PortletDataHandlerKeys.PORTLETS_MERGE_MODE_REPLACE);

    if (layout.isTypePortlet()
        && Validator.isNotNull(layout.getTypeSettings())
        && !portletsMergeMode.equals(PortletDataHandlerKeys.PORTLETS_MERGE_MODE_REPLACE)) {

      mergePortlets(importedLayout, layout.getTypeSettings(), portletsMergeMode);
    } else if (layout.isTypeLinkToLayout()) {
      importLinkedLayout(portletDataContext, layout, importedLayout, layoutElement, layouts);
    } else {
      updateTypeSettings(importedLayout, layout);
    }

    importedLayout.setHidden(layout.isHidden());
    importedLayout.setFriendlyURL(
        getUniqueFriendlyURL(portletDataContext, importedLayout, friendlyURL));

    if (layout.getIconImageId() > 0) {
      importLayoutIconImage(portletDataContext, importedLayout, layoutElement);
    } else if (importedLayout.getIconImageId() > 0) {
      _imageLocalService.deleteImage(importedLayout.getIconImageId());
    }

    if (existingLayout == null) {
      int priority =
          _layoutLocalServiceHelper.getNextPriority(
              groupId, privateLayout, parentLayoutId, null, -1);

      importedLayout.setPriority(priority);
    }

    importedLayout.setLayoutPrototypeUuid(layout.getLayoutPrototypeUuid());
    importedLayout.setLayoutPrototypeLinkEnabled(layout.isLayoutPrototypeLinkEnabled());

    ServiceContext serviceContext = portletDataContext.createServiceContext(layout);

    importedLayout.setExpandoBridgeAttributes(serviceContext);

    StagingUtil.updateLastImportSettings(layoutElement, importedLayout, portletDataContext);

    fixImportTypeSettings(importedLayout);

    importTheme(portletDataContext, layout, importedLayout);

    _layoutLocalService.updateLayout(importedLayout);

    _layoutSetLocalService.updatePageCount(groupId, privateLayout);

    Map<Long, Long> layoutPlids =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Layout.class);

    layoutPlids.put(layout.getPlid(), importedLayout.getPlid());

    layouts.put(oldLayoutId, importedLayout);

    importAssets(portletDataContext, layout, importedLayout);

    importLayoutFriendlyURLs(portletDataContext, layout, importedLayout);

    portletDataContext.importClassedModel(layout, importedLayout);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Layout newLayout = _persistence.create(pk);

    newLayout.setMvccVersion(RandomTestUtil.nextLong());

    newLayout.setUuid(RandomTestUtil.randomString());

    newLayout.setGroupId(RandomTestUtil.nextLong());

    newLayout.setCompanyId(RandomTestUtil.nextLong());

    newLayout.setUserId(RandomTestUtil.nextLong());

    newLayout.setUserName(RandomTestUtil.randomString());

    newLayout.setCreateDate(RandomTestUtil.nextDate());

    newLayout.setModifiedDate(RandomTestUtil.nextDate());

    newLayout.setPrivateLayout(RandomTestUtil.randomBoolean());

    newLayout.setLayoutId(RandomTestUtil.nextLong());

    newLayout.setParentLayoutId(RandomTestUtil.nextLong());

    newLayout.setName(RandomTestUtil.randomString());

    newLayout.setTitle(RandomTestUtil.randomString());

    newLayout.setDescription(RandomTestUtil.randomString());

    newLayout.setKeywords(RandomTestUtil.randomString());

    newLayout.setRobots(RandomTestUtil.randomString());

    newLayout.setType(RandomTestUtil.randomString());

    newLayout.setTypeSettings(RandomTestUtil.randomString());

    newLayout.setHidden(RandomTestUtil.randomBoolean());

    newLayout.setFriendlyURL(RandomTestUtil.randomString());

    newLayout.setIconImageId(RandomTestUtil.nextLong());

    newLayout.setThemeId(RandomTestUtil.randomString());

    newLayout.setColorSchemeId(RandomTestUtil.randomString());

    newLayout.setWapThemeId(RandomTestUtil.randomString());

    newLayout.setWapColorSchemeId(RandomTestUtil.randomString());

    newLayout.setCss(RandomTestUtil.randomString());

    newLayout.setPriority(RandomTestUtil.nextInt());

    newLayout.setLayoutPrototypeUuid(RandomTestUtil.randomString());

    newLayout.setLayoutPrototypeLinkEnabled(RandomTestUtil.randomBoolean());

    newLayout.setSourcePrototypeLayoutUuid(RandomTestUtil.randomString());

    _layouts.add(_persistence.update(newLayout));

    Layout existingLayout = _persistence.findByPrimaryKey(newLayout.getPrimaryKey());

    Assert.assertEquals(existingLayout.getMvccVersion(), newLayout.getMvccVersion());
    Assert.assertEquals(existingLayout.getUuid(), newLayout.getUuid());
    Assert.assertEquals(existingLayout.getPlid(), newLayout.getPlid());
    Assert.assertEquals(existingLayout.getGroupId(), newLayout.getGroupId());
    Assert.assertEquals(existingLayout.getCompanyId(), newLayout.getCompanyId());
    Assert.assertEquals(existingLayout.getUserId(), newLayout.getUserId());
    Assert.assertEquals(existingLayout.getUserName(), newLayout.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingLayout.getCreateDate()),
        Time.getShortTimestamp(newLayout.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingLayout.getModifiedDate()),
        Time.getShortTimestamp(newLayout.getModifiedDate()));
    Assert.assertEquals(existingLayout.getPrivateLayout(), newLayout.getPrivateLayout());
    Assert.assertEquals(existingLayout.getLayoutId(), newLayout.getLayoutId());
    Assert.assertEquals(existingLayout.getParentLayoutId(), newLayout.getParentLayoutId());
    Assert.assertEquals(existingLayout.getName(), newLayout.getName());
    Assert.assertEquals(existingLayout.getTitle(), newLayout.getTitle());
    Assert.assertEquals(existingLayout.getDescription(), newLayout.getDescription());
    Assert.assertEquals(existingLayout.getKeywords(), newLayout.getKeywords());
    Assert.assertEquals(existingLayout.getRobots(), newLayout.getRobots());
    Assert.assertEquals(existingLayout.getType(), newLayout.getType());
    Assert.assertEquals(existingLayout.getTypeSettings(), newLayout.getTypeSettings());
    Assert.assertEquals(existingLayout.getHidden(), newLayout.getHidden());
    Assert.assertEquals(existingLayout.getFriendlyURL(), newLayout.getFriendlyURL());
    Assert.assertEquals(existingLayout.getIconImageId(), newLayout.getIconImageId());
    Assert.assertEquals(existingLayout.getThemeId(), newLayout.getThemeId());
    Assert.assertEquals(existingLayout.getColorSchemeId(), newLayout.getColorSchemeId());
    Assert.assertEquals(existingLayout.getWapThemeId(), newLayout.getWapThemeId());
    Assert.assertEquals(existingLayout.getWapColorSchemeId(), newLayout.getWapColorSchemeId());
    Assert.assertEquals(existingLayout.getCss(), newLayout.getCss());
    Assert.assertEquals(existingLayout.getPriority(), newLayout.getPriority());
    Assert.assertEquals(
        existingLayout.getLayoutPrototypeUuid(), newLayout.getLayoutPrototypeUuid());
    Assert.assertEquals(
        existingLayout.getLayoutPrototypeLinkEnabled(), newLayout.getLayoutPrototypeLinkEnabled());
    Assert.assertEquals(
        existingLayout.getSourcePrototypeLayoutUuid(), newLayout.getSourcePrototypeLayoutUuid());
  }