Enabling and Accessing Data Scopes

Apps can restrict their data to specific scopes (e.g., Global, Site, Page). Here, you’ll learn how to

For more detailed information about scoping, see Data Scopes.

Enabling Scoping

  1. Scope your app’s entities. In your service layer, your entities must have a companyId attribute of type long to enable scoping by portal instance, and a groupId attribute of type long to enable scoping by Site. Using Service Builder is the simplest way to do this. For instructions on this, see Service Builder Persistence and Business Logic with Service Builder.

  2. To enable scoping in your app, set the property "com.liferay.portlet.scopeable=true" in your portlet class’s @Component annotation. For example, the Web Content Display Portlet’s portlet class sets this component property:

    @Component(
        immediate = true,
        property = {
            ...
            "com.liferay.portlet.scopeable=true",
            ...,
        },
        service = Portlet.class
    )
    public class JournalContentPortlet extends MVCPortlet {
        ...
    }
    

Accessing Your App’s Scope

Users can typically set an app’s scope to a page, a Site, or the entire portal. To handle your app’s data, you must access it in its current scope. Your app’s scope is available in these ways:

  1. Via the scopeGroupId variable injected in JSPs that use the <liferay-theme:defineObjects /> tag. This variable contains your app’s current scope. For example, the Liferay Bookmarks app’s view.jsp uses its scopeGroupId to retrieve the bookmarks and total number of bookmarks in the current scope:

    ...
    total = BookmarksEntryServiceUtil.getGroupEntriesCount(scopeGroupId, groupEntriesUserId);
    
    bookmarksSearchContainer.setTotal(total);
    bookmarksSearchContainer.setResults(BookmarksEntryServiceUtil.getGroupEntries(scopeGroupId, groupEntriesUserId, bookmarksSearchContainer.getStart(), bookmarksSearchContainer.getEnd()));
    ...
    
  2. By calling the getScopeGroupId() method on the request’s ThemeDisplay. This method returns your app’s current scope. For example, the Liferay Blogs app’s EditEntryMVCActionCommand class does this in its subscribe and unsubscribe methods:

    protected void subscribe(ActionRequest actionRequest) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
            WebKeys.THEME_DISPLAY);
    
        _blogsEntryService.subscribe(themeDisplay.getScopeGroupId());
    }
    
    protected void unsubscribe(ActionRequest actionRequest) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
            WebKeys.THEME_DISPLAY);
    
        _blogsEntryService.unsubscribe(themeDisplay.getScopeGroupId());
    }
    

    If you know your app always needs the portal instance ID, use themeDisplay.getCompanyId().

  3. By calling the getScopeGroupId() method on a ServiceContext object. See Understanding Service Context for an example and more details. If you know your app always needs the portal instance ID, use the ServiceContext object’s getCompanyId() method.

Accessing the Site Scope

To access the Site scope regardless of your app’s current scope, use the ThemeDisplay method getSiteGroupId(). For more information on this use case, see Accessing the Site Scope Across Apps.

For example, the Web Content app’s edit_feed.jsp uses the getSiteGroupId() method to get the Site ID, which is required to retrieve Structures:

ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(themeDisplay.getSiteGroupId(), 
    PortalUtil.getClassNameId(JournalArticle.class), ddmStructureKey, true);

Data Scopes

Service Builder

Service Builder Project

Business Logic with Service Builder

« Retrieving PortletsUsing the Message Bus »
Was this article helpful?
0 out of 0 found this helpful