001 /** 002 * Copyright (c) 2000-2012 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.impl; 016 017 import com.liferay.portal.kernel.exception.PortalException; 018 import com.liferay.portal.kernel.exception.SystemException; 019 import com.liferay.portal.kernel.search.Indexer; 020 import com.liferay.portal.kernel.search.IndexerRegistryUtil; 021 import com.liferay.portal.kernel.util.ListUtil; 022 import com.liferay.portal.model.Address; 023 import com.liferay.portal.model.EmailAddress; 024 import com.liferay.portal.model.OrgLabor; 025 import com.liferay.portal.model.Organization; 026 import com.liferay.portal.model.OrganizationConstants; 027 import com.liferay.portal.model.Phone; 028 import com.liferay.portal.model.User; 029 import com.liferay.portal.model.Website; 030 import com.liferay.portal.security.auth.PrincipalException; 031 import com.liferay.portal.security.permission.ActionKeys; 032 import com.liferay.portal.security.permission.PermissionChecker; 033 import com.liferay.portal.service.ServiceContext; 034 import com.liferay.portal.service.base.OrganizationServiceBaseImpl; 035 import com.liferay.portal.service.permission.GroupPermissionUtil; 036 import com.liferay.portal.service.permission.OrganizationPermissionUtil; 037 import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil; 038 import com.liferay.portal.service.permission.PortalPermissionUtil; 039 import com.liferay.portal.service.permission.UserPermissionUtil; 040 import com.liferay.portlet.usersadmin.util.UsersAdminUtil; 041 042 import java.util.Iterator; 043 import java.util.LinkedHashMap; 044 import java.util.List; 045 046 /** 047 * The implementation of the organization remote service. 048 * 049 * @author Brian Wing Shun Chan 050 * @author Jorge Ferrer 051 * @author Julio Camarero 052 */ 053 public class OrganizationServiceImpl extends OrganizationServiceBaseImpl { 054 055 /** 056 * Adds the organizations to the group. 057 * 058 * @param groupId the primary key of the group 059 * @param organizationIds the primary keys of the organizations 060 * @throws PortalException if a group or organization with the primary key 061 * could not be found or if the user did not have permission to 062 * assign group members 063 * @throws SystemException if a system exception occurred 064 */ 065 public void addGroupOrganizations(long groupId, long[] organizationIds) 066 throws PortalException, SystemException { 067 068 GroupPermissionUtil.check( 069 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS); 070 071 organizationLocalService.addGroupOrganizations( 072 groupId, organizationIds); 073 } 074 075 /** 076 * Adds an organization with additional parameters. 077 * 078 * <p> 079 * This method handles the creation and bookkeeping of the organization 080 * including its resources, metadata, and internal data structures. 081 * </p> 082 * 083 * @param parentOrganizationId the primary key of the organization's parent 084 * organization 085 * @param name the organization's name 086 * @param type the organization's type 087 * @param recursable whether the permissions of the organization are to be 088 * inherited by its sub-organizations 089 * @param regionId the primary key of the organization's region 090 * @param countryId the primary key of the organization's country 091 * @param statusId the organization's workflow status 092 * @param comments the comments about the organization 093 * @param site whether the organization is to be associated with a main 094 * site 095 * @param addresses the organization's addresses 096 * @param emailAddresses the organization's email addresses 097 * @param orgLabors the organization's hours of operation 098 * @param phones the organization's phone numbers 099 * @param websites the organization's websites 100 * @param serviceContext the organization's service context (optionally 101 * <code>null</code>). Can set asset category IDs, asset tag names, 102 * and expando bridge attributes for the organization. 103 * @return the organization 104 * @throws PortalException if a parent organization with the primary key 105 * could not be found, if the organization's information was 106 * invalid, or if the user did not have permission to add the 107 * organization 108 * @throws SystemException if a system exception occurred 109 */ 110 public Organization addOrganization( 111 long parentOrganizationId, String name, String type, 112 boolean recursable, long regionId, long countryId, int statusId, 113 String comments, boolean site, List<Address> addresses, 114 List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors, 115 List<Phone> phones, List<Website> websites, 116 ServiceContext serviceContext) 117 throws PortalException, SystemException { 118 119 boolean indexingEnabled = serviceContext.isIndexingEnabled(); 120 121 serviceContext.setIndexingEnabled(false); 122 123 try { 124 Organization organization = addOrganization( 125 parentOrganizationId, name, type, recursable, regionId, 126 countryId, statusId, comments, site, serviceContext); 127 128 UsersAdminUtil.updateAddresses( 129 Organization.class.getName(), organization.getOrganizationId(), 130 addresses); 131 132 UsersAdminUtil.updateEmailAddresses( 133 Organization.class.getName(), organization.getOrganizationId(), 134 emailAddresses); 135 136 UsersAdminUtil.updateOrgLabors( 137 organization.getOrganizationId(), orgLabors); 138 139 UsersAdminUtil.updatePhones( 140 Organization.class.getName(), organization.getOrganizationId(), 141 phones); 142 143 UsersAdminUtil.updateWebsites( 144 Organization.class.getName(), organization.getOrganizationId(), 145 websites); 146 147 if (indexingEnabled) { 148 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer( 149 Organization.class); 150 151 indexer.reindex(organization); 152 } 153 154 return organization; 155 } 156 finally { 157 serviceContext.setIndexingEnabled(indexingEnabled); 158 } 159 } 160 161 /** 162 * Adds an organization. 163 * 164 * <p> 165 * This method handles the creation and bookkeeping of the organization 166 * including its resources, metadata, and internal data structures. 167 * </p> 168 * 169 * @param parentOrganizationId the primary key of the organization's parent 170 * organization 171 * @param name the organization's name 172 * @param type the organization's type 173 * @param recursable whether the permissions of the organization are to be 174 * inherited by its sub-organizations 175 * @param regionId the primary key of the organization's region 176 * @param countryId the primary key of the organization's country 177 * @param statusId the organization's workflow status 178 * @param comments the comments about the organization 179 * @param site whether the organization is to be associated with a main 180 * site 181 * @param serviceContext the organization's service context (optionally 182 * <code>null</code>). Can set asset category IDs, asset tag names, 183 * and expando bridge attributes for the organization. 184 * @return the organization 185 * @throws PortalException if the parent organization with the primary key 186 * could not be found, if the organization information was invalid, 187 * or if the user did not have permission to add the organization 188 * @throws SystemException if a system exception occurred 189 */ 190 public Organization addOrganization( 191 long parentOrganizationId, String name, String type, 192 boolean recursable, long regionId, long countryId, int statusId, 193 String comments, boolean site, ServiceContext serviceContext) 194 throws PortalException, SystemException { 195 196 if (!OrganizationPermissionUtil.contains( 197 getPermissionChecker(), parentOrganizationId, 198 ActionKeys.MANAGE_SUBORGANIZATIONS) && 199 !PortalPermissionUtil.contains( 200 getPermissionChecker(), ActionKeys.ADD_ORGANIZATION)) { 201 202 throw new PrincipalException( 203 "User " + getUserId() + " does not have permissions to add " + 204 "an organization with parent " + parentOrganizationId); 205 } 206 207 return organizationLocalService.addOrganization( 208 getUserId(), parentOrganizationId, name, type, recursable, regionId, 209 countryId, statusId, comments, site, serviceContext); 210 } 211 212 /** 213 * Assigns the password policy to the organizations, removing any other 214 * currently assigned password policies. 215 * 216 * @param passwordPolicyId the primary key of the password policy 217 * @param organizationIds the primary keys of the organizations 218 * @throws PortalException if the user did not have permission to update the 219 * password policy 220 * @throws SystemException if a system exception occurred 221 */ 222 public void addPasswordPolicyOrganizations( 223 long passwordPolicyId, long[] organizationIds) 224 throws PortalException, SystemException { 225 226 PasswordPolicyPermissionUtil.check( 227 getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE); 228 229 organizationLocalService.addPasswordPolicyOrganizations( 230 passwordPolicyId, organizationIds); 231 } 232 233 /** 234 * Deletes the logo of the organization. 235 * 236 * @param organizationId the primary key of the organization 237 * @throws PortalException if an organization with the primary key could not 238 * be found, if the organization's logo could not be found, or if 239 * the user did not have permission to update the organization 240 * @throws SystemException if a system exception occurred 241 */ 242 public void deleteLogo(long organizationId) 243 throws PortalException, SystemException { 244 245 OrganizationPermissionUtil.check( 246 getPermissionChecker(), organizationId, ActionKeys.UPDATE); 247 248 organizationLocalService.deleteLogo(organizationId); 249 } 250 251 /** 252 * Deletes the organization. The organization's associated resources and 253 * assets are also deleted. 254 * 255 * @param organizationId the primary key of the organization 256 * @throws PortalException if an organization with the primary key could not 257 * be found, if the user did not have permission to delete the 258 * organization, if the organization had a workflow in approved 259 * status, or if the organization was a parent organization 260 * @throws SystemException if a system exception occurred 261 */ 262 public void deleteOrganization(long organizationId) 263 throws PortalException, SystemException { 264 265 OrganizationPermissionUtil.check( 266 getPermissionChecker(), organizationId, ActionKeys.DELETE); 267 268 organizationLocalService.deleteOrganization(organizationId); 269 } 270 271 /** 272 * Returns all the organizations which the user has permission to manage. 273 * 274 * @param actionId the permitted action 275 * @param max the maximum number of the organizations to be considered 276 * @return the organizations which the user has permission to manage 277 * @throws PortalException if a portal exception occurred 278 * @throws SystemException if a system exception occurred 279 * @deprecated Replaced by {@link #getOrganizations(long, long, int, int)} 280 */ 281 public List<Organization> getManageableOrganizations( 282 String actionId, int max) 283 throws PortalException, SystemException { 284 285 PermissionChecker permissionChecker = getPermissionChecker(); 286 287 if (permissionChecker.isCompanyAdmin()) { 288 return organizationLocalService.search( 289 permissionChecker.getCompanyId(), 290 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null, 291 null, null, null, 0, max); 292 } 293 294 LinkedHashMap<String, Object> params = 295 new LinkedHashMap<String, Object>(); 296 297 List<Organization> userOrganizations = 298 organizationLocalService.getUserOrganizations( 299 permissionChecker.getUserId()); 300 301 params.put("organizationsTree", userOrganizations); 302 303 List<Organization> manageableOrganizations = 304 organizationLocalService.search( 305 permissionChecker.getCompanyId(), 306 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null, 307 null, null, params, 0, max); 308 309 manageableOrganizations = ListUtil.copy(manageableOrganizations); 310 311 Iterator<Organization> itr = manageableOrganizations.iterator(); 312 313 while (itr.hasNext()) { 314 Organization organization = itr.next(); 315 316 if (!OrganizationPermissionUtil.contains( 317 permissionChecker, organization, actionId)) { 318 319 itr.remove(); 320 } 321 } 322 323 return manageableOrganizations; 324 } 325 326 /** 327 * Returns the organization with the primary key. 328 * 329 * @param organizationId the primary key of the organization 330 * @return the organization with the primary key 331 * @throws PortalException if an organization with the primary key could not 332 * be found or if the user did not have permission to view the 333 * organization 334 * @throws SystemException if a system exception occurred 335 */ 336 public Organization getOrganization(long organizationId) 337 throws PortalException, SystemException { 338 339 OrganizationPermissionUtil.check( 340 getPermissionChecker(), organizationId, ActionKeys.VIEW); 341 342 return organizationLocalService.getOrganization(organizationId); 343 } 344 345 /** 346 * Returns the primary key of the organization with the name. 347 * 348 * @param companyId the primary key of the organization's company 349 * @param name the organization's name 350 * @return the primary key of the organization with the name, or 351 * <code>0</code> if the organization could not be found 352 * @throws PortalException if the user did not have permission to view the 353 * organization 354 * @throws SystemException if a system exception occurred 355 */ 356 public long getOrganizationId(long companyId, String name) 357 throws PortalException, SystemException { 358 359 long organizationId = organizationLocalService.getOrganizationId( 360 companyId, name); 361 362 OrganizationPermissionUtil.check( 363 getPermissionChecker(), organizationId, ActionKeys.VIEW); 364 365 return organizationId; 366 } 367 368 /** 369 * Returns all the organizations belonging to the parent organization. 370 * 371 * @param companyId the primary key of the organizations' company 372 * @param parentOrganizationId the primary key of the organizations' parent 373 * organization 374 * @return the organizations belonging to the parent organization 375 * @throws SystemException if a system exception occurred 376 */ 377 public List<Organization> getOrganizations( 378 long companyId, long parentOrganizationId) 379 throws SystemException { 380 381 return organizationPersistence.filterFindByC_P( 382 companyId, parentOrganizationId); 383 } 384 385 /** 386 * Returns a range of all the organizations belonging to the parent 387 * organization. 388 * 389 * <p> 390 * Useful when paginating results. Returns a maximum of <code>end - 391 * start</code> instances. <code>start</code> and <code>end</code> are not 392 * primary keys, they are indexes in the result set. Thus, <code>0</code> 393 * refers to the first result in the set. Setting both <code>start</code> 394 * and <code>end</code> to {@link 395 * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full 396 * result set. 397 * </p> 398 * 399 * @param companyId the primary key of the organizations' company 400 * @param parentOrganizationId the primary key of the organizations' parent 401 * organization 402 * @param start the lower bound of the range of organizations to return 403 * @param end the upper bound of the range of organizations to return (not 404 * inclusive) 405 * @return the range of organizations belonging to the parent organization 406 * @throws SystemException if a system exception occurred 407 */ 408 public List<Organization> getOrganizations( 409 long companyId, long parentOrganizationId, int start, int end) 410 throws SystemException { 411 412 return organizationPersistence.filterFindByC_P( 413 companyId, parentOrganizationId, start, end); 414 } 415 416 /** 417 * Returns the number of organizations belonging to the parent organization. 418 * 419 * @param companyId the primary key of the organizations' company 420 * @param parentOrganizationId the primary key of the organizations' parent 421 * organization 422 * @return the number of organizations belonging to the parent organization 423 * @throws SystemException if a system exception occurred 424 */ 425 public int getOrganizationsCount(long companyId, long parentOrganizationId) 426 throws SystemException { 427 428 return organizationPersistence.filterCountByC_P( 429 companyId, parentOrganizationId); 430 } 431 432 /** 433 * Returns all the organizations associated with the user. 434 * 435 * @param userId the primary key of the user 436 * @return the organizations associated with the user 437 * @throws PortalException if a user with the primary key could not be found 438 * @throws SystemException if a system exception occurred 439 */ 440 public List<Organization> getUserOrganizations(long userId) 441 throws PortalException, SystemException { 442 443 UserPermissionUtil.check( 444 getPermissionChecker(), userId, ActionKeys.VIEW); 445 446 return organizationLocalService.getUserOrganizations(userId); 447 } 448 449 /** 450 * Sets the organizations in the group, removing and adding organizations to 451 * the group as necessary. 452 * 453 * @param groupId the primary key of the group 454 * @param organizationIds the primary keys of the organizations 455 * @throws PortalException if a group or organization with the primary key 456 * could not be found or if the user did not have permission to 457 * assign group members 458 * @throws SystemException if a system exception occurred 459 */ 460 public void setGroupOrganizations(long groupId, long[] organizationIds) 461 throws PortalException, SystemException { 462 463 GroupPermissionUtil.check( 464 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS); 465 466 organizationLocalService.setGroupOrganizations( 467 groupId, organizationIds); 468 } 469 470 /** 471 * Removes the organizations from the group. 472 * 473 * @param groupId the primary key of the group 474 * @param organizationIds the primary keys of the organizations 475 * @throws PortalException if a group or organization with the primary key 476 * could not be found or if the user did not have permission to 477 * assign group members 478 * @throws SystemException if a system exception occurred 479 */ 480 public void unsetGroupOrganizations(long groupId, long[] organizationIds) 481 throws PortalException, SystemException { 482 483 GroupPermissionUtil.check( 484 getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS); 485 486 organizationLocalService.unsetGroupOrganizations( 487 groupId, organizationIds); 488 } 489 490 /** 491 * Removes the organizations from the password policy. 492 * 493 * @param passwordPolicyId the primary key of the password policy 494 * @param organizationIds the primary keys of the organizations 495 * @throws PortalException if a password policy or organization with the 496 * primary key could not be found, or if the user did not have 497 * permission to update the password policy 498 * @throws SystemException if a system exception occurred 499 */ 500 public void unsetPasswordPolicyOrganizations( 501 long passwordPolicyId, long[] organizationIds) 502 throws PortalException, SystemException { 503 504 PasswordPolicyPermissionUtil.check( 505 getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE); 506 507 organizationLocalService.unsetPasswordPolicyOrganizations( 508 passwordPolicyId, organizationIds); 509 } 510 511 /** 512 * Updates the organization with additional parameters. 513 * 514 * @param organizationId the primary key of the organization 515 * @param parentOrganizationId the primary key of the organization's parent 516 * organization 517 * @param name the organization's name 518 * @param type the organization's type 519 * @param recursable whether the permissions of the organization are to be 520 * inherited by its sub-organizations 521 * @param regionId the primary key of the organization's region 522 * @param countryId the primary key of the organization's country 523 * @param statusId the organization's workflow status 524 * @param comments the comments about the organization 525 * @param site whether the organization is to be associated with a main 526 * site 527 * @param addresses the organization's addresses 528 * @param emailAddresses the organization's email addresses 529 * @param orgLabors the organization's hours of operation 530 * @param phones the organization's phone numbers 531 * @param websites the organization's websites 532 * @param serviceContext the organization's service context (optionally 533 * <code>null</code>). Can set asset category IDs and asset tag 534 * names for the organization, and merge expando bridge attributes 535 * for the organization. 536 * @return the organization 537 * @throws PortalException if an organization or parent organization with 538 * the primary key could not be found, if the user did not have 539 * permission to update the organization information, or if the new 540 * information was invalid 541 * @throws SystemException if a system exception occurred 542 */ 543 public Organization updateOrganization( 544 long organizationId, long parentOrganizationId, String name, 545 String type, boolean recursable, long regionId, long countryId, 546 int statusId, String comments, boolean site, 547 List<Address> addresses, List<EmailAddress> emailAddresses, 548 List<OrgLabor> orgLabors, List<Phone> phones, 549 List<Website> websites, ServiceContext serviceContext) 550 throws PortalException, SystemException { 551 552 OrganizationPermissionUtil.check( 553 getPermissionChecker(), organizationId, ActionKeys.UPDATE); 554 555 if (addresses != null) { 556 UsersAdminUtil.updateAddresses( 557 Organization.class.getName(), organizationId, addresses); 558 } 559 560 if (emailAddresses != null) { 561 UsersAdminUtil.updateEmailAddresses( 562 Organization.class.getName(), organizationId, emailAddresses); 563 } 564 565 if (orgLabors != null) { 566 UsersAdminUtil.updateOrgLabors(organizationId, orgLabors); 567 } 568 569 if (phones != null) { 570 UsersAdminUtil.updatePhones( 571 Organization.class.getName(), organizationId, phones); 572 } 573 574 if (websites != null) { 575 UsersAdminUtil.updateWebsites( 576 Organization.class.getName(), organizationId, websites); 577 } 578 579 User user = getUser(); 580 581 return organizationLocalService.updateOrganization( 582 user.getCompanyId(), organizationId, parentOrganizationId, name, 583 type, recursable, regionId, countryId, statusId, comments, site, 584 serviceContext); 585 } 586 587 /** 588 * Updates the organization. 589 * 590 * @param organizationId the primary key of the organization 591 * @param parentOrganizationId the primary key of the organization's parent 592 * organization 593 * @param name the organization's name 594 * @param type the organization's type 595 * @param recursable whether permissions of the organization are to be 596 * inherited by its sub-organizations 597 * @param regionId the primary key of the organization's region 598 * @param countryId the primary key of the organization's country 599 * @param statusId the organization's workflow status 600 * @param comments the comments about the organization 601 * @param site whether the organization is to be associated with a main 602 * site 603 * @param serviceContext the organization's service context (optionally 604 * <code>null</code>). Can set asset category IDs and asset tag 605 * names for the organization, and merge expando bridge attributes 606 * for the organization. 607 * @return the organization 608 * @throws PortalException if an organization or parent organization with 609 * the primary key could not be found, if the user did not have 610 * permission to update the organization, or if the new information 611 * was invalid 612 * @throws SystemException if a system exception occurred 613 */ 614 public Organization updateOrganization( 615 long organizationId, long parentOrganizationId, String name, 616 String type, boolean recursable, long regionId, long countryId, 617 int statusId, String comments, boolean site, 618 ServiceContext serviceContext) 619 throws PortalException, SystemException { 620 621 return updateOrganization( 622 organizationId, parentOrganizationId, name, type, recursable, 623 regionId, countryId, statusId, comments, site, null, null, null, 624 null, null, serviceContext); 625 } 626 627 }