1. 程式人生 > 實用技巧 >Liferay7 BPM門戶開發之20: 理解Asset Framework

Liferay7 BPM門戶開發之20: 理解Asset Framework

參考:https://www.cnblogs.com/starcrm/p/6046522.html

Liferay7 BPM門戶開發之20: 理解Asset Framework

Asset框架用於將您開發的門戶內容新增Liferay的核心繫統功能。
打個比方,你開發了一個事件TodoList管理的外掛,在列表顯示的時候,你可以整合Asset框架,讓你的自定義內容支援Tag標籤、分類、評論、星標等功能。它可以關聯任意的門戶內容,文字、Int、Image、documents、blog entries, bookmarks,或者任何您自己定義的內容。

這帶來門戶的一致關聯性,非常有益。

Asset框架主要有下列功能:

  • 1.關聯Tag標籤到自定義的內容型別,可以建立或關聯已存在的Tag。
  • 2.關聯類別到自定義的內容型別。作者只能在預定義的詞彙中的類別中進行選擇。
  • 3.在控制面板中管理標籤。管理員可以合併Tag。
  • 4.在控制面板中管理類別,管理員管理類別層級。
  • 5.將評論關聯到內容。
  • 6.通過0到5顆星的評級功能來對內容進行評級。
  • 7.分配連結到內容的社交標籤。
  • 8.對asset增加自定義欄位。
  • 9.將一個asset與另一個asset設定為相關聯(即相關文章 或類似的內容,有點像淘寶裡的相似商品)。
  • 10.標記不妥的文章。
  • 11.追蹤內容的瀏覽次數。
  • 12.和工作流整合。
  • 13.通過資源釋出器(Asset Publisher)portlet來發布和管理內容。

為自定義內容注入Asset關聯 (新增、修改、刪除)

首先要在工程的service.xml檔案增加一行
<reference package-path="com.liferay.portlet.asset" entity="AssetEntry" />
然後執行Service Builder。

比如修改一條AssetEntry,一般呼叫方法是assetEntryLocalService的updateEntry方法:

AssetEntry updateEntry(
long userId, long groupId, Date createDate, Date modifiedDate,
String className, long classPK, String classUuid, long classTypeId,
long[] categoryIds, String[] tagNames, boolean visible,
Date startDate, Date endDate, Date expirationDate, String mimeType,
String title, String description, String summary, String url,
String layoutUuid, int height, int width, Integer priority,
boolean sync)
throws PortalException, SystemException


如果不用Service Builder體系也可以用AssetLocalServiceUtil的靜態方法。

引數的介紹:

  • userId: 使用者ID
獲取userId可以通過2種方式:
long userId = PortalUtil.getUserId(request)
long userId = serviceContext.getUserId();
  • groupId: 內容所處的範圍維度,如果不支援界限範圍,那麼直接傳0.
獲取groupId的2種方式:
long groupId = serviceContext.getScopeGroupId();
long groupId = PortalUtil.getScopeGroupId(renderRequest)
  • createDate: 建立日期
  • modifiedDate: 修改日期
  • className: 實體型別名稱,比如[YourClassName].class.getName().
  • classPK: 實體例項主鍵
  • classUuid: 用於關聯跨域的實體例項,方便與內容的匯入匯出需求,想像一下,如果用了int自增,就沒法同步內容了
  • classTypeId: 一般是0
  • categoryIds: 分類IDs
  • assetTagNames: 標籤名稱s,注意這是個陣列
獲取categoryIds和assetTagNames方式:
ServiceContext serviceContext = ServiceContextFactory.getInstance(
actionRequest);
long[] assetCategoryIds = serviceContext.getAssetCategoryIds();
String[] assetTagNames = serviceContext.getAssetTagNames();
  • visible: 是否可見
  • startDate: Asset Publisher顯示內容的日期,一般是null
  • endDate: Asset Publisher停止顯示內容的日期,一般是null
  • expirationDate: 過期時間,過期即不顯示該實體內容,一般是null
  • mimetype: 比如 ContentTypes.TEXT_HTML, 用於實體的展示格式
  • title: 標題
  • description:
  • summary: 短介紹
  • url: 實體關聯的URL,一般是null
  • layoutUuid: 佈局ID,一般是null
  • height: 可以是0
  • width: 可以是0
  • priority: 優先順序,一般是null
  • sync: 設定同步開關,一般false

