« Volver a 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 archivos adjuntos
45223 Accesos
Promedio (9 Votos)
La valoración media es de 3.66666666666667 estrellas de 5.
Comentarios
Respuestas anidadas Autor Fecha
How do you populate the ServiceContext? Where... Hiran Chaudhuri 30 de agosto de 2011 1:18
Another sample I'd be interested in: - item... Hiran Chaudhuri 30 de agosto de 2011 1:19
This is perfect example of the Trash API. Natalie D 18 de febrero de 2012 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
Publicado el día 30/08/11 1:18.
Another sample I'd be interested in:
- item accessible by two organizations
Publicado el día 30/08/11 1:19.
This is perfect example of the Trash API.
Publicado el día 18/02/12 5:57.