Example usage for com.liferay.portal.kernel.model.adapter ModelAdapterUtil adapt

List of usage examples for com.liferay.portal.kernel.model.adapter ModelAdapterUtil adapt

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.model.adapter ModelAdapterUtil adapt.

Prototype

public static <T, V> V adapt(T adapteeModel, Class<T> adapteeModelClass, Class<V> adaptedModelClass) 

Source Link

Usage

From source file:com.liferay.asset.categories.admin.web.internal.exportimport.staged.model.repository.StagedAssetLinkStagedModelRepository.java

License:Open Source License

@Override
public StagedAssetLink addStagedModel(PortletDataContext portletDataContext, StagedAssetLink stagedAssetLink)
        throws PortalException {

    long userId = portletDataContext.getUserId(stagedAssetLink.getUserUuid());

    AssetEntry assetEntry1 = fetchAssetEntry(portletDataContext.getScopeGroupId(),
            stagedAssetLink.getEntry1Uuid());
    AssetEntry assetEntry2 = fetchAssetEntry(portletDataContext.getScopeGroupId(),
            stagedAssetLink.getEntry2Uuid());

    if ((assetEntry1 == null) || (assetEntry2 == null)) {
        return null;
    }//from  w  ww . j  a  va2s.  c o m

    AssetLink assetLink = _assetLinkLocalService.addLink(userId, assetEntry1.getEntryId(),
            assetEntry2.getEntryId(), stagedAssetLink.getType(), stagedAssetLink.getWeight());

    return ModelAdapterUtil.adapt(assetLink, AssetLink.class, StagedAssetLink.class);
}

From source file:com.liferay.asset.categories.admin.web.internal.exportimport.staged.model.repository.StagedAssetLinkStagedModelRepository.java

License:Open Source License

public StagedAssetLink fetchExistingAssetLink(long groupId, String assetEntry1Uuid, String assetEntry2Uuid)
        throws PortalException {

    AssetEntry assetEntry1 = fetchAssetEntry(groupId, assetEntry1Uuid);
    AssetEntry assetEntry2 = fetchAssetEntry(groupId, assetEntry2Uuid);

    if ((assetEntry1 == null) || (assetEntry2 == null)) {
        return null;
    }//from   w w  w .  j a v a  2s . com

    DynamicQuery dynamicQuery = getAssetLinkDynamicQuery(assetEntry1.getEntryId(), assetEntry2.getEntryId());

    List<AssetLink> assetLinks = _assetLinkLocalService.dynamicQuery(dynamicQuery);

    if (ListUtil.isNotEmpty(assetLinks)) {
        return ModelAdapterUtil.adapt(assetLinks.get(0), AssetLink.class, StagedAssetLink.class);
    }

    return null;
}

From source file:com.liferay.asset.categories.admin.web.internal.exportimport.staged.model.repository.StagedAssetLinkStagedModelRepository.java

License:Open Source License

@Override
public List<StagedAssetLink> fetchStagedModelsByUuidAndCompanyId(String uuid, long companyId) {

    DynamicQuery dynamicQuery = getAssetLinkDynamicQuery(companyId, 0, parseAssetEntry1Uuid(uuid),
            parseAssetEntry2Uuid(uuid));

    dynamicQuery.addOrder(OrderFactoryUtil.desc("linkId"));

    List<AssetLink> assetLinks = _assetLinkLocalService.dynamicQuery(dynamicQuery);

    if (ListUtil.isNotEmpty(assetLinks)) {
        return ModelAdapterUtil.adapt(assetLinks, AssetLink.class, StagedAssetLink.class);
    }//from  w w  w  .j a va2s.  co m

    return Collections.emptyList();
}

From source file:com.liferay.asset.categories.admin.web.internal.exportimport.staged.model.repository.StagedAssetLinkStagedModelRepository.java

License:Open Source License

@Override
public StagedAssetLink saveStagedModel(StagedAssetLink stagedAssetLink) throws PortalException {

    AssetLink assetLink = _assetLinkLocalService.updateAssetLink(stagedAssetLink);

    return ModelAdapterUtil.adapt(assetLink, AssetLink.class, StagedAssetLink.class);
}

From source file:com.liferay.asset.categories.admin.web.internal.exportimport.staged.model.repository.StagedAssetLinkStagedModelRepository.java

License:Open Source License

@Override
public StagedAssetLink updateStagedModel(PortletDataContext portletDataContext, StagedAssetLink stagedAssetLink)
        throws PortalException {

    long userId = portletDataContext.getUserId(stagedAssetLink.getUserUuid());

    AssetLink assetLink = _assetLinkLocalService.updateLink(userId, stagedAssetLink.getEntryId1(),
            stagedAssetLink.getEntryId2(), stagedAssetLink.getType(), stagedAssetLink.getWeight());

    return ModelAdapterUtil.adapt(assetLink, AssetLink.class, StagedAssetLink.class);
}

