Search in sources :

Example 1 with LayoutTypePortlet

use of com.liferay.portal.kernel.model.LayoutTypePortlet in project liferay-db-setup-core by ableneo.

the class SetupPages method setLayoutTemplate.

public static void setLayoutTemplate(final Layout layout, final PageType page, final long userid) {
    if (layout.getLayoutType() instanceof LayoutTypePortlet) {
        LayoutTypePortlet portletLayout = (LayoutTypePortlet) layout.getLayoutType();
        if (page.isClearPage()) {
            if (page.getPagePortlet() != null && !page.getPagePortlet().isEmpty() && page.getLinkToUrl() != null && !page.getLinkToUrl().equals("")) {
                LOG.error("This is a link page! It cannot be cleared. If you intend to use this page for portlets, please delete this page, or remove the link from the page!");
            } else {
                removeAllPortlets(userid, portletLayout, layout);
            }
        }
        String themeId = null;
        try {
            if (!Validator.isBlank(page.getLayoutThemeId())) {
                themeId = page.getLayoutThemeId();
            }
            LayoutTemplate layoutTemplate = LayoutTemplateLocalServiceUtil.getLayoutTemplate(page.getLayout(), false, themeId);
            if (layoutTemplate != null) {
                LOG.info(String.format("Setting layout to %1$s for page %2$s", page.getLayout(), page.getName()));
                if (themeId != null) {
                    LOG.info(String.format(LAYOUT_WAS_LOOKED_UP_IN_THEME, themeId));
                }
                portletLayout.setLayoutTemplateId(UserLocalServiceUtil.getDefaultUserId(layout.getCompanyId()), layoutTemplate.getLayoutTemplateId());
                LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
            } else {
                LOG.error(String.format("Layout template %1$s not found !", page.getLayout()));
                if (themeId != null) {
                    LOG.error(String.format(LAYOUT_WAS_LOOKED_UP_IN_THEME, themeId));
                }
            }
        } catch (Exception e) {
            LOG.error(String.format("Error by setting layout template : %1$s", page.getLayout()), e);
            if (themeId != null) {
                LOG.error(String.format(LAYOUT_WAS_LOOKED_UP_IN_THEME, themeId));
            }
        }
    }
}
Also used : LayoutTemplate(com.liferay.portal.kernel.model.LayoutTemplate) LayoutTypePortlet(com.liferay.portal.kernel.model.LayoutTypePortlet) ReadOnlyException(javax.portlet.ReadOnlyException) NoSuchLayoutException(com.liferay.portal.kernel.exception.NoSuchLayoutException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException)

Example 2 with LayoutTypePortlet

use of com.liferay.portal.kernel.model.LayoutTypePortlet in project liferay-db-setup-core by ableneo.

the class SetupPages method insertPortletIntoPage.

