Example usage for com.liferay.portal.kernel.xml Element addElement

List of usage examples for com.liferay.portal.kernel.xml Element addElement

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.xml Element addElement.

Prototype

public Element addElement(QName qName);

Source Link

Usage

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

License:Open Source License

public void exportPortlet(PortletDataContext portletDataContext, long plid, Element parentElement,
        boolean exportPermissions, boolean exportPortletArchivedSetups, boolean exportPortletData,
        boolean exportPortletSetup, boolean exportPortletUserPreferences) throws Exception {

    long layoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;

    Layout layout = _layoutLocalService.fetchLayout(plid);

    if (layout != null) {
        layoutId = layout.getLayoutId();
    }/*from  w  ww.  ja  va  2  s .  c  o m*/

    Portlet portlet = _portletLocalService.getPortletById(portletDataContext.getCompanyId(),
            portletDataContext.getPortletId());

    if ((portlet == null) || portlet.isUndeployedPortlet()) {
        if (_log.isDebugEnabled()) {
            _log.debug("Do not export portlet " + portletDataContext.getPortletId()
                    + " because the portlet is not deployed");
        }

        return;
    }

    if (!portlet.isInstanceable() && !portlet.isPreferencesUniquePerLayout()
            && portletDataContext.hasNotUniquePerLayout(portletDataContext.getPortletId())) {

        return;
    }

    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
        PortletDataContext clonedPortletDataContext = _portletDataContextFactory
                .clonePortletDataContext(portletDataContext);

        ManifestSummary manifestSummary = clonedPortletDataContext.getManifestSummary();

        manifestSummary.resetCounters();

        PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

        portletDataHandler.prepareManifestSummary(clonedPortletDataContext);

        _portletDataHandlerStatusMessageSender.sendStatusMessage("portlet", portletDataContext.getPortletId(),
                manifestSummary);
    }

    Document document = SAXReaderUtil.createDocument();

    Element portletElement = document.addElement("portlet");

    portletElement.addAttribute("portlet-id", portletDataContext.getPortletId());
    portletElement.addAttribute("root-portlet-id", portletDataContext.getRootPortletId());
    portletElement.addAttribute("old-plid", String.valueOf(plid));
    portletElement.addAttribute("scope-group-id", String.valueOf(portletDataContext.getScopeGroupId()));
    portletElement.addAttribute("scope-layout-type", portletDataContext.getScopeType());
    portletElement.addAttribute("scope-layout-uuid", portletDataContext.getScopeLayoutUuid());
    portletElement.addAttribute("private-layout", String.valueOf(portletDataContext.isPrivateLayout()));

    // Data

    if (exportPortletData) {
        javax.portlet.PortletPreferences jxPortletPreferences = null;

        if (ExportImportThreadLocal.isInitialLayoutStagingInProcess()) {
            if (layout != null) {
                Group liveGroup = layout.getGroup();

                Group stagingGroup = liveGroup.getStagingGroup();

                layout.setGroupId(stagingGroup.getGroupId());

                jxPortletPreferences = PortletPreferencesFactoryUtil.getStrictPortletSetup(layout,
                        portletDataContext.getPortletId());

                layout.setGroupId(liveGroup.getGroupId());
            } else {
                Group liveGroup = _groupLocalService.getGroup(portletDataContext.getGroupId());

                Group stagingGroup = liveGroup.getStagingGroup();

                jxPortletPreferences = PortletPreferencesFactoryUtil.getStrictPortletSetup(
                        portletDataContext.getCompanyId(), stagingGroup.getGroupId(),
                        portletDataContext.getPortletId());
            }
        } else {
            if (layout != null) {
                jxPortletPreferences = PortletPreferencesFactoryUtil.getStrictPortletSetup(layout,
                        portletDataContext.getPortletId());
            } else {
                jxPortletPreferences = PortletPreferencesFactoryUtil.getStrictPortletSetup(
                        portletDataContext.getCompanyId(), portletDataContext.getGroupId(),
                        portletDataContext.getPortletId());
            }
        }

        if (!portlet.isPreferencesUniquePerLayout()) {
            StringBundler sb = new StringBundler(5);

            sb.append(portletDataContext.getPortletId());
            sb.append(StringPool.AT);
            sb.append(portletDataContext.getScopeType());
            sb.append(StringPool.AT);
            sb.append(portletDataContext.getScopeLayoutUuid());

            String dataKey = sb.toString();

            if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
                portletDataContext.putNotUniquePerLayout(dataKey);

                exportPortletData(portletDataContext, portlet, layout, jxPortletPreferences, portletElement);
            }
        } else {
            exportPortletData(portletDataContext, portlet, layout, jxPortletPreferences, portletElement);
        }
    }

    // Portlet preferences

    if (exportPortletSetup) {

        // Company

        exportPortletPreferences(portletDataContext, portletDataContext.getCompanyId(),
                PortletKeys.PREFS_OWNER_TYPE_COMPANY, false, layout, plid, portlet.getRootPortletId(),
                portletElement);

        // Group

        exportPortletPreferences(portletDataContext, portletDataContext.getScopeGroupId(),
                PortletKeys.PREFS_OWNER_TYPE_GROUP, false, layout, PortletKeys.PREFS_PLID_SHARED,
                portlet.getRootPortletId(), portletElement);

        // Group embedded portlets

        exportPortletPreferences(portletDataContext, portletDataContext.getScopeGroupId(),
                PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false, layout, PortletKeys.PREFS_PLID_SHARED,
                portlet.getPortletId(), portletElement);

        // Layout

        exportPortletPreferences(portletDataContext, PortletKeys.PREFS_OWNER_ID_DEFAULT,
                PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false, layout, plid, portletDataContext.getPortletId(),
                portletElement);
    }

    // Portlet user preferences

    if (exportPortletUserPreferences) {
        List<PortletPreferences> portletPreferencesList = _portletPreferencesLocalService.getPortletPreferences(
                PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletDataContext.getPortletId());

        for (PortletPreferences portletPreferences : portletPreferencesList) {

            boolean defaultUser = false;

            if (portletPreferences.getOwnerId() == PortletKeys.PREFS_OWNER_ID_DEFAULT) {

                defaultUser = true;
            }

            exportPortletPreferences(portletDataContext, portletPreferences.getOwnerId(),
                    PortletKeys.PREFS_OWNER_TYPE_USER, defaultUser, layout, plid,
                    portletDataContext.getPortletId(), portletElement);
        }

        try {
            PortletPreferences groupPortletPreferences = _portletPreferencesLocalService.getPortletPreferences(
                    portletDataContext.getScopeGroupId(), PortletKeys.PREFS_OWNER_TYPE_GROUP,
                    PortletKeys.PREFS_PLID_SHARED, portlet.getRootPortletId());

            exportPortletPreference(portletDataContext, portletDataContext.getScopeGroupId(),
                    PortletKeys.PREFS_OWNER_TYPE_GROUP, false, groupPortletPreferences,
                    portlet.getRootPortletId(), PortletKeys.PREFS_PLID_SHARED, portletElement);
        } catch (NoSuchPortletPreferencesException nsppe) {

            // LPS-52675

            if (_log.isDebugEnabled()) {
                _log.debug(nsppe, nsppe);
            }
        }
    }

    // Archived setups

    if (exportPortletArchivedSetups) {
        List<PortletItem> portletItems = _portletItemLocalService.getPortletItems(
                portletDataContext.getGroupId(), portletDataContext.getRootPortletId(),
                PortletPreferences.class.getName());

        for (PortletItem portletItem : portletItems) {
            exportPortletPreferences(portletDataContext, portletItem.getPortletItemId(),
                    PortletKeys.PREFS_OWNER_TYPE_ARCHIVED, false, null, plid, portletItem.getPortletId(),
                    portletElement);
        }
    }

    // Permissions

    if (exportPermissions) {
        _permissionExporter.exportPortletPermissions(portletDataContext, portletDataContext.getPortletId(),
                layout, portletElement);
    }

    // Zip

    StringBundler pathSB = new StringBundler(4);

    pathSB.append(ExportImportPathUtil.getPortletPath(portletDataContext));
    pathSB.append(StringPool.SLASH);
    pathSB.append(plid);
    pathSB.append("/portlet.xml");

    String path = pathSB.toString();

    Element element = parentElement.addElement("portlet");

    element.addAttribute("portlet-id", portletDataContext.getPortletId());
    element.addAttribute("layout-id", String.valueOf(layoutId));
    element.addAttribute("path", path);
    element.addAttribute("portlet-data", String.valueOf(exportPortletData));

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    element.addAttribute("schema-version", portletDataHandler.getSchemaVersion());

    StringBundler configurationOptionsSB = new StringBundler(6);

    if (exportPortletSetup) {
        configurationOptionsSB.append("setup");
        configurationOptionsSB.append(StringPool.COMMA);
    }

    if (exportPortletArchivedSetups) {
        configurationOptionsSB.append("archived-setups");
        configurationOptionsSB.append(StringPool.COMMA);
    }

    if (exportPortletUserPreferences) {
        configurationOptionsSB.append("user-preferences");
        configurationOptionsSB.append(StringPool.COMMA);
    }

    if (configurationOptionsSB.index() > 0) {
        configurationOptionsSB.setIndex(configurationOptionsSB.index() - 1);
    }

    element.addAttribute("portlet-configuration", configurationOptionsSB.toString());

    try {
        portletDataContext.addZipEntry(path, document.formattedString());
    } catch (IOException ioe) {
        if (_log.isWarnEnabled()) {
            _log.warn(ioe.getMessage());
        }
    }
}

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

License:Open Source License

public void exportPortletData(PortletDataContext portletDataContext, Portlet portlet, Layout layout,
        javax.portlet.PortletPreferences jxPortletPreferences, Element parentElement) throws Exception {

    if (portlet == null) {
        return;/* w w w  . j  a  v a  2 s  . com*/
    }

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    if ((portletDataHandler == null) || portletDataHandler.isDataPortletInstanceLevel()) {

        return;
    }

    Group group = _groupLocalService.getGroup(portletDataContext.getGroupId());

    long plid = LayoutConstants.DEFAULT_PLID;

    if (layout != null) {
        group = layout.getGroup();
        plid = layout.getPlid();
    }

    if (group.isStagingGroup()) {
        group = group.getLiveGroup();
    }

    String portletId = portlet.getPortletId();

    if (ExportImportThreadLocal.isStagingInProcess() && !group.isStagedPortlet(portletId)) {

        if (_log.isDebugEnabled()) {
            _log.debug("Not exporting data for " + portletId + " because it is configured not to be staged");
        }

        return;
    }

    if (_log.isDebugEnabled()) {
        _log.debug("Exporting data for " + portletId);
    }

    String path = ExportImportPathUtil.getPortletDataPath(portletDataContext);

    if (portletDataContext.hasPrimaryKey(String.class, path)) {
        return;
    }

    Date originalStartDate = portletDataContext.getStartDate();

    Date portletLastPublishDate = ExportImportDateUtil.getLastPublishDate(portletDataContext,
            jxPortletPreferences);

    portletDataContext.setStartDate(portletLastPublishDate);

    long groupId = portletDataContext.getGroupId();

    portletDataContext.setGroupId(portletDataContext.getScopeGroupId());

    portletDataContext.clearScopedPrimaryKeys();

    String data = null;

    try {
        data = portletDataHandler.exportData(portletDataContext, portletId, jxPortletPreferences);
    } finally {
        portletDataContext.setGroupId(groupId);
        portletDataContext.setStartDate(originalStartDate);
    }

    if (Validator.isNull(data)) {
        if (_log.isDebugEnabled()) {
            _log.debug("Not exporting data for " + portletId + " because null data was returned");
        }

        return;
    }

    Element portletDataElement = parentElement.addElement("portlet-data");

    portletDataElement.addAttribute("path", path);

    portletDataContext.addZipEntry(path, data);

    boolean updateLastPublishDate = MapUtil.getBoolean(portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);

    if (ExportImportThreadLocal.isStagingInProcess() && updateLastPublishDate) {

        DateRange adjustedDateRange = new DateRange(portletLastPublishDate, portletDataContext.getEndDate());

        _exportImportProcessCallbackRegistry.registerCallback(portletDataContext.getExportImportProcessId(),
                new UpdatePortletLastPublishDateCallable(adjustedDateRange, portletDataContext.getEndDate(),
                        portletDataContext.getGroupId(), plid, portletId));
    }
}

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 ww.j  ava2s .co  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.PortletExportController.java

License:Open Source License

protected void exportExpandoTables(PortletDataContext portletDataContext) throws Exception {

    Document document = SAXReaderUtil.createDocument();

    Element rootElement = document.addElement("expando-tables");

    Map<String, List<ExpandoColumn>> expandoColumnsMap = portletDataContext.getExpandoColumns();

    for (Map.Entry<String, List<ExpandoColumn>> entry : expandoColumnsMap.entrySet()) {

        String className = entry.getKey();

        Element expandoTableElement = rootElement.addElement("expando-table");

        expandoTableElement.addAttribute("class-name", className);

        List<ExpandoColumn> expandoColumns = entry.getValue();

        for (ExpandoColumn expandoColumn : expandoColumns) {
            Element expandoColumnElement = expandoTableElement.addElement("expando-column");

            expandoColumnElement.addAttribute("column-id", String.valueOf(expandoColumn.getColumnId()));
            expandoColumnElement.addAttribute("name", expandoColumn.getName());
            expandoColumnElement.addAttribute("type", String.valueOf(expandoColumn.getType()));

            DocUtil.add(expandoColumnElement, "default-data", expandoColumn.getDefaultData());

            Element typeSettingsElement = expandoColumnElement.addElement("type-settings");

            UnicodeProperties typeSettingsProperties = expandoColumn.getTypeSettingsProperties();

            typeSettingsElement.addCDATA(typeSettingsProperties.toString());
        }/*from ww w.jav a2s .c om*/
    }

    portletDataContext.addZipEntry(ExportImportPathUtil.getRootPath(portletDataContext) + "/expando-tables.xml",
            document.formattedString());
}

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

