当使用Service Builder时,可以在服务模块中实现服务。由于您的应用程序工程是组件,因此可以从Web模块引用服务层。
You’ll implement services for guestbooks and entries in the guestbook-service
module’s GuestbookLocalServiceImpl
and EntryLocalServiceImpl
, respectively.
请按照以下步骤在以下位置实现留言簿服务GuestbookLocalServiceImpl
:
- 在
com.liferay.docs.guestbook.service.impl
包中,打开GuestbookLocalServiceImpl
。然后添加这个addGuestbook
方法:public Guestbook addGuestbook( long userId, String name, ServiceContext serviceContext) throws PortalException { long groupId = serviceContext.getScopeGroupId(); User user = userLocalService.getUserById(userId); Date now = new Date(); validate(name); long guestbookId = counterLocalService.increment(); Guestbook guestbook = guestbookPersistence.create(guestbookId); guestbook.setUuid(serviceContext.getUuid()); guestbook.setUserId(userId); guestbook.setGroupId(groupId); guestbook.setCompanyId(user.getCompanyId()); guestbook.setUserName(user.getFullName()); guestbook.setCreateDate(serviceContext.getCreateDate(now)); guestbook.setModifiedDate(serviceContext.getModifiedDate(now)); guestbook.setName(name); guestbook.setExpandoBridgeAttributes(serviceContext); guestbookPersistence.update(guestbook); return guestbook; }
此方法将一个留言簿添加到数据库。它从环境中检索元数据(例如当前用户的ID,组ID等)以及用户传递的数据。它验证这些数据并用它来构造一个
Guestbook
对象。然后该方法将对象持久化到数据库并返回对象。您仅需在此实现业务逻辑,因为Service Builder已生成模型以及将模型映射数据库的所有代码。 - 添加获取
Guestbook
对象的方法:public List<Guestbook> getGuestbooks(long groupId) { return guestbookPersistence.findByGroupId(groupId); } public List<Guestbook> getGuestbooks(long groupId, int start, int end, OrderByComparator<Guestbook> obc) { return guestbookPersistence.findByGroupId(groupId, start, end, obc); } public List<Guestbook> getGuestbooks(long groupId, int start, int end) { return guestbookPersistence.findByGroupId(groupId, start, end); } public int getGuestbooksCount(long groupId) { return guestbookPersistence.countByGroupId(groupId); }
调用Service Builder生成的查找器。第一种方法用网站特定的groupId检索留言簿列表。接下来的两个方法按可选顺序获取分页列表。最后的方法提供给定网站的留言簿数量统计。
- 最后,添加留言簿验证器方法:
protected void validate(String name) throws PortalException { if (Validator.isNull(name)) { throw new GuestbookNameException(); } }
此方法使用Liferay Portal
Validator
来验证输入文本。 - 按[CTRL] + [SHIFT] + O组织导入,并在出现提示时选择以下选项:
java.util.Date
com.liferay.portal.kernel.service.ServiceContext
com.liferay.docs.guestbook.model.Entry
com.liferay.portal.kernel.util.Validator
现在您已准备好实现服务EntryLocalServiceImpl
。现在按照以下步骤操作:
- 在
com.liferay.docs.guestbook.service.impl
包中,打开EntryLocalServiceImpl
。添加此addEntry
方法:public Entry addEntry( long userId, long guestbookId, String name, String email, String message, ServiceContext serviceContext) throws PortalException { long groupId = serviceContext.getScopeGroupId(); User user = userLocalService.getUserById(userId); Date now = new Date(); validate(name, email, message); long entryId = counterLocalService.increment(); Entry entry = entryPersistence.create(entryId); entry.setUuid(serviceContext.getUuid()); entry.setUserId(userId); entry.setGroupId(groupId); entry.setCompanyId(user.getCompanyId()); entry.setUserName(user.getFullName()); entry.setCreateDate(serviceContext.getCreateDate(now)); entry.setModifiedDate(serviceContext.getModifiedDate(now)); entry.setExpandoBridgeAttributes(serviceContext); entry.setGuestbookId(guestbookId); entry.setName(name); entry.setEmail(email); entry.setMessage(message); entryPersistence.update(entry); return entry; }
与
addGuestbook
方法一样,addEntry
从当前上下文获取数据以及用户输入的数据,验证数据并创建模型对象。然后该对象被持久化到数据库并返回。 - 添加此
updateEntry
方法:public Entry updateEntry ( long userId, long guestbookId, long entryId, String name, String email, String message, ServiceContext serviceContext) throws PortalException, SystemException { Date now = new Date(); validate(name, email, message); Entry entry = getEntry(entryId); User user = userLocalService.getUserById(userId); entry.setUserId(userId); entry.setUserName(user.getFullName()); entry.setModifiedDate(serviceContext.getModifiedDate(now)); entry.setName(name); entry.setEmail(email); entry.setMessage(message); entry.setExpandoBridgeAttributes(serviceContext); entryPersistence.update(entry); return entry; }
此方法首先检索条目并更新其数据以反映用户提交的内容,包括其修改日期。
- 添加此
deleteEntry
方法:public Entry deleteEntry (long entryId, ServiceContext serviceContext) throws PortalException { Entry entry = getEntry(entryId); entry = deleteEntry(entryId); return entry; }
此方法检索由
entry
定义的对象entryId
,将其从数据库中删除,然后返回已删除的对象。 - 添加获取
Entry
对象的方法:public List<Entry> getEntries(long groupId, long guestbookId) { return entryPersistence.findByG_G(groupId, guestbookId); } public List<Entry> getEntries(long groupId, long guestbookId, int start, int end) throws SystemException { return entryPersistence.findByG_G(groupId, guestbookId, start, end); } public List<Entry> getEntries( long groupId, long guestbookId, int start, int end, OrderByComparator<Entry> obc) { return entryPersistence.findByG_G(groupId, guestbookId, start, end, obc); } public int getEntriesCount(long groupId, long guestbookId) { return entryPersistence.countByG_G(groupId, guestbookId); }
这些方法就像
GuestbookLocalServiceImpl
的getter一样,调用Service Builder生成的查找器。getEntries*
但是,这些方法会从指定的留言簿和站点检索条目。第一种方法获取条目列表。下一个方法得到一个分页列表。第三种方法对分页列表进行排序,最后一种方法以整数形式获取条目总数。 - 添加
validate
方法:protected void validate(String name, String email, String entry) throws PortalException { if (Validator.isNull(name)) { throw new EntryNameException(); } if (!Validator.isEmailAddress(email)) { throw new EntryEmailException(); } if (Validator.isNull(entry)) { throw new EntryMessageException(); } }
此方法确保用户在创建条目时输入了相关数据。
- 按[CTRL] + [SHIFT] + O组织导入,并在出现提示时选择以下选项:
java.util.Date
com.liferay.portal.kernel.service.ServiceContext
com.liferay.docs.guestbook.model.Entry
com.liferay.portal.kernel.util.Validator
干得不错!这些本地服务方法实现了portlet类中引用的服务。
更新生成的类
现在已经实现了服务方法,必须将它们提供给其他应用程序。要做到这一点,buildService
再次运行:
OK!新的后端程序已经生成。