gpt4 book ai didi

liferay - 在 ModelListener 中更新 Web 内容的类别

转载 作者:行者123 更新时间:2023-12-02 10:55:35 24 4
gpt4 key购买 nike

我正在根据用户使用的结构链接一个类别。示例:我的结构是“A”,那么使用此结构创建的任何内容都应具有“A”类别。

我创建了一个在 JournalArticle 模型上扩展 BaseModelListener 的监听器。我重写了 onBeforeUpdate() ,如下所示:

@Override
public void onBeforeUpdate(JournalArticle aModel) throws ModelListenerException {
try {
long[] assetCategoryIds = refactorCategories(anArticle); // this reads the structure key and updates the list of category to the correct one.
String[] assetTagNames = getTags(anArticle);
long[] assetEntryLinkIds = getEntryLinkIds(anArticle);
JournalArticleLocalServiceUtil.updateAsset(anArticle.getUserId(), anArticle, assetCategoryIds, assetTagNames, assetEntryLinkIds);
} catch (PortalException |SystemException ex) {
throw new ModelListenerException("Cannot update the categories for articleId" + anArticle.getArticleId(), ex);
}
}

我使用一组业务规则更正了 categoryIds

以下是两种可能的情况:

  1. 没有分配类别:

    • 在这种情况下,上面的 onBeforeUpdate() 就像一个 super 按钮一样工作,并分配了正确的类别。
  2. 如果用户在调用之前分配了一个类别(可能是错误的):

    • 在这种情况下,会调用 onBeforeUpdate() 实现,并且我能够看到更新的 Assets (使用调试进行验证),但是当整个过程完成后,Web 内容不会反射(reflect)正确的内容类别。

我的理解: 当调用 updateAsset 时,它会获取更改后的类别。但是当 Hibernate 保存内容时,它会保存用户选择的类别。这对我来说似乎有点奇怪。

所以我想检查是否有任何可能的解决方案或API可以使用。

此外,我尝试了 onAfterCreate()onAfterUpdate()onBeforeCreate(),但运气不佳。

我相信我可以 Hook JournalArticleService 但如果可能的话,我希望使用监听器来执行此操作。

根据 Tobias 建议更新了代码:

@Override
public void onBeforeUpdate(JournalArticle aModel) throws ModelListenerException {
ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
try {
long[] assetCategoryIds = refactorCategories(anArticle); // this reads the structure key and updates the list of category to the correct one.
if (serviceContext != null) {
serviceContext.setAssetCategoryIds(assetCategoryIds);
}
} catch (PortalException |SystemException ex) {
throw new ModelListenerException("Cannot update the categories for articleId" + anArticle.getArticleId(), ex);
}
}

最佳答案

我建议创建一个钩子(Hook)插件并使用服务包装器来扩展JournalArticleLocalService。我发现一个服务 Hook 更适合任务。根据 Liferay 认证计划,通过服务 Hook 执行此操作也是首选方式。

一般来说:

  • 我使用模型监听器来更新文章属性,然后再将其保留(例如自定义 URL 标题)。
  • 我使用服务 Hook 来更新相关 Assets 条目(例如类别和标签)。

扩展服务示例:

public class MyJournalArticleLocalService extends JournalArticleLocalServiceWrapper {

@Override
public JournalArticle addArticle(..., ServiceContext serviceContext) throws PortalException, SystemException {

// set the right category in the service context, as the service context
// that the original service will work with can be modified here
serviceContext.setAssetCategoryIds(refactorCategories(...));

// delegate to the original implementation
return super.addArticle(..., serviceContext);
}

@Override
public JournalArticle updateArticle(..., , ServiceContext serviceContext) throws PortalException, SystemException {

// set the right category in the service context, as the service context
// that the original service will work with can be modified here
serviceContext.setAssetCategoryIds(refactorCategories(...));

// delegate to the original implementation
return super.updateArticle(..., serviceContext);
}
}

通过服务 Hook ,您可以在 Liferay 完成持久化文章并创建/更新相关 Assets 条目之前或之后更新类别。

您可以在liferay-hook.xml描述符中注册服务钩子(Hook):

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">

<hook>
<service>
<service-type>com.liferay.portlet.journal.service.JournalArticleLocalService</service-type>
<service-impl>com.test.MyJournalArticleLocalService</service-impl>
</service>
</hook>

关于liferay - 在 ModelListener 中更新 Web 内容的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34198462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com