License:Open Source License

protected void exportLocks(PortletDataContext portletDataContext) throws Exception {

    Document document = SAXReaderUtil.createDocument();

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

    Map<String, Lock> locksMap = portletDataContext.getLocks();

    for (Map.Entry<String, Lock> entry : locksMap.entrySet()) {
        Lock lock = entry.getValue();

        String entryKey = entry.getKey();

        int pos = entryKey.indexOf(CharPool.POUND);

        String className = entryKey.substring(0, pos);
        String key = entryKey.substring(pos + 1);

        String path = getLockPath(portletDataContext, className, key, lock);

        Element assetElement = rootElement.addElement("asset");

        assetElement.addAttribute("path", path);
        assetElement.addAttribute("class-name", className);
        assetElement.addAttribute("key", key);

        portletDataContext.addZipEntry(path, lock);
    }/*from   w ww. j  a  v a2  s  .  c o  m*/

    portletDataContext.addZipEntry(ExportImportPathUtil.getRootPath(portletDataContext) + "/locks.xml",
            document.formattedString());
}

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

License:Open Source License

protected void exportPortletPreference(PortletDataContext portletDataContext, long ownerId, int ownerType,
        boolean defaultUser, PortletPreferences portletPreferences, String portletId, long plid,
        Element parentElement) throws Exception {

    String preferencesXML = portletPreferences.getPreferences();

    if (Validator.isNull(preferencesXML)) {
        preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
    }//from w  w  w  .j a va2  s .  c  o  m

    javax.portlet.PortletPreferences jxPortletPreferences = PortletPreferencesFactoryUtil
            .fromDefaultXML(preferencesXML);

    Portlet portlet = _portletLocalService.getPortletById(portletDataContext.getCompanyId(), portletId);

    Element portletPreferencesElement = parentElement.addElement("portlet-preferences");

    if ((portlet != null) && (portlet.getPortletDataHandlerInstance() != null)) {

        Element exportDataRootElement = portletDataContext.getExportDataRootElement();

        try {
            portletDataContext.clearScopedPrimaryKeys();

            Element preferenceDataElement = portletPreferencesElement.addElement("preference-data");

            portletDataContext.setExportDataRootElement(preferenceDataElement);

            ExportImportPortletPreferencesProcessor exportImportPortletPreferencesProcessor = ExportImportPortletPreferencesProcessorRegistryUtil
                    .getExportImportPortletPreferencesProcessor(portlet.getRootPortletId());

            if (exportImportPortletPreferencesProcessor != null) {
                List<Capability> exportCapabilities = exportImportPortletPreferencesProcessor
                        .getExportCapabilities();

                if (ListUtil.isNotEmpty(exportCapabilities)) {
                    for (Capability exportCapability : exportCapabilities) {
                        exportCapability.process(portletDataContext, jxPortletPreferences);
                    }
                }

                exportImportPortletPreferencesProcessor.processExportPortletPreferences(portletDataContext,
                        jxPortletPreferences);
            } else {
                PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

                jxPortletPreferences = portletDataHandler.processExportPortletPreferences(portletDataContext,
                        portletId, jxPortletPreferences);
            }
        } finally {
            portletDataContext.setExportDataRootElement(exportDataRootElement);
        }
    }

    Document document = SAXReaderUtil.read(PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));

    Element rootElement = document.getRootElement();

    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
    rootElement.addAttribute("default-user", String.valueOf(defaultUser));
    rootElement.addAttribute("plid", String.valueOf(plid));
    rootElement.addAttribute("portlet-id", portletId);

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
        PortletItem portletItem = _portletItemLocalService.getPortletItem(ownerId);

        rootElement.addAttribute("archive-user-uuid", portletItem.getUserUuid());
        rootElement.addAttribute("archive-name", portletItem.getName());
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
        User user = _userLocalService.fetchUserById(ownerId);

        if (user == null) {
            return;
        }

        rootElement.addAttribute("user-uuid", user.getUserUuid());
    }

    List<Node> nodes = document
            .selectNodes("/portlet-preferences/preference[name/text() = " + "'last-publish-date']");

    for (Node node : nodes) {
        node.detach();
    }

    String path = ExportImportPathUtil.getPortletPreferencesPath(portletDataContext, portletId, ownerId,
            ownerType, plid);

    portletPreferencesElement.addAttribute("path", path);

    portletDataContext.addZipEntry(path, document.formattedString(StringPool.TAB, false, false));
}

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

