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