1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.WebKeys;
30  import com.liferay.portal.model.PortletPreferencesIds;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portlet.PortletPreferencesFactoryUtil;
34  import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
35  
36  import java.io.Serializable;
37  
38  import java.util.Enumeration;
39  import java.util.HashMap;
40  import java.util.Map;
41  
42  import javax.portlet.PortletRequest;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  /**
47   * <a href="ServiceContextFactory.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Raymond Augé
51   */
52  public class ServiceContextFactory {
53  
54      public static ServiceContext getInstance(
55              String className, PortletRequest portletRequest)
56          throws PortalException, SystemException {
57  
58          ServiceContext serviceContext = new ServiceContext();
59  
60          // Theme display
61  
62          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
63              WebKeys.THEME_DISPLAY);
64  
65          serviceContext.setCompanyId(themeDisplay.getCompanyId());
66          serviceContext.setLanguageId(themeDisplay.getLanguageId());
67          serviceContext.setLayoutFullURL(
68              PortalUtil.getLayoutFullURL(themeDisplay));
69          serviceContext.setLayoutURL(PortalUtil.getLayoutURL(themeDisplay));
70          serviceContext.setPathMain(PortalUtil.getPathMain());
71          serviceContext.setPlid(themeDisplay.getPlid());
72          serviceContext.setPortalURL(PortalUtil.getPortalURL(portletRequest));
73          serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
74          serviceContext.setUserDisplayURL(
75              themeDisplay.getUser().getDisplayURL(themeDisplay));
76          serviceContext.setUserId(themeDisplay.getUserId());
77  
78          // Attributes
79  
80          Map<String, Serializable> attributes =
81              new HashMap<String, Serializable>();
82  
83          Enumeration<String> enu = portletRequest.getParameterNames();
84  
85          while (enu.hasMoreElements()) {
86              String param = enu.nextElement();
87  
88              String[] values = portletRequest.getParameterValues(param);
89  
90              if ((values != null) && (values.length > 0)) {
91                  if (values.length == 1) {
92                      attributes.put(param, values[0]);
93                  }
94                  else {
95                      attributes.put(param, values);
96                  }
97              }
98          }
99  
100         serviceContext.setAttributes(attributes);
101 
102         // Command
103 
104         String cmd = ParamUtil.getString(portletRequest, Constants.CMD);
105 
106         serviceContext.setCommand(cmd);
107 
108         // Expando
109 
110         Map<String, Serializable> expandoBridgeAttributes =
111             PortalUtil.getExpandoBridgeAttributes(
112                 ExpandoBridgeFactoryUtil.getExpandoBridge(className),
113                 portletRequest);
114 
115         serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);
116 
117         // Permissions
118 
119         boolean addCommunityPermissions = ParamUtil.getBoolean(
120             portletRequest, "addCommunityPermissions");
121         boolean addGuestPermissions = ParamUtil.getBoolean(
122             portletRequest, "addGuestPermissions");
123         String[] communityPermissions = PortalUtil.getCommunityPermissions(
124             portletRequest);
125         String[] guestPermissions = PortalUtil.getGuestPermissions(
126             portletRequest);
127 
128         serviceContext.setAddCommunityPermissions(addCommunityPermissions);
129         serviceContext.setAddGuestPermissions(addGuestPermissions);
130         serviceContext.setCommunityPermissions(communityPermissions);
131         serviceContext.setGuestPermissions(guestPermissions);
132 
133         // Portlet preferences ids
134 
135         HttpServletRequest request = PortalUtil.getHttpServletRequest(
136             portletRequest);
137 
138         String portletId = PortalUtil.getPortletId(portletRequest);
139 
140         PortletPreferencesIds portletPreferencesIds =
141             PortletPreferencesFactoryUtil.getPortletPreferencesIds(
142                 request, portletId);
143 
144         serviceContext.setPortletPreferencesIds(portletPreferencesIds);
145 
146         // Tags
147 
148         String[] tagsCategories = PortalUtil.getTagsCategories(portletRequest);
149         String[] tagsEntries = PortalUtil.getTagsEntries(portletRequest);
150 
151         serviceContext.setTagsCategories(tagsCategories);
152 
153         serviceContext.setTagsEntries(tagsEntries);
154 
155         return serviceContext;
156     }
157 
158 }