简体   繁体   English

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

[英]How to set a category to a Liferay Web Content in Java?

In Liferay 7, I have a Web Content, a vocabulary and a category.在 Liferay 7 中,我有一个 Web 内容、一个词汇表和一个类别。
How to set the category to the Web Content?如何将类别设置为 Web 内容?

I wrote this code:我写了这段代码:

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

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

At execution no error, but the category does not show up on the edit page of the created Web Content:执行时没有错误,但该类别没有显示在创建的 Web 内容的编辑页面上:

在此处输入图片说明

The category has been created successfully, but the Web Content does not get that category assigned.类别已成功创建,但 Web 内容未分配该类别。

What am I doing wrong?我究竟做错了什么?

I have also tried addAssetEntryAssetCategories , addAssetEntryAssetCategory , addAssetCategoryAssetEntry : same problem.我也试过addAssetEntryAssetCategoriesaddAssetEntryAssetCategoryaddAssetCategoryAssetEntry :同样的问题。

Try using any of these 2 functions to add category:尝试使用以下 2 个函数中的任何一个来添加类别:

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

In your code, you are using primary_key, however, as per documentation you should be using entry id and category id.在您的代码中,您使用的是primary_key,但是,根据文档,您应该使用条目ID 和类别ID。 So your function call should look like this:所以你的函数调用应该是这样的:

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

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

Since 7.0, they removed the getEntryId method from JournalArticle you would need an additional call to fetch it.从 7.0 开始,他们从 JournalArticle 中删除了 getEntryId 方法,您需要额外调用才能获取它。 There is a update method which you may also consider that would do this in single call.有一种update方法,您也可以考虑在单次调用中完成此操作。 I'm still using 6.2 and catching up 7 :).我仍在使用 6.2 并赶上 7 :)。

Please note categories are designed for use by administrators, not regular users.请注意,类别是为管理员而非普通用户设计的。

I am using liferay 7.1 dxp我正在使用 liferay 7.1 dxp

In my case I have to update category of journal article or web content using program.就我而言,我必须使用程序更新期刊文章或网络内容的类别。 In order to achieve this I have to use assetEntryAssetCategoryRel class.为了实现这一点,我必须使用 assetEntryAssetCategoryRel 类。 to access this and related class first I added dependency to my build.gradle file为了首先访问这个和相关的类,我在 build.gradle 文件中添加了依赖项

compileOnly group: "com.liferay", name: "com.liferay.asset.entry.rel.api", version: "1.1.0" 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);
                    
                }

I had assetentry and assetcategory object This works fine for me我有 assetentry 和 assetcategory 对象这对我来说很好用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM