protected void validatePrototypedLayouts(Class<?> clazz, long groupId) throws Exception {

    List<Layout> layouts = getLayouts(clazz);

    for (Layout layout : layouts) {
      Layout importedLayout =
          LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(
              layout.getUuid(), groupId, layout.getPrivateLayout());

      Assert.assertNotNull(importedLayout);
      Assert.assertEquals(
          layout.getTypeSettingsProperty(
              LayoutSetPrototypeStagedModelDataHandlerTest.class.getName()),
          importedLayout.getTypeSettingsProperty(
              LayoutSetPrototypeStagedModelDataHandlerTest.class.getName()));
    }

    List<LayoutFriendlyURL> layoutFriendlyURLs = getLayoutFriendlyURLs(clazz);

    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
      LayoutFriendlyURL importedLayoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURLByUuidAndGroupId(
              layoutFriendlyURL.getUuid(), groupId);

      Assert.assertNotNull(importedLayoutFriendlyURL);
      Assert.assertEquals(
          layoutFriendlyURL.getFriendlyURL(), importedLayoutFriendlyURL.getFriendlyURL());
    }
  }
  protected LayoutFriendlyURL getUniqueLayoutFriendlyURL(
      PortletDataContext portletDataContext,
      LayoutFriendlyURL layoutFriendlyURL,
      LayoutFriendlyURL existingLayoutFriendlyURL)
      throws Exception {

    String friendlyURL = layoutFriendlyURL.getFriendlyURL();

    for (int i = 1; ; i++) {
      LayoutFriendlyURL duplicateLayoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURL(
              portletDataContext.getScopeGroupId(),
              layoutFriendlyURL.isPrivateLayout(),
              layoutFriendlyURL.getFriendlyURL(),
              layoutFriendlyURL.getLanguageId());

      if ((duplicateLayoutFriendlyURL == null)
          || ((existingLayoutFriendlyURL != null)
              && (existingLayoutFriendlyURL.getLayoutFriendlyURLId()
                  == duplicateLayoutFriendlyURL.getLayoutFriendlyURLId()))) {

        break;
      }

      layoutFriendlyURL.setFriendlyURL(friendlyURL + i);
    }

    return layoutFriendlyURL;
  }
  @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());
  }
