Skip to content

Commit

Permalink
LPD-44292 Use importers to add different layout structure items
Browse files Browse the repository at this point in the history
  • Loading branch information
ealonso authored and brianchandotcom committed Dec 17, 2024
1 parent e6d5182 commit c21c697
Showing 1 changed file with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.liferay.fragment.service.FragmentEntryLinkLocalService;
import com.liferay.fragment.service.FragmentEntryLocalService;
import com.liferay.headless.admin.site.dto.v1_0.PageElement;
import com.liferay.headless.admin.site.internal.dto.v1_0.util.PageElementTypeUtil;
import com.liferay.headless.admin.site.internal.resource.util.GroupUtil;
import com.liferay.headless.admin.site.internal.resource.util.layout.structure.item.importer.CollectionItemLayoutStructureItemImporter;
import com.liferay.headless.admin.site.internal.resource.util.layout.structure.item.importer.CollectionLayoutStructureItemImporter;
Expand All @@ -23,6 +22,7 @@
import com.liferay.headless.admin.site.internal.resource.util.layout.structure.item.importer.FragmentLayoutStructureItemImporter;
import com.liferay.headless.admin.site.internal.resource.util.layout.structure.item.importer.LayoutStructureItemImporter;
import com.liferay.headless.admin.site.internal.resource.util.layout.structure.item.importer.RowLayoutStructureItemImporter;
import com.liferay.headless.admin.site.internal.resource.util.layout.structure.item.importer.context.LayoutStructureItemImporterContext;
import com.liferay.headless.admin.site.resource.v1_0.PageElementResource;
import com.liferay.layout.page.template.model.LayoutPageTemplateStructure;
import com.liferay.layout.page.template.service.LayoutPageTemplateStructureLocalService;
Expand All @@ -31,7 +31,6 @@
import com.liferay.layout.util.structure.LayoutStructureItemUtil;
import com.liferay.layout.util.structure.exception.NoSuchLayoutStructureItemException;
import com.liferay.portal.kernel.feature.flag.FeatureFlagManagerUtil;
import com.liferay.portal.kernel.json.JSONFactory;
import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.service.LayoutLocalService;
import com.liferay.portal.kernel.util.Validator;
Expand Down Expand Up @@ -314,7 +313,9 @@ public PageElement getSiteSiteByExternalReferenceCodePageElement(
layoutPageTemplateStructure.getData(
segmentsExperience.getSegmentsExperienceId()));

return _addPageElement(layout, layoutStructure, pageElement);
return _addPageElement(
groupId, layout, layoutStructure, pageElement,
segmentsExperience.getSegmentsExperienceId());
}

@Override
Expand Down Expand Up @@ -362,7 +363,9 @@ public PageElement putSiteSiteByExternalReferenceCodePageElement(
pageElementExternalReferenceCode);

if (layoutStructureItem == null) {
return _addPageElement(layout, layoutStructure, pageElement);
return _addPageElement(
groupId, layout, layoutStructure, pageElement,
segmentsExperience.getSegmentsExperienceId());
}

LayoutStructureItem parentLayoutStructureItem =
Expand Down Expand Up @@ -429,39 +432,40 @@ protected void activate(BundleContext bundleContext) {
PageElement.Type.ROW, new RowLayoutStructureItemImporter());
}

private void _addChildPageElements(
LayoutStructure layoutStructure, PageElement pageElement) {
private LayoutStructureItem _addLayoutStructureItem(
LayoutStructure layoutStructure,
LayoutStructureItemImporterContext
layoutStructureItemImporterContext,
PageElement pageElement)
throws Exception {

for (PageElement childPageElement : pageElement.getPageElements()) {
LayoutStructureItem layoutStructureItem =
layoutStructure.addLayoutStructureItem(
PageElementTypeUtil.toInternalType(
childPageElement.getType()),
childPageElement.getParentExternalReferenceCode(),
childPageElement.getPosition());
LayoutStructureItemImporter layoutStructureItemImporter =
_layoutStructureItemImporters.get(pageElement.getType());

layoutStructureItem.updateItemConfig(
_jsonFactory.createJSONObject());
LayoutStructureItem layoutStructureItem =
layoutStructureItemImporter.addLayoutStructureItem(
layoutStructure, layoutStructureItemImporterContext,
pageElement);

_addChildPageElements(layoutStructure, childPageElement);
for (PageElement childPageElement : pageElement.getPageElements()) {
_addLayoutStructureItem(
layoutStructure, layoutStructureItemImporterContext,
childPageElement);
}

return layoutStructureItem;
}

private PageElement _addPageElement(
Layout layout, LayoutStructure layoutStructure,
PageElement pageElement)
long groupId, Layout layout, LayoutStructure layoutStructure,
PageElement pageElement, long segmentsExperienceId)
throws Exception {

LayoutStructureItem layoutStructureItem =
layoutStructure.addLayoutStructureItem(
pageElement.getExternalReferenceCode(),
PageElementTypeUtil.toInternalType(pageElement.getType()),
_getParentExternalReferenceCode(pageElement, layoutStructure),
pageElement.getPosition());

layoutStructureItem.updateItemConfig(_jsonFactory.createJSONObject());

_addChildPageElements(layoutStructure, pageElement);
LayoutStructureItem layoutStructureItem = _addLayoutStructureItem(
layoutStructure,
new LayoutStructureItemImporterContext(
groupId, layout, segmentsExperienceId, contextUser.getUserId()),
pageElement);

_layoutPageTemplateStructureLocalService.
updateLayoutPageTemplateStructureData(
Expand Down Expand Up @@ -507,9 +511,6 @@ private String _getParentExternalReferenceCode(
@Reference
private FragmentEntryLocalService _fragmentEntryLocalService;

@Reference
private JSONFactory _jsonFactory;

@Reference
private LayoutLocalService _layoutLocalService;

Expand Down

0 comments on commit c21c697

Please sign in to comment.