Liferay7开发文档_3.3.3实现服务方法

当使用Service Builder时,可以在服务模块中实现服务。由于您的应用程序工程是组件,因此可以从Web模块引用服务层。

You’ll implement services for guestbooks and entries in the guestbook-service module’s GuestbookLocalServiceImpl and EntryLocalServiceImpl, respectively.

请按照以下步骤在以下位置实现留言簿服务GuestbookLocalServiceImpl

  1. 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已生成模型以及将模型映射数据库的所有代码。

  2. 添加获取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检索留言簿列表。接下来的两个方法按可选顺序获取分页列表。最后的方法提供给定网站的留言簿数量统计。

  3. 最后,添加留言簿验证器方法:
    protected void validate(String name) throws PortalException {
        if (Validator.isNull(name)) {
            throw new GuestbookNameException();
        }
    }
    

    此方法使用Liferay Portal Validator来验证输入文本。

  4. 按[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。现在按照以下步骤操作:

  1. 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从当前上下文获取数据以及用户输入的数据,验证数据并创建模型对象。然后该对象被持久化到数据库并返回。

  2. 添加此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;
    }
    

    此方法首先检索条目并更新其数据以反映用户提交的内容,包括其修改日期。

  3. 添加此deleteEntry方法:
    public Entry deleteEntry (long entryId, ServiceContext serviceContext)
        throws PortalException {
    
        Entry entry = getEntry(entryId);
    
        entry = deleteEntry(entryId);
    
        return entry;
    }
    

    此方法检索由entry定义的对象entryId,将其从数据库中删除,然后返回已删除的对象。

  4. 添加获取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*但是,这些方法会从指定的留言簿和站点检索条目。第一种方法获取条目列表。下一个方法得到一个分页列表。第三种方法对分页列表进行排序,最后一种方法以整数形式获取条目总数。

  5. 添加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();
        }
    }
    

    此方法确保用户在创建条目时输入了相关数据。

  6. 按[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!新的后端程序已经生成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值