Handling Assets at the Guestbook Service Layer

Enabling Assets at the Service Layer

Step 2 of 3

Before you can update the Service Layer to add the Asset Renderers, you must update your build.gradle to provide the portlet-api and javax.servlet-api libraries that the asset link service needs to function.

  1. Open the build.gradle file in your guestbook-service module.

  2. Add the following two lines in the dependencies section:

    compileOnly group: "javax.portlet", name: "portlet-api", version: "3.0.0"
    compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
    

Now you’ll update the guestbook service layer to use assets. You must update the add, update, and delete methods of your project’s GuestbookLocalServiceImpl:

  1. Open your project’s GuestbookLocalServiceImpl class and find the addGuestbook 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 the AssetEntry table in Liferay DXP’s database. AssetEntryLocalServiceImpl’s updateEntry 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 AssetEntryLocalServiceUtil.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 of updateEntry that takes a title parameter (to set the asset entry’s title). Since you want to set the asset title to guestbook.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. The assetLinkLocalService.updateLinks call adds the appropriate entries to the table so related assets work for your guestbook entities. The updateEntry method adds and updates asset entries the same way updateLink adds and updates asset links.

  2. Next, add the asset calls to GuestbookLocalServiceImpl’s updateGuestbook 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 and assetLinkLocalService.updateLinks adds or updates that entry’s asset links (related assets).

  3. 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.

  4. 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.

« Enabling Assets at the Service LayerHandling Assets for Entry Service Layer »
Was this article helpful?
0 out of 0 found this helpful