private static void insertPortletIntoPage(final PageType page, final Layout layout, final PagePortletType portlet, final long companyId, final long groupId) throws PortalException {
    long plid = layout.getPlid();
    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
    long runAsUserId = SetupConfigurationThreadLocal.getRunAsUserId();
    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
    String portletId = portlet.getPortletId();
    String column = portlet.getColumn();
    String portletIdInc = "";
    try {
        int columnPos = portlet.getColumnPosition();
        portletIdInc = layoutTypePortlet.addPortletId(runAsUserId, portletId, column, columnPos, false);
        if (portletIdInc == null) {
            portletIdInc = portletId;
        }
    } catch (SystemException e) {
        LOG.error("Add wrappedPortlet error ", e);
    }
    javax.portlet.PortletPreferences preferences = PortletPreferencesLocalServiceUtil.getPreferences(companyId, ownerId, ownerType, plid, portletIdInc);
    List<PortletPreference> prefsList = portlet.getPortletPreference();
    for (PortletPreference p : prefsList) {
        try {
            preferences.setValue(p.getKey(), resolvePortletPrefValue(p.getKey(), p.getValue(), portlet, companyId, groupId));
        } catch (ReadOnlyException e) {
            LOG.error("Portlet preferences (" + p.getKey() + ", " + p.getValue() + ") of " + "wrappedPortlet " + portlet.getPortletId() + " caused an excpetion! ");
        }
    }
    PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType, plid, portletIdInc, preferences);
    if (Validator.isNotNull(column) && Validator.isNotNull(portletIdInc)) {
        layoutTypePortlet.movePortletId(runAsUserId, portletIdInc, column, portlet.getColumnPosition());
    }
    LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
    if (portlet.getRolePermissions() != null) {
        LOG.info(" i portlet rights");
        updatePortletDisplayPermissions(portlet, portletIdInc, layout, page, companyId, groupId, portlet.getRolePermissions(), getDefaultPortletPermissions(layout.isPrivateLayout()));
    }
    LOG.info(" i portlet added.");
}
Also used : PortletPreference(com.ableneo.liferay.portal.setup.domain.PortletPreference) LayoutTypePortlet(com.liferay.portal.kernel.model.LayoutTypePortlet) SystemException(com.liferay.portal.kernel.exception.SystemException) ReadOnlyException(javax.portlet.ReadOnlyException)

Example 3 with LayoutTypePortlet

use of com.liferay.portal.kernel.model.LayoutTypePortlet in project liferay-db-setup-core by ableneo.

the class SetupPages method removeAllPortlets.

private static void removeAllPortlets(final long runasUser, final LayoutTypePortlet layoutTypePortlet, final Layout layout) {
    List<Portlet> portlets = null;
    try {
        portlets = layoutTypePortlet.getAllPortlets();
    } catch (SystemException e1) {
        LOG.error(e1);
    }
    if (portlets != null) {
        for (Portlet portlet : portlets) {
            String portletId = portlet.getPortletId();
            try {
                if (layoutTypePortlet.hasPortletId(portletId)) {
                    LOG.debug(String.format("Removing wrappedPortlet %1$s", portletId));
                    layoutTypePortlet.removePortletId(runasUser, portletId);
                    String rootPortletId = PortletIdCodec.decodePortletName(portletId);
                    LOG.debug(String.format("Root portletId: %1$s", rootPortletId));
                    ResourceLocalServiceUtil.deleteResource(layout.getCompanyId(), rootPortletId, ResourceConstants.SCOPE_INDIVIDUAL, PortletPermissionUtil.getPrimaryKey(layout.getPlid(), portletId));
                    LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
                    List<PortletPreferences> list = PortletPreferencesLocalServiceUtil.getPortletPreferences(PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid(), portletId);
                    for (PortletPreferences p : list) {
                        PortletPreferencesLocalServiceUtil.deletePortletPreferences(p);
                    }
                }
            } catch (PortalException | SystemException e) {
                LOG.error(e);
            }
        }
    }
}
Also used : Portlet(com.liferay.portal.kernel.model.Portlet) LayoutTypePortlet(com.liferay.portal.kernel.model.LayoutTypePortlet) SystemException(com.liferay.portal.kernel.exception.SystemException) PortletPreferences(com.liferay.portal.kernel.model.PortletPreferences) PortalException(com.liferay.portal.kernel.exception.PortalException)

Aggregations

SystemException (com.liferay.portal.kernel.exception.SystemException)3 LayoutTypePortlet (com.liferay.portal.kernel.model.LayoutTypePortlet)3 PortalException (com.liferay.portal.kernel.exception.PortalException)2 ReadOnlyException (javax.portlet.ReadOnlyException)2 PortletPreference (com.ableneo.liferay.portal.setup.domain.PortletPreference)1 NoSuchLayoutException (com.liferay.portal.kernel.exception.NoSuchLayoutException)1 LayoutTemplate (com.liferay.portal.kernel.model.LayoutTemplate)1 Portlet (com.liferay.portal.kernel.model.Portlet)1 PortletPreferences (com.liferay.portal.kernel.model.PortletPreferences)1