Message Boards

listener model create/update/delete liferay 7.1

Alberto Imaginanet, modified 3 Years ago.

listener model create/update/delete liferay 7.1

New Member Posts: 12 Join Date: 2/14/20 Recent Posts
Hi everyone!!!I need to create a module with a listener that runs when news create/ edit / delete. This listener will do the following:
- Remove featured category from all news
- Put the featured category to the 5 most recent. Has anyone done something like this? I need some advice how to do it.

Thank you!
thumbnail
David H Nebinger, modified 3 Years ago.

RE: listener model create/update/delete liferay 7.1

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
I don't know that I'd do this inside of a model listener. I mean, conceivably it should work, but typically I look at a model listener to make a change to the current entity or perhaps mirror some or all of the current entity out to some other table or entity.

But I wouldn't look at it as the point to retrieve and update other entities of the same type. Because as you update those, well the model listener is going to get triggered on those, and pretty soon you're left trying to decide if you should apply the update or not within the context of a model listener and things get really messy.

For what you want to do, I'd actually use a service wrapper. Here it's kind of expected that you're going to be doing more work on other entities, for example, but you also won't face the same kind of re-invoke when you persist the changes to the other entities.
Alberto Imaginanet, modified 3 Years ago.

RE: listener model create/update/delete liferay 7.1

New Member Posts: 12 Join Date: 2/14/20 Recent Posts
Can you explain how to use a service wrapper for this case?
thumbnail
David H Nebinger, modified 3 Years ago.

RE: listener model create/update/delete liferay 7.1

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
You use the service wrapper to override the add, update and delete methods.

In each one you do your thing, i.e. for add/update you find the 5 w/ the "new" category, find the one to remove it and use the injected original reference to save the update. You then modify the current incoming one, either on way in or if it is easier, on way out to add the "new" category.

For the delete, it's about the same except you're trying to find an older one to re-apply the "new" category to.

https://help.liferay.com/hc/en-us/articles/360029144671-Overriding-Liferay-Services-Service-Wrappers-
Alberto Imaginanet, modified 3 Years ago.

RE: listener model create/update/delete liferay 7.1

New Member Posts: 12 Join Date: 2/14/20 Recent Posts
Do you know how to change categories en articles? I only found these classes
JournalArticleLocalServiceUtil
AssetCategoryLocalServiceUtil
But can not find method for change categories in articles.