- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在根据用户使用的结构链接一个类别。示例:我的结构是“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
。
以下是两种可能的情况:
没有分配类别:
onBeforeUpdate()
就像一个 super 按钮一样工作,并分配了正确的类别。如果用户在调用之前分配了一个类别(可能是错误的):
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 执行此操作也是首选方式。
一般来说:
扩展服务示例:
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/
我正在根据用户使用的结构链接一个类别。示例:我的结构是“A”,那么使用此结构创建的任何内容都应具有“A”类别。 我创建了一个在 JournalArticle 模型上扩展 BaseModelListen
我是一名优秀的程序员,十分优秀!