License:Open Source License

protected void exportServicePortletPreference(PortletDataContext portletDataContext, long ownerId,
        int ownerType, PortletPreferences portletPreferences, String serviceName, Element parentElement)
        throws Exception {

    String preferencesXML = portletPreferences.getPreferences();

    if (Validator.isNull(preferencesXML)) {
        preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
    }//w w  w . j  a  v a 2s . c  o m

    javax.portlet.PortletPreferences jxPortletPreferences = PortletPreferencesFactoryUtil
            .fromDefaultXML(preferencesXML);

    Document document = SAXReaderUtil.read(PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));

    Element rootElement = document.getRootElement();

    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
    rootElement.addAttribute("default-user", String.valueOf(false));
    rootElement.addAttribute("service-name", serviceName);

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
        PortletItem portletItem = _portletItemLocalService.getPortletItem(ownerId);

        rootElement.addAttribute("archive-user-uuid", portletItem.getUserUuid());
        rootElement.addAttribute("archive-name", portletItem.getName());
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
        User user = _userLocalService.fetchUserById(ownerId);

        if (user == null) {
            return;
        }

        rootElement.addAttribute("user-uuid", user.getUserUuid());
    }

    List<Node> nodes = document
            .selectNodes("/portlet-preferences/preference[name/text() = " + "'last-publish-date']");

    for (Node node : nodes) {
        node.detach();
    }

    Element serviceElement = parentElement.addElement("service");

    String path = ExportImportPathUtil.getServicePortletPreferencesPath(portletDataContext, serviceName,
            ownerId, ownerType);

    serviceElement.addAttribute("path", path);

    serviceElement.addAttribute("service-name", serviceName);

    portletDataContext.addZipEntry(path, document.formattedString());
}

From source file:com.liferay.exportimport.internal.content.processor.test.DefaultExportImportContentProcessorTest.java

License:Open Source License

@Before
public void setUp() throws Exception {
    _defaultLocale = LocaleUtil.getDefault();
    _nonDefaultLocale = getNonDefaultLocale();

    _liveGroup = GroupTestUtil.addGroup();
    _stagingGroup = GroupTestUtil.addGroup();

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_stagingGroup.getGroupId(),
            TestPropsValues.getUserId());

    _fileEntry = DLAppLocalServiceUtil.addFileEntry(TestPropsValues.getUserId(), _stagingGroup.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString() + ".txt",
            ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
            serviceContext);//from  w w w .j  a v a2  s. com

    ThumbnailCapability thumbnailCapability = _fileEntry.getRepositoryCapability(ThumbnailCapability.class);

    _fileEntry = thumbnailCapability.setLargeImageId(_fileEntry, _fileEntry.getFileEntryId());

    TestReaderWriter testReaderWriter = new TestReaderWriter();

    _portletDataContextExport = PortletDataContextFactoryUtil.createExportPortletDataContext(
            _stagingGroup.getCompanyId(), _stagingGroup.getGroupId(), new HashMap<>(),
            new Date(System.currentTimeMillis() - Time.HOUR), new Date(), testReaderWriter);

    Document document = SAXReaderUtil.createDocument();

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

    manifestRootElement.addElement("header");

    testReaderWriter.addEntry("/manifest.xml", document.asXML());

    Element rootElement = SAXReaderUtil.createElement("root");

    _portletDataContextExport.setExportDataRootElement(rootElement);

    _stagingPrivateLayout = addMultiLocaleLayout(_stagingGroup, true);
    _stagingPublicLayout = addMultiLocaleLayout(_stagingGroup, false);

    _portletDataContextExport.setPlid(_stagingPublicLayout.getPlid());

    _portletDataContextImport = PortletDataContextFactoryUtil.createImportPortletDataContext(
            _liveGroup.getCompanyId(), _liveGroup.getGroupId(), new HashMap<>(), new TestUserIdStrategy(),
            testReaderWriter);

    _portletDataContextImport.setImportDataRootElement(rootElement);

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

    _portletDataContextExport.setMissingReferencesElement(missingReferencesElement);

    _portletDataContextImport.setMissingReferencesElement(missingReferencesElement);

    _livePrivateLayout = addMultiLocaleLayout(_liveGroup, true);
    _livePublicLayout = addMultiLocaleLayout(_liveGroup, false);

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

    layoutPlids.put(_stagingPrivateLayout.getPlid(), _livePrivateLayout.getPlid());
    layoutPlids.put(_stagingPublicLayout.getPlid(), _livePublicLayout.getPlid());

    _portletDataContextImport.setPlid(_livePublicLayout.getPlid());

    _portletDataContextImport.setSourceGroupId(_stagingGroup.getGroupId());

    rootElement.addElement("entry");

    _referrerStagedModel = JournalTestUtil.addArticle(_stagingGroup.getGroupId(), RandomTestUtil.randomString(),
            RandomTestUtil.randomString());

    _exportImportContentProcessor = ExportImportContentProcessorRegistryUtil
            .getExportImportContentProcessor(String.class.getName());

    _layoutReferencesExportImportContentProcessor = _serviceTracker.getService();
}

