Enabling Assets at the Service Layer
Step 2 of 3
In this section, you’ll update the guestbook service layer to use assets. You
must update the add, update, and delete methods of your project’s
GuestbookLocalServiceImpl
. Follow these steps to do so:
-
Open your project’s
GuestbookLocalServiceImpl
class and find theaddGuestbook
method. Add the call to add the asset entries below the call that adds resources:AssetEntry assetEntry = assetEntryLocalService.updateEntry(userId, groupId, guestbook.getCreateDate(), guestbook.getModifiedDate(), Guestbook.class.getName(), guestbookId, guestbook.getUuid(), 0, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(), true, true, null, null, null, null, ContentTypes.TEXT_HTML, guestbook.getName(), null, null, null, null, 0, 0, null); assetLinkLocalService.updateLinks(userId, assetEntry.getEntryId(), serviceContext.getAssetLinkEntryIds(), AssetLinkConstants.TYPE_RELATED);
Calling
assetEntryLocalService.updateEntry
adds a new row (corresponding to the guestbook that’s being added) to theAssetEntry
table in Liferay DXP’s database.AssetEntryLocalServiceImpl
’supdateEntry
method both adds and updates asset entries because it checks to see whether the asset entry already exists in the database and then takes the appropriate action. If you check the Javadoc for Liferay DXP’sAssetEntryLocalServiceUtil.updateEntry
, you’ll see that this method is overloaded. Now, why did you use a version of this method with such a long method signature? Because there’s only one version ofupdateEntry
that takes atitle
parameter (to set the asset entry’s title). Since you want to set the asset title toguestbook.getName()
, that’s the version you use.Later, you’ll update the Guestbook Admin portlet’s form for adding guestbooks to allow the selection of related assets, which are stored in the database’s
AssetLink
table. TheassetLinkLocalService.updateLinks
call adds the appropriate entries to the table so related assets work for your guestbook entities. TheupdateEntry
method adds and updates asset entries the same wayupdateLink
adds and updates asset links. -
Next, add the asset calls to
GuestbookLocalServiceImpl
’supdateGuestbook
method, directly after the resource call:AssetEntry assetEntry = assetEntryLocalService.updateEntry(guestbook.getUserId(), guestbook.getGroupId(), guestbook.getCreateDate(), guestbook.getModifiedDate(), Guestbook.class.getName(), guestbookId, guestbook.getUuid(), 0, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(), true, true, guestbook.getCreateDate(), null, null, null, ContentTypes.TEXT_HTML, guestbook.getName(), null, null, null, null, 0, 0, serviceContext.getAssetPriority()); assetLinkLocalService.updateLinks(serviceContext.getUserId(), assetEntry.getEntryId(), serviceContext.getAssetLinkEntryIds(), AssetLinkConstants.TYPE_RELATED);
Here,
assetEntryLocalService.updateEntry
updates an existing asset entry andassetLinkLocalService.updateLinks
adds or updates that entry’s asset links (related assets). -
Next, add the asset calls to the
deleteGuestbook
method, directly after the resource calls:AssetEntry assetEntry = assetEntryLocalService.fetchEntry( Guestbook.class.getName(), guestbookId); assetLinkLocalService.deleteLinks(assetEntry.getEntryId()); assetEntryLocalService.deleteEntry(assetEntry);
Here, you use the guestbook’s class name and ID to retrieve the corresponding asset entry. Then you delete that asset entry’s asset links and the asset entry itself.
-
Finally, organize your imports, save the file, and run Service Builder to apply the changes.
Next, you’ll do the same thing for guestbook entries.