001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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                    return _headers.get(HttpHeaders.USER_AGENT);
251            }
252    
253            public String getUserDisplayURL() {
254                    return _userDisplayURL;
255            }
256    
257            public long getUserId() {
258                    return _userId;
259            }
260    
261            public String getUuid() {
262                    String uuid = _uuid;
263    
264                    _uuid = null;
265    
266                    return uuid;
267            }
268    
269            public int getWorkflowAction() {
270                    return _workflowAction;
271            }
272    
273            public boolean isCommandAdd() {
274                    if (Validator.equals(_command, Constants.ADD)) {
275                            return true;
276                    }
277                    else {
278                            return false;
279                    }
280            }
281    
282            public boolean isCommandUpdate() {
283                    if (Validator.equals(_command, Constants.UPDATE)) {
284                            return true;
285                    }
286                    else {
287                            return false;
288                    }
289            }
290    
291            public boolean isIndexingEnabled() {
292                    return _indexingEnabled;
293            }
294    
295            public boolean isSignedIn() {
296                    return _signedIn;
297            }
298    
299            public Serializable removeAttribute(String name) {
300                    return _attributes.remove(name);
301            }
302    
303            /**
304             * @deprecated As of 6.1, renamed to {@link
305             *             #setAddGroupPermissions(boolean)}
306             */
307            public void setAddCommunityPermissions(boolean addCommunityPermissions) {
308                    setAddGroupPermissions(addCommunityPermissions);
309            }
310    
311            public void setAddGroupPermissions(boolean addGroupPermissions) {
312                    _addGroupPermissions = addGroupPermissions;
313            }
314    
315            public void setAddGuestPermissions(boolean addGuestPermissions) {
316                    _addGuestPermissions = addGuestPermissions;
317            }
318    
319            public void setAssetCategoryIds(long[] assetCategoryIds) {
320                    _assetCategoryIds = assetCategoryIds;
321            }
322    
323            public void setAssetLinkEntryIds(long[] _assetLinkEntryIds) {
324              this._assetLinkEntryIds = _assetLinkEntryIds;
325            }
326    
327            public void setAssetTagNames(String[] assetTagNames) {
328                    _assetTagNames = assetTagNames;
329            }
330    
331            public void setAttribute(String name, Serializable value) {
332                    _attributes.put(name, value);
333            }
334    
335            public void setAttributes(Map<String, Serializable> attributes) {
336                    _attributes = attributes;
337            }
338    
339            public void setCommand(String command) {
340                    _command = command;
341            }
342    
343            /**
344             * @deprecated As of 6.1, renamed to {@link #setGroupPermissions(String[])}
345             */
346            public void setCommunityPermissions(String[] communityPermissions) {
347                    setGroupPermissions(communityPermissions);
348            }
349    
350            public void setCompanyId(long companyId) {
351                    _companyId = companyId;
352            }
353    
354            public void setCreateDate(Date createDate) {
355                    _createDate = createDate;
356            }
357    
358            public void setCurrentURL(String currentURL) {
359                    _currentURL = currentURL;
360            }
361    
362            public void setExpandoBridgeAttributes(
363                    Map<String, Serializable> expandoBridgeAttributes) {
364    
365                    _expandoBridgeAttributes = expandoBridgeAttributes;
366            }
367    
368            public void setGroupPermissions(String[] groupPermissions) {
369                    _groupPermissions = groupPermissions;
370            }
371    
372            public void setGuestPermissions(String[] guestPermissions) {
373                    _guestPermissions = guestPermissions;
374            }
375    
376            public void setHeaders(Map<String, String> headers) {
377                    _headers = headers;
378            }
379    
380            public void setIndexingEnabled(boolean indexingEnabled) {
381                    _indexingEnabled = indexingEnabled;
382            }
383    
384            public void setLanguageId(String languageId) {
385                    _languageId = languageId;
386            }
387    
388            public void setLayoutFullURL(String layoutFullURL) {
389                    _layoutFullURL = layoutFullURL;
390            }
391    
392            public void setLayoutURL(String layoutURL) {
393                    _layoutURL = layoutURL;
394            }
395    
396            public void setModifiedDate(Date modifiedDate) {
397                    _modifiedDate = modifiedDate;
398            }
399    
400            public void setPathMain(String pathMain) {
401                    _pathMain = pathMain;
402            }
403    
404            public void setPlid(long plid) {
405                    _plid = plid;
406            }
407    
408            public void setPortalURL(String portalURL) {
409                    _portalURL = portalURL;
410            }
411    
412            public void setPortletPreferencesIds(
413                    PortletPreferencesIds portletPreferencesIds) {
414    
415                    _portletPreferencesIds = portletPreferencesIds;
416            }
417    
418            public void setRemoteAddr(String remoteAddr) {
419                    _remoteAddr = remoteAddr;
420            }
421    
422            public void setRemoteHost(String remoteHost) {
423                    _remoteHost = remoteHost;
424            }
425    
426            public void setScopeGroupId(long scopeGroupId) {
427                    _scopeGroupId = scopeGroupId;
428            }
429    
430            public void setSignedIn(boolean signedIn) {
431                    _signedIn = signedIn;
432            }
433    
434            public void setUserDisplayURL(String userDisplayURL) {
435                    _userDisplayURL = userDisplayURL;
436            }
437    
438            public void setUserId(long userId) {
439                    _userId = userId;
440            }
441    
442            public void setUuid(String uuid) {
443                    _uuid = uuid;
444            }
445    
446            public void setWorkflowAction(int workflowAction) {
447                    _workflowAction = workflowAction;
448            }
449    
450            private boolean _addGroupPermissions;
451            private boolean _addGuestPermissions;
452            private long[] _assetCategoryIds;
453            private long[] _assetLinkEntryIds;
454            private String[] _assetTagNames;
455            private Map<String, Serializable> _attributes;
456            private String _command;
457            private long _companyId;
458            private Date _createDate;
459            private String _currentURL;
460            private Map<String, Serializable> _expandoBridgeAttributes;
461            private String[] _groupPermissions;
462            private String[] _guestPermissions;
463            private Map<String, String> _headers;
464            private boolean _indexingEnabled = true;
465            private String _languageId;
466            private String _layoutFullURL;
467            private String _layoutURL;
468            private Date _modifiedDate;
469            private String _pathMain;
470            private String _portalURL;
471            private PortletPreferencesIds _portletPreferencesIds;
472            private String _remoteAddr;
473            private String _remoteHost;
474            private long _scopeGroupId;
475            private boolean _signedIn;
476            private String _userDisplayURL;
477            private long _plid;
478            private int _workflowAction = WorkflowConstants.ACTION_PUBLISH;
479            private long _userId;
480            private String _uuid;
481    
482    }