Thursday, August 21, 2014

Asset category in custom portlet in Liferay

Whenever one of your custom content is created you need to let the asset framework know. Don't worry, it is simple. You just need to invoke a method of the asset framework. When invoking this method you will also let the framework know about the tags and/or categories of the content that was just authored.

This is the code you can put it to any jsp page for implementing category popup.

// vManag is the object which is refering to custom portlet named VideoManagement.

In JSP

  <tr><td><b>Categories</b></td>  
 <%  
 String categoryId = null,categoryIds = "";  
 if(Validator.isNotNull(vManag)) {
 if(Validator.isNull(vManag.getCategoryIds())){  
   categoryId = null; // while adding record  
 }else{  
   categoryId = vManag.getCategoryIds(); // get all catgs for that record  
 }  
 if(Validator.isNotNull(categoryId)){  
   String id[] = categoryId.split(",");  
   for(int i=0;i<id.length;i++)  
   {  
    if(String.valueOf(id[i]).equals("") || String.valueOf(id[i]).equals(null)){  
     //return;  
    }else{  
       List<AssetCategory> assetCategory = AssetCategoryLocalServiceUtil.getCategories();  
   for(AssetCategory aCat : assetCategory){  
     if(id[i].equals(String.valueOf(aCat.getCategoryId()))){  
       AssetCategory asset  =(AssetCategory) AssetCategoryLocalServiceUtil .getCategory(aCat.getCategoryId());  
   if(Validator.isNotNull(asset.getCategoryId())){  
      categoryIds= categoryIds.concat(String.valueOf(asset.getCategoryId()));  
     if(i<id.length-1)  
      categoryIds= categoryIds.concat(",");   
      // append categoryId with comma except last  
     }  
    }  
   }  
  }  
  }  
 }  
 } %>  

 <td><liferay-ui:asset-categories-selector curCategoryIds='<%= Validator.isNull(vManag) ? "" : categoryIds %>'/></td></tr>  


The tag <liferay-ui:asset-categories-selector> uses AssetCategoryServiceUtil.getCategories(className, classPK) to get the list of categories and populates the curCategoryIds from this list.

All the methods that you will need to invoke are part of the AssetEntryLocalService. In particular you should access these methods using either the static methods of AssetLocalServiceUtil or by using an instance of the AssetEntryLocalService injected by Spring.

First time you are adding record in custom table so you have to select cateogry from popup.It will show all categories in the category popup by specifying empty string in curCategoryIds and At the time of editing record you have to get categoryIds [like 64664,64613] from custom table that is already stored after adding record and then assign categoryIds to curCategoryIds in asset-categories-selector tag.

So you can see the category selected and if 'select' button is clicked, the same category is shown as checked and that will be useful when you editing any record of custom-table to show default selected values in that popup.

When user select any category from that popup. It will automatically create a hidden field with name assetCategoryIds in the same page.

 <input type="hidden" value="64664,64613" name="_videosmanagement_WAR_videosmanagementportlet_assetCategoryIds" id="_videosmanagement_WAR_videosmanagementportlet_assetCategoryIds" class="aui-field-input aui-field-input-hidden">  

In Java class

So here you can get the values of selected categoryIds like 64664,64613[two category selected in popup]

you can make entry in custom table for adding/editing record and also in assetEntry table for that same recordId.

This is the logic for making entry in assetEntry table for that recordId.

 com.liferay.portal.service.ServiceContext serviceContext = null;  
 String categoryIds = ParamUtil.getString(actionRequest,"assetCategoryIds");  
 try {  
 serviceContext = ServiceContextFactory.getInstance( VideoManagement.class.getName(), actionRequest);  
 } catch (PortalException e) {  
 e.printStackTrace();  
 } catch (SystemException e) {  
 e.printStackTrace();  
 }  
 AssetEntry assetEntry = null;  
 try {  
 assetEntry = AssetEntryLocalServiceUtil.updateEntry(  
 userId,  
 groupId,  
 VideoManagement.class.getName(),  
 vManag.getVideoId(),  
 serviceContext.getUuid(),  
 "".equals(StringUtil.trim(categoryIds)) ? null: categoryIds, null, true, new Date(),null, release_date, null, "text/html", video_title,description, summary, url, 0, 0, null, false);  
 } catch (Exception e) {  
 LOGGER.debug("Error while Converting StringArray to LongArray ! :"  
 + e.getMessage());  
 // e.printStackTrace();  
 }  

userId :- is the identifier of the user who created the content.

VideoManagement.class.getName() :- which is the class name refering to custom portlet named VideoManagement.

vManag.getVideoId() :- represent as classPK which is unique recordId for that content[specify Id of that record which is already added/updated].

assetCategoryIds :- represent the categories that have been selected by the author of the content.

Url :- which specify the url of content i.e for videomanagement portlet you can specify videoURL.

Hope this would be helpful for those who need to develop asset category functionality in custom portlet

Refer more,
https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/asset-framewo-4

No comments:

Post a Comment

Popular Posts