Example usage for com.liferay.portal.kernel.portlet PortletPreferencesFactoryUtil getLayoutPortletSetup

List of usage examples for com.liferay.portal.kernel.portlet PortletPreferencesFactoryUtil getLayoutPortletSetup

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.portlet PortletPreferencesFactoryUtil getLayoutPortletSetup.

Prototype

public static PortletPreferences getLayoutPortletSetup(Layout layout, String portletId) 

Source Link

Usage

From source file:com.liferay.asset.publisher.web.internal.exportimport.portlet.preferences.processor.AssetPublisherExportImportPortletPreferencesProcessor.java

License:Open Source License

protected void restorePortletPreference(PortletDataContext portletDataContext, String name,
        PortletPreferences portletPreferences) throws Exception {

    Layout layout = _layoutLocalService.getLayout(portletDataContext.getPlid());

    PortletPreferences originalPortletPreferences = PortletPreferencesFactoryUtil.getLayoutPortletSetup(layout,
            portletDataContext.getPortletId());

    String[] values = originalPortletPreferences.getValues(name, new String[] { StringPool.BLANK });

    portletPreferences.setValues(name, values);
}

From source file:com.liferay.exportimport.controller.PortletExportController.java

License:Open Source License

protected File doExport(PortletDataContext portletDataContext) throws Exception {

    boolean exportPermissions = MapUtil.getBoolean(portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.PERMISSIONS);

    if (_log.isDebugEnabled()) {
        _log.debug("Export permissions " + exportPermissions);
    }/*  w  w w  . ja v  a 2  s . c o m*/

    StopWatch stopWatch = new StopWatch();

    stopWatch.start();

    Layout layout = _layoutLocalService.getLayout(portletDataContext.getPlid());

    if (!layout.isTypeControlPanel() && !layout.isTypePanel() && !layout.isTypePortlet()) {

        StringBundler sb = new StringBundler(4);

        sb.append("Unable to export layout ");
        sb.append(layout.getPlid());
        sb.append(" because it has an invalid type: ");
        sb.append(layout.getType());

        throw new LayoutImportException(sb.toString());
    }

    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

    if (serviceContext == null) {
        serviceContext = new ServiceContext();

        serviceContext.setCompanyId(layout.getCompanyId());
        serviceContext.setSignedIn(false);

        long defaultUserId = _userLocalService.getDefaultUserId(layout.getCompanyId());

        serviceContext.setUserId(defaultUserId);

        ServiceContextThreadLocal.pushServiceContext(serviceContext);
    }

    long layoutSetBranchId = MapUtil.getLong(portletDataContext.getParameterMap(), "layoutSetBranchId");

    serviceContext.setAttribute("layoutSetBranchId", layoutSetBranchId);

    long scopeGroupId = portletDataContext.getGroupId();

    javax.portlet.PortletPreferences jxPortletPreferences = PortletPreferencesFactoryUtil
            .getLayoutPortletSetup(layout, portletDataContext.getPortletId());

    String scopeType = GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeType", null));
    String scopeLayoutUuid = GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeLayoutUuid", null));

    if (Validator.isNotNull(scopeType)) {
        Group scopeGroup = null;

        if (scopeType.equals("company")) {
            scopeGroup = _groupLocalService.getCompanyGroup(layout.getCompanyId());
        } else if (Validator.isNotNull(scopeLayoutUuid)) {
            scopeGroup = layout.getScopeGroup();
        }

        if (scopeGroup != null) {
            scopeGroupId = scopeGroup.getGroupId();
        }
    }

    portletDataContext.setScopeType(scopeType);
    portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);

    Document document = SAXReaderUtil.createDocument();

    Element rootElement = document.addElement("root");

    portletDataContext.setExportDataRootElement(rootElement);

    Element headerElement = rootElement.addElement("header");

    headerElement.addAttribute("available-locales", StringUtil.merge(
            LanguageUtil.getAvailableLocales(_portal.getSiteGroupId(portletDataContext.getScopeGroupId()))));
    headerElement.addAttribute("build-number", String.valueOf(ReleaseInfo.getBuildNumber()));
    headerElement.addAttribute("export-date", Time.getRFC822());

    if (portletDataContext.hasDateRange()) {
        headerElement.addAttribute("start-date", String.valueOf(portletDataContext.getStartDate()));
        headerElement.addAttribute("end-date", String.valueOf(portletDataContext.getEndDate()));
    }

    headerElement.addAttribute("type", portletDataContext.getType());
    headerElement.addAttribute("company-id", String.valueOf(portletDataContext.getCompanyId()));
    headerElement.addAttribute("company-group-id", String.valueOf(portletDataContext.getCompanyGroupId()));
    headerElement.addAttribute("group-id", String.valueOf(scopeGroupId));
    headerElement.addAttribute("user-personal-site-group-id",
            String.valueOf(portletDataContext.getUserPersonalSiteGroupId()));
    headerElement.addAttribute("private-layout", String.valueOf(layout.isPrivateLayout()));
    headerElement.addAttribute("root-portlet-id", portletDataContext.getRootPortletId());
    headerElement.addAttribute("schema-version", ExportImportConstants.EXPORT_IMPORT_SCHEMA_VERSION);

    Element missingReferencesElement = rootElement.addElement("missing-references");

    portletDataContext.setMissingReferencesElement(missingReferencesElement);

    Map<String, Boolean> exportPortletControlsMap = _exportImportHelper.getExportPortletControlsMap(
            layout.getCompanyId(), portletDataContext.getPortletId(), portletDataContext.getParameterMap());

    exportPortlet(portletDataContext, layout.getPlid(), rootElement, exportPermissions,
            exportPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS),
            exportPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_DATA),
            exportPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_SETUP),
            exportPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_USER_PREFERENCES));
    exportService(portletDataContext, rootElement,
            exportPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_SETUP));

    exportAssetLinks(portletDataContext);
    exportExpandoTables(portletDataContext);
    exportLocks(portletDataContext);

    portletDataContext.addDeletionSystemEventStagedModelTypes(new StagedModelType(StagedAssetLink.class));

    _deletionSystemEventExporter.exportDeletionSystemEvents(portletDataContext);

    if (exportPermissions) {
        _permissionExporter.exportPortletDataPermissions(portletDataContext);
    }

    _exportImportHelper.writeManifestSummary(document, portletDataContext.getManifestSummary());

    if (_log.isInfoEnabled()) {
        _log.info("Exporting portlet took " + stopWatch.getTime() + " ms");
    }

    try {
        portletDataContext.addZipEntry("/manifest.xml", document.formattedString());
    } catch (IOException ioe) {
        throw new SystemException("Unable to create the export LAR manifest file for portlet "
                + portletDataContext.getPortletId(), ioe);
    }

    ZipWriter zipWriter = portletDataContext.getZipWriter();

    return zipWriter.getFile();
}