一個例子更能簡單說明問題:
比如在insult例項呼叫updateEntry方法. 在add-XXX方法中呼叫updateEntry在實體新增後,或者在update-XXX方法中。
注意2點:

  • Insult是實體類
  • insult是Insult的一個例項
long classTypeId = 0;
boolean visible = true;
Date startDate = null;
Date endDate = null;
Date expirationDate = null;
String mimeType = ContentTypes.TEXT_HTML;
String title = insult.getInsultString();
String description = insult.getInsultString();
String summary = insult.getInsultString();
String url = null;
String layoutUuid = null;
int height = 0;
int width = 0;
Integer priority = null;
boolean sync = false;

assetEntryLocalService.updateEntry(
userId, groupId, insult.getCreateDate(),
insult.getModifiedDate(), Insult.class.getName(),
insult.getInsultId(), insult.getUuid(), classTypeId,
serviceContext.getAssetCategoryIds(),
serviceContext.getAssetTagNames(), visible, startDate, endDate,
expirationDate, mimeType, title, description, summary, url,
layoutUuid, height, width, priority, sync);

Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(Insult.class);
indexer.reindex(insult);


在刪除實體時,應同時刪除相關的asset和索引。

assetEntryLocalService.deleteEntry(
Insult.class.getName(), insult.getInsultId());

Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(Insult.class);
indexer.delete(insult);

在JSP頁面中顯示型別和標籤Tag選擇器

比如一個portlet中的 edit_XXXentry.jsp:
新增:

<liferay-ui:asset-categories-error />
<liferay-ui:asset-tags-error />
...
<aui:fieldset-group markupView="lexicon">
...
<aui:fieldset collapsed="<%= true %>" collapsible="<%= true %>" label="categorization">
<aui:input name="categories" type="assetCategories" />
<aui:input name="tags" type="assetTags" />
</aui:fieldset>
...
</aui:fieldset-group>

用於展示:

<p><liferay-ui:message key="categories" />:</p>

<div class="entry-categories">
<liferay-ui:asset-categories-summary
className="<%= BlogsEntry.class.getName() %>"
classPK="<%= entry.getEntryId() %>"
portletURL="<%= renderResponse.createRenderURL() %>"
/>
</div>
...
<div class="entry-tags">
<p><liferay-ui:message key="tags" />:</p>
<liferay-ui:asset-tags-summary
className="<%= BlogsEntry.class.getName() %>"
classPK="<%= entry.getEntryId() %>"
portletURL="<%= renderResponse.createRenderURL() %>"
/>
</div>

在JSP頁面中顯示通用評論

<%
long insultId = ParamUtil.getLong(renderRequest, "insultId");
Insult ins = InsultLocalServiceUtil.getInsult(insultId);
%>

<liferay-ui:panel-container extended="<%=false%>"
id="insultCommentsPanelContainer" persistState="<%=true%>">

<liferay-ui:panel collapsible="<%=true%>" extended="<%=true%>"
id="insultCommentsPanel" persistState="<%=true%>"
title='<%=LanguageUtil.get(pageContext, "comments")%>'>

<portlet:actionURL name="invokeTaglibDiscussion" var="discussionURL" />

<%
String currentUrl = PortalUtil.getCurrentURL(request);
%>

<liferay-ui:discussion className="<%=Insult.class.getName()%>"
classPK="<%=ins.getInsultId()%>"
formAction="<%=discussionURL%>" formName="fm2"
ratingsEnabled="<%=true%>" redirect="<%=currentUrl%>"
subject="<%=ins.getInsultString()%>"
userId="<%=ins.getUserId()%>" />

</liferay-ui:panel>
</liferay-ui:panel-container>

到這裡,應該可以清楚的感覺到使用Asset框架的好處:只有用好它,才能繼承Liferay的核心關聯資產。