如何以编程方式插入 Liferay 博客条目

How to insert Liferay blogs entry programmatically

我使用 Liferay 6.0.6,我需要从我的自定义 Portlet 或预定作业中以编程方式插入博客条目。

我找到了 class BlogsEntryLocalServiceUtil 但不知道如何使用它。

谢谢。

这是解决方案:

Company company = CompanyLocalServiceUtil.getCompanyById(MY_COMPANY_ID);
Role adminRole = RoleLocalServiceUtil.getRole(company.getCompanyId(),"Administrator");
List<User> adminUsers = UserLocalServiceUtil.getRoleUsers(adminRole.getRoleId());

 //GET USER ADMIN
 User adminUser = null;
 for (User user : adminUsers){
     if (user.getEmailAddress().equals(MY_ADMIN_EMAIL)){
         adminUser = user;
         break;
     }
 }
 if (adminUser!=null) {
     long userID = adminUser.getUserId();
     long groudID = adminUser.getGroupIds()[0];

     PrincipalThreadLocal.setName(userID);
     PermissionChecker permissionChecker=PermissionCheckerFactoryUtil.create(adminUser, true);                                     

     PermissionThreadLocal.setPermissionChecker(permissionChecker);

     Calendar displayDate =CalendarFactoryUtil.getCalendar(TimeZone.getTimeZone(StringPool.UTC));

     boolean allowPingbacks = true;
     boolean allowTrackbacks = true;

     int actionPublish = WorkflowConstants.ACTION_PUBLISH;

     ServiceContext serviceContext = new ServiceContext();
     serviceContext.setWorkflowAction(actionPublish);
     serviceContext.setAssetCategoryIds(new long[]{category});
     serviceContext.setCreateDate(displayDate.getTime());

     serviceContext.getExpandoBridgeAttributes().put("key1", value1);
     serviceContext.getExpandoBridgeAttributes().put("key2", value2);

     serviceContext.setAddGuestPermissions(true);
     serviceContext.setScopeGroupId(groudID);

     //INSERT BLOGS ENTRY
     BlogsEntryLocalServiceUtil.addEntry(userID, title, content, displayDate.get(Calendar.MONTH), displayDate.get(Calendar.DAY_OF_MONTH), displayDate.get(Calendar.YEAR), displayDate.get(Calendar.HOUR), displayDate.get(Calendar.MINUTE), allowPingbacks, allowTrackbacks, new String[]{}, serviceContext);
 }

您提到的 class 中有可用的 addEntry 方法,即 BlogsEntryLocalServiceUtil,只需传递所需的参数,您就可以创建博客条目。

but don't know how to use it.

为此,您可以尝试查看 liferay 源代码的 portal-impl 文件夹并找到所有可用的 java 源代码以了解如何实现它们。