From source file:com.liferay.asset.lar.test.AssetLinkExportImportTest.java

License:Open Source License

protected void checkAssetLinksInLar(String assetEntryClassUuid) throws Exception {

    AssetEntry assetEntry = AssetEntryLocalServiceUtil.fetchEntry(group.getGroupId(), assetEntryClassUuid);

    Assert.assertNotNull(assetEntry);//from w  w w  . j a  va  2  s  .  c  o m

    List<AssetLink> assetLinks = AssetLinkLocalServiceUtil.getLinks(assetEntry.getEntryId());

    Assert.assertNotNull(assetLinks);
    Assert.assertTrue(assetLinks.size() == 2);

    PortletDataContext portletDataContext = getPortletDataContext();

    for (AssetLink assetLink : assetLinks) {
        StagedAssetLink stagedAssetLink = ModelAdapterUtil.adapt(assetLink, AssetLink.class,
                StagedAssetLink.class);

        stagedAssetLink = (StagedAssetLink) portletDataContext
                .getZipEntryAsObject(ExportImportPathUtil.getModelPath(stagedAssetLink));

        Assert.assertNotNull(stagedAssetLink);
    }
}

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

License:Open Source License

protected File doExport(PortletDataContext portletDataContext) throws Exception {

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

    boolean ignoreLastPublishDate = MapUtil.getBoolean(parameterMap,
            PortletDataHandlerKeys.IGNORE_LAST_PUBLISH_DATE);
    boolean permissions = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PERMISSIONS);

    if (_log.isDebugEnabled()) {
        _log.debug("Export permissions " + permissions);
    }/*  ww  w .j a  v  a2  s .  c om*/

    long companyId = portletDataContext.getCompanyId();

    long defaultUserId = _userLocalService.getDefaultUserId(companyId);

    ServiceContext serviceContext = ServiceContextThreadLocal.popServiceContext();

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

    serviceContext.setCompanyId(companyId);
    serviceContext.setSignedIn(true);

    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
        BackgroundTask backgroundTask = _backgroundTaskLocalService
                .getBackgroundTask(BackgroundTaskThreadLocal.getBackgroundTaskId());

        serviceContext.setUserId(backgroundTask.getUserId());
    } else {
        serviceContext.setUserId(defaultUserId);
    }

    serviceContext.setAttribute("exporting", Boolean.TRUE);

    long layoutSetBranchId = MapUtil.getLong(parameterMap, "layoutSetBranchId");

    serviceContext.setAttribute("layoutSetBranchId", layoutSetBranchId);

    ServiceContextThreadLocal.pushServiceContext(serviceContext);

    if (ignoreLastPublishDate) {
        portletDataContext.setEndDate(null);
        portletDataContext.setStartDate(null);
    }

    StopWatch stopWatch = new StopWatch();

    stopWatch.start();

    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(portletDataContext.getScopeGroupId())));

    headerElement.addAttribute("build-number", String.valueOf(ReleaseInfo.getBuildNumber()));

    headerElement.addAttribute("schema-version", ExportImportConstants.EXPORT_IMPORT_SCHEMA_VERSION);

    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("company-id", String.valueOf(portletDataContext.getCompanyId()));
    headerElement.addAttribute("company-group-id", String.valueOf(portletDataContext.getCompanyGroupId()));
    headerElement.addAttribute("group-id", String.valueOf(portletDataContext.getGroupId()));
    headerElement.addAttribute("user-personal-site-group-id",
            String.valueOf(portletDataContext.getUserPersonalSiteGroupId()));
    headerElement.addAttribute("private-layout", String.valueOf(portletDataContext.isPrivateLayout()));

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

    String type = "layout-set";

    if (group.isLayoutPrototype()) {
        type = "layout-prototype";

        LayoutPrototype layoutPrototype = _layoutPrototypeLocalService.getLayoutPrototype(group.getClassPK());

        headerElement.addAttribute("type-uuid", layoutPrototype.getUuid());
    } else if (group.isLayoutSetPrototype()) {
        type = "layout-set-prototype";

        LayoutSetPrototype layoutSetPrototype = _layoutSetPrototypeLocalService
                .getLayoutSetPrototype(group.getClassPK());

        headerElement.addAttribute("type-uuid", layoutSetPrototype.getUuid());
    }

    headerElement.addAttribute("type", type);
    portletDataContext.setType(type);

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

    portletDataContext.setMissingReferencesElement(missingReferencesElement);

    rootElement.addElement("site-portlets");
    rootElement.addElement("site-services");

    // Export the group

    LayoutSet layoutSet = _layoutSetLocalService.getLayoutSet(portletDataContext.getGroupId(),
            portletDataContext.isPrivateLayout());

    String layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();

    boolean layoutSetPrototypeSettings = MapUtil.getBoolean(portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.LAYOUT_SET_PROTOTYPE_SETTINGS);

    if (!group.isStaged() && Validator.isNotNull(layoutSetPrototypeUuid) && layoutSetPrototypeSettings) {

        LayoutSetPrototype layoutSetPrototype = _layoutSetPrototypeLocalService
                .getLayoutSetPrototypeByUuidAndCompanyId(layoutSetPrototypeUuid, companyId);

        headerElement.addAttribute("layout-set-prototype-uuid", layoutSetPrototypeUuid);

        headerElement.addAttribute("layout-set-prototype-name",
                layoutSetPrototype.getName(LocaleUtil.getDefault()));
    }

    StagedGroup stagedGroup = ModelAdapterUtil.adapt(group, Group.class, StagedGroup.class);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, stagedGroup);

    // Export other models

    _portletExportController.exportAssetLinks(portletDataContext);
    _portletExportController.exportExpandoTables(portletDataContext);
    _portletExportController.exportLocks(portletDataContext);

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

    _deletionSystemEventExporter.exportDeletionSystemEvents(portletDataContext);

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

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

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

    portletDataContext.addZipEntry("/manifest.xml", document.formattedString());

    ZipWriter zipWriter = portletDataContext.getZipWriter();

    return zipWriter.getFile();
}

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

