This wiki does not contain official documentation and is currently deprecated and read only. Please try reading the documentation on the Liferay Developer Network, the new site dedicated to Liferay documentation. DISCOVER Build your web site, collaborate with your colleagues, manage your content, and more. DEVELOP Build applications that run inside Liferay, extend the features provided out of the box with Liferay's APIs. DISTRIBUTE Let the world know about your app by publishing it in Liferay's marketplace. PARTICIPATE Become a part of Liferay's community, meet other Liferay users, and get involved in the open source project. ServiceContext Pattern
Table of Contents [-]
Introduction #
The Service Context is an object that contains context information about a given API call. By using this pattern it is possible to consolidate many different methods with different sets of optional parameters into a single, easier to use method. It also aggregates information necessary for transversal features such as permissioning, tagging, categorization, etc.
Fields #
Here are the most significant fields of the ServiceContext object as of v5.2:
boolean addCommunityPermissions; boolean addGuestPermissions; Map<String, Serializable> attributes; String[] communityPermissions; long companyId; Map<String, Serializable> expandoBridgeAttributes; String[] guestPermissions; String languageId; String layoutURL; String pathMain; String portalURL; PortletPreferencesIds portletPreferencesIds; long scopeGroupId; String[] tagsCategories; String[] tagsEntries; String userDisplayURL; long plid; long userId;
Usage #
All of the fields of this object are optional, although the services that store any type of content will require you to specify at least the scopeGroupId. Here is a simple example of how to create a ServiceContext instance and pass it to a service API:
ServiceContext serviceContext = new ServiceContext(); serviceContext.setScopeGroupId(myGroupId); BlogsEntryServiceUtil.addEntry(...., serviceContext);
If you are invoking the service from a servlet, an struts action or any other frontend class which has access to the portletRequest, you can use a utility method that will create the ServiceContext object and fill it with all the necessary values automatically. In that case the above example could be rewritten as follows:
ServiceContext serviceContext = ServiceContextFactory.getInstance(BlogsEntry.class.getName(), portletRequest); BlogsEntryServiceUtil.addEntry(..., serviceContext);