001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.servlet.HttpHeaders;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.PortletPreferencesIds;
024    
025    import java.io.Serializable;
026    
027    import java.util.Date;
028    import java.util.LinkedHashMap;
029    import java.util.Map;
030    
031    /**
032     * @author Raymond Augé
033     * @author Brian Wing Shun Chan
034     * @author Jorge Ferrer
035     */
036    public class ServiceContext implements Cloneable, Serializable {
037    
038            public ServiceContext() {
039                    _attributes = new LinkedHashMap<String, Serializable>();
040                    _expandoBridgeAttributes = new LinkedHashMap<String, Serializable>();
041            }
042    
043            @Override
044            public Object clone() {
045                    ServiceContext serviceContext = new ServiceContext();
046    
047                    serviceContext.setAddGroupPermissions(getAddGroupPermissions());
048                    serviceContext.setAddGuestPermissions(getAddGuestPermissions());
049                    serviceContext.setAssetCategoryIds(getAssetCategoryIds());
050                    serviceContext.setAssetLinkEntryIds(getAssetLinkEntryIds());
051                    serviceContext.setAssetTagNames(getAssetTagNames());
052                    serviceContext.setAttributes(getAttributes());
053                    serviceContext.setCommand(getCommand());
054                    serviceContext.setCompanyId(getCompanyId());
055                    serviceContext.setCreateDate(getCreateDate());
056                    serviceContext.setCurrentURL(getCurrentURL());
057                    serviceContext.setExpandoBridgeAttributes(getExpandoBridgeAttributes());
058                    serviceContext.setGroupPermissions(getGroupPermissions());
059                    serviceContext.setGuestPermissions(getGuestPermissions());
060                    serviceContext.setHeaders(getHeaders());
061                    serviceContext.setIndexingEnabled(isIndexingEnabled());
062                    serviceContext.setLanguageId(getLanguageId());
063                    serviceContext.setLayoutFullURL(getLayoutFullURL());
064                    serviceContext.setLayoutURL(getLayoutURL());
065                    serviceContext.setModifiedDate(getModifiedDate());
066                    serviceContext.setPathMain(getPathMain());
067                    serviceContext.setPlid(getPlid());
068                    serviceContext.setPortalURL(getPortalURL());
069                    serviceContext.setPortletPreferencesIds(getPortletPreferencesIds());
070                    serviceContext.setRemoteAddr(getRemoteAddr());
071                    serviceContext.setRemoteHost(getRemoteHost());
072                    serviceContext.setScopeGroupId(getScopeGroupId());
073                    serviceContext.setSignedIn(isSignedIn());
074                    serviceContext.setUserDisplayURL(getUserDisplayURL());
075                    serviceContext.setUserId(getUserId());
076                    serviceContext.setUuid(getUuid());
077                    serviceContext.setWorkflowAction(getWorkflowAction());
078    
079                    return serviceContext;
080            }
081    
082            /**
083             * @deprecated As of 6.1, renamed to {@link #getAddGroupPermissions()}
084             */
085            public boolean getAddCommunityPermissions() {
086                    return getAddGroupPermissions();
087            }
088    
089            public boolean getAddGuestPermissions() {
090                    return _addGuestPermissions;
091            }
092    
093            public boolean getAddGroupPermissions() {
094                    return _addGroupPermissions;
095            }
096    
097            public long[] getAssetCategoryIds() {
098                    return _assetCategoryIds;
099            }
100    
101            public long[] getAssetLinkEntryIds() {
102              return _assetLinkEntryIds;
103            }
104    
105            public String[] getAssetTagNames() {
106                    return _assetTagNames;
107            }
108    
109            public Serializable getAttribute(String name) {
110                    return _attributes.get(name);
111            }
112    
113            public Map<String, Serializable> getAttributes() {
114                    return _attributes;
115            }
116    
117            public String getCommand() {
118                    return _command;
119            }
120    
121            /**
122             * @deprecated As of 6.1, renamed to {@link #getGroupPermissions()}
123             */
124            public String[] getCommunityPermissions() {
125                    return getGroupPermissions();
126            }
127    
128            public long getCompanyId() {
129                    return _companyId;
130            }
131    
132            public Date getCreateDate() {
133                    return _createDate;
134            }
135    
136            public Date getCreateDate(Date defaultCreateDate) {
137                    if (_createDate != null) {
138                            return _createDate;
139                    }
140                    else if (defaultCreateDate != null) {
141                            return defaultCreateDate;
142                    }
143                    else {
144                            return new Date();
145                    }
146            }
147    
148            public String getCurrentURL() {
149                    return _currentURL;
150            }
151    
152            public Map<String, Serializable> getExpandoBridgeAttributes() {
153                    return _expandoBridgeAttributes;
154            }
155    
156            public String[] getGroupPermissions() {
157                    return _groupPermissions;
158            }
159    
160            public long getGuestOrUserId() throws PortalException, SystemException {
161                    long userId = getUserId();
162    
163                    if (userId > 0) {
164                            return userId;
165                    }
166    
167                    long companyId = getCompanyId();
168    
169                    if (companyId > 0) {
170                            return UserLocalServiceUtil.getDefaultUserId(getCompanyId());
171                    }
172    
173                    return 0;
174            }
175    
176            public String[] getGuestPermissions() {
177                    return _guestPermissions;
178            }
179    
180            public Map<String, String> getHeaders() {
181                    return _headers;
182            }
183    
184            public String getLanguageId() {
185                    return _languageId;
186            }
187    
188            public String getLayoutFullURL() {
189                    return _layoutFullURL;
190            }
191    
192            public String getLayoutURL() {
193                    return _layoutURL;
194            }
195    
196            public Date getModifiedDate() {
197                    return _modifiedDate;
198            }
199    
200            public Date getModifiedDate(Date defaultModifiedDate) {
201                    if (_modifiedDate != null) {
202                            return _modifiedDate;
203                    }
204                    else if (defaultModifiedDate != null) {
205                            return defaultModifiedDate;
206                    }
207                    else {
208                            return new Date();
209                    }
210            }
211    
212            public String getPathMain() {
213                    return _pathMain;
214            }
215    
216            public long getPlid() {
217                    return _plid;
218            }
219    
220            public String getPortalURL() {
221                    return _portalURL;
222            }
223    
224            public String getPortletId() {
225                    if (_portletPreferencesIds != null) {
226                            return _portletPreferencesIds.getPortletId();
227                    }
228                    else {
229                            return null;
230                    }
231            }
232    
233            public PortletPreferencesIds getPortletPreferencesIds() {
234                    return _portletPreferencesIds;
235            }
236    
237            public String getRemoteAddr() {
238                    return _remoteAddr;
239            }
240    
241            public String getRemoteHost() {
242                    return _remoteHost;
243            }
244    
245            public long getScopeGroupId() {
246                    return _scopeGroupId;
247            }
248    
249            public String getUserAgent() {
250                    if (_headers == null) {
251                            return null;
252                    }
253    
254                    return _headers.get(HttpHeaders.USER_AGENT);
255            }
256    
257            public String getUserDisplayURL() {
258                    return _userDisplayURL;
259            }
260    
261            public long getUserId() {
262                    return _userId;
263            }
264    
265            public String getUuid() {
266                    String uuid = _uuid;
267    
268                    _uuid = null;
269    
270                    return uuid;
271            }
272    
273            public int getWorkflowAction() {
274                    return _workflowAction;
275            }
276    
277            public boolean isCommandAdd() {
278                    if (Validator.equals(_command, Constants.ADD)) {
279                            return true;
280                    }
281                    else {
282                            return false;
283                    }
284            }
285    
286            public boolean isCommandUpdate() {
287                    if (Validator.equals(_command, Constants.UPDATE)) {
288                            return true;
289                    }
290                    else {
291                            return false;
292                    }
293            }
294    
295            public boolean isIndexingEnabled() {
296                    return _indexingEnabled;
297            }
298    
299            public boolean isSignedIn() {
300                    return _signedIn;
301            }
302    
303            public Serializable removeAttribute(String name) {
304                    return _attributes.remove(name);
305            }
306    
307            /**
308             * @deprecated As of 6.1, renamed to {@link
309             *             #setAddGroupPermissions(boolean)}
310             */
311            public void setAddCommunityPermissions(boolean addCommunityPermissions) {
312                    setAddGroupPermissions(addCommunityPermissions);
313            }
314    
315            public void setAddGroupPermissions(boolean addGroupPermissions) {
316                    _addGroupPermissions = addGroupPermissions;
317            }
318    
319            public void setAddGuestPermissions(boolean addGuestPermissions) {
320                    _addGuestPermissions = addGuestPermissions;
321            }
322    
323            public void setAssetCategoryIds(long[] assetCategoryIds) {
324                    _assetCategoryIds = assetCategoryIds;
325            }
326    
327            public void setAssetLinkEntryIds(long[] _assetLinkEntryIds) {
328              this._assetLinkEntryIds = _assetLinkEntryIds;
329            }
330    
331            public void setAssetTagNames(String[] assetTagNames) {
332                    _assetTagNames = assetTagNames;
333            }
334    
335            public void setAttribute(String name, Serializable value) {
336                    _attributes.put(name, value);
337            }
338    
339            public void setAttributes(Map<String, Serializable> attributes) {
340                    _attributes = attributes;
341            }
342    
343            public void setCommand(String command) {
344                    _command = command;
345            }
346    
347            /**
348             * @deprecated As of 6.1, renamed to {@link #setGroupPermissions(String[])}
349             */
350            public void setCommunityPermissions(String[] communityPermissions) {
351                    setGroupPermissions(communityPermissions);
352            }
353    
354            public void setCompanyId(long companyId) {
355                    _companyId = companyId;
356            }
357    
358            public void setCreateDate(Date createDate) {
359                    _createDate = createDate;
360            }
361    
362            public void setCurrentURL(String currentURL) {
363                    _currentURL = currentURL;
364            }
365    
366            public void setExpandoBridgeAttributes(
367                    Map<String, Serializable> expandoBridgeAttributes) {
368    
369                    _expandoBridgeAttributes = expandoBridgeAttributes;
370            }
371    
372            public void setGroupPermissions(String[] groupPermissions) {
373                    _groupPermissions = groupPermissions;
374            }
375    
376            public void setGuestPermissions(String[] guestPermissions) {
377                    _guestPermissions = guestPermissions;
378            }
379    
380            public void setHeaders(Map<String, String> headers) {
381                    _headers = headers;
382            }
383    
384            public void setIndexingEnabled(boolean indexingEnabled) {
385                    _indexingEnabled = indexingEnabled;
386            }
387    
388            public void setLanguageId(String languageId) {
389                    _languageId = languageId;
390            }
391    
392            public void setLayoutFullURL(String layoutFullURL) {
393                    _layoutFullURL = layoutFullURL;
394            }
395    
396            public void setLayoutURL(String layoutURL) {
397                    _layoutURL = layoutURL;
398            }
399    
400            public void setModifiedDate(Date modifiedDate) {
401                    _modifiedDate = modifiedDate;
402            }
403    
404            public void setPathMain(String pathMain) {
405                    _pathMain = pathMain;
406            }
407    
408            public void setPlid(long plid) {
409                    _plid = plid;
410            }
411    
412            public void setPortalURL(String portalURL) {
413                    _portalURL = portalURL;
414            }
415    
416            public void setPortletPreferencesIds(
417                    PortletPreferencesIds portletPreferencesIds) {
418    
419                    _portletPreferencesIds = portletPreferencesIds;
420            }
421    
422            public void setRemoteAddr(String remoteAddr) {
423                    _remoteAddr = remoteAddr;
424            }
425    
426            public void setRemoteHost(String remoteHost) {
427                    _remoteHost = remoteHost;
428            }
429    
430            public void setScopeGroupId(long scopeGroupId) {
431                    _scopeGroupId = scopeGroupId;
432            }
433    
434            public void setSignedIn(boolean signedIn) {
435                    _signedIn = signedIn;
436            }
437    
438            public void setUserDisplayURL(String userDisplayURL) {
439                    _userDisplayURL = userDisplayURL;
440            }
441    
442            public void setUserId(long userId) {
443                    _userId = userId;
444            }
445    
446            public void setUuid(String uuid) {
447                    _uuid = uuid;
448            }
449    
450            public void setWorkflowAction(int workflowAction) {
451                    _workflowAction = workflowAction;
452            }
453    
454            private boolean _addGroupPermissions;
455            private boolean _addGuestPermissions;
456            private long[] _assetCategoryIds;
457            private long[] _assetLinkEntryIds;
458            private String[] _assetTagNames;
459            private Map<String, Serializable> _attributes;
460            private String _command;
461            private long _companyId;
462            private Date _createDate;
463            private String _currentURL;
464            private Map<String, Serializable> _expandoBridgeAttributes;
465            private String[] _groupPermissions;
466            private String[] _guestPermissions;
467            private Map<String, String> _headers;
468            private boolean _indexingEnabled = true;
469            private String _languageId;
470            private String _layoutFullURL;
471            private String _layoutURL;
472            private Date _modifiedDate;
473            private String _pathMain;
474            private String _portalURL;
475            private PortletPreferencesIds _portletPreferencesIds;
476            private String _remoteAddr;
477            private String _remoteHost;
478            private long _scopeGroupId;
479            private boolean _signedIn;
480            private String _userDisplayURL;
481            private long _plid;
482            private int _workflowAction = WorkflowConstants.ACTION_PUBLISH;
483            private long _userId;
484            private String _uuid;
485    
486    }