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

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

Introduction

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

Prototype

public List<Element> elements(String name);

Source Link

Usage

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();/*w  w  w  .j av  a 2  s  .com*/

        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.controller.PortletImportController.java

License:Open Source License

protected void doImportPortletInfo(PortletDataContext portletDataContext, long userId) throws Exception {

    Map<String, String[]> parameterMap = portletDataContext.getParameterMap();

    boolean importPermissions = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PERMISSIONS);

    StopWatch stopWatch = new StopWatch();

    stopWatch.start();//from   ww w  .  j  a  v a 2 s . c  o  m

    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

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

        serviceContext.setCompanyId(portletDataContext.getCompanyId());
        serviceContext.setSignedIn(false);
        serviceContext.setUserId(userId);

        ServiceContextThreadLocal.pushServiceContext(serviceContext);
    }

    // LAR validation

    validateFile(portletDataContext.getCompanyId(), portletDataContext.getGroupId(),
            portletDataContext.getPortletId(), portletDataContext.getZipReader());

    // Source and target group id

    Map<Long, Long> groupIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Group.class);

    groupIds.put(portletDataContext.getSourceGroupId(), portletDataContext.getGroupId());

    // Manifest

    ManifestSummary manifestSummary = _exportImportHelper.getManifestSummary(portletDataContext);

    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
        _portletDataHandlerStatusMessageSender.sendStatusMessage("portlet", portletDataContext.getPortletId(),
                manifestSummary);
    }

    portletDataContext.setManifestSummary(manifestSummary);

    // Read expando tables, locks and permissions to make them
    // available to the data handlers through the portlet data context

    Element rootElement = portletDataContext.getImportDataRootElement();

    Element portletElement = null;

    try {
        portletElement = rootElement.element("portlet");

        Document portletDocument = SAXReaderUtil
                .read(portletDataContext.getZipEntryAsString(portletElement.attributeValue("path")));

        portletElement = portletDocument.getRootElement();
    } catch (DocumentException de) {
        throw new SystemException("Unable to parse XML document for portlet "
                + portletDataContext.getPortletId() + " during import", de);
    }

    LayoutCache layoutCache = new LayoutCache();

    if (importPermissions) {
        _permissionImporter.checkRoles(layoutCache, portletDataContext.getCompanyId(),
                portletDataContext.getGroupId(), userId, portletElement);

        _permissionImporter.readPortletDataPermissions(portletDataContext);
    }

    String layoutsImportMode = MapUtil.getString(parameterMap, PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE);

    if (!layoutsImportMode.equals(PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

        readExpandoTables(portletDataContext);
    }

    readLocks(portletDataContext);

    Element portletDataElement = portletElement.element("portlet-data");

    Map<String, Boolean> importPortletControlsMap = _exportImportHelper.getImportPortletControlsMap(
            portletDataContext.getCompanyId(), portletDataContext.getPortletId(), parameterMap,
            portletDataElement, manifestSummary);

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

    try {

        // Portlet preferences

        importPortletPreferences(portletDataContext, layout.getCompanyId(), portletDataContext.getGroupId(),
                layout, portletElement, true,
                importPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS),
                importPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_DATA),
                importPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_SETUP),
                importPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_USER_PREFERENCES));

        // Portlet data

        if (importPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_DATA)) {

            if (_log.isDebugEnabled()) {
                _log.debug("Importing portlet data");
            }

            importPortletData(portletDataContext, portletDataElement);
        }
    } finally {
        resetPortletScope(portletDataContext, portletDataContext.getGroupId());
    }

    // Portlet permissions

    if (importPermissions) {
        if (_log.isDebugEnabled()) {
            _log.debug("Importing portlet permissions");
        }

        _permissionImporter.importPortletPermissions(layoutCache, portletDataContext.getCompanyId(),
                portletDataContext.getGroupId(), userId, layout, portletElement,
                portletDataContext.getPortletId());

        if (userId > 0) {
            Indexer<User> indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);

            User user = _userLocalService.fetchUser(userId);

            indexer.reindex(user);
        }
    }

    // Asset links

    if (_log.isDebugEnabled()) {
        _log.debug("Importing asset links");
    }

    importAssetLinks(portletDataContext);

    // Deletion system events

    _deletionSystemEventImporter.importDeletionSystemEvents(portletDataContext);

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

    // Service portlet preferences

    boolean importPortletSetup = importPortletControlsMap.get(PortletDataHandlerKeys.PORTLET_SETUP);

    if (importPortletSetup) {
        try {
            List<Element> serviceElements = rootElement.elements("service");

            for (Element serviceElement : serviceElements) {
                Document serviceDocument = SAXReaderUtil
                        .read(portletDataContext.getZipEntryAsString(serviceElement.attributeValue("path")));

                importServicePortletPreferences(portletDataContext, serviceDocument.getRootElement());
            }
        } catch (DocumentException de) {
            throw new SystemException("Unable to parse XML service information for portlet "
                    + portletDataContext.getPortletId() + " during import", de);
        } catch (PortalException pe) {
            throw new PortletDataException(
                    "Unable to import service preferences for portlet " + portletDataContext.getPortletId(),
                    pe);
        }
    }

    ZipReader zipReader = portletDataContext.getZipReader();

    zipReader.close();
}

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