From source file:com.liferay.exportimport.controller.PortletImportController.java

License:Open Source License

public void importPortletPreferences(PortletDataContext portletDataContext, long companyId, long groupId,
        Layout layout, Element parentElement, boolean preserveScopeLayoutId,
        boolean importPortletArchivedSetups, boolean importPortletData, boolean importPortletSetup,
        boolean importPortletUserPreferences) throws Exception {

    long plid = LayoutConstants.DEFAULT_PLID;
    String scopeType = StringPool.BLANK;
    String scopeLayoutUuid = StringPool.BLANK;

    if (layout != null) {
        plid = layout.getPlid();/*from  w ww .j av  a 2 s  .c  o m*/

        if (preserveScopeLayoutId) {
            javax.portlet.PortletPreferences jxPortletPreferences = PortletPreferencesFactoryUtil
                    .getLayoutPortletSetup(layout, portletDataContext.getPortletId());

            scopeType = GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeType", null));
            scopeLayoutUuid = GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeLayoutUuid", null));

            portletDataContext.setScopeType(scopeType);
            portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
        }
    }

    List<Element> portletPreferencesElements = parentElement.elements("portlet-preferences");

    for (Element portletPreferencesElement : portletPreferencesElements) {
        String path = portletPreferencesElement.attributeValue("path");

        if (portletDataContext.isPathNotProcessed(path)) {
            String xml = null;

            Element element = null;

            try {
                xml = portletDataContext.getZipEntryAsString(path);

                Document preferencesDocument = SAXReaderUtil.read(xml);

                element = preferencesDocument.getRootElement();
            } catch (DocumentException de) {
                throw new SystemException(
                        "Unable to parse XML portlet preferences for portlet "
                                + portletDataContext.getPortletId() + " while importing portlet preferences",
                        de);
            }

            long ownerId = GetterUtil.getLong(element.attributeValue("owner-id"));
            int ownerType = GetterUtil.getInteger(element.attributeValue("owner-type"));

            if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) || !importPortletSetup) {

                continue;
            }

            if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) && !importPortletArchivedSetups) {

                continue;
            }

            if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_USER)
                    && (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT) && !importPortletUserPreferences) {

                continue;
            }

            long curPlid = plid;
            String curPortletId = portletDataContext.getPortletId();

            if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
                curPlid = PortletKeys.PREFS_PLID_SHARED;
                curPortletId = portletDataContext.getRootPortletId();
                ownerId = portletDataContext.getScopeGroupId();
            }

            long elementPlid = GetterUtil.getLong(element.attributeValue("plid"));

            if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT)
                    && (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT)
                    && (elementPlid == PortletKeys.PREFS_PLID_SHARED)) {

                curPlid = PortletKeys.PREFS_PLID_SHARED;
                ownerId = portletDataContext.getScopeGroupId();
            }

            if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
                String userUuid = element.attributeValue("archive-user-uuid");

                long userId = portletDataContext.getUserId(userUuid);

                String name = element.attributeValue("archive-name");

                curPortletId = portletDataContext.getRootPortletId();

                PortletItem portletItem = _portletItemLocalService.updatePortletItem(userId, groupId, name,
                        curPortletId, PortletPreferences.class.getName());

                curPlid = LayoutConstants.DEFAULT_PLID;
                ownerId = portletItem.getPortletItemId();
            }

            if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
                String userUuid = element.attributeValue("user-uuid");

                ownerId = portletDataContext.getUserId(userUuid);
            }

            boolean defaultUser = GetterUtil.getBoolean(element.attributeValue("default-user"));

            if (defaultUser) {
                ownerId = _userLocalService.getDefaultUserId(companyId);
            }

            javax.portlet.PortletPreferences jxPortletPreferences = PortletPreferencesFactoryUtil
                    .fromXML(companyId, ownerId, ownerType, curPlid, curPortletId, xml);

            Element importDataRootElement = portletDataContext.getImportDataRootElement();

            try {
                Element preferenceDataElement = portletPreferencesElement.element("preference-data");

                if (preferenceDataElement != null) {
                    portletDataContext.setImportDataRootElement(preferenceDataElement);
                }

                ExportImportPortletPreferencesProcessor exportImportPortletPreferencesProcessor = ExportImportPortletPreferencesProcessorRegistryUtil
                        .getExportImportPortletPreferencesProcessor(
                                PortletIdCodec.decodePortletName(curPortletId));

                if (exportImportPortletPreferencesProcessor != null) {
                    List<Capability> importCapabilities = exportImportPortletPreferencesProcessor
                            .getImportCapabilities();

                    if (ListUtil.isNotEmpty(importCapabilities)) {
                        for (Capability importCapability : importCapabilities) {

                            importCapability.process(portletDataContext, jxPortletPreferences);
                        }
                    }

                    exportImportPortletPreferencesProcessor.processImportPortletPreferences(portletDataContext,
                            jxPortletPreferences);
                } else {
                    PortletDataHandler portletDataHandler = _portletDataHandlerProvider
                            .provide(portletDataContext.getCompanyId(), curPortletId);

                    if (portletDataHandler != null) {
                        jxPortletPreferences = portletDataHandler.processImportPortletPreferences(
                                portletDataContext, curPortletId, jxPortletPreferences);
                    }
                }
            } finally {
                portletDataContext.setImportDataRootElement(importDataRootElement);
            }

            updatePortletPreferences(portletDataContext, ownerId, ownerType, curPlid, curPortletId,
                    PortletPreferencesFactoryUtil.toXML(jxPortletPreferences), importPortletData);
        }
    }

    if (preserveScopeLayoutId && (layout != null)) {
        javax.portlet.PortletPreferences jxPortletPreferences = PortletPreferencesFactoryUtil
                .getLayoutPortletSetup(layout, portletDataContext.getPortletId());

        try {
            jxPortletPreferences.setValue("lfrScopeType", scopeType);
            jxPortletPreferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);

            jxPortletPreferences.store();
        } finally {
            portletDataContext.setScopeType(scopeType);
            portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
        }
    }
}

From source file:com.liferay.exportimport.web.internal.portlet.action.ActionUtil.java

License:Open Source License

public static PortletPreferences getLayoutPortletSetup(PortletRequest portletRequest, Portlet portlet) {

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

    Layout layout = themeDisplay.getLayout();

    return PortletPreferencesFactoryUtil.getLayoutPortletSetup(layout, portlet.getPortletId());
}