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!新的後端程序已經生成。

發佈了43 篇原創文章 · 獲贊 1 · 訪問量 8523
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章