License:Open Source License

protected void readExpandoTables(PortletDataContext portletDataContext) throws Exception {

    String xml = portletDataContext.getZipEntryAsString(
            ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/expando-tables.xml");

    if (xml == null) {
        return;//from   w ww  .ja  v a 2s.c  o m
    }

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> expandoTableElements = rootElement.elements("expando-table");

    for (Element expandoTableElement : expandoTableElements) {
        String className = expandoTableElement.attributeValue("class-name");

        ExpandoTable expandoTable = null;

        try {
            expandoTable = _expandoTableLocalService.getDefaultTable(portletDataContext.getCompanyId(),
                    className);
        } catch (NoSuchTableException nste) {

            // LPS-52675

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

            expandoTable = _expandoTableLocalService.addDefaultTable(portletDataContext.getCompanyId(),
                    className);
        }

        List<Element> expandoColumnElements = expandoTableElement.elements("expando-column");

        for (Element expandoColumnElement : expandoColumnElements) {
            long columnId = GetterUtil.getLong(expandoColumnElement.attributeValue("column-id"));
            String name = expandoColumnElement.attributeValue("name");
            int type = GetterUtil.getInteger(expandoColumnElement.attributeValue("type"));
            String defaultData = expandoColumnElement.elementText("default-data");
            String typeSettings = expandoColumnElement.elementText("type-settings");

            Serializable defaultDataObject = ExpandoConverterUtil.getAttributeFromString(type, defaultData);

            ExpandoColumn expandoColumn = _expandoColumnLocalService.getColumn(expandoTable.getTableId(), name);

            if (expandoColumn != null) {
                _expandoColumnLocalService.updateColumn(expandoColumn.getColumnId(), name, type,
                        defaultDataObject);
            } else {
                expandoColumn = _expandoColumnLocalService.addColumn(expandoTable.getTableId(), name, type,
                        defaultDataObject);
            }

            _expandoColumnLocalService.updateTypeSettings(expandoColumn.getColumnId(), typeSettings);

            portletDataContext.importPermissions(ExpandoColumn.class, columnId, expandoColumn.getColumnId());
        }
    }
}

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

License:Open Source License

protected void readLocks(PortletDataContext portletDataContext) throws Exception {

    String xml = portletDataContext
            .getZipEntryAsString(ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/locks.xml");

    if (xml == null) {
        return;/*  ww  w .  j av a 2s.c  om*/
    }

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> assetElements = rootElement.elements("asset");

    for (Element assetElement : assetElements) {
        String path = assetElement.attributeValue("path");
        String className = assetElement.attributeValue("class-name");
        String key = assetElement.attributeValue("key");

        Lock lock = (Lock) portletDataContext.getZipEntryAsObject(path);

        if (lock != null) {
            portletDataContext.addLocks(className, key, lock);
        }
    }
}

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

License:Open Source License

public void checkRoles(LayoutCache layoutCache, long companyId, long groupId, long userId,
        Element portletElement) throws Exception {

    Element permissionsElement = portletElement.element("permissions");

    if (permissionsElement == null) {
        return;//from   w  w w  .j  a  v a2  s. c  o  m
    }

    List<Element> roleElements = permissionsElement.elements("role");

    for (Element roleElement : roleElements) {
        checkRole(layoutCache, companyId, groupId, userId, roleElement);
    }
}

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

License:Open Source License

public void readPortletDataPermissions(PortletDataContext portletDataContext) throws Exception {

    String xml = portletDataContext.getZipEntryAsString(
            ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/portlet-data-permissions.xml");

    if (xml == null) {
        return;//from  ww w  .j a v  a2s .co  m
    }

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> portletDataElements = rootElement.elements("portlet-data");

    for (Element portletDataElement : portletDataElements) {
        String resourceName = portletDataElement.attributeValue("resource-name");
        long resourcePK = GetterUtil.getLong(portletDataElement.attributeValue("resource-pk"));

        List<KeyValuePair> permissions = new ArrayList<>();

        List<Element> permissionsElements = portletDataElement.elements("permissions");

        for (Element permissionsElement : permissionsElements) {
            String roleName = permissionsElement.attributeValue("role-name");
            String actions = permissionsElement.attributeValue("actions");

            KeyValuePair permission = new KeyValuePair(roleName, actions);

            permissions.add(permission);
        }

        portletDataContext.addPermissions(resourceName, resourcePK, permissions);
    }
}

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

License:Open Source License

protected List<String> getActions(Element element) {
    List<String> actions = new ArrayList<>();

    List<Element> actionKeyElements = element.elements("action-key");

    for (Element actionKeyElement : actionKeyElements) {
        actions.add(actionKeyElement.getText());
    }// w  w  w  .jav  a2s .  c  om

    return actions;
}

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

License:Open Source License

protected void importPermissions(LayoutCache layoutCache, long companyId, long groupId, long userId,
        Layout layout, String resourceName, String resourcePrimKey, Element permissionsElement)
        throws Exception {

    Map<Long, Set<String>> existingRoleIdsToActionIds = ExportImportPermissionUtil
            .getRoleIdsToActionIds(companyId, resourceName, resourcePrimKey);

    Map<Long, String[]> importedRoleIdsToActionIds = new HashMap<>();

    List<Element> roleElements = permissionsElement.elements("role");

    for (Element roleElement : roleElements) {
        Role role = checkRole(layoutCache, companyId, groupId, userId, roleElement);

        if (role == null) {
            continue;
        }/*from w  ww.j  av a  2 s .com*/

        Group group = GroupLocalServiceUtil.getGroup(groupId);

        if (!group.isLayoutPrototype() && !group.isLayoutSetPrototype() && layout.isPrivateLayout()) {

            String roleName = role.getName();

            if (roleName.equals(RoleConstants.GUEST)) {
                continue;
            }
        }

        List<String> actions = getActions(roleElement);

        importedRoleIdsToActionIds.put(role.getRoleId(), actions.toArray(new String[actions.size()]));
    }

    Map<Long, String[]> roleIdsToActionIds = ExportImportPermissionUtil
            .mergeImportedPermissionsWithExistingPermissions(existingRoleIdsToActionIds,
                    importedRoleIdsToActionIds);

    ExportImportPermissionUtil.updateResourcePermissions(companyId, groupId, resourceName, resourcePrimKey,
            roleIdsToActionIds);
}

From source file:com.liferay.exportimport.test.util.lar.BaseStagedModelDataHandlerTestCase.java

License:Open Source License

protected void validateExport(PortletDataContext portletDataContext, StagedModel stagedModel,
        Map<String, List<StagedModel>> dependentStagedModelsMap) throws Exception {

    Element rootElement = portletDataContext.getExportDataRootElement();

    List<Element> stagedModelGroupElements = new ArrayList<>();

    Class<?> stagedModelClass = getStagedModelClass();

    String stagedModelClassSimpleName = stagedModelClass.getSimpleName();

    stagedModelGroupElements.addAll(rootElement.elements(stagedModelClassSimpleName));

    for (String dependentStagedModelClassSimpleName : dependentStagedModelsMap.keySet()) {

        stagedModelGroupElements.addAll(rootElement.elements(dependentStagedModelClassSimpleName));
    }/* ww w.j  a  v a  2s.c o m*/

    for (Element stagedModelGroupElement : stagedModelGroupElements) {
        String className = stagedModelGroupElement.getName();

        if (className.equals("missing-references")) {
            continue;
        }

        List<StagedModel> dependentStagedModels = dependentStagedModelsMap.get(className);

        if (dependentStagedModels == null) {
            dependentStagedModels = new ArrayList<>();
        } else {
            dependentStagedModels = ListUtil.copy(dependentStagedModels);
        }

        if (className.equals(stagedModelClassSimpleName)) {
            dependentStagedModels.add(stagedModel);
        }

        List<Element> elements = stagedModelGroupElement.elements();

        Assert.assertEquals(elements.toString(), dependentStagedModels.size(), elements.size());

        for (Element element : elements) {
            String path = element.attributeValue("path");

            Assert.assertNotNull(path);

            Iterator<StagedModel> iterator = dependentStagedModels.iterator();

            while (iterator.hasNext()) {
                StagedModel dependentStagedModel = iterator.next();

                String dependentStagedModelPath = ExportImportPathUtil.getModelPath(dependentStagedModel);

                if (path.equals(dependentStagedModelPath)) {
                    iterator.remove();
                }
            }
        }

        Assert.assertTrue("There is more than one element exported with the same path",
                dependentStagedModels.isEmpty());
    }
}

From source file:com.liferay.google.apps.connector.GetNextItems.java

License:Open Source License

public GetNextItems(String url, Element atomFeedElement) throws GoogleAppsException {

    List<Element> atomLinkElements = atomFeedElement.elements(GHelperUtil.getAtomQName("link"));

    for (Element atomLinkElement : atomLinkElements) {
        String rel = atomLinkElement.attributeValue("rel");

        if (rel.equals("next")) {
            String href = atomLinkElement.attributeValue("href");

            if (!href.equals(url)) {
                getNextItems(href);/*www .  ja va  2s.  c  o m*/
            }

            break;
        }
    }
}