Liferay7开发文档_3.5.3修改服务层

本文介绍如何为留言簿应用增加修改和删除留言簿的功能,并提供获取留言簿数量的方法。通过具体的代码示例,展示了如何更新及删除留言簿条目,确保数据库的一致性和完整性。

前面章节,在GuestbookLocalServiceImpl写了一个addGuestbook服务方法,但从来没有使用。为了留言簿具备完整功能,还须加上修改和删除留言簿的方法,以及返回留言簿数量。

添加留言服务方法

请记住,使用Service Builder时,您需要在*Impl类中定义您的服务。向*Impl类添加或删除方法,或更改*Impl类中方法的参数后,必须运行Service Builder。Service Builder自动更新受影响的接口及其他代码。

按照以下步骤添加所需的留言簿服务方法:

  1. Go to the guestbook-service project and open GuestbookLocalServiceImpl.java in the com.liferay.docs.guestbook.service.impl package. Add the following method for updating a guestbook:
    public Guestbook updateGuestbook(long userId, long guestbookId,
        String name, ServiceContext serviceContext) throws PortalException,
                    SystemException {
    
            Date now = new Date();
    
            validate(name);
    
            Guestbook guestbook = getGuestbook(guestbookId);
    
            User user = userLocalService.getUser(userId);
    
            guestbook.setUserId(userId);
            guestbook.setUserName(user.getFullName());
            guestbook.setModifiedDate(serviceContext.getModifiedDate(now));
            guestbook.setName(name);
            guestbook.setExpandoBridgeAttributes(serviceContext);
    
            guestbookPersistence.update(guestbook);
    
            return guestbook;
    }
    

    updateGuestbook方法通过ID 检索Guestbook,并替换为用户输入的数据,然后调用持久层以保存数据库。

  2. Next, add the following method for deleting a guestbook:
    public Guestbook deleteGuestbook(long guestbookId,
                    ServiceContext serviceContext) throws PortalException,
                    SystemException {
    
            Guestbook guestbook = getGuestbook(guestbookId);
    
            List<Entry> entries = entryLocalService.getEntries(
                            serviceContext.getScopeGroupId(), guestbookId);
    
            for (Entry entry : entries) {
                    entryLocalService.deleteEntry(entry.getEntryId());
            }
    
            guestbook = deleteGuestbook(guestbook);
    
            return guestbook;
    }
    

    重点考虑如果删除还有条目的留言簿会发生什么情况。如果您刚刚删除了留言簿,留言簿的条目仍然存在于数据库中,会成为孤儿数据。deleteGuestbook在删除该留言簿之前,会调用服务以删除留言簿的条目。

  3. Use [CTRL]+[SHIFT]+O to update your imports, then save GuestbookLocalServiceImpl.java.
  4. In the Gradle Tasks pane on the right side in Liferay IDE, run Service Builder by opening the guestbook-service module and double-clicking buildService.

现在已完成服务层修改,可以开始处理Guestbook Admin portlet自身了。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值