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 @JSON(include = false) 426 public Map<String, String> getHeaders() { 427 return _headers; 428 } 429 430 /** 431 * Returns the language ID of the locale of this service context's current 432 * user. 433 * 434 * @return the language ID 435 */ 436 public String getLanguageId() { 437 if (_languageId != null) { 438 return _languageId; 439 } 440 441 return LocaleUtil.toLanguageId(LocaleUtil.getMostRelevantLocale()); 442 } 443 444 /** 445 * Returns the complete URL of the current page if a page context can be 446 * determined for this service context. 447 * 448 * @return the complete URL of the current page 449 */ 450 public String getLayoutFullURL() { 451 return _layoutFullURL; 452 } 453 454 /** 455 * Returns the relative URL of the current page if a page context can be 456 * determined for this service context. 457 * 458 * @return the relative URL of the current page 459 */ 460 public String getLayoutURL() { 461 return _layoutURL; 462 } 463 464 @JSON(include = false) 465 public LiferayPortletRequest getLiferayPortletRequest() { 466 if (_request == null) { 467 return null; 468 } 469 470 LiferayPortletRequest liferayPortletRequest = 471 (LiferayPortletRequest)_request.getAttribute( 472 JavaConstants.JAVAX_PORTLET_REQUEST); 473 474 return liferayPortletRequest; 475 } 476 477 @JSON(include = false) 478 public LiferayPortletResponse getLiferayPortletResponse() { 479 if (_request == null) { 480 return null; 481 } 482 483 LiferayPortletResponse liferayPortletResponse = 484 (LiferayPortletResponse)_request.getAttribute( 485 JavaConstants.JAVAX_PORTLET_RESPONSE); 486 487 return liferayPortletResponse; 488 } 489 490 public Locale getLocale() { 491 return LocaleUtil.fromLanguageId(_languageId); 492 } 493 494 /** 495 * Returns the date when an entity was modified if this service context is 496 * being passed as a parameter to a method which updates an entity. 497 * 498 * @return the date when an entity was modified if this service context is 499 * being passed as a parameter to a method which updates an entity 500 */ 501 public Date getModifiedDate() { 502 return _modifiedDate; 503 } 504 505 /** 506 * Returns the date when an entity was modified if this service context is 507 * being passed as a parameter to a method which modifies an entity. 508 * 509 * @param defaultModifiedDate an optional default modified date to use if 510 * this service context does not have a modified date 511 * @return the modified date if available; the default date otherwise 512 */ 513 public Date getModifiedDate(Date defaultModifiedDate) { 514 if (_modifiedDate != null) { 515 return _modifiedDate; 516 } 517 else if (defaultModifiedDate != null) { 518 return defaultModifiedDate; 519 } 520 else { 521 return new Date(); 522 } 523 } 524 525 public String getPathFriendlyURLPrivateGroup() { 526 return _pathFriendlyURLPrivateGroup; 527 } 528 529 public String getPathFriendlyURLPrivateUser() { 530 return _pathFriendlyURLPrivateUser; 531 } 532 533 public String getPathFriendlyURLPublic() { 534 return _pathFriendlyURLPublic; 535 } 536 537 /** 538 * Returns the main context path of the portal, concatenated with 539 * <code>/c</code>. 540 * 541 * @return the main context path of the portal 542 */ 543 public String getPathMain() { 544 return _pathMain; 545 } 546 547 /** 548 * Returns the portal layout ID of the current page of this service context. 549 * 550 * @return the portal layout ID of the current page 551 */ 552 public long getPlid() { 553 return _plid; 554 } 555 556 /** 557 * Returns the URL of this service context's portal, including the protocol, 558 * domain, and non-default port relative to the company instance and any 559 * virtual host. 560 * 561 * <p> 562 * The URL returned does not include the port if a default port is used. 563 * </p> 564 * 565 * @return the URL of this service context's portal, including the protocol, 566 * domain, and non-default port relative to company instance and any 567 * virtual host 568 */ 569 public String getPortalURL() { 570 return _portalURL; 571 } 572 573 /** 574 * Returns the ID of the current portlet if this service context is being 575 * passed as a parameter to a portlet. 576 * 577 * @return the ID of the current portlet 578 * @see com.liferay.portal.model.PortletPreferencesIds 579 */ 580 public String getPortletId() { 581 if (_portletPreferencesIds == null) { 582 return null; 583 } 584 585 return _portletPreferencesIds.getPortletId(); 586 } 587 588 /** 589 * Returns the portlet preferences IDs of the current portlet if the service 590 * context is being passed as a parameter to a portlet. 591 * 592 * <p> 593 * The {@link com.liferay.portal.model.PortletPreferencesIds} can be used to 594 * look up portlet preferences of the current portlet. 595 * </p> 596 * 597 * @return the portlet preferences IDs of the current portlet 598 * @see com.liferay.portal.model.PortletPreferencesIds 599 */ 600 public PortletPreferencesIds getPortletPreferencesIds() { 601 return _portletPreferencesIds; 602 } 603 604 /** 605 * Returns the remote address of the user making the request in this service 606 * context. 607 * 608 * @return the remote address of the user making the request 609 */ 610 public String getRemoteAddr() { 611 return _remoteAddr; 612 } 613 614 /** 615 * Returns the remote host name of the user making the request in this 616 * service context. 617 * 618 * @return the remote host name of the user making the request 619 */ 620 public String getRemoteHost() { 621 return _remoteHost; 622 } 623 624 @JSON(include = false) 625 public HttpServletRequest getRequest() { 626 return _request; 627 } 628 629 @JSON(include = false) 630 public HttpServletResponse getResponse() { 631 LiferayPortletResponse liferayPortletResponse = 632 getLiferayPortletResponse(); 633 634 if (liferayPortletResponse == null) { 635 return null; 636 } 637 638 return PortalUtil.getHttpServletResponse(liferayPortletResponse); 639 } 640 641 public String getRootPortletId() { 642 String portletId = getPortletId(); 643 644 if (portletId == null) { 645 return null; 646 } 647 648 return PortletConstants.getRootPortletId(portletId); 649 } 650 651 public Group getScopeGroup() throws PortalException { 652 return GroupLocalServiceUtil.getGroup(_scopeGroupId); 653 } 654 655 /** 656 * Returns the ID of the group corresponding to the current data scope of 657 * this service context. 658 * 659 * @return the ID of the group corresponding to the current data scope 660 * @see com.liferay.portal.model.Group 661 */ 662 public long getScopeGroupId() { 663 return _scopeGroupId; 664 } 665 666 public ThemeDisplay getThemeDisplay() { 667 if (_request == null) { 668 return null; 669 } 670 671 return (ThemeDisplay)_request.getAttribute(WebKeys.THEME_DISPLAY); 672 } 673 674 public TimeZone getTimeZone() { 675 return _timeZone; 676 } 677 678 /** 679 * Returns the user-agent request header of this service context. 680 * 681 * @return the user-agent request header 682 * @see com.liferay.portal.kernel.servlet.HttpHeaders 683 */ 684 public String getUserAgent() { 685 if (_request == null) { 686 return null; 687 } 688 689 return _request.getHeader(HttpHeaders.USER_AGENT); 690 } 691 692 /** 693 * Returns the complete URL of this service context's current user's profile 694 * page. 695 * 696 * @return the complete URL of this service context's current user's profile 697 * page 698 */ 699 public String getUserDisplayURL() { 700 return _userDisplayURL; 701 } 702 703 /** 704 * Returns the ID of this service context's current user. 705 * 706 * @return the ID of this service context's current user 707 */ 708 public long getUserId() { 709 return _userId; 710 } 711 712 /** 713 * Returns the UUID of this service context's current entity. 714 * 715 * @return the UUID of this service context's current entity 716 */ 717 public String getUuid() { 718 String uuid = _uuid; 719 720 _uuid = null; 721 722 return uuid; 723 } 724 725 /** 726 * Returns the workflow action to take if this service context is being 727 * passed as a parameter to a method that processes a workflow action. 728 * 729 * @return the workflow action to take 730 */ 731 public int getWorkflowAction() { 732 return _workflowAction; 733 } 734 735 /** 736 * Returns <code>true</code> if this service context is being passed as a 737 * parameter to a method which manipulates a resource to which default group 738 * permissions apply. 739 * 740 * @return <code>true</code> if this service context is being passed as a 741 * parameter to a method which manipulates a resource to which 742 * default group permissions apply; <code>false</code> otherwise 743 */ 744 public boolean isAddGroupPermissions() { 745 return _addGroupPermissions; 746 } 747 748 /** 749 * Returns <code>true</code> if this service context is being passed as a 750 * parameter to a method which manipulates a resource to which default guest 751 * permissions apply. 752 * 753 * @return <code>true</code> if this service context is being passed as a 754 * parameter to a method which manipulates a resource to which 755 * default guest permissions apply; <code>false</code> otherwise 756 */ 757 public boolean isAddGuestPermissions() { 758 return _addGuestPermissions; 759 } 760 761 public boolean isAssetEntryVisible() { 762 return _assetEntryVisible; 763 } 764 765 /** 766 * Returns <code>true</code> if this service context contains an add command 767 * (i.e. has command value {@link 768 * com.liferay.portal.kernel.util.Constants#ADD}) 769 * 770 * @return <code>true</code> if this service context contains an add 771 * command; <code>false</code> otherwise 772 */ 773 public boolean isCommandAdd() { 774 if (Validator.equals(_command, Constants.ADD) || 775 Validator.equals(_command, Constants.ADD_DYNAMIC) || 776 Validator.equals(_command, Constants.ADD_MULTIPLE)) { 777 778 return true; 779 } 780 else { 781 return false; 782 } 783 } 784 785 /** 786 * Returns <code>true</code> if this service context contains an update 787 * command (i.e. has command value {@link 788 * com.liferay.portal.kernel.util.Constants#UPDATE}) 789 * 790 * @return <code>true</code> if this service context contains an update 791 * command; <code>false</code> otherwise 792 */ 793 public boolean isCommandUpdate() { 794 if (Validator.equals(_command, Constants.UPDATE)) { 795 return true; 796 } 797 else { 798 return false; 799 } 800 } 801 802 public boolean isDeriveDefaultPermissions() { 803 return _deriveDefaultPermissions; 804 } 805 806 /** 807 * Returns <code>true</code> if portal exceptions should be handled as 808 * failures, possibly halting processing, or <code>false</code> if the 809 * exceptions should be handled differently, possibly allowing processing to 810 * continue in some manner. Services may check this flag to execute desired 811 * behavior. 812 * 813 * <p> 814 * Batch invocation of such services (exposed as a JSON web services) can 815 * result in execution of all service invocations, in spite of portal 816 * exceptions. 817 * </p> 818 * 819 * <p> 820 * If this flag is set to <code>false</code>, services can implement logic 821 * that allows processing to continue, while collecting information 822 * regarding the exceptions for returning to the caller. For example, the 823 * {@link 824 * com.liferay.portlet.asset.service.impl.AssetVocabularyServiceImpl#deleteVocabularies( 825 * long[], ServiceContext)} method uses the list it returns to give 826 * information on vocabularies it fails to delete; it returns an empty list 827 * if all deletions are successful. 828 * </p> 829 * 830 * @return <code>true</code> if portal exceptions are to be handled as 831 * failures; <code>false</code> if portal exceptions can be handled 832 * differently, possibly allowing processing to continue in some 833 * manner 834 */ 835 public boolean isFailOnPortalException() { 836 return _failOnPortalException; 837 } 838 839 /** 840 * Returns whether the primary entity of this service context is to be 841 * indexed/re-indexed. 842 * 843 * @return <code>true</code> the primary entity of this service context is 844 * to be indexed/re-indexed; <code>false</code> otherwise 845 */ 846 public boolean isIndexingEnabled() { 847 return _indexingEnabled; 848 } 849 850 /** 851 * Returns <code>true</code> if the sender of this service context's request 852 * is signed in. 853 * 854 * @return <code>true</code> if the sender of this service context's request 855 * is signed in; <code>false</code> otherwise 856 */ 857 public boolean isSignedIn() { 858 return _signedIn; 859 } 860 861 public void merge(ServiceContext serviceContext) { 862 setAddGroupPermissions(serviceContext.isAddGroupPermissions()); 863 setAddGuestPermissions(serviceContext.isAddGuestPermissions()); 864 865 if (serviceContext.getAssetCategoryIds() != null) { 866 setAssetCategoryIds(serviceContext.getAssetCategoryIds()); 867 } 868 869 if (serviceContext.getAssetLinkEntryIds() != null) { 870 setAssetLinkEntryIds(serviceContext.getAssetLinkEntryIds()); 871 } 872 873 if (serviceContext.getAssetTagNames() != null) { 874 setAssetTagNames(serviceContext.getAssetTagNames()); 875 } 876 877 if (serviceContext.getAttributes() != null) { 878 setAttributes(serviceContext.getAttributes()); 879 } 880 881 if (Validator.isNotNull(serviceContext.getCommand())) { 882 setCommand(serviceContext.getCommand()); 883 } 884 885 if (serviceContext.getCompanyId() > 0) { 886 setCompanyId(serviceContext.getCompanyId()); 887 } 888 889 if (serviceContext.getCreateDate() != null) { 890 setCreateDate(serviceContext.getCreateDate()); 891 } 892 893 if (Validator.isNotNull(serviceContext.getCurrentURL())) { 894 setCurrentURL(serviceContext.getCurrentURL()); 895 } 896 897 if (serviceContext.getExpandoBridgeAttributes() != null) { 898 setExpandoBridgeAttributes( 899 serviceContext.getExpandoBridgeAttributes()); 900 } 901 902 if (serviceContext.getGroupPermissions() != null) { 903 setGroupPermissions(serviceContext.getGroupPermissions()); 904 } 905 906 if (serviceContext.getGuestPermissions() != null) { 907 setGuestPermissions(serviceContext.getGuestPermissions()); 908 } 909 910 if (serviceContext.getHeaders() != null) { 911 setHeaders(serviceContext.getHeaders()); 912 } 913 914 setFailOnPortalException(serviceContext.isFailOnPortalException()); 915 setLanguageId(serviceContext.getLanguageId()); 916 917 if (Validator.isNotNull(serviceContext.getLayoutFullURL())) { 918 setLayoutFullURL(serviceContext.getLayoutFullURL()); 919 } 920 921 if (Validator.isNotNull(serviceContext.getLayoutURL())) { 922 setLayoutURL(serviceContext.getLayoutURL()); 923 } 924 925 if (serviceContext.getModifiedDate() != null) { 926 setModifiedDate(serviceContext.getModifiedDate()); 927 } 928 929 if (Validator.isNotNull(serviceContext.getPathMain())) { 930 setPathMain(serviceContext.getPathMain()); 931 } 932 933 if (serviceContext.getPlid() > 0) { 934 setPlid(serviceContext.getPlid()); 935 } 936 937 if (Validator.isNotNull(serviceContext.getPortalURL())) { 938 setPortalURL(serviceContext.getPortalURL()); 939 } 940 941 if (serviceContext.getPortletPreferencesIds() != null) { 942 setPortletPreferencesIds(serviceContext.getPortletPreferencesIds()); 943 } 944 945 if (Validator.isNotNull(serviceContext.getRemoteAddr())) { 946 setRemoteAddr(serviceContext.getRemoteAddr()); 947 } 948 949 if (Validator.isNotNull(serviceContext.getRemoteHost())) { 950 setRemoteHost(serviceContext.getRemoteHost()); 951 } 952 953 if (serviceContext.getScopeGroupId() > 0) { 954 setScopeGroupId(serviceContext.getScopeGroupId()); 955 } 956 957 setSignedIn(serviceContext.isSignedIn()); 958 959 if (Validator.isNotNull(serviceContext.getUserDisplayURL())) { 960 setUserDisplayURL(serviceContext.getUserDisplayURL()); 961 } 962 963 if (serviceContext.getUserId() > 0) { 964 setUserId(serviceContext.getUserId()); 965 } 966 967 if (Validator.isNotNull(serviceContext.getUuid())) { 968 setUuid(serviceContext.getUuid()); 969 } 970 971 if (serviceContext.getWorkflowAction() > 0) { 972 setWorkflowAction(serviceContext.getWorkflowAction()); 973 } 974 } 975 976 /** 977 * Removes the mapping of the serializable object to the name of the 978 * standard parameter of this service context. 979 * 980 * @param name the name of the standard parameter 981 * @return the serializable object associated to the name 982 */ 983 public Serializable removeAttribute(String name) { 984 return _attributes.remove(name); 985 } 986 987 /** 988 * Sets whether or not default community permissions should apply to a 989 * resource being manipulated by a method to which this service context is 990 * passed as a parameter. 991 * 992 * @param addCommunityPermissions indicates whether or not to apply 993 * default community permissions 994 * @deprecated As of 6.1.0, renamed to {@link 995 * #setAddGroupPermissions(boolean)} 996 */ 997 @Deprecated 998 public void setAddCommunityPermissions(boolean addCommunityPermissions) { 999 setAddGroupPermissions(addCommunityPermissions); 1000 } 1001 1002 /** 1003 * Sets whether or not default group permissions should apply to a resource 1004 * being manipulated by a method to which this service context is passed as 1005 * a parameter. 1006 * 1007 * @param addGroupPermissions indicates whether or not to apply default 1008 * group permissions 1009 */ 1010 public void setAddGroupPermissions(boolean addGroupPermissions) { 1011 _addGroupPermissions = addGroupPermissions; 1012 } 1013 1014 /** 1015 * Sets whether or not default guest permissions should apply to a resource 1016 * being manipulated by a method to which this service context is passed as 1017 * a parameter. 1018 * 1019 * @param addGuestPermissions indicates whether or not to apply default 1020 * guest permissions 1021 */ 1022 public void setAddGuestPermissions(boolean addGuestPermissions) { 1023 _addGuestPermissions = addGuestPermissions; 1024 } 1025 1026 /** 1027 * Sets an array of asset category IDs to be applied to an asset entry if 1028 * this service context is being passed as a parameter to a method which 1029 * manipulates the asset entry. 1030 * 1031 * @param assetCategoryIds the primary keys of the asset categories 1032 */ 1033 public void setAssetCategoryIds(long[] assetCategoryIds) { 1034 _assetCategoryIds = assetCategoryIds; 1035 } 1036 1037 public void setAssetEntryVisible(boolean assetEntryVisible) { 1038 _assetEntryVisible = assetEntryVisible; 1039 } 1040 1041 /** 1042 * Sets an array of the primary keys of asset entries to be linked to an 1043 * asset entry if this service context is being passed as a parameter to a 1044 * method which manipulates the asset entry. 1045 * 1046 * @param assetLinkEntryIds the primary keys of the asset entries to be 1047 * linked to an asset entry 1048 */ 1049 public void setAssetLinkEntryIds(long[] assetLinkEntryIds) { 1050 _assetLinkEntryIds = assetLinkEntryIds; 1051 } 1052 1053 /** 1054 * Sets an array of asset tag names to be applied to an asset entry if this 1055 * service context is being passed as a parameter to a method which 1056 * manipulates the asset entry. 1057 * 1058 * @param assetTagNames the tag names to be applied to an asset entry 1059 */ 1060 public void setAssetTagNames(String[] assetTagNames) { 1061 _assetTagNames = assetTagNames; 1062 } 1063 1064 /** 1065 * Sets a mapping of a standard parameter's name to its serializable object. 1066 * 1067 * @param name the standard parameter name to associate with the value 1068 * @param value the serializable object to be associated with the name 1069 */ 1070 public void setAttribute(String name, Serializable value) { 1071 _attributes.put(name, value); 1072 } 1073 1074 /** 1075 * Sets the map of the name/value pairs that are the standard parameters of 1076 * this service context. Each value must be serializable. 1077 * 1078 * @param attributes the map of the name/value pairs that are the standard 1079 * parameters of this service context 1080 */ 1081 public void setAttributes(Map<String, Serializable> attributes) { 1082 _attributes = attributes; 1083 } 1084 1085 /** 1086 * Sets the value of the {@link 1087 * com.liferay.portal.kernel.util.Constants#CMD} parameter used in most 1088 * Liferay forms for internal portlets. 1089 * 1090 * @param command the value of the {@link 1091 * com.liferay.portal.kernel.util.Constants#CMD} parameter 1092 */ 1093 public void setCommand(String command) { 1094 _command = command; 1095 } 1096 1097 /** 1098 * Sets an array containing specific community permissions for a resource if 1099 * this service context is being passed as a parameter to a method which 1100 * manipulates the resource. 1101 * 1102 * @param communityPermissions the community permissions (optionally 1103 * <code>null</code>) 1104 * @deprecated As of 6.1.0, renamed to {@link 1105 * #setGroupPermissions(String[])} 1106 */ 1107 @Deprecated 1108 public void setCommunityPermissions(String[] communityPermissions) { 1109 setGroupPermissions(communityPermissions); 1110 } 1111 1112 /** 1113 * Sets the company ID of this service context's current portal instance. 1114 * 1115 * @param companyId the primary key of this service context's current portal 1116 * instance 1117 */ 1118 public void setCompanyId(long companyId) { 1119 _companyId = companyId; 1120 } 1121 1122 /** 1123 * Sets the date when an entity was created if this service context is being 1124 * passed as a parameter to a method which creates an entity. 1125 * 1126 * @param createDate the date the entity was created 1127 */ 1128 public void setCreateDate(Date createDate) { 1129 _createDate = createDate; 1130 } 1131 1132 /** 1133 * Sets the current URL of this service context 1134 * 1135 * @param currentURL the current URL of this service context 1136 */ 1137 public void setCurrentURL(String currentURL) { 1138 _currentURL = currentURL; 1139 } 1140 1141 public void setDeriveDefaultPermissions(boolean deriveDefaultPermissions) { 1142 _deriveDefaultPermissions = deriveDefaultPermissions; 1143 } 1144 1145 /** 1146 * Sets an arbitrary number of attributes of an entity to be persisted. 1147 * 1148 * <p> 1149 * These attributes should only include fields that {@link 1150 * com.liferay.portal.service.ServiceContext} does not possess by default. 1151 * </p> 1152 * 1153 * @param expandoBridgeAttributes the expando bridge attributes (optionally 1154 * <code>null</code>) 1155 */ 1156 public void setExpandoBridgeAttributes( 1157 Map<String, Serializable> expandoBridgeAttributes) { 1158 1159 _expandoBridgeAttributes = expandoBridgeAttributes; 1160 } 1161 1162 /** 1163 * Sets whether portal exceptions should be handled as failures, possibly 1164 * halting processing, or if exceptions should be handled differently, 1165 * possibly allowing processing to continue in some manner. 1166 * 1167 * @param failOnPortalException whether portal exceptions should be handled 1168 * as failures, or if portal exceptions should be handled 1169 * differently, possibly allowing processing to continue in some 1170 * manner 1171 * @see #isFailOnPortalException() 1172 */ 1173 public void setFailOnPortalException(boolean failOnPortalException) { 1174 _failOnPortalException = failOnPortalException; 1175 } 1176 1177 /** 1178 * Sets the date when an <code>aui:form</code> was generated in this service 1179 * context. The form date can be used in detecting situations in which an 1180 * entity has been modified while another client was editing that entity. 1181 * 1182 * <p> 1183 * Example: 1184 * </p> 1185 * 1186 * <p> 1187 * Person1 and person2 start editing the same version of a Web Content 1188 * article. Person1 publishes changes to the article first. When person2 1189 * attempts to publish changes to that article, the service implementation 1190 * finds that a modification to that article has already been published some 1191 * time after person2 started editing the article. Since the article 1192 * modification date was found to be later than the form date for person2, 1193 * person2 could be alerted to the modification and make a backup copy of 1194 * his edits before synchronizing with the published changes by person1. 1195 * </p> 1196 * 1197 * @param formDate the date that an <code>aui:form</code> was generated for 1198 * this service context (optionally <code>null</code>) 1199 */ 1200 public void setFormDate(Date formDate) { 1201 _formDate = formDate; 1202 } 1203 1204 /** 1205 * Sets an array containing specific group permissions for a resource if 1206 * this service context is being passed as a parameter to a method which 1207 * manipulates the resource. 1208 * 1209 * @param groupPermissions the permissions (optionally <code>null</code>) 1210 */ 1211 public void setGroupPermissions(String[] groupPermissions) { 1212 _groupPermissions = groupPermissions; 1213 } 1214 1215 /** 1216 * Sets an array containing specific guest permissions for a resource if 1217 * this service context is being passed as a parameter to a method which 1218 * manipulates the resource. 1219 * 1220 * @param guestPermissions the guest permissions (optionally 1221 * <code>null</code>) 1222 */ 1223 public void setGuestPermissions(String[] guestPermissions) { 1224 _guestPermissions = guestPermissions; 1225 } 1226 1227 /** 1228 * Sets the map of request header name/value pairs of this service context. 1229 * 1230 * @param headers map of request header name/value pairs of this service 1231 * context 1232 * @see com.liferay.portal.kernel.servlet.HttpHeaders 1233 */ 1234 public void setHeaders(Map<String, String> headers) { 1235 _headers = headers; 1236 } 1237 1238 /** 1239 * Sets whether the primary entity of this service context is to be 1240 * indexed/re-indexed. 1241 * 1242 * <p> 1243 * The entity is only indexed/re-indexed if the method receiving this 1244 * service context as a parameter does indexing. 1245 * </p> 1246 * 1247 * @param indexingEnabled whether the primary entity of this service context 1248 * is to be indexed/re-indexed (default is <code>true</code>) 1249 */ 1250 public void setIndexingEnabled(boolean indexingEnabled) { 1251 _indexingEnabled = indexingEnabled; 1252 } 1253 1254 /** 1255 * Sets the language ID of the locale of this service context. 1256 * 1257 * @param languageId the language ID of the locale of this service context's 1258 * current user 1259 */ 1260 public void setLanguageId(String languageId) { 1261 _languageId = languageId; 1262 } 1263 1264 /** 1265 * Sets the complete URL of the current page for this service context. 1266 * 1267 * @param layoutFullURL the complete URL of the current page if a page 1268 * context can be determined for this service context 1269 */ 1270 public void setLayoutFullURL(String layoutFullURL) { 1271 _layoutFullURL = layoutFullURL; 1272 } 1273 1274 /** 1275 * Sets the relative URL of the current page for this service context. 1276 * 1277 * @param layoutURL the relative URL of the current page if a page context 1278 * can be determined for this service context 1279 */ 1280 public void setLayoutURL(String layoutURL) { 1281 _layoutURL = layoutURL; 1282 } 1283 1284 /** 1285 * Sets the date when an entity was modified in this service context. 1286 * 1287 * @param modifiedDate the date when an entity was modified in this service 1288 * context 1289 */ 1290 public void setModifiedDate(Date modifiedDate) { 1291 _modifiedDate = modifiedDate; 1292 } 1293 1294 public void setPathFriendlyURLPrivateGroup( 1295 String pathFriendlyURLPrivateGroup) { 1296 1297 _pathFriendlyURLPrivateGroup = pathFriendlyURLPrivateGroup; 1298 } 1299 1300 public void setPathFriendlyURLPrivateUser( 1301 String pathFriendlyURLPrivateUser) { 1302 1303 _pathFriendlyURLPrivateUser = pathFriendlyURLPrivateUser; 1304 } 1305 1306 public void setPathFriendlyURLPublic(String pathFriendlyURLPublic) { 1307 _pathFriendlyURLPublic = pathFriendlyURLPublic; 1308 } 1309 1310 /** 1311 * Sets the main context path of the portal, concatenated with 1312 * <code>/c</code>. 1313 * 1314 * @param pathMain the main context path of the portal 1315 */ 1316 public void setPathMain(String pathMain) { 1317 _pathMain = pathMain; 1318 } 1319 1320 /** 1321 * Sets the portal layout ID of the current page in this service context. 1322 * 1323 * @param plid the portal layout ID of the current page 1324 */ 1325 public void setPlid(long plid) { 1326 _plid = plid; 1327 } 1328 1329 /** 1330 * Sets the URL of this service context's portal, including the protocol, 1331 * domain, and non-default port relative to the company instance and any 1332 * virtual host. 1333 * 1334 * <p> 1335 * The URL should not include the port if a default port is used. 1336 * </p> 1337 * 1338 * @param portalURL the portal URL 1339 */ 1340 public void setPortalURL(String portalURL) { 1341 _portalURL = portalURL; 1342 } 1343 1344 /** 1345 * Sets the portlet preferences IDs of the current portlet if this service 1346 * context is being passed as a parameter to a portlet. 1347 * 1348 * <p> 1349 * The {@link com.liferay.portal.model.PortletPreferencesIds} can be used to 1350 * look up portlet preferences of the current portlet. 1351 * </p> 1352 * 1353 * @param portletPreferencesIds the portlet preferences 1354 * @see com.liferay.portal.model.PortletPreferencesIds 1355 */ 1356 public void setPortletPreferencesIds( 1357 PortletPreferencesIds portletPreferencesIds) { 1358 1359 _portletPreferencesIds = portletPreferencesIds; 1360 } 1361 1362 /** 1363 * Sets the remote address of the user making the request in this service 1364 * context. 1365 * 1366 * @param remoteAddr the remote address of the user making the request in 1367 * this service context 1368 */ 1369 public void setRemoteAddr(String remoteAddr) { 1370 _remoteAddr = remoteAddr; 1371 } 1372 1373 /** 1374 * Sets the remote host name of the user making the request in this service 1375 * context. 1376 * 1377 * @param remoteHost the remote host name of the user making the request in 1378 * this service context 1379 */ 1380 public void setRemoteHost(String remoteHost) { 1381 _remoteHost = remoteHost; 1382 } 1383 1384 /** 1385 * Sets the optional request used when instantiating this service context. 1386 * The field is volatile and so will be discarded on serialization. 1387 * 1388 * @param request the request 1389 */ 1390 public void setRequest(HttpServletRequest request) { 1391 _request = request; 1392 } 1393 1394 /** 1395 * Sets the ID of the group corresponding to the current data scope of this 1396 * service context. 1397 * 1398 * @param scopeGroupId the ID of the group corresponding to the current data 1399 * scope of this service context 1400 * @see com.liferay.portal.model.Group 1401 */ 1402 public void setScopeGroupId(long scopeGroupId) { 1403 _scopeGroupId = scopeGroupId; 1404 } 1405 1406 /** 1407 * Sets whether the sender of this service context's request is signed in. 1408 * 1409 * @param signedIn whether the sender of this service context's request is 1410 * signed in 1411 */ 1412 public void setSignedIn(boolean signedIn) { 1413 _signedIn = signedIn; 1414 } 1415 1416 public void setTimeZone(TimeZone timeZone) { 1417 _timeZone = timeZone; 1418 } 1419 1420 /** 1421 * Sets the complete URL of this service context's current user's profile 1422 * page. 1423 * 1424 * @param userDisplayURL the complete URL of the current user's profile page 1425 */ 1426 public void setUserDisplayURL(String userDisplayURL) { 1427 _userDisplayURL = userDisplayURL; 1428 } 1429 1430 /** 1431 * Sets the ID of this service context's current user. 1432 * 1433 * @param userId the ID of the current user 1434 */ 1435 public void setUserId(long userId) { 1436 _userId = userId; 1437 } 1438 1439 /** 1440 * Sets the UUID of this service context's current entity. 1441 * 1442 * @param uuid the UUID of the current entity 1443 */ 1444 public void setUuid(String uuid) { 1445 _uuid = uuid; 1446 } 1447 1448 /** 1449 * Sets the workflow action to take if this service context is being passed 1450 * as parameter to a method that processes a workflow action. 1451 * 1452 * @param workflowAction workflow action to take (default is {@link 1453 * com.liferay.portal.kernel.workflow.WorkflowConstants#ACTION_PUBLISH}) 1454 */ 1455 public void setWorkflowAction(int workflowAction) { 1456 _workflowAction = workflowAction; 1457 } 1458 1459 public String translate(String pattern, Object... arguments) { 1460 Locale locale = getLocale(); 1461 1462 return LanguageUtil.format(locale, pattern, arguments); 1463 } 1464 1465 public void validateModifiedDate( 1466 AuditedModel auditedModel, Class<? extends PortalException> clazz) 1467 throws PortalException { 1468 1469 int value = DateUtil.compareTo( 1470 auditedModel.getModifiedDate(), _formDate); 1471 1472 if (value > 0) { 1473 try { 1474 throw clazz.newInstance(); 1475 } 1476 catch (IllegalAccessException iae) { 1477 throw new RuntimeException(iae); 1478 } 1479 catch (InstantiationException ie) { 1480 throw new RuntimeException(ie); 1481 } 1482 } 1483 } 1484 1485 private boolean _addGroupPermissions; 1486 private boolean _addGuestPermissions; 1487 private long[] _assetCategoryIds; 1488 private boolean _assetEntryVisible = true; 1489 private long[] _assetLinkEntryIds; 1490 private String[] _assetTagNames; 1491 private Map<String, Serializable> _attributes; 1492 private String _command; 1493 private long _companyId; 1494 private Date _createDate; 1495 private String _currentURL; 1496 private boolean _deriveDefaultPermissions; 1497 private Map<String, Serializable> _expandoBridgeAttributes; 1498 private boolean _failOnPortalException = true; 1499 private Date _formDate; 1500 private String[] _groupPermissions; 1501 private String[] _guestPermissions; 1502 private transient Map<String, String> _headers; 1503 private boolean _indexingEnabled = true; 1504 private String _languageId; 1505 private String _layoutFullURL; 1506 private String _layoutURL; 1507 private Date _modifiedDate; 1508 private String _pathFriendlyURLPrivateGroup; 1509 private String _pathFriendlyURLPrivateUser; 1510 private String _pathFriendlyURLPublic; 1511 private String _pathMain; 1512 private long _plid; 1513 private String _portalURL; 1514 private PortletPreferencesIds _portletPreferencesIds; 1515 private String _remoteAddr; 1516 private String _remoteHost; 1517 private transient HttpServletRequest _request; 1518 private long _scopeGroupId; 1519 private boolean _signedIn; 1520 private TimeZone _timeZone; 1521 private String _userDisplayURL; 1522 private long _userId; 1523 private String _uuid; 1524 private int _workflowAction = WorkflowConstants.ACTION_PUBLISH; 1525 1526 }