001 /** 002 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 package com.liferay.portal.service; 016 017 import com.liferay.portal.kernel.exception.PortalException; 018 import com.liferay.portal.kernel.exception.SystemException; 019 import com.liferay.portal.kernel.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 777 return true; 778 } 779 else { 780 return false; 781 } 782 } 783 784 /** 785 * Returns <code>true</code> if this service context contains an update 786 * command (i.e. has command value {@link 787 * com.liferay.portal.kernel.util.Constants#UPDATE}) 788 * 789 * @return <code>true</code> if this service context contains an update 790 * command; <code>false</code> otherwise 791 */ 792 public boolean isCommandUpdate() { 793 if (Validator.equals(_command, Constants.UPDATE)) { 794 return true; 795 } 796 else { 797 return false; 798 } 799 } 800 801 public boolean isDeriveDefaultPermissions() { 802 return _deriveDefaultPermissions; 803 } 804 805 /** 806 * Returns <code>true</code> if portal exceptions should be handled as 807 * failures, possibly halting processing, or <code>false</code> if the 808 * exceptions should be handled differently, possibly allowing processing to 809 * continue in some manner. Services may check this flag to execute desired 810 * behavior. 811 * 812 * <p> 813 * Batch invocation of such services (exposed as a JSON web services) can 814 * result in execution of all service invocations, in spite of portal 815 * exceptions. 816 * </p> 817 * 818 * <p> 819 * If this flag is set to <code>false</code>, services can implement logic 820 * that allows processing to continue, while collecting information 821 * regarding the exceptions for returning to the caller. For example, the 822 * {@link 823 * com.liferay.portlet.asset.service.impl.AssetVocabularyServiceImpl#deleteVocabularies( 824 * long[], ServiceContext)} method uses the list it returns to give 825 * information on vocabularies it fails to delete; it returns an empty list 826 * if all deletions are successful. 827 * </p> 828 * 829 * @return <code>true</code> if portal exceptions are to be handled as 830 * failures; <code>false</code> if portal exceptions can be handled 831 * differently, possibly allowing processing to continue in some 832 * manner 833 */ 834 public boolean isFailOnPortalException() { 835 return _failOnPortalException; 836 } 837 838 /** 839 * Returns whether the primary entity of this service context is to be 840 * indexed/re-indexed. 841 * 842 * @return <code>true</code> the primary entity of this service context is 843 * to be indexed/re-indexed; <code>false</code> otherwise 844 */ 845 public boolean isIndexingEnabled() { 846 return _indexingEnabled; 847 } 848 849 /** 850 * Returns <code>true</code> if the sender of this service context's request 851 * is signed in. 852 * 853 * @return <code>true</code> if the sender of this service context's request 854 * is signed in; <code>false</code> otherwise 855 */ 856 public boolean isSignedIn() { 857 return _signedIn; 858 } 859 860 public void merge(ServiceContext serviceContext) { 861 setAddGroupPermissions(serviceContext.isAddGroupPermissions()); 862 setAddGuestPermissions(serviceContext.isAddGuestPermissions()); 863 864 if (serviceContext.getAssetCategoryIds() != null) { 865 setAssetCategoryIds(serviceContext.getAssetCategoryIds()); 866 } 867 868 if (serviceContext.getAssetLinkEntryIds() != null) { 869 setAssetLinkEntryIds(serviceContext.getAssetLinkEntryIds()); 870 } 871 872 if (serviceContext.getAssetTagNames() != null) { 873 setAssetTagNames(serviceContext.getAssetTagNames()); 874 } 875 876 if (serviceContext.getAttributes() != null) { 877 setAttributes(serviceContext.getAttributes()); 878 } 879 880 if (Validator.isNotNull(serviceContext.getCommand())) { 881 setCommand(serviceContext.getCommand()); 882 } 883 884 if (serviceContext.getCompanyId() > 0) { 885 setCompanyId(serviceContext.getCompanyId()); 886 } 887 888 if (serviceContext.getCreateDate() != null) { 889 setCreateDate(serviceContext.getCreateDate()); 890 } 891 892 if (Validator.isNotNull(serviceContext.getCurrentURL())) { 893 setCurrentURL(serviceContext.getCurrentURL()); 894 } 895 896 if (serviceContext.getExpandoBridgeAttributes() != null) { 897 setExpandoBridgeAttributes( 898 serviceContext.getExpandoBridgeAttributes()); 899 } 900 901 if (serviceContext.getGroupPermissions() != null) { 902 setGroupPermissions(serviceContext.getGroupPermissions()); 903 } 904 905 if (serviceContext.getGuestPermissions() != null) { 906 setGuestPermissions(serviceContext.getGuestPermissions()); 907 } 908 909 if (serviceContext.getHeaders() != null) { 910 setHeaders(serviceContext.getHeaders()); 911 } 912 913 setFailOnPortalException(serviceContext.isFailOnPortalException()); 914 setLanguageId(serviceContext.getLanguageId()); 915 916 if (Validator.isNotNull(serviceContext.getLayoutFullURL())) { 917 setLayoutFullURL(serviceContext.getLayoutFullURL()); 918 } 919 920 if (Validator.isNotNull(serviceContext.getLayoutURL())) { 921 setLayoutURL(serviceContext.getLayoutURL()); 922 } 923 924 if (serviceContext.getModifiedDate() != null) { 925 setModifiedDate(serviceContext.getModifiedDate()); 926 } 927 928 if (Validator.isNotNull(serviceContext.getPathMain())) { 929 setPathMain(serviceContext.getPathMain()); 930 } 931 932 if (serviceContext.getPlid() > 0) { 933 setPlid(serviceContext.getPlid()); 934 } 935 936 if (Validator.isNotNull(serviceContext.getPortalURL())) { 937 setPortalURL(serviceContext.getPortalURL()); 938 } 939 940 if (serviceContext.getPortletPreferencesIds() != null) { 941 setPortletPreferencesIds(serviceContext.getPortletPreferencesIds()); 942 } 943 944 if (Validator.isNotNull(serviceContext.getRemoteAddr())) { 945 setRemoteAddr(serviceContext.getRemoteAddr()); 946 } 947 948 if (Validator.isNotNull(serviceContext.getRemoteHost())) { 949 setRemoteHost(serviceContext.getRemoteHost()); 950 } 951 952 if (serviceContext.getScopeGroupId() > 0) { 953 setScopeGroupId(serviceContext.getScopeGroupId()); 954 } 955 956 setSignedIn(serviceContext.isSignedIn()); 957 958 if (Validator.isNotNull(serviceContext.getUserDisplayURL())) { 959 setUserDisplayURL(serviceContext.getUserDisplayURL()); 960 } 961 962 if (serviceContext.getUserId() > 0) { 963 setUserId(serviceContext.getUserId()); 964 } 965 966 if (Validator.isNotNull(serviceContext.getUuid())) { 967 setUuid(serviceContext.getUuid()); 968 } 969 970 if (serviceContext.getWorkflowAction() > 0) { 971 setWorkflowAction(serviceContext.getWorkflowAction()); 972 } 973 } 974 975 /** 976 * Removes the mapping of the serializable object to the name of the 977 * standard parameter of this service context. 978 * 979 * @param name the name of the standard parameter 980 * @return the serializable object associated to the name 981 */ 982 public Serializable removeAttribute(String name) { 983 return _attributes.remove(name); 984 } 985 986 /** 987 * Sets whether or not default community permissions should apply to a 988 * resource being manipulated by a method to which this service context is 989 * passed as a parameter. 990 * 991 * @param addCommunityPermissions indicates whether or not to apply 992 * default community permissions 993 * @deprecated As of 6.1.0, renamed to {@link 994 * #setAddGroupPermissions(boolean)} 995 */ 996 public void setAddCommunityPermissions(boolean addCommunityPermissions) { 997 setAddGroupPermissions(addCommunityPermissions); 998 } 999 1000 /** 1001 * Sets whether or not default group permissions should apply to a resource 1002 * being manipulated by a method to which this service context is passed as 1003 * a parameter. 1004 * 1005 * @param addGroupPermissions indicates whether or not to apply default 1006 * group permissions 1007 */ 1008 public void setAddGroupPermissions(boolean addGroupPermissions) { 1009 _addGroupPermissions = addGroupPermissions; 1010 } 1011 1012 /** 1013 * Sets whether or not default guest permissions should apply to a resource 1014 * being manipulated by a method to which this service context is passed as 1015 * a parameter. 1016 * 1017 * @param addGuestPermissions indicates whether or not to apply default 1018 * guest permissions 1019 */ 1020 public void setAddGuestPermissions(boolean addGuestPermissions) { 1021 _addGuestPermissions = addGuestPermissions; 1022 } 1023 1024 /** 1025 * Sets an array of asset category IDs to be applied to an asset entry if 1026 * this service context is being passed as a parameter to a method which 1027 * manipulates the asset entry. 1028 * 1029 * @param assetCategoryIds the primary keys of the asset categories 1030 */ 1031 public void setAssetCategoryIds(long[] assetCategoryIds) { 1032 _assetCategoryIds = assetCategoryIds; 1033 } 1034 1035 public void setAssetEntryVisible(boolean assetEntryVisible) { 1036 _assetEntryVisible = assetEntryVisible; 1037 } 1038 1039 /** 1040 * Sets an array of the primary keys of asset entries to be linked to an 1041 * asset entry if this service context is being passed as a parameter to a 1042 * method which manipulates the asset entry. 1043 * 1044 * @param assetLinkEntryIds the primary keys of the asset entries to be 1045 * linked to an asset entry 1046 */ 1047 public void setAssetLinkEntryIds(long[] assetLinkEntryIds) { 1048 _assetLinkEntryIds = assetLinkEntryIds; 1049 } 1050 1051 /** 1052 * Sets an array of asset tag names to be applied to an asset entry if this 1053 * service context is being passed as a parameter to a method which 1054 * manipulates the asset entry. 1055 * 1056 * @param assetTagNames the tag names to be applied to an asset entry 1057 */ 1058 public void setAssetTagNames(String[] assetTagNames) { 1059 _assetTagNames = assetTagNames; 1060 } 1061 1062 /** 1063 * Sets a mapping of a standard parameter's name to its serializable object. 1064 * 1065 * @param name the standard parameter name to associate with the value 1066 * @param value the serializable object to be associated with the name 1067 */ 1068 public void setAttribute(String name, Serializable value) { 1069 _attributes.put(name, value); 1070 } 1071 1072 /** 1073 * Sets the map of the name/value pairs that are the standard parameters of 1074 * this service context. Each value must be serializable. 1075 * 1076 * @param attributes the map of the name/value pairs that are the standard 1077 * parameters of this service context 1078 */ 1079 public void setAttributes(Map<String, Serializable> attributes) { 1080 _attributes = attributes; 1081 } 1082 1083 /** 1084 * Sets the value of the {@link 1085 * com.liferay.portal.kernel.util.Constants#CMD} parameter used in most 1086 * Liferay forms for internal portlets. 1087 * 1088 * @param command the value of the {@link 1089 * com.liferay.portal.kernel.util.Constants#CMD} parameter 1090 */ 1091 public void setCommand(String command) { 1092 _command = command; 1093 } 1094 1095 /** 1096 * Sets an array containing specific community permissions for a resource if 1097 * this service context is being passed as a parameter to a method which 1098 * manipulates the resource. 1099 * 1100 * @param communityPermissions the community permissions (optionally 1101 * <code>null</code>) 1102 * @deprecated As of 6.1.0, renamed to {@link 1103 * #setGroupPermissions(String[])} 1104 */ 1105 public void setCommunityPermissions(String[] communityPermissions) { 1106 setGroupPermissions(communityPermissions); 1107 } 1108 1109 /** 1110 * Sets the company ID of this service context's current portal instance. 1111 * 1112 * @param companyId the primary key of this service context's current portal 1113 * instance 1114 */ 1115 public void setCompanyId(long companyId) { 1116 _companyId = companyId; 1117 } 1118 1119 /** 1120 * Sets the date when an entity was created if this service context is being 1121 * passed as a parameter to a method which creates an entity. 1122 * 1123 * @param createDate the date the entity was created 1124 */ 1125 public void setCreateDate(Date createDate) { 1126 _createDate = createDate; 1127 } 1128 1129 /** 1130 * Sets the current URL of this service context 1131 * 1132 * @param currentURL the current URL of this service context 1133 */ 1134 public void setCurrentURL(String currentURL) { 1135 _currentURL = currentURL; 1136 } 1137 1138 public void setDeriveDefaultPermissions(boolean deriveDefaultPermissions) { 1139 _deriveDefaultPermissions = deriveDefaultPermissions; 1140 } 1141 1142 /** 1143 * Sets an arbitrary number of attributes of an entity to be persisted. 1144 * 1145 * <p> 1146 * These attributes should only include fields that {@link 1147 * com.liferay.portal.service.ServiceContext} does not possess by default. 1148 * </p> 1149 * 1150 * @param expandoBridgeAttributes the expando bridge attributes (optionally 1151 * <code>null</code>) 1152 */ 1153 public void setExpandoBridgeAttributes( 1154 Map<String, Serializable> expandoBridgeAttributes) { 1155 1156 _expandoBridgeAttributes = expandoBridgeAttributes; 1157 } 1158 1159 /** 1160 * Sets whether portal exceptions should be handled as failures, possibly 1161 * halting processing, or if exceptions should be handled differently, 1162 * possibly allowing processing to continue in some manner. 1163 * 1164 * @param failOnPortalException whether portal exceptions should be handled 1165 * as failures, or if portal exceptions should be handled 1166 * differently, possibly allowing processing to continue in some 1167 * manner 1168 * @see #isFailOnPortalException() 1169 */ 1170 public void setFailOnPortalException(boolean failOnPortalException) { 1171 _failOnPortalException = failOnPortalException; 1172 } 1173 1174 /** 1175 * Sets the date when an <code>aui:form</code> was generated in this service 1176 * context. The form date can be used in detecting situations in which an 1177 * entity has been modified while another client was editing that entity. 1178 * 1179 * <p> 1180 * Example: 1181 * </p> 1182 * 1183 * <p> 1184 * Person1 and person2 start editing the same version of a Web Content 1185 * article. Person1 publishes changes to the article first. When person2 1186 * attempts to publish changes to that article, the service implementation 1187 * finds that a modification to that article has already been published some 1188 * time after person2 started editing the article. Since the article 1189 * modification date was found to be later than the form date for person2, 1190 * person2 could be alerted to the modification and make a backup copy of 1191 * his edits before synchronizing with the published changes by person1. 1192 * </p> 1193 * 1194 * @param formDate the date that an <code>aui:form</code> was generated for 1195 * this service context (optionally <code>null</code>) 1196 */ 1197 public void setFormDate(Date formDate) { 1198 _formDate = formDate; 1199 } 1200 1201 /** 1202 * Sets an array containing specific group permissions for a resource if 1203 * this service context is being passed as a parameter to a method which 1204 * manipulates the resource. 1205 * 1206 * @param groupPermissions the permissions (optionally <code>null</code>) 1207 */ 1208 public void setGroupPermissions(String[] groupPermissions) { 1209 _groupPermissions = groupPermissions; 1210 } 1211 1212 /** 1213 * Sets an array containing specific guest permissions for a resource if 1214 * this service context is being passed as a parameter to a method which 1215 * manipulates the resource. 1216 * 1217 * @param guestPermissions the guest permissions (optionally 1218 * <code>null</code>) 1219 */ 1220 public void setGuestPermissions(String[] guestPermissions) { 1221 _guestPermissions = guestPermissions; 1222 } 1223 1224 /** 1225 * Sets the map of request header name/value pairs of this service context. 1226 * 1227 * @param headers map of request header name/value pairs of this service 1228 * context 1229 * @see com.liferay.portal.kernel.servlet.HttpHeaders 1230 */ 1231 public void setHeaders(Map<String, String> headers) { 1232 _headers = headers; 1233 } 1234 1235 /** 1236 * Sets whether the primary entity of this service context is to be 1237 * indexed/re-indexed. 1238 * 1239 * <p> 1240 * The entity is only indexed/re-indexed if the method receiving this 1241 * service context as a parameter does indexing. 1242 * </p> 1243 * 1244 * @param indexingEnabled whether the primary entity of this service context 1245 * is to be indexed/re-indexed (default is <code>true</code>) 1246 */ 1247 public void setIndexingEnabled(boolean indexingEnabled) { 1248 _indexingEnabled = indexingEnabled; 1249 } 1250 1251 /** 1252 * Sets the language ID of the locale of this service context. 1253 * 1254 * @param languageId the language ID of the locale of this service context's 1255 * current user 1256 */ 1257 public void setLanguageId(String languageId) { 1258 _languageId = languageId; 1259 } 1260 1261 /** 1262 * Sets the complete URL of the current page for this service context. 1263 * 1264 * @param layoutFullURL the complete URL of the current page if a page 1265 * context can be determined for this service context 1266 */ 1267 public void setLayoutFullURL(String layoutFullURL) { 1268 _layoutFullURL = layoutFullURL; 1269 } 1270 1271 /** 1272 * Sets the relative URL of the current page for this service context. 1273 * 1274 * @param layoutURL the relative URL of the current page if a page context 1275 * can be determined for this service context 1276 */ 1277 public void setLayoutURL(String layoutURL) { 1278 _layoutURL = layoutURL; 1279 } 1280 1281 /** 1282 * Sets the date when an entity was modified in this service context. 1283 * 1284 * @param modifiedDate the date when an entity was modified in this service 1285 * context 1286 */ 1287 public void setModifiedDate(Date modifiedDate) { 1288 _modifiedDate = modifiedDate; 1289 } 1290 1291 public void setPathFriendlyURLPrivateGroup( 1292 String pathFriendlyURLPrivateGroup) { 1293 1294 _pathFriendlyURLPrivateGroup = pathFriendlyURLPrivateGroup; 1295 } 1296 1297 public void setPathFriendlyURLPrivateUser( 1298 String pathFriendlyURLPrivateUser) { 1299 1300 _pathFriendlyURLPrivateUser = pathFriendlyURLPrivateUser; 1301 } 1302 1303 public void setPathFriendlyURLPublic(String pathFriendlyURLPublic) { 1304 _pathFriendlyURLPublic = pathFriendlyURLPublic; 1305 } 1306 1307 /** 1308 * Sets the main context path of the portal, concatenated with 1309 * <code>/c</code>. 1310 * 1311 * @param pathMain the main context path of the portal 1312 */ 1313 public void setPathMain(String pathMain) { 1314 _pathMain = pathMain; 1315 } 1316 1317 /** 1318 * Sets the portal layout ID of the current page in this service context. 1319 * 1320 * @param plid the portal layout ID of the current page 1321 */ 1322 public void setPlid(long plid) { 1323 _plid = plid; 1324 } 1325 1326 /** 1327 * Sets the URL of this service context's portal, including the protocol, 1328 * domain, and non-default port relative to the company instance and any 1329 * virtual host. 1330 * 1331 * <p> 1332 * The URL should not include the port if a default port is used. 1333 * </p> 1334 * 1335 * @param portalURL the portal URL 1336 */ 1337 public void setPortalURL(String portalURL) { 1338 _portalURL = portalURL; 1339 } 1340 1341 /** 1342 * Sets the portlet preferences IDs of the current portlet if this service 1343 * context is being passed as a parameter to a portlet. 1344 * 1345 * <p> 1346 * The {@link com.liferay.portal.model.PortletPreferencesIds} can be used to 1347 * look up portlet preferences of the current portlet. 1348 * </p> 1349 * 1350 * @param portletPreferencesIds the portlet preferences 1351 * @see com.liferay.portal.model.PortletPreferencesIds 1352 */ 1353 public void setPortletPreferencesIds( 1354 PortletPreferencesIds portletPreferencesIds) { 1355 1356 _portletPreferencesIds = portletPreferencesIds; 1357 } 1358 1359 /** 1360 * Sets the remote address of the user making the request in this service 1361 * context. 1362 * 1363 * @param remoteAddr the remote address of the user making the request in 1364 * this service context 1365 */ 1366 public void setRemoteAddr(String remoteAddr) { 1367 _remoteAddr = remoteAddr; 1368 } 1369 1370 /** 1371 * Sets the remote host name of the user making the request in this service 1372 * context. 1373 * 1374 * @param remoteHost the remote host name of the user making the request in 1375 * this service context 1376 */ 1377 public void setRemoteHost(String remoteHost) { 1378 _remoteHost = remoteHost; 1379 } 1380 1381 /** 1382 * Sets the optional request used when instantiating this service context. 1383 * The field is volatile and so will be discarded on serialization. 1384 * 1385 * @param request the request 1386 */ 1387 public void setRequest(HttpServletRequest request) { 1388 _request = request; 1389 } 1390 1391 /** 1392 * Sets the ID of the group corresponding to the current data scope of this 1393 * service context. 1394 * 1395 * @param scopeGroupId the ID of the group corresponding to the current data 1396 * scope of this service context 1397 * @see com.liferay.portal.model.Group 1398 */ 1399 public void setScopeGroupId(long scopeGroupId) { 1400 _scopeGroupId = scopeGroupId; 1401 } 1402 1403 /** 1404 * Sets whether the sender of this service context's request is signed in. 1405 * 1406 * @param signedIn whether the sender of this service context's request is 1407 * signed in 1408 */ 1409 public void setSignedIn(boolean signedIn) { 1410 _signedIn = signedIn; 1411 } 1412 1413 public void setTimeZone(TimeZone timeZone) { 1414 _timeZone = timeZone; 1415 } 1416 1417 /** 1418 * Sets the complete URL of this service context's current user's profile 1419 * page. 1420 * 1421 * @param userDisplayURL the complete URL of the current user's profile page 1422 */ 1423 public void setUserDisplayURL(String userDisplayURL) { 1424 _userDisplayURL = userDisplayURL; 1425 } 1426 1427 /** 1428 * Sets the ID of this service context's current user. 1429 * 1430 * @param userId the ID of the current user 1431 */ 1432 public void setUserId(long userId) { 1433 _userId = userId; 1434 } 1435 1436 /** 1437 * Sets the UUID of this service context's current entity. 1438 * 1439 * @param uuid the UUID of the current entity 1440 */ 1441 public void setUuid(String uuid) { 1442 _uuid = uuid; 1443 } 1444 1445 /** 1446 * Sets the workflow action to take if this service context is being passed 1447 * as parameter to a method that processes a workflow action. 1448 * 1449 * @param workflowAction workflow action to take (default is {@link 1450 * com.liferay.portal.kernel.workflow.WorkflowConstants#ACTION_PUBLISH}) 1451 */ 1452 public void setWorkflowAction(int workflowAction) { 1453 _workflowAction = workflowAction; 1454 } 1455 1456 public String translate(String pattern, Object... arguments) { 1457 Locale locale = getLocale(); 1458 1459 return LanguageUtil.format(locale, pattern, arguments); 1460 } 1461 1462 public void validateModifiedDate( 1463 AuditedModel auditedModel, Class<? extends PortalException> clazz) 1464 throws PortalException { 1465 1466 int value = DateUtil.compareTo( 1467 auditedModel.getModifiedDate(), _formDate); 1468 1469 if (value > 0) { 1470 try { 1471 throw clazz.newInstance(); 1472 } 1473 catch (IllegalAccessException iae) { 1474 throw new RuntimeException(iae); 1475 } 1476 catch (InstantiationException ie) { 1477 throw new RuntimeException(ie); 1478 } 1479 } 1480 } 1481 1482 private boolean _addGroupPermissions; 1483 private boolean _addGuestPermissions; 1484 private long[] _assetCategoryIds; 1485 private boolean _assetEntryVisible = true; 1486 private long[] _assetLinkEntryIds; 1487 private String[] _assetTagNames; 1488 private Map<String, Serializable> _attributes; 1489 private String _command; 1490 private long _companyId; 1491 private Date _createDate; 1492 private String _currentURL; 1493 private boolean _deriveDefaultPermissions; 1494 private Map<String, Serializable> _expandoBridgeAttributes; 1495 private boolean _failOnPortalException = true; 1496 private Date _formDate; 1497 private String[] _groupPermissions; 1498 private String[] _guestPermissions; 1499 private transient Map<String, String> _headers; 1500 private boolean _indexingEnabled = true; 1501 private String _languageId; 1502 private String _layoutFullURL; 1503 private String _layoutURL; 1504 private Date _modifiedDate; 1505 private String _pathFriendlyURLPrivateGroup; 1506 private String _pathFriendlyURLPrivateUser; 1507 private String _pathFriendlyURLPublic; 1508 private String _pathMain; 1509 private long _plid; 1510 private String _portalURL; 1511 private PortletPreferencesIds _portletPreferencesIds; 1512 private String _remoteAddr; 1513 private String _remoteHost; 1514 private transient HttpServletRequest _request; 1515 private long _scopeGroupId; 1516 private boolean _signedIn; 1517 private TimeZone _timeZone; 1518 private String _userDisplayURL; 1519 private long _userId; 1520 private String _uuid; 1521 private int _workflowAction = WorkflowConstants.ACTION_PUBLISH; 1522 1523 }