License:Open Source License

protected void exportAssetLinks(PortletDataContext portletDataContext) throws Exception {

    Document document = SAXReaderUtil.createDocument();

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

    Element exportDataRootElement = portletDataContext.getExportDataRootElement();

    try {/*from   w w w.  ja  va 2  s .  c om*/
        portletDataContext.setExportDataRootElement(rootElement);

        ActionableDynamicQuery linkActionableDynamicQuery = _assetLinkLocalService
                .getExportActionbleDynamicQuery(portletDataContext);

        linkActionableDynamicQuery.performActions();

        for (long linkId : portletDataContext.getAssetLinkIds()) {
            AssetLink assetLink = _assetLinkLocalService.getAssetLink(linkId);

            StagedAssetLink stagedAssetLink = ModelAdapterUtil.adapt(assetLink, AssetLink.class,
                    StagedAssetLink.class);

            portletDataContext.addClassedModel(portletDataContext.getExportDataElement(stagedAssetLink),
                    ExportImportPathUtil.getModelPath(stagedAssetLink), stagedAssetLink);
        }
    } finally {
        portletDataContext.setExportDataRootElement(exportDataRootElement);
    }

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

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

License:Open Source License

public void exportTheme(PortletDataContext portletDataContext, LayoutSet layoutSet) throws Exception {

    boolean exportThemeSettings = MapUtil.getBoolean(portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.THEME_REFERENCE);

    if (_log.isDebugEnabled()) {
        _log.debug("Export theme settings " + exportThemeSettings);
    }//from  w ww.  ja v  a 2s .c o m

    if (!exportThemeSettings) {
        return;
    }

    Theme theme = layoutSet.getTheme();

    if (theme == null) {
        if (_log.isWarnEnabled()) {
            _log.warn("Unable to export theme " + layoutSet.getThemeId());
        }

        return;
    }

    StagedTheme stagedTheme = ModelAdapterUtil.adapt(theme, Theme.class, StagedTheme.class);

    if (!portletDataContext.isPerformDirectBinaryImport()) {
        Element layoutSetElement = portletDataContext.getExportDataElement(layoutSet);

        portletDataContext.addReferenceElement(layoutSet, layoutSetElement, stagedTheme,
                PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
    }

    exportThemeSettings(portletDataContext, stagedTheme.getThemeId(), layoutSet.getColorSchemeId(),
            layoutSet.getCss());
}

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

License:Open Source License

public void exportTheme(PortletDataContext portletDataContext, LayoutSetBranch layoutSetBranch)
        throws Exception {

    boolean exportThemeSettings = MapUtil.getBoolean(portletDataContext.getParameterMap(),
            PortletDataHandlerKeys.THEME_REFERENCE);

    if (_log.isDebugEnabled()) {
        _log.debug("Export theme settings " + exportThemeSettings);
    }/* w  w  w. j  a  v  a  2s . c om*/

    if (!exportThemeSettings) {
        return;
    }

    StagedTheme stagedTheme = ModelAdapterUtil.adapt(layoutSetBranch.getTheme(), Theme.class,
            StagedTheme.class);

    if (!portletDataContext.isPerformDirectBinaryImport()) {
        Element layoutSetBranchElement = portletDataContext.getExportDataElement(layoutSetBranch);

        portletDataContext.addReferenceElement(layoutSetBranch, layoutSetBranchElement, stagedTheme,
                PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
    }

    exportThemeSettings(portletDataContext, stagedTheme.getThemeId(), layoutSetBranch.getColorSchemeId(),
            layoutSetBranch.getCss());
}