From source file:com.liferay.exportimport.internal.content.processor.test.DefaultExportImportContentProcessorTest.java

License:Open Source License

@Test
public void testExportLinksToUserLayouts() throws Exception {
    User user = TestPropsValues.getUser();

    Group group = user.getGroup();

    Layout privateLayout = LayoutTestUtil.addLayout(group, true);
    Layout publicLayout = LayoutTestUtil.addLayout(group, false);

    PortletDataContext portletDataContextExport = PortletDataContextFactoryUtil.createExportPortletDataContext(
            group.getCompanyId(), group.getGroupId(), new HashMap<>(),
            new Date(System.currentTimeMillis() - Time.HOUR), new Date(), new TestReaderWriter());

    Element rootElement = SAXReaderUtil.createElement("root");

    portletDataContextExport.setExportDataRootElement(rootElement);

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

    portletDataContextExport.setMissingReferencesElement(missingReferencesElement);

    JournalArticle journalArticle = JournalTestUtil.addArticle(group.getGroupId(),
            RandomTestUtil.randomString(), RandomTestUtil.randomString());

    String content = replaceLinksToLayoutsParameters(getContent("layout_links_user_group.txt"), privateLayout,
            publicLayout);/*  w w w .jav  a2 s. c  om*/

    content = _exportImportContentProcessor.replaceExportContentReferences(portletDataContextExport,
            journalArticle, content, true, true);

    assertLinksToLayouts(content, privateLayout, 0);
    assertLinksToLayouts(content, privateLayout, privateLayout.getGroupId());
    assertLinksToLayouts(content, publicLayout, 0);
    assertLinksToLayouts(content, publicLayout, publicLayout.getGroupId());
}

From source file:com.liferay.exportimport.lar.DeletionSystemEventExporter.java

License:Open Source License

protected void exportDeletionSystemEvent(PortletDataContext portletDataContext, SystemEvent systemEvent,
        Element deletionSystemEventsElement) {

    Element deletionSystemEventElement = deletionSystemEventsElement.addElement("deletion-system-event");

    deletionSystemEventElement.addAttribute("class-name",
            PortalUtil.getClassName(systemEvent.getClassNameId()));
    deletionSystemEventElement.addAttribute("extra-data", systemEvent.getExtraData());
    deletionSystemEventElement.addAttribute("group-id", String.valueOf(systemEvent.getGroupId()));

    if (systemEvent.getReferrerClassNameId() > 0) {
        deletionSystemEventElement.addAttribute("referrer-class-name",
                PortalUtil.getClassName(systemEvent.getReferrerClassNameId()));
    }//from w  ww .j  a va 2s.co m

    deletionSystemEventElement.addAttribute("uuid", systemEvent.getClassUuid());

    ManifestSummary manifestSummary = portletDataContext.getManifestSummary();

    manifestSummary.incrementModelDeletionCount(
            new StagedModelType(systemEvent.getClassNameId(), systemEvent.getReferrerClassNameId()));
}