
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);