« Back to Development

ServiceContext Pattern

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

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);
0 Attachments
Average (9 Votes)
The average rating is 3.66666666666667 stars out of 5.
Threaded Replies Author Date
How do you populate the ServiceContext? Where... Hiran Chaudhuri August 30, 2011 1:18 AM
Another sample I'd be interested in: - item... Hiran Chaudhuri August 30, 2011 1:19 AM
This is perfect example of the Trash API. Natalie D February 18, 2012 5:57 AM

How do you populate the ServiceContext?

Where do you get values for communityPermissions, guestPermissions?
What exactly is the scopeGroupId?
Could someone add samples for
- item accessible by anyone
- item accessible by community
- item accessible by organization
- item accessible by user only
- item accessible by a specified group of people
Posted on 8/30/11 1:18 AM.
Another sample I'd be interested in:
- item accessible by two organizations
Posted on 8/30/11 1:19 AM.
This is perfect example of the Trash API.
Posted on 2/18/12 5:57 AM.