001 /** 002 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 003 * 004 * The contents of this file are subject to the terms of the Liferay Enterprise 005 * Subscription License ("License"). You may not use this file except in 006 * compliance with the License. You can obtain a copy of the License by 007 * contacting Liferay, Inc. See the License for the specific language governing 008 * permissions and limitations under the License, including but not limited to 009 * distribution rights of the Software. 010 * 011 * 012 * 013 */ 014 015 package com.liferay.portal.service; 016 017 import com.liferay.portal.kernel.exception.PortalException; 018 import com.liferay.portal.kernel.exception.SystemException; 019 import com.liferay.portal.kernel.json.JSON; 020 import com.liferay.portal.kernel.language.LanguageUtil; 021 import com.liferay.portal.kernel.portlet.LiferayPortletRequest; 022 import com.liferay.portal.kernel.portlet.LiferayPortletResponse; 023 import com.liferay.portal.kernel.servlet.HttpHeaders; 024 import com.liferay.portal.kernel.util.Constants; 025 import com.liferay.portal.kernel.util.DateUtil; 026 import com.liferay.portal.kernel.util.JavaConstants; 027 import com.liferay.portal.kernel.util.LocaleUtil; 028 import com.liferay.portal.kernel.util.Validator; 029 import com.liferay.portal.kernel.util.WebKeys; 030 import com.liferay.portal.kernel.workflow.WorkflowConstants; 031 import com.liferay.portal.model.AuditedModel; 032 import com.liferay.portal.model.Group; 033 import com.liferay.portal.model.PortletConstants; 034 import com.liferay.portal.model.PortletPreferencesIds; 035 import com.liferay.portal.model.Role; 036 import com.liferay.portal.model.RoleConstants; 037 import com.liferay.portal.security.permission.ResourceActionsUtil; 038 import com.liferay.portal.theme.ThemeDisplay; 039 import com.liferay.portal.util.PortalUtil; 040 041 import java.io.Serializable; 042 043 import java.util.ArrayList; 044 import java.util.Date; 045 import java.util.LinkedHashMap; 046 import java.util.List; 047 import java.util.Locale; 048 import java.util.Map; 049 import java.util.TimeZone; 050 051 import javax.servlet.http.HttpServletRequest; 052 import javax.servlet.http.HttpServletResponse; 053 054 /** 055 * Contains context information about a given API call. 056 * 057 * <p> 058 * The <code>ServiceContext</code> object simplifies method signatures and 059 * provides a way to consolidate many different methods with different sets of 060 * optional parameters into a single, easier to use method. It also aggregates 061 * information necessary for transversal features such as permissioning, 062 * tagging, categorization, etc. 063 * </p> 064 * 065 * @author Raymond Aug?? 066 * @author Brian Wing Shun Chan 067 * @author Jorge Ferrer 068 */ 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, SystemException { 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 public boolean getAddCommunityPermissions() { 200 return isAddGroupPermissions(); 201 } 202 203 /** 204 * Returns the asset category IDs to be applied to an asset entry if the 205 * service context is being passed as a parameter to a method which 206 * manipulates the asset entry. 207 * 208 * @return the asset category IDs 209 */ 210 public long[] getAssetCategoryIds() { 211 return _assetCategoryIds; 212 } 213 214 /** 215 * Returns the primary keys of the asset entries linked to an asset entry if 216 * the service context is being passed as a parameter to a method which 217 * manipulates the asset entry. 218 * 219 * @return the primary keys of the asset entries 220 */ 221 public long[] getAssetLinkEntryIds() { 222 return _assetLinkEntryIds; 223 } 224 225 /** 226 * Returns the asset tag names to be applied to an asset entry if the 227 * service context is being passed as a parameter to a method which 228 * manipulates the asset entry. 229 * 230 * @return the asset tag names 231 */ 232 public String[] getAssetTagNames() { 233 return _assetTagNames; 234 } 235 236 /** 237 * Returns the serializable object associated with the name of the standard 238 * parameter of this service context. 239 * 240 * @param name the name of the standard parameter 241 * @return the serializable object associated with the name 242 */ 243 public Serializable getAttribute(String name) { 244 return _attributes.get(name); 245 } 246 247 /** 248 * Returns the map of name/value pairs that are the standard parameters of 249 * this service context. Each value is serializable. 250 * 251 * @return the map of name/value pairs 252 */ 253 public Map<String, Serializable> getAttributes() { 254 return _attributes; 255 } 256 257 /** 258 * Returns the value of the {@link 259 * com.liferay.portal.kernel.util.Constants#CMD} parameter used in most 260 * Liferay forms for internal portlets. 261 * 262 * @return the value of the command parameter 263 */ 264 public String getCommand() { 265 return _command; 266 } 267 268 /** 269 * Returns the specific community permissions for a resource if the service 270 * context is being passed as a parameter to a method which manipulates the 271 * resource. 272 * 273 * @return the community permissions 274 * @deprecated As of 6.1.0, renamed to {@link #getGroupPermissions()} 275 */ 276 public String[] getCommunityPermissions() { 277 return getGroupPermissions(); 278 } 279 280 /** 281 * Returns the company ID of this service context's current portal instance. 282 * 283 * @return the company ID 284 */ 285 public long getCompanyId() { 286 return _companyId; 287 } 288 289 /** 290 * Returns the date when an entity was created if this service context is 291 * being passed as a parameter to a method which creates an entity. 292 * 293 * @return the creation date 294 */ 295 public Date getCreateDate() { 296 return _createDate; 297 } 298 299 /** 300 * Returns the date when an entity was created (or a default date) if this 301 * service context is being passed as a parameter to a method which creates 302 * an entity. 303 * 304 * @param defaultCreateDate an optional default create date to use if the 305 * service context does not have a create date 306 * @return the creation date if available; the default date otherwise 307 */ 308 public Date getCreateDate(Date defaultCreateDate) { 309 if (_createDate != null) { 310 return _createDate; 311 } 312 else if (defaultCreateDate != null) { 313 return defaultCreateDate; 314 } 315 else { 316 return new Date(); 317 } 318 } 319 320 /** 321 * Returns the current URL of this service context 322 * 323 * @return the current URL 324 */ 325 public String getCurrentURL() { 326 return _currentURL; 327 } 328 329 /** 330 * Returns an arbitrary number of attributes of an entity to be persisted. 331 * 332 * <p> 333 * These attributes only include fields that this service context does not 334 * possess by default. 335 * </p> 336 * 337 * @return the expando bridge attributes 338 */ 339 public Map<String, Serializable> getExpandoBridgeAttributes() { 340 return _expandoBridgeAttributes; 341 } 342 343 /** 344 * Returns the date when an <code>aui:form</code> was generated in this 345 * service context. The form date can be used in detecting situations in 346 * which an entity has been modified while another client was editing that 347 * entity. </p> 348 * 349 * <p> 350 * Example: 351 * </p> 352 * 353 * <p> 354 * Person1 and person2 start editing the same version of a Web Content 355 * article. Person1 publishes changes to the article first. When person2 356 * attempts to publish changes to that article, the service implementation 357 * finds that a modification to that article has already been published some 358 * time after person2 started editing the article. Since the the article 359 * modification date was found to be later than the form date for person2, 360 * person2 could be alerted to the modification and make a backup copy of 361 * his edits before synchronizing with the published changes by person1. 362 * </p> 363 */ 364 public Date getFormDate() { 365 return _formDate; 366 } 367 368 /** 369 * Returns the specific group permissions for a resource if this service 370 * context is being passed as a parameter to a method which manipulates the 371 * resource. 372 * 373 * @return the specific group permissions 374 */ 375 public String[] getGroupPermissions() { 376 return _groupPermissions; 377 } 378 379 /** 380 * Returns this service context's user ID or guest ID if no user ID is 381 * available. 382 * 383 * @return the user ID, or guest ID if there is no user in this service 384 * context, or <code>0</code> if there is no company in this service 385 * context 386 * @throws PortalException if a default user for the company could not be 387 * found 388 * @throws SystemException if a system exception occurred 389 */ 390 public long getGuestOrUserId() throws PortalException, SystemException { 391 long userId = getUserId(); 392 393 if (userId > 0) { 394 return userId; 395 } 396 397 long companyId = getCompanyId(); 398 399 if (companyId > 0) { 400 return UserLocalServiceUtil.getDefaultUserId(getCompanyId()); 401 } 402 403 return 0; 404 } 405 406 /** 407 * Returns the specific guest permissions for a resource if this service 408 * context is being passed as a parameter to a method which manipulates the 409 * resource. 410 * 411 * @return the specific guest permissions 412 */ 413 public String[] getGuestPermissions() { 414 return _guestPermissions; 415 } 416 417 /** 418 * Returns the the map of request header name/value pairs of this service 419 * context. 420 * 421 * @return the the map of request header name/value pairs 422 * @see com.liferay.portal.kernel.servlet.HttpHeaders 423 */ 424 @JSON(include = false) 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 public LiferayPortletRequest getLiferayPortletRequest() { 464 if (_request == null) { 465 return null; 466 } 467 468 LiferayPortletRequest liferayPortletRequest = 469 (LiferayPortletRequest)_request.getAttribute( 470 JavaConstants.JAVAX_PORTLET_REQUEST); 471 472 return liferayPortletRequest; 473 } 474 475 public LiferayPortletResponse getLiferayPortletResponse() { 476 if (_request == null) { 477 return null; 478 } 479 480 LiferayPortletResponse liferayPortletResponse = 481 (LiferayPortletResponse)_request.getAttribute( 482 JavaConstants.JAVAX_PORTLET_RESPONSE); 483 484 return liferayPortletResponse; 485 } 486 487 public Locale getLocale() { 488 return LocaleUtil.fromLanguageId(_languageId); 489 } 490 491 /** 492 * Returns the date when an entity was modified if this service context is 493 * being passed as a parameter to a method which updates an entity. 494 * 495 * @return 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 public Date getModifiedDate() { 499 return _modifiedDate; 500 } 501 502 /** 503 * Returns the date when an entity was modified if this service context is 504 * being passed as a parameter to a method which modifies an entity. 505 * 506 * @param defaultModifiedDate an optional default modified date to use if 507 * this service context does not have a modified date 508 * @return the modified date if available; the default date otherwise 509 */ 510 public Date getModifiedDate(Date defaultModifiedDate) { 511 if (_modifiedDate != null) { 512 return _modifiedDate; 513 } 514 else if (defaultModifiedDate != null) { 515 return defaultModifiedDate; 516 } 517 else { 518 return new Date(); 519 } 520 } 521 522 public String getPathFriendlyURLPrivateGroup() { 523 return _pathFriendlyURLPrivateGroup; 524 } 525 526 public String getPathFriendlyURLPrivateUser() { 527 return _pathFriendlyURLPrivateUser; 528 } 529 530 public String getPathFriendlyURLPublic() { 531 return _pathFriendlyURLPublic; 532 } 533 534 /** 535 * Returns the main context path of the portal, concatenated with 536 * <code>/c</code>. 537 * 538 * @return the main context path of the portal 539 */ 540 public String getPathMain() { 541 return _pathMain; 542 } 543 544 /** 545 * Returns the portal layout ID of the current page of this service context. 546 * 547 * @return the portal layout ID of the current page 548 */ 549 public long getPlid() { 550 return _plid; 551 } 552 553 /** 554 * Returns the URL of this service context's portal, including the protocol, 555 * domain, and non-default port relative to the company instance and any 556 * virtual host. 557 * 558 * <p> 559 * The URL returned does not include the port if a default port is used. 560 * </p> 561 * 562 * @return the URL of this service context's portal, including the protocol, 563 * domain, and non-default port relative to company instance and any 564 * virtual host 565 */ 566 public String getPortalURL() { 567 return _portalURL; 568 } 569 570 /** 571 * Returns the ID of the current portlet if this service context is being 572 * passed as a parameter to a portlet. 573 * 574 * @return the ID of the current portlet 575 * @see com.liferay.portal.model.PortletPreferencesIds 576 */ 577 public String getPortletId() { 578 if (_portletPreferencesIds == null) { 579 return null; 580 } 581 582 return _portletPreferencesIds.getPortletId(); 583 } 584 585 /** 586 * Returns the portlet preferences IDs of the current portlet if the service 587 * context is being passed as a parameter to a portlet. 588 * 589 * <p> 590 * The {@link com.liferay.portal.model.PortletPreferencesIds} can be used to 591 * look up portlet preferences of the current portlet. 592 * </p> 593 * 594 * @return the portlet preferences IDs of the current portlet 595 * @see com.liferay.portal.model.PortletPreferencesIds 596 */ 597 public PortletPreferencesIds getPortletPreferencesIds() { 598 return _portletPreferencesIds; 599 } 600 601 /** 602 * Returns the remote address of the user making the request in this service 603 * context. 604 * 605 * @return the remote address of the user making the request 606 */ 607 public String getRemoteAddr() { 608 return _remoteAddr; 609 } 610 611 /** 612 * Returns the remote host name of the user making the request in this 613 * service context. 614 * 615 * @return the remote host name of the user making the request 616 */ 617 public String getRemoteHost() { 618 return _remoteHost; 619 } 620 621 public HttpServletRequest getRequest() { 622 return _request; 623 } 624 625 public HttpServletResponse getResponse() { 626 LiferayPortletResponse liferayPortletResponse = 627 getLiferayPortletResponse(); 628 629 if (liferayPortletResponse == null) { 630 return null; 631 } 632 633 return PortalUtil.getHttpServletResponse(liferayPortletResponse); 634 } 635 636 public String getRootPortletId() { 637 String portletId = getPortletId(); 638 639 if (portletId == null) { 640 return null; 641 } 642 643 return PortletConstants.getRootPortletId(portletId); 644 } 645 646 public Group getScopeGroup() throws PortalException, SystemException { 647 return GroupLocalServiceUtil.getGroup(_scopeGroupId); 648 } 649 650 /** 651 * Returns the ID of the group corresponding to the current data scope of 652 * this service context. 653 * 654 * @return the ID of the group corresponding to the current data scope 655 * @see com.liferay.portal.model.Group 656 */ 657 public long getScopeGroupId() { 658 return _scopeGroupId; 659 } 660 661 public ThemeDisplay getThemeDisplay() { 662 if (_request == null) { 663 return null; 664 } 665 666 return (ThemeDisplay)_request.getAttribute(WebKeys.THEME_DISPLAY); 667 } 668 669 public TimeZone getTimeZone() { 670 return _timeZone; 671 } 672 673 /** 674 * Returns the user-agent request header of this service context. 675 * 676 * @return the user-agent request header 677 * @see com.liferay.portal.kernel.servlet.HttpHeaders 678 */ 679 public String getUserAgent() { 680 if (_request == null) { 681 return null; 682 } 683 684 return _request.getHeader(HttpHeaders.USER_AGENT); 685 } 686 687 /** 688 * Returns the complete URL of this service context's current user's profile 689 * page. 690 * 691 * @return the complete URL of this service context's current user's profile 692 * page 693 */ 694 public String getUserDisplayURL() { 695 return _userDisplayURL; 696 } 697 698 /** 699 * Returns the ID of this service context's current user. 700 * 701 * @return the ID of this service context's current user 702 */ 703 public long getUserId() { 704 return _userId; 705 } 706 707 /** 708 * Returns the UUID of this service context's current entity. 709 * 710 * @return the UUID of this service context's current entity 711 */ 712 public String getUuid() { 713 String uuid = _uuid; 714 715 _uuid = null; 716 717 return uuid; 718 } 719 720 public String getUuidWithoutReset() { 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 Validator.equals(_command, Constants.ADD_WEBDAV)) { 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 Validator.equals(_command, Constants.UPDATE_WEBDAV)) { 796 797 return true; 798 } 799 else { 800 return false; 801 } 802 } 803 804 public boolean isDeriveDefaultPermissions() { 805 return _deriveDefaultPermissions; 806 } 807 808 /** 809 * Returns <code>true</code> if portal exceptions should be handled as 810 * failures, possibly halting processing, or <code>false</code> if the 811 * exceptions should be handled differently, possibly allowing processing to 812 * continue in some manner. Services may check this flag to execute desired 813 * behavior. 814 * 815 * <p> 816 * Batch invocation of such services (exposed as a JSON web services) can 817 * result in execution of all service invocations, in spite of portal 818 * exceptions. 819 * </p> 820 * 821 * <p> 822 * If this flag is set to <code>false</code>, services can implement logic 823 * that allows processing to continue, while collecting information 824 * regarding the exceptions for returning to the caller. For example, the 825 * {@link 826 * com.liferay.portlet.asset.service.impl.AssetVocabularyServiceImpl#deleteVocabularies( 827 * long[], ServiceContext)} method uses the list it returns to give 828 * information on vocabularies it fails to delete; it returns an empty list 829 * if all deletions are successful. 830 * </p> 831 * 832 * @return <code>true</code> if portal exceptions are to be handled as 833 * failures; <code>false</code> if portal exceptions can be handled 834 * differently, possibly allowing processing to continue in some 835 * manner 836 */ 837 public boolean isFailOnPortalException() { 838 return _failOnPortalException; 839 } 840 841 /** 842 * Returns whether the primary entity of this service context is to be 843 * indexed/re-indexed. 844 * 845 * @return <code>true</code> the primary entity of this service context is 846 * to be indexed/re-indexed; <code>false</code> otherwise 847 */ 848 public boolean isIndexingEnabled() { 849 return _indexingEnabled; 850 } 851 852 /** 853 * Returns <code>true</code> if the sender of this service context's request 854 * is signed in. 855 * 856 * @return <code>true</code> if the sender of this service context's request 857 * is signed in; <code>false</code> otherwise 858 */ 859 public boolean isSignedIn() { 860 return _signedIn; 861 } 862 863 public void merge(ServiceContext serviceContext) { 864 setAddGroupPermissions(serviceContext.isAddGroupPermissions()); 865 setAddGuestPermissions(serviceContext.isAddGuestPermissions()); 866 867 if (serviceContext.getAssetCategoryIds() != null) { 868 setAssetCategoryIds(serviceContext.getAssetCategoryIds()); 869 } 870 871 if (serviceContext.getAssetLinkEntryIds() != null) { 872 setAssetLinkEntryIds(serviceContext.getAssetLinkEntryIds()); 873 } 874 875 if (serviceContext.getAssetTagNames() != null) { 876 setAssetTagNames(serviceContext.getAssetTagNames()); 877 } 878 879 if (serviceContext.getAttributes() != null) { 880 setAttributes(serviceContext.getAttributes()); 881 } 882 883 if (Validator.isNotNull(serviceContext.getCommand())) { 884 setCommand(serviceContext.getCommand()); 885 } 886 887 if (serviceContext.getCompanyId() > 0) { 888 setCompanyId(serviceContext.getCompanyId()); 889 } 890 891 if (serviceContext.getCreateDate() != null) { 892 setCreateDate(serviceContext.getCreateDate()); 893 } 894 895 if (Validator.isNotNull(serviceContext.getCurrentURL())) { 896 setCurrentURL(serviceContext.getCurrentURL()); 897 } 898 899 if (serviceContext.getExpandoBridgeAttributes() != null) { 900 setExpandoBridgeAttributes( 901 serviceContext.getExpandoBridgeAttributes()); 902 } 903 904 if (serviceContext.getGroupPermissions() != null) { 905 setGroupPermissions(serviceContext.getGroupPermissions()); 906 } 907 908 if (serviceContext.getGuestPermissions() != null) { 909 setGuestPermissions(serviceContext.getGuestPermissions()); 910 } 911 912 if (serviceContext.getHeaders() != null) { 913 setHeaders(serviceContext.getHeaders()); 914 } 915 916 setFailOnPortalException(serviceContext.isFailOnPortalException()); 917 setLanguageId(serviceContext.getLanguageId()); 918 919 if (Validator.isNotNull(serviceContext.getLayoutFullURL())) { 920 setLayoutFullURL(serviceContext.getLayoutFullURL()); 921 } 922 923 if (Validator.isNotNull(serviceContext.getLayoutURL())) { 924 setLayoutURL(serviceContext.getLayoutURL()); 925 } 926 927 if (serviceContext.getModifiedDate() != null) { 928 setModifiedDate(serviceContext.getModifiedDate()); 929 } 930 931 if (Validator.isNotNull(serviceContext.getPathMain())) { 932 setPathMain(serviceContext.getPathMain()); 933 } 934 935 if (serviceContext.getPlid() > 0) { 936 setPlid(serviceContext.getPlid()); 937 } 938 939 if (Validator.isNotNull(serviceContext.getPortalURL())) { 940 setPortalURL(serviceContext.getPortalURL()); 941 } 942 943 if (serviceContext.getPortletPreferencesIds() != null) { 944 setPortletPreferencesIds(serviceContext.getPortletPreferencesIds()); 945 } 946 947 if (Validator.isNotNull(serviceContext.getRemoteAddr())) { 948 setRemoteAddr(serviceContext.getRemoteAddr()); 949 } 950 951 if (Validator.isNotNull(serviceContext.getRemoteHost())) { 952 setRemoteHost(serviceContext.getRemoteHost()); 953 } 954 955 if (serviceContext.getScopeGroupId() > 0) { 956 setScopeGroupId(serviceContext.getScopeGroupId()); 957 } 958 959 setSignedIn(serviceContext.isSignedIn()); 960 961 if (Validator.isNotNull(serviceContext.getUserDisplayURL())) { 962 setUserDisplayURL(serviceContext.getUserDisplayURL()); 963 } 964 965 if (serviceContext.getUserId() > 0) { 966 setUserId(serviceContext.getUserId()); 967 } 968 969 if (Validator.isNotNull(serviceContext.getUuid())) { 970 setUuid(serviceContext.getUuid()); 971 } 972 973 if (serviceContext.getWorkflowAction() > 0) { 974 setWorkflowAction(serviceContext.getWorkflowAction()); 975 } 976 } 977 978 /** 979 * Removes the mapping of the serializable object to the name of the 980 * standard parameter of this service context. 981 * 982 * @param name the name of the standard parameter 983 * @return the serializable object associated to the name 984 */ 985 public Serializable removeAttribute(String name) { 986 return _attributes.remove(name); 987 } 988 989 /** 990 * Sets whether or not default community permissions should apply to a 991 * resource being manipulated by a method to which this service context is 992 * passed as a parameter. 993 * 994 * @param addCommunityPermissions indicates whether or not to apply 995 * default community permissions 996 * @deprecated As of 6.1.0, renamed to {@link 997 * #setAddGroupPermissions(boolean)} 998 */ 999 public void setAddCommunityPermissions(boolean addCommunityPermissions) { 1000 setAddGroupPermissions(addCommunityPermissions); 1001 } 1002 1003 /** 1004 * Sets whether or not default group permissions should apply to a resource 1005 * being manipulated by a method to which this service context is passed as 1006 * a parameter. 1007 * 1008 * @param addGroupPermissions indicates whether or not to apply default 1009 * group permissions 1010 */ 1011 public void setAddGroupPermissions(boolean addGroupPermissions) { 1012 _addGroupPermissions = addGroupPermissions; 1013 } 1014 1015 /** 1016 * Sets whether or not default guest permissions should apply to a resource 1017 * being manipulated by a method to which this service context is passed as 1018 * a parameter. 1019 * 1020 * @param addGuestPermissions indicates whether or not to apply default 1021 * guest permissions 1022 */ 1023 public void setAddGuestPermissions(boolean addGuestPermissions) { 1024 _addGuestPermissions = addGuestPermissions; 1025 } 1026 1027 /** 1028 * Sets an array of asset category IDs to be applied to an asset entry if 1029 * this service context is being passed as a parameter to a method which 1030 * manipulates the asset entry. 1031 * 1032 * @param assetCategoryIds the primary keys of the asset categories 1033 */ 1034 public void setAssetCategoryIds(long[] assetCategoryIds) { 1035 _assetCategoryIds = assetCategoryIds; 1036 } 1037 1038 public void setAssetEntryVisible(boolean assetEntryVisible) { 1039 _assetEntryVisible = assetEntryVisible; 1040 } 1041 1042 /** 1043 * Sets an array of the primary keys of asset entries to be linked to an 1044 * asset entry if this service context is being passed as a parameter to a 1045 * method which manipulates the asset entry. 1046 * 1047 * @param assetLinkEntryIds the primary keys of the asset entries to be 1048 * linked to an asset entry 1049 */ 1050 public void setAssetLinkEntryIds(long[] assetLinkEntryIds) { 1051 _assetLinkEntryIds = assetLinkEntryIds; 1052 } 1053 1054 /** 1055 * Sets an array of asset tag names to be applied to an asset entry if this 1056 * service context is being passed as a parameter to a method which 1057 * manipulates the asset entry. 1058 * 1059 * @param assetTagNames the tag names to be applied to an asset entry 1060 */ 1061 public void setAssetTagNames(String[] assetTagNames) { 1062 _assetTagNames = assetTagNames; 1063 } 1064 1065 /** 1066 * Sets a mapping of a standard parameter's name to its serializable object. 1067 * 1068 * @param name the standard parameter name to associate with the value 1069 * @param value the serializable object to be associated with the name 1070 */ 1071 public void setAttribute(String name, Serializable value) { 1072 _attributes.put(name, value); 1073 } 1074 1075 /** 1076 * Sets the map of the name/value pairs that are the standard parameters of 1077 * this service context. Each value must be serializable. 1078 * 1079 * @param attributes the map of the name/value pairs that are the standard 1080 * parameters of this service context 1081 */ 1082 public void setAttributes(Map<String, Serializable> attributes) { 1083 _attributes = attributes; 1084 } 1085 1086 /** 1087 * Sets the value of the {@link 1088 * com.liferay.portal.kernel.util.Constants#CMD} parameter used in most 1089 * Liferay forms for internal portlets. 1090 * 1091 * @param command the value of the {@link 1092 * com.liferay.portal.kernel.util.Constants#CMD} parameter 1093 */ 1094 public void setCommand(String command) { 1095 _command = command; 1096 } 1097 1098 /** 1099 * Sets an array containing specific community permissions for a resource if 1100 * this service context is being passed as a parameter to a method which 1101 * manipulates the resource. 1102 * 1103 * @param communityPermissions the community permissions (optionally 1104 * <code>null</code>) 1105 * @deprecated As of 6.1.0, renamed to {@link 1106 * #setGroupPermissions(String[])} 1107 */ 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 }