Exemplo n.º 4
0
  @Override
  public Map<Locale, String> getFriendlyURLMap() throws SystemException {
    Map<Locale, String> friendlyURLMap = new HashMap<Locale, String>();

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

    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
      friendlyURLMap.put(
          LocaleUtil.fromLanguageId(layoutFriendlyURL.getLanguageId()),
          layoutFriendlyURL.getFriendlyURL());
    }

    return friendlyURLMap;
  }
  protected LayoutFriendlyURL fetchExistingLayoutFriendlyURL(
      PortletDataContext portletDataContext, LayoutFriendlyURL layoutFriendlyURL, long plid) {

    LayoutFriendlyURL existingLayoutFriendlyURL =
        fetchStagedModelByUuidAndGroupId(
            layoutFriendlyURL.getUuid(), portletDataContext.getScopeGroupId());

    if (existingLayoutFriendlyURL == null) {
      existingLayoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.fetchLayoutFriendlyURL(
              plid, layoutFriendlyURL.getLanguageId(), false);
    }

    return existingLayoutFriendlyURL;
  }
  /**
   * Adds the layout friendly u r l to the database. Also notifies the appropriate model listeners.
   *
   * @param layoutFriendlyURL the layout friendly u r l
   * @return the layout friendly u r l that was added
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public LayoutFriendlyURL addLayoutFriendlyURL(LayoutFriendlyURL layoutFriendlyURL) {
    layoutFriendlyURL.setNew(true);

    return layoutFriendlyURLPersistence.update(layoutFriendlyURL);
  }
  protected void deleteMissingLayoutFriendlyURLs(
      PortletDataContext portletDataContext, Layout layout) {

    Map<Long, Long> layoutFriendlyURLIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(LayoutFriendlyURL.class);

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

    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
      if (!layoutFriendlyURLIds.containsValue(layoutFriendlyURL.getLayoutFriendlyURLId())) {

        _layoutFriendlyURLLocalService.deleteLayoutFriendlyURL(layoutFriendlyURL);
      }
    }
  }
Exemplo n.º 8
0
  @Override
  public String getFriendlyURL(Locale locale) {
    Layout layout = this;

    String friendlyURL = layout.getFriendlyURL();

    try {
      LayoutFriendlyURL layoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURL(
              layout.getPlid(), LocaleUtil.toLanguageId(locale));

      friendlyURL = layoutFriendlyURL.getFriendlyURL();
    } catch (Exception e) {
    }

    return friendlyURL;
  }
  @Override
  protected void validateImport(
      Map<String, List<StagedModel>> dependentStagedModelsMap, Group group) throws Exception {

    List<StagedModel> dependentStagedModels =
        dependentStagedModelsMap.get(Layout.class.getSimpleName());

    Assert.assertEquals(1, dependentStagedModels.size());

    Layout parentLayout = (Layout) dependentStagedModels.get(0);

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        parentLayout.getUuid(), group.getGroupId(), false);

    List<LayoutFriendlyURL> parentLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(parentLayout.getPlid());

    LayoutFriendlyURL parentLayoutFriendlyURL = parentLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        parentLayoutFriendlyURL.getUuid(), group.getGroupId());
  }
  @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);
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, LayoutFriendlyURL layoutFriendlyURL) throws Exception {

    long userId = portletDataContext.getUserId(layoutFriendlyURL.getUserUuid());

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

    long plid = MapUtil.getLong(plids, layoutFriendlyURL.getPlid(), layoutFriendlyURL.getPlid());

    ServiceContext serviceContext = portletDataContext.createServiceContext(layoutFriendlyURL);

    LayoutFriendlyURL importedLayoutFriendlyURL = null;

    if (portletDataContext.isDataStrategyMirror()) {
      LayoutFriendlyURL existingLayoutFriendlyURL =
          fetchExistingLayoutFriendlyURL(portletDataContext, layoutFriendlyURL, plid);

      layoutFriendlyURL =
          getUniqueLayoutFriendlyURL(
              portletDataContext, layoutFriendlyURL, existingLayoutFriendlyURL);

      if (existingLayoutFriendlyURL == null) {
        serviceContext.setUuid(layoutFriendlyURL.getUuid());

        importedLayoutFriendlyURL =
            LayoutFriendlyURLLocalServiceUtil.addLayoutFriendlyURL(
                userId,
                portletDataContext.getCompanyId(),
                portletDataContext.getScopeGroupId(),
                plid,
                portletDataContext.isPrivateLayout(),
                layoutFriendlyURL.getFriendlyURL(),
                layoutFriendlyURL.getLanguageId(),
                serviceContext);
      } else {
        importedLayoutFriendlyURL =
            LayoutFriendlyURLLocalServiceUtil.updateLayoutFriendlyURL(
                userId,
                portletDataContext.getCompanyId(),
                portletDataContext.getScopeGroupId(),
                plid,
                portletDataContext.isPrivateLayout(),
                layoutFriendlyURL.getFriendlyURL(),
                layoutFriendlyURL.getLanguageId(),
                serviceContext);
      }
    } else {
      layoutFriendlyURL = getUniqueLayoutFriendlyURL(portletDataContext, layoutFriendlyURL, null);

      importedLayoutFriendlyURL =
          LayoutFriendlyURLLocalServiceUtil.addLayoutFriendlyURL(
              userId,
              portletDataContext.getCompanyId(),
              portletDataContext.getScopeGroupId(),
              plid,
              portletDataContext.isPrivateLayout(),
              layoutFriendlyURL.getFriendlyURL(),
              layoutFriendlyURL.getLanguageId(),
              serviceContext);
    }

    portletDataContext.importClassedModel(layoutFriendlyURL, importedLayoutFriendlyURL);
  }