001    /**
002     * Copyright (c) 2000-2013 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.language.LanguageUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
021    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022    import com.liferay.portal.kernel.servlet.HttpHeaders;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.DateUtil;
025    import com.liferay.portal.kernel.util.JavaConstants;
026    import com.liferay.portal.kernel.util.LocaleUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.util.WebKeys;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.AuditedModel;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.PortletConstants;
033    import com.liferay.portal.model.PortletPreferencesIds;
034    import com.liferay.portal.model.Role;
035    import com.liferay.portal.model.RoleConstants;
036    import com.liferay.portal.security.permission.ResourceActionsUtil;
037    import com.liferay.portal.theme.ThemeDisplay;
038    import com.liferay.portal.util.PortalUtil;
039    
040    import java.io.Serializable;
041    
042    import java.util.ArrayList;
043    import java.util.Date;
044    import java.util.LinkedHashMap;
045    import java.util.List;
046    import java.util.Locale;
047    import java.util.Map;
048    import java.util.TimeZone;
049    
050    import javax.servlet.http.HttpServletRequest;
051    import javax.servlet.http.HttpServletResponse;
052    
053    /**
054     * Contains context information about a given API call.
055     *
056     * <p>
057     * The <code>ServiceContext</code> object simplifies method signatures and
058     * provides a way to consolidate many different methods with different sets of
059     * optional parameters into a single, easier to use method. It also aggregates
060     * information necessary for transversal features such as permissioning,
061     * tagging, categorization, etc.
062     * </p>
063     *
064     * @author Raymond Augé
065     * @author Brian Wing Shun Chan
066     * @author Jorge Ferrer
067     */
068    public class ServiceContext implements Cloneable, Serializable {
069    
070            /**
071             * Creates a new service context object with an attributes map and an
072             * expando bridge attributes map. The attributes map contains standard
073             * service context parameters and the expando bridge attributes map contains
074             * optional service context parameters.
075             */
076            public ServiceContext() {
077                    _attributes = new LinkedHashMap<String, Serializable>();
078                    _expandoBridgeAttributes = new LinkedHashMap<String, Serializable>();
079            }
080    
081            /**
082             * Returns a new service context object identical to this service context
083             * object.
084             *
085             * @return a new service context object
086             */
087            @Override
088            public Object clone() {
089                    ServiceContext serviceContext = new ServiceContext();
090    
091                    serviceContext.setAddGroupPermissions(isAddGroupPermissions());
092                    serviceContext.setAddGuestPermissions(isAddGuestPermissions());
093                    serviceContext.setAssetCategoryIds(getAssetCategoryIds());
094                    serviceContext.setAssetEntryVisible(isAssetEntryVisible());
095                    serviceContext.setAssetLinkEntryIds(getAssetLinkEntryIds());
096                    serviceContext.setAssetTagNames(getAssetTagNames());
097                    serviceContext.setAttributes(getAttributes());
098                    serviceContext.setCommand(getCommand());
099                    serviceContext.setCompanyId(getCompanyId());
100                    serviceContext.setCreateDate(getCreateDate());
101                    serviceContext.setCurrentURL(getCurrentURL());
102                    serviceContext.setExpandoBridgeAttributes(getExpandoBridgeAttributes());
103                    serviceContext.setFailOnPortalException(isFailOnPortalException());
104                    serviceContext.setGroupPermissions(getGroupPermissions());
105                    serviceContext.setGuestPermissions(getGuestPermissions());
106                    serviceContext.setHeaders(getHeaders());
107                    serviceContext.setIndexingEnabled(isIndexingEnabled());
108                    serviceContext.setLanguageId(getLanguageId());
109                    serviceContext.setLayoutFullURL(getLayoutFullURL());
110                    serviceContext.setLayoutURL(getLayoutURL());
111                    serviceContext.setModifiedDate(getModifiedDate());
112                    serviceContext.setPathFriendlyURLPrivateGroup(
113                            getPathFriendlyURLPrivateGroup());
114                    serviceContext.setPathFriendlyURLPrivateUser(
115                            getPathFriendlyURLPrivateUser());
116                    serviceContext.setPathFriendlyURLPublic(getPathFriendlyURLPublic());
117                    serviceContext.setPathMain(getPathMain());
118                    serviceContext.setPlid(getPlid());
119                    serviceContext.setPortalURL(getPortalURL());
120                    serviceContext.setPortletPreferencesIds(getPortletPreferencesIds());
121                    serviceContext.setRemoteAddr(getRemoteAddr());
122                    serviceContext.setRemoteHost(getRemoteHost());
123                    serviceContext.setRequest(getRequest());
124                    serviceContext.setScopeGroupId(getScopeGroupId());
125                    serviceContext.setSignedIn(isSignedIn());
126                    serviceContext.setUserDisplayURL(getUserDisplayURL());
127                    serviceContext.setUserId(getUserId());
128                    serviceContext.setUuid(getUuid());
129                    serviceContext.setWorkflowAction(getWorkflowAction());
130    
131                    return serviceContext;
132            }
133    
134            /**
135             * Derive default permissions based on the logic found in
136             * portal-web/docroot/html/taglib/ui/input_permissions/page.jsp. Do not
137             * update this logic updating the logic in the JSP.
138             */
139            public void deriveDefaultPermissions(long repositoryId, String modelName)
140                    throws PortalException, SystemException {
141    
142                    long siteGroupId = PortalUtil.getSiteGroupId(repositoryId);
143    
144                    Group siteGroup = GroupLocalServiceUtil.getGroup(siteGroupId);
145    
146                    Role defaultGroupRole = RoleLocalServiceUtil.getDefaultGroupRole(
147                            siteGroupId);
148    
149                    List<String> groupPermissions = new ArrayList<String>();
150                    List<String> guestPermissions = new ArrayList<String>();
151    
152                    String[] roleNames = {RoleConstants.GUEST, defaultGroupRole.getName()};
153    
154                    List<String> supportedActions =
155                            ResourceActionsUtil.getModelResourceActions(modelName);
156                    List<String> groupDefaultActions =
157                            ResourceActionsUtil.getModelResourceGroupDefaultActions(modelName);
158                    List<String> guestDefaultActions =
159                            ResourceActionsUtil.getModelResourceGuestDefaultActions(modelName);
160                    List<String> guestUnsupportedActions =
161                            ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
162                                    modelName);
163    
164                    for (String roleName : roleNames) {
165                            for (String action : supportedActions) {
166                                    if (roleName.equals(RoleConstants.GUEST) &&
167                                            !guestUnsupportedActions.contains(action) &&
168                                            guestDefaultActions.contains(action) &&
169                                            siteGroup.hasPublicLayouts()) {
170    
171                                            guestPermissions.add(action);
172                                    }
173                                    else if (roleName.equals(defaultGroupRole.getName()) &&
174                                                     groupDefaultActions.contains(action)) {
175    
176                                            groupPermissions.add(action);
177                                    }
178                            }
179                    }
180    
181                    setGroupPermissions(
182                            groupPermissions.toArray(new String[groupPermissions.size()]));
183                    setGuestPermissions(
184                            guestPermissions.toArray(new String[guestPermissions.size()]));
185            }
186    
187            /**
188             * Returns <code>true</code> if this service context is being passed as a
189             * parameter to a method which manipulates a resource to which default group
190             * permissions apply.
191             *
192             * @return     <code>true</code> if this service context is being passed as
193             *             a parameter to a method which manipulates a resource to which
194             *             default community permissions apply; <code>false</code>
195             *             otherwise
196             * @deprecated As of 6.1, renamed to {@link #isAddGroupPermissions()}
197             */
198            public boolean getAddCommunityPermissions() {
199                    return isAddGroupPermissions();
200            }
201    
202            /**
203             * Returns the asset category IDs to be applied to an asset entry if the
204             * service context is being passed as a parameter to a method which
205             * manipulates the asset entry.
206             *
207             * @return the asset category IDs
208             */
209            public long[] getAssetCategoryIds() {
210                    return _assetCategoryIds;
211            }
212    
213            /**
214             * Returns the primary keys of the asset entries linked to an asset entry if
215             * the service context is being passed as a parameter to a method which
216             * manipulates the asset entry.
217             *
218             * @return the primary keys of the asset entries
219             */
220            public long[] getAssetLinkEntryIds() {
221                    return _assetLinkEntryIds;
222            }
223    
224            /**
225             * Returns the asset tag names to be applied to an asset entry if the
226             * service context is being passed as a parameter to a method which
227             * manipulates the asset entry.
228             *
229             * @return the asset tag names
230             */
231            public String[] getAssetTagNames() {
232                    return _assetTagNames;
233            }
234    
235            /**
236             * Returns the serializable object associated with the name of the standard
237             * parameter of this service context.
238             *
239             * @param  name the name of the standard parameter
240             * @return the serializable object associated with the name
241             */
242            public Serializable getAttribute(String name) {
243                    return _attributes.get(name);
244            }
245    
246            /**
247             * Returns the map of name/value pairs that are the standard parameters of
248             * this service context. Each value is serializable.
249             *
250             * @return the map of name/value pairs
251             */
252            public Map<String, Serializable> getAttributes() {
253                    return _attributes;
254            }
255    
256            /**
257             * Returns the value of the {@link
258             * com.liferay.portal.kernel.util.Constants#CMD} parameter used in most
259             * Liferay forms for internal portlets.
260             *
261             * @return the value of the command parameter
262             */
263            public String getCommand() {
264                    return _command;
265            }
266    
267            /**
268             * Returns the specific community permissions for a resource if the service
269             * context is being passed as a parameter to a method which manipulates the
270             * resource.
271             *
272             * @return     the community permissions
273             * @deprecated As of 6.1, renamed to {@link #getGroupPermissions()}
274             */
275            public String[] getCommunityPermissions() {
276                    return getGroupPermissions();
277            }
278    
279            /**
280             * Returns the company ID of this service context's current portal instance.
281             *
282             * @return the company ID
283             */
284            public long getCompanyId() {
285                    return _companyId;
286            }
287    
288            /**
289             * Returns the date when an entity was created if this service context is
290             * being passed as a parameter to a method which creates an entity.
291             *
292             * @return the creation date
293             */
294            public Date getCreateDate() {
295                    return _createDate;
296            }
297    
298            /**
299             * Returns the date when an entity was created (or a default date) if this
300             * service context is being passed as a parameter to a method which creates
301             * an entity.
302             *
303             * @param  defaultCreateDate an optional default create date to use if the
304             *         service context does not have a create date
305             * @return the creation date if available; the default date otherwise
306             */
307            public Date getCreateDate(Date defaultCreateDate) {
308                    if (_createDate != null) {
309                            return _createDate;
310                    }
311                    else if (defaultCreateDate != null) {
312                            return defaultCreateDate;
313                    }
314                    else {
315                            return new Date();
316                    }
317            }
318    
319            /**
320             * Returns the current URL of this service context
321             *
322             * @return the current URL
323             */
324            public String getCurrentURL() {
325                    return _currentURL;
326            }
327    
328            /**
329             * Returns an arbitrary number of attributes of an entity to be persisted.
330             *
331             * <p>
332             * These attributes only include fields that this service context does not
333             * possess by default.
334             * </p>
335             *
336             * @return the expando bridge attributes
337             */
338            public Map<String, Serializable> getExpandoBridgeAttributes() {
339                    return _expandoBridgeAttributes;
340            }
341    
342            /**
343             * Returns the date when an <code>aui:form</code> was generated in this
344             * service context. The form date can be used in detecting situations in
345             * which an entity has been modified while another client was editing that
346             * entity. </p>
347             *
348             * <p>
349             * Example:
350             * </p>
351             *
352             * <p>
353             * Person1 and person2 start editing the same version of a Web Content
354             * article. Person1 publishes changes to the article first. When person2
355             * attempts to publish changes to that article, the service implementation
356             * finds that a modification to that article has already been published some
357             * time after person2 started editing the article. Since the the article
358             * modification date was found to be later than the form date for person2,
359             * person2 could be alerted to the modification and make a backup copy of
360             * his edits before synchronizing with the published changes by person1.
361             * </p>
362             */
363            public Date getFormDate() {
364                    return _formDate;
365            }
366    
367            /**
368             * Returns the specific group permissions for a resource if this service
369             * context is being passed as a parameter to a method which manipulates the
370             * resource.
371             *
372             * @return the specific group permissions
373             */
374            public String[] getGroupPermissions() {
375                    return _groupPermissions;
376            }
377    
378            /**
379             * Returns this service context's user ID or guest ID if no user ID is
380             * available.
381             *
382             * @return the user ID, or guest ID if there is no user in this service
383             *         context, or <code>0</code> if there is no company in this service
384             *         context
385             * @throws PortalException if a default user for the company could not be
386             *         found
387             * @throws SystemException if a system exception occurred
388             */
389            public long getGuestOrUserId() throws PortalException, SystemException {
390                    long userId = getUserId();
391    
392                    if (userId > 0) {
393                            return userId;
394                    }
395    
396                    long companyId = getCompanyId();
397    
398                    if (companyId > 0) {
399                            return UserLocalServiceUtil.getDefaultUserId(getCompanyId());
400                    }
401    
402                    return 0;
403            }
404    
405            /**
406             * Returns the specific guest permissions for a resource if this service
407             * context is being passed as a parameter to a method which manipulates the
408             * resource.
409             *
410             * @return the specific guest permissions
411             */
412            public String[] getGuestPermissions() {
413                    return _guestPermissions;
414            }
415    
416            /**
417             * Returns the the map of request header name/value pairs of this service
418             * context.
419             *
420             * @return the the map of request header name/value pairs
421             * @see    com.liferay.portal.kernel.servlet.HttpHeaders
422             */
423            public Map<String, String> getHeaders() {
424                    return _headers;
425            }
426    
427            /**
428             * Returns the language ID of the locale of this service context's current
429             * user.
430             *
431             * @return the language ID
432             */
433            public String getLanguageId() {
434                    if (_languageId != null) {
435                            return _languageId;
436                    }
437    
438                    return LocaleUtil.toLanguageId(LocaleUtil.getMostRelevantLocale());
439            }
440    
441            /**
442             * Returns the complete URL of the current page if a page context can be
443             * determined for this service context.
444             *
445             * @return the complete URL of the current page
446             */
447            public String getLayoutFullURL() {
448                    return _layoutFullURL;
449            }
450    
451            /**
452             * Returns the relative URL of the current page if a page context can be
453             * determined for this service context.
454             *
455             * @return the relative URL of the current page
456             */
457            public String getLayoutURL() {
458                    return _layoutURL;
459            }
460    
461            public LiferayPortletRequest getLiferayPortletRequest() {
462                    if (_request == null) {
463                            return null;
464                    }
465    
466                    LiferayPortletRequest liferayPortletRequest =
467                            (LiferayPortletRequest)_request.getAttribute(
468                                    JavaConstants.JAVAX_PORTLET_REQUEST);
469    
470                    return liferayPortletRequest;
471            }
472    
473            public LiferayPortletResponse getLiferayPortletResponse() {
474                    if (_request == null) {
475                            return null;
476                    }
477    
478                    LiferayPortletResponse liferayPortletResponse =
479                            (LiferayPortletResponse)_request.getAttribute(
480                                    JavaConstants.JAVAX_PORTLET_RESPONSE);
481    
482                    return liferayPortletResponse;
483            }
484    
485            public Locale getLocale() {
486                    return LocaleUtil.fromLanguageId(_languageId);
487            }
488    
489            /**
490             * Returns the date when an entity was modified if this service context is
491             * being passed as a parameter to a method which updates an entity.
492             *
493             * @return the date when an entity was modified if this service context is
494             *         being passed as a parameter to a method which updates an entity
495             */
496            public Date getModifiedDate() {
497                    return _modifiedDate;
498            }
499    
500            /**
501             * Returns the date when an entity was modified if this service context is
502             * being passed as a parameter to a method which modifies an entity.
503             *
504             * @param  defaultModifiedDate an optional default modified date to use if
505             *         this service context does not have a modified date
506             * @return the modified date if available; the default date otherwise
507             */
508            public Date getModifiedDate(Date defaultModifiedDate) {
509                    if (_modifiedDate != null) {
510                            return _modifiedDate;
511                    }
512                    else if (defaultModifiedDate != null) {
513                            return defaultModifiedDate;
514                    }
515                    else {
516                            return new Date();
517                    }
518            }
519    
520            public String getPathFriendlyURLPrivateGroup() {
521                    return _pathFriendlyURLPrivateGroup;
522            }
523    
524            public String getPathFriendlyURLPrivateUser() {
525                    return _pathFriendlyURLPrivateUser;
526            }
527    
528            public String getPathFriendlyURLPublic() {
529                    return _pathFriendlyURLPublic;
530            }
531    
532            /**
533             * Returns the main context path of the portal, concatenated with
534             * <code>/c</code>.
535             *
536             * @return the main context path of the portal
537             */
538            public String getPathMain() {
539                    return _pathMain;
540            }
541    
542            /**
543             * Returns the portal layout ID of the current page of this service context.
544             *
545             * @return the portal layout ID of the current page
546             */
547            public long getPlid() {
548                    return _plid;
549            }
550    
551            /**
552             * Returns the URL of this service context's portal, including the protocol,
553             * domain, and non-default port relative to the company instance and any
554             * virtual host.
555             *
556             * <p>
557             * The URL returned does not include the port if a default port is used.
558             * </p>
559             *
560             * @return the URL of this service context's portal, including the protocol,
561             *         domain, and non-default port relative to company instance and any
562             *         virtual host
563             */
564            public String getPortalURL() {
565                    return _portalURL;
566            }
567    
568            /**
569             * Returns the ID of the current portlet if this service context is being
570             * passed as a parameter to a portlet.
571             *
572             * @return the ID of the current portlet
573             * @see    com.liferay.portal.model.PortletPreferencesIds
574             */
575            public String getPortletId() {
576                    if (_portletPreferencesIds == null) {
577                            return null;
578                    }
579    
580                    return _portletPreferencesIds.getPortletId();
581            }
582    
583            /**
584             * Returns the portlet preferences IDs of the current portlet if the service
585             * context is being passed as a parameter to a portlet.
586             *
587             * <p>
588             * The {@link com.liferay.portal.model.PortletPreferencesIds} can be used to
589             * look up portlet preferences of the current portlet.
590             * </p>
591             *
592             * @return the portlet preferences IDs of the current portlet
593             * @see    com.liferay.portal.model.PortletPreferencesIds
594             */
595            public PortletPreferencesIds getPortletPreferencesIds() {
596                    return _portletPreferencesIds;
597            }
598    
599            /**
600             * Returns the remote address of the user making the request in this service
601             * context.
602             *
603             * @return the remote address of the user making the request
604             */
605            public String getRemoteAddr() {
606                    return _remoteAddr;
607            }
608    
609            /**
610             * Returns the remote host name of the user making the request in this
611             * service context.
612             *
613             * @return the remote host name of the user making the request
614             */
615            public String getRemoteHost() {
616                    return _remoteHost;
617            }
618    
619            public HttpServletRequest getRequest() {
620                    return _request;
621            }
622    
623            public HttpServletResponse getResponse() {
624                    LiferayPortletResponse liferayPortletResponse =
625                            getLiferayPortletResponse();
626    
627                    if (liferayPortletResponse == null) {
628                            return null;
629                    }
630    
631                    return PortalUtil.getHttpServletResponse(liferayPortletResponse);
632            }
633    
634            public String getRootPortletId() {
635                    String portletId = getPortletId();
636    
637                    if (portletId == null) {
638                            return null;
639                    }
640    
641                    return PortletConstants.getRootPortletId(portletId);
642            }
643    
644            public Group getScopeGroup() throws PortalException, SystemException {
645                    return GroupLocalServiceUtil.getGroup(_scopeGroupId);
646            }
647    
648            /**
649             * Returns the ID of the group corresponding to the current data scope of
650             * this service context.
651             *
652             * @return the ID of the group corresponding to the current data scope
653             * @see    com.liferay.portal.model.Group
654             */
655            public long getScopeGroupId() {
656                    return _scopeGroupId;
657            }
658    
659            public ThemeDisplay getThemeDisplay() {
660                    if (_request == null) {
661                            return null;
662                    }
663    
664                    return (ThemeDisplay)_request.getAttribute(WebKeys.THEME_DISPLAY);
665            }
666    
667            public TimeZone getTimeZone() {
668                    return _timeZone;
669            }
670    
671            /**
672             * Returns the user-agent request header of this service context.
673             *
674             * @return the user-agent request header
675             * @see    com.liferay.portal.kernel.servlet.HttpHeaders
676             */
677            public String getUserAgent() {
678                    if (_request == null) {
679                            return null;
680                    }
681    
682                    return _request.getHeader(HttpHeaders.USER_AGENT);
683            }
684    
685            /**
686             * Returns the complete URL of this service context's current user's profile
687             * page.
688             *
689             * @return the complete URL of this service context's current user's profile
690             *         page
691             */
692            public String getUserDisplayURL() {
693                    return _userDisplayURL;
694            }
695    
696            /**
697             * Returns the ID of this service context's current user.
698             *
699             * @return the ID of this service context's current user
700             */
701            public long getUserId() {
702                    return _userId;
703            }
704    
705            /**
706             * Returns the UUID of this service context's current entity.
707             *
708             * @return the UUID of this service context's current entity
709             */
710            public String getUuid() {
711                    String uuid = _uuid;
712    
713                    _uuid = null;
714    
715                    return uuid;
716            }
717    
718            /**
719             * Returns the workflow action to take if this service context is being
720             * passed as a parameter to a method that processes a workflow action.
721             *
722             * @return the workflow action to take
723             */
724            public int getWorkflowAction() {
725                    return _workflowAction;
726            }
727    
728            /**
729             * Returns <code>true</code> if this service context is being passed as a
730             * parameter to a method which manipulates a resource to which default group
731             * permissions apply.
732             *
733             * @return <code>true</code> if this service context is being passed as a
734             *         parameter to a method which manipulates a resource to which
735             *         default group permissions apply; <code>false</code> otherwise
736             */
737            public boolean isAddGroupPermissions() {
738                    return _addGroupPermissions;
739            }
740    
741            /**
742             * Returns <code>true</code> if this service context is being passed as a
743             * parameter to a method which manipulates a resource to which default guest
744             * permissions apply.
745             *
746             * @return <code>true</code> if this service context is being passed as a
747             *         parameter to a method which manipulates a resource to which
748             *         default guest permissions apply; <code>false</code> otherwise
749             */
750            public boolean isAddGuestPermissions() {
751                    return _addGuestPermissions;
752            }
753    
754            public boolean isAssetEntryVisible() {
755                    return _assetEntryVisible;
756            }
757    
758            /**
759             * Returns <code>true</code> if this service context contains an add command
760             * (i.e. has command value {@link
761             * com.liferay.portal.kernel.util.Constants#ADD})
762             *
763             * @return <code>true</code> if this service context contains an add
764             *         command; <code>false</code> otherwise
765             */
766            public boolean isCommandAdd() {
767                    if (Validator.equals(_command, Constants.ADD) ||
768                            Validator.equals(_command, Constants.ADD_MULTIPLE)) {
769    
770                            return true;
771                    }
772                    else {
773                            return false;
774                    }
775            }
776    
777            /**
778             * Returns <code>true</code> if this service context contains an update
779             * command (i.e. has command value {@link
780             * com.liferay.portal.kernel.util.Constants#UPDATE})
781             *
782             * @return <code>true</code> if this service context contains an update
783             *         command; <code>false</code> otherwise
784             */
785            public boolean isCommandUpdate() {
786                    if (Validator.equals(_command, Constants.UPDATE)) {
787                            return true;
788                    }
789                    else {
790                            return false;
791                    }
792            }
793    
794            public boolean isDeriveDefaultPermissions() {
795                    return _deriveDefaultPermissions;
796            }
797    
798            /**
799             * Returns <code>true</code> if portal exceptions should be handled as
800             * failures, possibly halting processing, or <code>false</code> if the
801             * exceptions should be handled differently, possibly allowing processing to
802             * continue in some manner. Services may check this flag to execute desired
803             * behavior.
804             *
805             * <p>
806             * Batch invocation of such services (exposed as a JSON web services) can
807             * result in execution of all service invocations, in spite of portal
808             * exceptions.
809             * </p>
810             *
811             * <p>
812             * If this flag is set to <code>false</code>, services can implement logic
813             * that allows processing to continue, while collecting information
814             * regarding the exceptions for returning to the caller. For example, the
815             * {@link
816             * com.liferay.portlet.asset.service.impl.AssetVocabularyServiceImpl#deleteVocabularies(
817             * long[], ServiceContext)} method uses the list it returns to give
818             * information on vocabularies it fails to delete; it returns an empty list
819             * if all deletions are successful.
820             * </p>
821             *
822             * @return <code>true</code> if portal exceptions are to be handled as
823             *         failures; <code>false</code> if portal exceptions can be handled
824             *         differently, possibly allowing processing to continue in some
825             *         manner
826             */
827            public boolean isFailOnPortalException() {
828                    return _failOnPortalException;
829            }
830    
831            /**
832             * Returns whether the primary entity of this service context is to be
833             * indexed/re-indexed.
834             *
835             * @return <code>true</code> the primary entity of this service context is
836             *         to be indexed/re-indexed; <code>false</code> otherwise
837             */
838            public boolean isIndexingEnabled() {
839                    return _indexingEnabled;
840            }
841    
842            /**
843             * Returns <code>true</code> if the sender of this service context's request
844             * is signed in.
845             *
846             * @return <code>true</code> if the sender of this service context's request
847             *         is signed in; <code>false</code> otherwise
848             */
849            public boolean isSignedIn() {
850                    return _signedIn;
851            }
852    
853            public void merge(ServiceContext serviceContext) {
854                    setAddGroupPermissions(serviceContext.isAddGroupPermissions());
855                    setAddGuestPermissions(serviceContext.isAddGuestPermissions());
856    
857                    if (serviceContext.getAssetCategoryIds() != null) {
858                            setAssetCategoryIds(serviceContext.getAssetCategoryIds());
859                    }
860    
861                    if (serviceContext.getAssetLinkEntryIds() != null) {
862                            setAssetLinkEntryIds(serviceContext.getAssetLinkEntryIds());
863                    }
864    
865                    if (serviceContext.getAssetTagNames() != null) {
866                            setAssetTagNames(serviceContext.getAssetTagNames());
867                    }
868    
869                    if (serviceContext.getAttributes() != null) {
870                            setAttributes(serviceContext.getAttributes());
871                    }
872    
873                    if (Validator.isNotNull(serviceContext.getCommand())) {
874                            setCommand(serviceContext.getCommand());
875                    }
876    
877                    if (serviceContext.getCompanyId() > 0) {
878                            setCompanyId(serviceContext.getCompanyId());
879                    }
880    
881                    if (serviceContext.getCreateDate() != null) {
882                            setCreateDate(serviceContext.getCreateDate());
883                    }
884    
885                    if (Validator.isNotNull(serviceContext.getCurrentURL())) {
886                            setCurrentURL(serviceContext.getCurrentURL());
887                    }
888    
889                    if (serviceContext.getExpandoBridgeAttributes() != null) {
890                            setExpandoBridgeAttributes(
891                                    serviceContext.getExpandoBridgeAttributes());
892                    }
893    
894                    if (serviceContext.getGroupPermissions() != null) {
895                            setGroupPermissions(serviceContext.getGroupPermissions());
896                    }
897    
898                    if (serviceContext.getGuestPermissions() != null) {
899                            setGuestPermissions(serviceContext.getGuestPermissions());
900                    }
901    
902                    if (serviceContext.getHeaders() != null) {
903                            setHeaders(serviceContext.getHeaders());
904                    }
905    
906                    setFailOnPortalException(serviceContext.isFailOnPortalException());
907                    setLanguageId(serviceContext.getLanguageId());
908    
909                    if (Validator.isNotNull(serviceContext.getLayoutFullURL())) {
910                            setLayoutFullURL(serviceContext.getLayoutFullURL());
911                    }
912    
913                    if (Validator.isNotNull(serviceContext.getLayoutURL())) {
914                            setLayoutURL(serviceContext.getLayoutURL());
915                    }
916    
917                    if (serviceContext.getModifiedDate() != null) {
918                            setModifiedDate(serviceContext.getModifiedDate());
919                    }
920    
921                    if (Validator.isNotNull(serviceContext.getPathMain())) {
922                            setPathMain(serviceContext.getPathMain());
923                    }
924    
925                    if (serviceContext.getPlid() > 0) {
926                            setPlid(serviceContext.getPlid());
927                    }
928    
929                    if (Validator.isNotNull(serviceContext.getPortalURL())) {
930                            setPortalURL(serviceContext.getPortalURL());
931                    }
932    
933                    if (serviceContext.getPortletPreferencesIds() != null) {
934                            setPortletPreferencesIds(serviceContext.getPortletPreferencesIds());
935                    }
936    
937                    if (Validator.isNotNull(serviceContext.getRemoteAddr())) {
938                            setRemoteAddr(serviceContext.getRemoteAddr());
939                    }
940    
941                    if (Validator.isNotNull(serviceContext.getRemoteHost())) {
942                            setRemoteHost(serviceContext.getRemoteHost());
943                    }
944    
945                    if (serviceContext.getScopeGroupId() > 0) {
946                            setScopeGroupId(serviceContext.getScopeGroupId());
947                    }
948    
949                    setSignedIn(serviceContext.isSignedIn());
950    
951                    if (Validator.isNotNull(serviceContext.getUserDisplayURL())) {
952    
953                            setUserDisplayURL(serviceContext.getUserDisplayURL());
954                    }
955    
956                    if (serviceContext.getUserId() > 0) {
957                            setUserId(serviceContext.getUserId());
958                    }
959    
960                    if (Validator.isNotNull(serviceContext.getUuid())) {
961                            setUuid(serviceContext.getUuid());
962                    }
963    
964                    if (serviceContext.getWorkflowAction() > 0) {
965                            setWorkflowAction(serviceContext.getWorkflowAction());
966                    }
967            }
968    
969            /**
970             * Removes the mapping of the serializable object to the name of the
971             * standard parameter of this service context.
972             *
973             * @param  name the name of the standard parameter
974             * @return the serializable object associated to the name
975             */
976            public Serializable removeAttribute(String name) {
977                    return _attributes.remove(name);
978            }
979    
980            /**
981             * Sets whether or not default community permissions should apply to a
982             * resource being manipulated by a method to which this service context is
983             * passed as a parameter.
984             *
985             * @param      addCommunityPermissions indicates whether or not to apply
986             *             default community permissions
987             * @deprecated As of 6.1, renamed to {@link
988             *             #setAddGroupPermissions(boolean)}
989             */
990            public void setAddCommunityPermissions(boolean addCommunityPermissions) {
991                    setAddGroupPermissions(addCommunityPermissions);
992            }
993    
994            /**
995             * Sets whether or not default group permissions should apply to a resource
996             * being manipulated by a method to which this service context is passed as
997             * a parameter.
998             *
999             * @param addGroupPermissions indicates whether or not to apply default
1000             *        group permissions
1001             */
1002            public void setAddGroupPermissions(boolean addGroupPermissions) {
1003                    _addGroupPermissions = addGroupPermissions;
1004            }
1005    
1006            /**
1007             * Sets whether or not default guest permissions should apply to a resource
1008             * being manipulated by a method to which this service context is passed as
1009             * a parameter.
1010             *
1011             * @param addGuestPermissions indicates whether or not to apply default
1012             *        guest permissions
1013             */
1014            public void setAddGuestPermissions(boolean addGuestPermissions) {
1015                    _addGuestPermissions = addGuestPermissions;
1016            }
1017    
1018            /**
1019             * Sets an array of asset category IDs to be applied to an asset entry if
1020             * this service context is being passed as a parameter to a method which
1021             * manipulates the asset entry.
1022             *
1023             * @param assetCategoryIds the primary keys of the asset categories
1024             */
1025            public void setAssetCategoryIds(long[] assetCategoryIds) {
1026                    _assetCategoryIds = assetCategoryIds;
1027            }
1028    
1029            public void setAssetEntryVisible(boolean assetEntryVisible) {
1030                    _assetEntryVisible = assetEntryVisible;
1031            }
1032    
1033            /**
1034             * Sets an array of the primary keys of asset entries to be linked to an
1035             * asset entry if this service context is being passed as a parameter to a
1036             * method which manipulates the asset entry.
1037             *
1038             * @param assetLinkEntryIds the primary keys of the asset entries to be
1039             *        linked to an asset entry
1040             */
1041            public void setAssetLinkEntryIds(long[] assetLinkEntryIds) {
1042                    _assetLinkEntryIds = assetLinkEntryIds;
1043            }
1044    
1045            /**
1046             * Sets an array of asset tag names to be applied to an asset entry if this
1047             * service context is being passed as a parameter to a method which
1048             * manipulates the asset entry.
1049             *
1050             * @param assetTagNames the tag names to be applied to an asset entry
1051             */
1052            public void setAssetTagNames(String[] assetTagNames) {
1053                    _assetTagNames = assetTagNames;
1054            }
1055    
1056            /**
1057             * Sets a mapping of a standard parameter's name to its serializable object.
1058             *
1059             * @param name the standard parameter name to associate with the value
1060             * @param value the serializable object to be associated with the name
1061             */
1062            public void setAttribute(String name, Serializable value) {
1063                    _attributes.put(name, value);
1064            }
1065    
1066            /**
1067             * Sets the map of the name/value pairs that are the standard parameters of
1068             * this service context. Each value must be serializable.
1069             *
1070             * @param attributes the map of the name/value pairs that are the standard
1071             *        parameters of this service context
1072             */
1073            public void setAttributes(Map<String, Serializable> attributes) {
1074                    _attributes = attributes;
1075            }
1076    
1077            /**
1078             * Sets the value of the {@link
1079             * com.liferay.portal.kernel.util.Constants#CMD} parameter used in most
1080             * Liferay forms for internal portlets.
1081             *
1082             * @param command the value of the {@link
1083             *        com.liferay.portal.kernel.util.Constants#CMD} parameter
1084             */
1085            public void setCommand(String command) {
1086                    _command = command;
1087            }
1088    
1089            /**
1090             * Sets an array containing specific community permissions for a resource if
1091             * this service context is being passed as a parameter to a method which
1092             * manipulates the resource.
1093             *
1094             * @param      communityPermissions the community permissions (optionally
1095             *             <code>null</code>)
1096             * @deprecated As of 6.1, renamed to {@link #setGroupPermissions(String[])}
1097             */
1098            public void setCommunityPermissions(String[] communityPermissions) {
1099                    setGroupPermissions(communityPermissions);
1100            }
1101    
1102            /**
1103             * Sets the company ID of this service context's current portal instance.
1104             *
1105             * @param companyId the primary key of this service context's current portal
1106             *        instance
1107             */
1108            public void setCompanyId(long companyId) {
1109                    _companyId = companyId;
1110            }
1111    
1112            /**
1113             * Sets the date when an entity was created if this service context is being
1114             * passed as a parameter to a method which creates an entity.
1115             *
1116             * @param createDate the date the entity was created
1117             */
1118            public void setCreateDate(Date createDate) {
1119                    _createDate = createDate;
1120            }
1121    
1122            /**
1123             * Sets the current URL of this service context
1124             *
1125             * @param currentURL the current URL of this service context
1126             */
1127            public void setCurrentURL(String currentURL) {
1128                    _currentURL = currentURL;
1129            }
1130    
1131            public void setDeriveDefaultPermissions(boolean deriveDefaultPermissions) {
1132                    _deriveDefaultPermissions = deriveDefaultPermissions;
1133            }
1134    
1135            /**
1136             * Sets an arbitrary number of attributes of an entity to be persisted.
1137             *
1138             * <p>
1139             * These attributes should only include fields that {@link
1140             * com.liferay.portal.service.ServiceContext} does not possess by default.
1141             * </p>
1142             *
1143             * @param expandoBridgeAttributes the expando bridge attributes (optionally
1144             *        <code>null</code>)
1145             */
1146            public void setExpandoBridgeAttributes(
1147                    Map<String, Serializable> expandoBridgeAttributes) {
1148    
1149                    _expandoBridgeAttributes = expandoBridgeAttributes;
1150            }
1151    
1152            /**
1153             * Sets whether portal exceptions should be handled as failures, possibly
1154             * halting processing, or if exceptions should be handled differently,
1155             * possibly allowing processing to continue in some manner.
1156             *
1157             * @param failOnPortalException whether portal exceptions should be handled
1158             *        as failures, or if portal exceptions should be handled
1159             *        differently, possibly allowing processing to continue in some
1160             *        manner
1161             * @see   #isFailOnPortalException()
1162             */
1163            public void setFailOnPortalException(boolean failOnPortalException) {
1164                    _failOnPortalException = failOnPortalException;
1165            }
1166    
1167            /**
1168             * Sets the date when an <code>aui:form</code> was generated in this service
1169             * context. The form date can be used in detecting situations in which an
1170             * entity has been modified while another client was editing that entity.
1171             *
1172             * <p>
1173             * Example:
1174             * </p>
1175             *
1176             * <p>
1177             * Person1 and person2 start editing the same version of a Web Content
1178             * article. Person1 publishes changes to the article first. When person2
1179             * attempts to publish changes to that article, the service implementation
1180             * finds that a modification to that article has already been published some
1181             * time after person2 started editing the article. Since the article
1182             * modification date was found to be later than the form date for person2,
1183             * person2 could be alerted to the modification and make a backup copy of
1184             * his edits before synchronizing with the published changes by person1.
1185             * </p>
1186             *
1187             * @param formDate the date that an <code>aui:form</code> was generated for
1188             *        this service context (optionally <code>null</code>)
1189             */
1190            public void setFormDate(Date formDate) {
1191                    _formDate = formDate;
1192            }
1193    
1194            /**
1195             * Sets an array containing specific group permissions for a resource if
1196             * this service context is being passed as a parameter to a method which
1197             * manipulates the resource.
1198             *
1199             * @param groupPermissions the permissions (optionally <code>null</code>)
1200             */
1201            public void setGroupPermissions(String[] groupPermissions) {
1202                    _groupPermissions = groupPermissions;
1203            }
1204    
1205            /**
1206             * Sets an array containing specific guest permissions for a resource if
1207             * this service context is being passed as a parameter to a method which
1208             * manipulates the resource.
1209             *
1210             * @param guestPermissions the guest permissions (optionally
1211             *        <code>null</code>)
1212             */
1213            public void setGuestPermissions(String[] guestPermissions) {
1214                    _guestPermissions = guestPermissions;
1215            }
1216    
1217            /**
1218             * Sets the map of request header name/value pairs of this service context.
1219             *
1220             * @param headers map of request header name/value pairs of this service
1221             *        context
1222             * @see   com.liferay.portal.kernel.servlet.HttpHeaders
1223             */
1224            public void setHeaders(Map<String, String> headers) {
1225                    _headers = headers;
1226            }
1227    
1228            /**
1229             * Sets whether the primary entity of this service context is to be
1230             * indexed/re-indexed.
1231             *
1232             * <p>
1233             * The entity is only indexed/re-indexed if the method receiving this
1234             * service context as a parameter does indexing.
1235             * </p>
1236             *
1237             * @param indexingEnabled whether the primary entity of this service context
1238             *        is to be indexed/re-indexed (default is <code>true</code>)
1239             */
1240            public void setIndexingEnabled(boolean indexingEnabled) {
1241                    _indexingEnabled = indexingEnabled;
1242            }
1243    
1244            /**
1245             * Sets the language ID of the locale of this service context.
1246             *
1247             * @param languageId the language ID of the locale of this service context's
1248             *        current user
1249             */
1250            public void setLanguageId(String languageId) {
1251                    _languageId = languageId;
1252            }
1253    
1254            /**
1255             * Sets the complete URL of the current page for this service context.
1256             *
1257             * @param layoutFullURL the complete URL of the current page if a page
1258             *        context can be determined for this service context
1259             */
1260            public void setLayoutFullURL(String layoutFullURL) {
1261                    _layoutFullURL = layoutFullURL;
1262            }
1263    
1264            /**
1265             * Sets the relative URL of the current page for this service context.
1266             *
1267             * @param layoutURL the relative URL of the current page if a page context
1268             *        can be determined for this service context
1269             */
1270            public void setLayoutURL(String layoutURL) {
1271                    _layoutURL = layoutURL;
1272            }
1273    
1274            /**
1275             * Sets the date when an entity was modified in this service context.
1276             *
1277             * @param modifiedDate the date when an entity was modified in this service
1278             *        context
1279             */
1280            public void setModifiedDate(Date modifiedDate) {
1281                    _modifiedDate = modifiedDate;
1282            }
1283    
1284            public void setPathFriendlyURLPrivateGroup(
1285                    String pathFriendlyURLPrivateGroup) {
1286    
1287                    _pathFriendlyURLPrivateGroup = pathFriendlyURLPrivateGroup;
1288            }
1289    
1290            public void setPathFriendlyURLPrivateUser(
1291                    String pathFriendlyURLPrivateUser) {
1292    
1293                    _pathFriendlyURLPrivateUser = pathFriendlyURLPrivateUser;
1294            }
1295    
1296            public void setPathFriendlyURLPublic(String pathFriendlyURLPublic) {
1297                    _pathFriendlyURLPublic = pathFriendlyURLPublic;
1298            }
1299    
1300            /**
1301             * Sets the main context path of the portal, concatenated with
1302             * <code>/c</code>.
1303             *
1304             * @param pathMain the main context path of the portal
1305             */
1306            public void setPathMain(String pathMain) {
1307                    _pathMain = pathMain;
1308            }
1309    
1310            /**
1311             * Sets the portal layout ID of the current page in this service context.
1312             *
1313             * @param plid the portal layout ID of the current page
1314             */
1315            public void setPlid(long plid) {
1316                    _plid = plid;
1317            }
1318    
1319            /**
1320             * Sets the URL of this service context's portal, including the protocol,
1321             * domain, and non-default port relative to the company instance and any
1322             * virtual host.
1323             *
1324             * <p>
1325             * The URL should not include the port if a default port is used.
1326             * </p>
1327             *
1328             * @param portalURL the portal URL
1329             */
1330            public void setPortalURL(String portalURL) {
1331                    _portalURL = portalURL;
1332            }
1333    
1334            /**
1335             * Sets the portlet preferences IDs of the current portlet if this service
1336             * context is being passed as a parameter to a portlet.
1337             *
1338             * <p>
1339             * The {@link com.liferay.portal.model.PortletPreferencesIds} can be used to
1340             * look up portlet preferences of the current portlet.
1341             * </p>
1342             *
1343             * @param portletPreferencesIds the portlet preferences
1344             * @see   com.liferay.portal.model.PortletPreferencesIds
1345             */
1346            public void setPortletPreferencesIds(
1347                    PortletPreferencesIds portletPreferencesIds) {
1348    
1349                    _portletPreferencesIds = portletPreferencesIds;
1350            }
1351    
1352            /**
1353             * Sets the remote address of the user making the request in this service
1354             * context.
1355             *
1356             * @param remoteAddr the remote address of the user making the request in
1357             *        this service context
1358             */
1359            public void setRemoteAddr(String remoteAddr) {
1360                    _remoteAddr = remoteAddr;
1361            }
1362    
1363            /**
1364             * Sets the remote host name of the user making the request in this service
1365             * context.
1366             *
1367             * @param remoteHost the remote host name of the user making the request in
1368             *        this service context
1369             */
1370            public void setRemoteHost(String remoteHost) {
1371                    _remoteHost = remoteHost;
1372            }
1373    
1374            /**
1375             * Sets the optional request used when instantiating this service context.
1376             * The field is volatile and so will be discarded on serialization.
1377             *
1378             * @param request the request
1379             */
1380            public void setRequest(HttpServletRequest request) {
1381                    _request = request;
1382            }
1383    
1384            /**
1385             * Sets the ID of the group corresponding to the current data scope of this
1386             * service context.
1387             *
1388             * @param scopeGroupId the ID of the group corresponding to the current data
1389             *        scope of this service context
1390             * @see   com.liferay.portal.model.Group
1391             */
1392            public void setScopeGroupId(long scopeGroupId) {
1393                    _scopeGroupId = scopeGroupId;
1394            }
1395    
1396            /**
1397             * Sets whether the sender of this service context's request is signed in.
1398             *
1399             * @param signedIn whether the sender of this service context's request is
1400             *        signed in
1401             */
1402            public void setSignedIn(boolean signedIn) {
1403                    _signedIn = signedIn;
1404            }
1405    
1406            public void setTimeZone(TimeZone timeZone) {
1407                    _timeZone = timeZone;
1408            }
1409    
1410            /**
1411             * Sets the complete URL of this service context's current user's profile
1412             * page.
1413             *
1414             * @param userDisplayURL the complete URL of the current user's profile page
1415             */
1416            public void setUserDisplayURL(String userDisplayURL) {
1417                    _userDisplayURL = userDisplayURL;
1418            }
1419    
1420            /**
1421             * Sets the ID of this service context's current user.
1422             *
1423             * @param userId the ID of the current user
1424             */
1425            public void setUserId(long userId) {
1426                    _userId = userId;
1427            }
1428    
1429            /**
1430             * Sets the UUID of this service context's current entity.
1431             *
1432             * @param uuid the UUID of the current entity
1433             */
1434            public void setUuid(String uuid) {
1435                    _uuid = uuid;
1436            }
1437    
1438            /**
1439             * Sets the workflow action to take if this service context is being passed
1440             * as parameter to a method that processes a workflow action.
1441             *
1442             * @param workflowAction workflow action to take (default is {@link
1443             *        com.liferay.portal.kernel.workflow.WorkflowConstants#ACTION_PUBLISH})
1444             */
1445            public void setWorkflowAction(int workflowAction) {
1446                    _workflowAction = workflowAction;
1447            }
1448    
1449            public String translate(String pattern, Object... arguments) {
1450                    Locale locale = getLocale();
1451    
1452                    return LanguageUtil.format(locale, pattern, arguments);
1453            }
1454    
1455            public void validateModifiedDate(
1456                            AuditedModel auditedModel, Class<? extends PortalException> clazz)
1457                    throws PortalException {
1458    
1459                    int value = DateUtil.compareTo(
1460                            auditedModel.getModifiedDate(), _formDate);
1461    
1462                    if (value > 0) {
1463                            try {
1464                                    throw clazz.newInstance();
1465                            }
1466                            catch (IllegalAccessException iae) {
1467                                    throw new RuntimeException(iae);
1468                            }
1469                            catch (InstantiationException ie) {
1470                                    throw new RuntimeException(ie);
1471                            }
1472                    }
1473            }
1474    
1475            private boolean _addGroupPermissions;
1476            private boolean _addGuestPermissions;
1477            private long[] _assetCategoryIds;
1478            private boolean _assetEntryVisible = true;
1479            private long[] _assetLinkEntryIds;
1480            private String[] _assetTagNames;
1481            private Map<String, Serializable> _attributes;
1482            private String _command;
1483            private long _companyId;
1484            private Date _createDate;
1485            private String _currentURL;
1486            private boolean _deriveDefaultPermissions;
1487            private Map<String, Serializable> _expandoBridgeAttributes;
1488            private boolean _failOnPortalException = true;
1489            private Date _formDate;
1490            private String[] _groupPermissions;
1491            private String[] _guestPermissions;
1492            private Map<String, String> _headers;
1493            private boolean _indexingEnabled = true;
1494            private String _languageId;
1495            private String _layoutFullURL;
1496            private String _layoutURL;
1497            private Date _modifiedDate;
1498            private String _pathFriendlyURLPrivateGroup;
1499            private String _pathFriendlyURLPrivateUser;
1500            private String _pathFriendlyURLPublic;
1501            private String _pathMain;
1502            private long _plid;
1503            private String _portalURL;
1504            private PortletPreferencesIds _portletPreferencesIds;
1505            private String _remoteAddr;
1506            private String _remoteHost;
1507            private transient HttpServletRequest _request;
1508            private long _scopeGroupId;
1509            private boolean _signedIn;
1510            private TimeZone _timeZone;
1511            private String _userDisplayURL;
1512            private long _userId;
1513            private String _uuid;
1514            private int _workflowAction = WorkflowConstants.ACTION_PUBLISH;
1515    
1516    }