如何在Java中将类别设置为Liferay Web内容?

时间:2017-06-01 11:30:14

标签: liferay liferay-7

在Liferay 7中,我有一个Web内容,一个词汇和一个类别 如何将类别设置为Web内容?

我写了这段代码:

article = JournalArticleLocalServiceUtil.addArticle(...);
category = AssetCategoryLocalServiceUtil.addCategory(...);

AssetCategoryLocalServiceUtil.setAssetEntryAssetCategories(
    article.getPrimaryKey(), new long[]{category.getPrimaryKey()});

执行时没有错误,但该类别未显示在创建的Web内容的编辑页面上:

enter image description here

该类别已成功创建,但Web内容未分配该类别。

我做错了什么?

我还尝试了addAssetEntryAssetCategoriesaddAssetEntryAssetCategoryaddAssetCategoryAssetEntry:同样的问题。

2 个答案:

答案 0 :(得分:2)

尝试使用这两个功能中的任何一个来添加类别:

addAssetEntryAssetCategory(long entryId, long categoryId);
addAssetEntryAssetCategories(long entryId, long[] categoryIds);

在您的代码中,您使用的是primary_key,但是,根据文档,您应该使用条目ID和类别ID。所以你的函数调用应该是这样的:

AssetEntry entry = AssetEntryLocalServiceUtil.fetchEntry(JournalArticle.class.getName(),  article.getResourcePrimKey());

AssetCategoryLocalServiceUtil.addAssetEntryAssetCategory(
    entry.getEntryId(), category.getCategoryId());

自7.0以来,他们从JournalArticle中删除了getEntryId方法,您需要额外调用才能获取它。有一种update方法,您也可以考虑在单次调用中执行此操作。我仍在使用6.2并追赶7:)。

请注意,类别仅供管理员使用,而不是普通用户。

答案 1 :(得分:0)

我使用的是 liferay 7.1 dxp

就我而言,我必须使用程序更新期刊文章或网络内容的类别。 为了实现这一点,我必须使用 assetEntryAssetCategoryRel 类。 为了首先访问这个和相关的类,我在 build.gradle 文件中添加了依赖

compileOnly 组:“com.liferay”,名称:“com.liferay.asset.entry.rel.api”,版本:“1.1.0”

List<AssetEntryAssetCategoryRel> assetEntryAssetCategoryRelsByAssetEntryId = AssetEntryAssetCategoryRelLocalServiceUtil.
                    getAssetEntryAssetCategoryRelsByAssetEntryId(assetEntry.getEntryId());
if(assetEntryAssetCategoryRelsByAssetEntryId!=null && !assetEntryAssetCategoryRelsByAssetEntryId.isEmpty()){
                    AssetEntryAssetCategoryRel assetEntryAssetCategoryRel = assetEntryAssetCategoryRelsByAssetEntryId.get(0);
                    assetEntryAssetCategoryRel.setAssetCategoryId(assetCategory.getCategoryId());
                    assetEntryAssetCategoryRel = AssetEntryAssetCategoryRelLocalServiceUtil.updateAssetEntryAssetCategoryRel(assetEntryAssetCategoryRel);
                    
                }

我有 assetentry 和 assetcategory 对象 这对我来说很好