« 返回到 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();
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);
0 附件
45229 查看
平均 (9 票)
满分为 5,平均得分为 3.66666666666667。
评论
讨论主题回复 作者 日期
How do you populate the ServiceContext? Where... Hiran Chaudhuri 2011年8月30日 上午1:18
Another sample I'd be interested in: - item... Hiran Chaudhuri 2011年8月30日 上午1:19
This is perfect example of the Trash API. Natalie D 2012年2月18日 上午5:57

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
在 11-8-30 上午1:18 发帖。
Another sample I'd be interested in:
- item accessible by two organizations
在 11-8-30 上午1:19 发帖。
This is perfect example of the Trash API.
在 12-2-18 上午5:57 发帖。