Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.service.impl;
import com.liferay.asset.kernel.service.AssetEntryLocalService;
import com.liferay.expando.kernel.service.ExpandoRowLocalService;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.bean.BeanReference;
import com.liferay.portal.kernel.dao.orm.QueryDefinition;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.DuplicateOrganizationException;
import com.liferay.portal.kernel.exception.OrganizationNameException;
import com.liferay.portal.kernel.exception.OrganizationParentException;
import com.liferay.portal.kernel.exception.OrganizationTypeException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.RequiredOrganizationException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.model.Company;
import com.liferay.portal.kernel.model.Country;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.GroupConstants;
import com.liferay.portal.kernel.model.ListTypeConstants;
import com.liferay.portal.kernel.model.Organization;
import com.liferay.portal.kernel.model.OrganizationConstants;
import com.liferay.portal.kernel.model.Region;
import com.liferay.portal.kernel.model.ResourceConstants;
import com.liferay.portal.kernel.model.Role;
import com.liferay.portal.kernel.model.SystemEventConstants;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.UserGroupRole;
import com.liferay.portal.kernel.model.role.RoleConstants;
import com.liferay.portal.kernel.search.BaseModelSearchResult;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.IndexerRegistryUtil;
import com.liferay.portal.kernel.search.QueryConfig;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.kernel.search.Sort;
import com.liferay.portal.kernel.search.SortFactoryUtil;
import com.liferay.portal.kernel.search.reindexer.ReindexerBridge;
import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
import com.liferay.portal.kernel.service.AddressLocalService;
import com.liferay.portal.kernel.service.EmailAddressLocalService;
import com.liferay.portal.kernel.service.GroupLocalService;
import com.liferay.portal.kernel.service.ListTypeLocalService;
import com.liferay.portal.kernel.service.PasswordPolicyRelLocalService;
import com.liferay.portal.kernel.service.PhoneLocalService;
import com.liferay.portal.kernel.service.ResourceLocalService;
import com.liferay.portal.kernel.service.RoleLocalService;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.UserGroupRoleLocalService;
import com.liferay.portal.kernel.service.UserLocalService;
import com.liferay.portal.kernel.service.WebsiteLocalService;
import com.liferay.portal.kernel.service.persistence.CompanyPersistence;
import com.liferay.portal.kernel.service.persistence.CountryPersistence;
import com.liferay.portal.kernel.service.persistence.GroupPersistence;
import com.liferay.portal.kernel.service.persistence.RegionPersistence;
import com.liferay.portal.kernel.service.persistence.UserFinder;
import com.liferay.portal.kernel.service.persistence.UserPersistence;
import com.liferay.portal.kernel.systemevent.SystemEvent;
import com.liferay.portal.kernel.transaction.TransactionCommitCallbackUtil;
import com.liferay.portal.kernel.tree.TreeModelTasksAdapter;
import com.liferay.portal.kernel.tree.TreePathUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.LinkedHashMapBuilder;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.MapUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.ServiceProxyFactory;
import com.liferay.portal.kernel.util.SetUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.comparator.OrganizationIdComparator;
import com.liferay.portal.kernel.util.comparator.OrganizationNameComparator;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
import com.liferay.portal.util.PropsValues;
import com.liferay.portlet.usersadmin.search.OrganizationUsersSearcher;
import com.liferay.users.admin.kernel.file.uploads.UserFileUploadsSettings;
import com.liferay.users.admin.kernel.organization.types.OrganizationTypesSettings;
import com.liferay.users.admin.kernel.util.UsersAdminUtil;
import com.liferay.util.dao.orm.CustomSQLUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* Provides the local service for accessing, adding, deleting, and updating
* organizations.
*
* @author Brian Wing Shun Chan
* @author Jorge Ferrer
* @author Julio Camarero
* @author Hugo Huijser
* @author Juan Fernández
* @author Marco Leo
*/
public class OrganizationLocalServiceImpl
extends OrganizationLocalServiceBaseImpl {
/**
* Adds the organization to the group.
*
* @param groupId the primary key of the group
* @param organizationId the primary key of the organization
*/
@Override
public void addGroupOrganization(long groupId, long organizationId) {
super.addGroupOrganization(groupId, organizationId);
try {
reindexUsers(organizationId);
}
catch (PortalException portalException) {
throw new SystemException(portalException);
}
}
/**
* Adds the organization to the group.
*
* @param groupId the primary key of the group
* @param organization the organization
*/
@Override
public void addGroupOrganization(long groupId, Organization organization) {
super.addGroupOrganization(groupId, organization);
try {
reindexUsers(organization);
}
catch (PortalException portalException) {
throw new SystemException(portalException);
}
}
/**
* Adds the organizations to the group.
*
* @param groupId the primary key of the group
* @param organizations the organizations
*/
@Override
public void addGroupOrganizations(
long groupId, List organizations) {
super.addGroupOrganizations(groupId, organizations);
try {
reindexUsers(organizations);
}
catch (PortalException portalException) {
throw new SystemException(portalException);
}
}
/**
* Adds the organizations to the group.
*
* @param groupId the primary key of the group
* @param organizationIds the primary keys of the organizations
*/
@Override
public void addGroupOrganizations(long groupId, long[] organizationIds) {
super.addGroupOrganizations(groupId, organizationIds);
try {
reindexUsers(organizationIds);
}
catch (PortalException portalException) {
throw new SystemException(portalException);
}
}
/**
* Adds an organization.
*
*
* This method handles the creation and bookkeeping of the organization
* including its resources, metadata, and internal data structures. It is
* not necessary to make a subsequent call to {@link
* #addOrganizationResources(long, Organization)}.
*
*
* @param userId the primary key of the creator/owner of the organization
* @param parentOrganizationId the primary key of the organization's parent
* organization
* @param name the organization's name
* @param site whether the organization is to be associated with a main
* site
* @return the organization
*/
@Override
public Organization addOrganization(
long userId, long parentOrganizationId, String name, boolean site)
throws PortalException {
String[] types = getTypes();
return addOrganization(
userId, parentOrganizationId, name, types[0], 0, 0,
ListTypeConstants.ORGANIZATION_STATUS_DEFAULT, StringPool.BLANK,
site, null);
}
/**
* Adds an organization.
*
*
* This method handles the creation and bookkeeping of the organization
* including its resources, metadata, and internal data structures. It is
* not necessary to make a subsequent call to {@link
* #addOrganizationResources(long, Organization)}.
*
*
* @param userId the primary key of the creator/owner of the organization
* @param parentOrganizationId the primary key of the organization's parent
* organization
* @param name the organization's name
* @param type the organization's type
* @param regionId the primary key of the organization's region
* @param countryId the primary key of the organization's country
* @param statusListTypeId the organization's workflow status
* @param comments the comments about the organization
* @param site whether the organization is to be associated with a main
* site
* @param serviceContext the service context to be applied (optionally
* null). Can set asset category IDs, asset tag names,
* and expando bridge attributes for the organization.
* @return the organization
*/
@Override
public Organization addOrganization(
long userId, long parentOrganizationId, String name, String type,
long regionId, long countryId, long statusListTypeId,
String comments, boolean site, ServiceContext serviceContext)
throws PortalException {
// Organization
User user = _userPersistence.findByPrimaryKey(userId);
parentOrganizationId = getParentOrganizationId(
user.getCompanyId(), parentOrganizationId);
validate(
user.getCompanyId(), parentOrganizationId, name, type, countryId,
statusListTypeId);
long organizationId = counterLocalService.increment();
Organization organization = organizationPersistence.create(
organizationId);
if (serviceContext != null) {
organization.setUuid(serviceContext.getUuid());
}
organization.setCompanyId(user.getCompanyId());
organization.setUserId(user.getUserId());
organization.setUserName(user.getFullName());
organization.setParentOrganizationId(parentOrganizationId);
organization.setTreePath(organization.buildTreePath());
organization.setName(name);
organization.setType(type);
organization.setRecursable(true);
organization.setRegionId(regionId);
organization.setCountryId(countryId);
organization.setStatusListTypeId(statusListTypeId);
organization.setComments(comments);
organization.setExpandoBridgeAttributes(serviceContext);
organization = organizationPersistence.update(organization);
// Group
long parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
if (parentOrganizationId !=
OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
Organization parentOrganization =
organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
if (parentOrganization != null) {
Group parentGroup = parentOrganization.getGroup();
if (site && parentGroup.isSite()) {
parentGroupId = parentOrganization.getGroupId();
}
}
}
Group group = _groupLocalService.addGroup(
userId, parentGroupId, Organization.class.getName(), organizationId,
GroupConstants.DEFAULT_LIVE_GROUP_ID, getLocalizationMap(name),
null, GroupConstants.TYPE_SITE_PRIVATE, false,
GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null, site, true,
null);
// Role
Role role = _roleLocalService.getRole(
organization.getCompanyId(), RoleConstants.ORGANIZATION_OWNER);
_userGroupRoleLocalService.addUserGroupRoles(
userId, group.getGroupId(), new long[] {role.getRoleId()});
// Resources
addOrganizationResources(userId, organization);
// Asset
if (serviceContext != null) {
updateAsset(
userId, organization, serviceContext.getAssetCategoryIds(),
serviceContext.getAssetTagNames());
}
// Indexer
if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
Indexer indexer =
IndexerRegistryUtil.nullSafeGetIndexer(Organization.class);
indexer.reindex(organization);
}
return organization;
}
/**
* Adds a resource for each type of permission available on the
* organization.
*
* @param userId the primary key of the creator/owner of the organization
* @param organization the organization
*/
@Override
public void addOrganizationResources(long userId, Organization organization)
throws PortalException {
_resourceLocalService.addResources(
organization.getCompanyId(), 0, userId,
Organization.class.getName(), organization.getOrganizationId(),
false, false, false);
}
@Override
public User addOrganizationUserByEmailAddress(
String emailAddress, long organizationId,
ServiceContext serviceContext)
throws PortalException {
Organization organization = organizationPersistence.findByPrimaryKey(
organizationId);
User user = _userLocalService.fetchUserByEmailAddress(
serviceContext.getCompanyId(), emailAddress);
if (user == null) {
Group group = organization.getGroup();
long[] groupIds = {group.getGroupId()};
if (serviceContext.getScopeGroupId() > 0) {
groupIds = ArrayUtil.append(
groupIds, serviceContext.getScopeGroupId());
}
user = _userLocalService.addUserWithWorkflow(
serviceContext.getUserId(), serviceContext.getCompanyId(), true,
StringPool.BLANK, StringPool.BLANK, true, StringPool.BLANK,
emailAddress, serviceContext.getLocale(), emailAddress,
StringPool.BLANK, emailAddress, 0, 0, true, 1, 1, 1970,
StringPool.BLANK, groupIds, null, null, null, true,
serviceContext);
}
addUserOrganization(user.getUserId(), organizationId);
return user;
}
@Override
public Organization addOrUpdateOrganization(
String externalReferenceCode, long userId,
long parentOrganizationId, String name, String type, long regionId,
long countryId, long statusListTypeId, String comments,
boolean hasLogo, byte[] logoBytes, boolean site,
ServiceContext serviceContext)
throws PortalException {
User user = _userLocalService.getUser(userId);
Organization organization = organizationPersistence.fetchByERC_C(
externalReferenceCode, user.getCompanyId());
if (organization == null) {
organization = addOrganization(
userId, parentOrganizationId, name, type, regionId, countryId,
statusListTypeId, comments, site, serviceContext);
organization.setExternalReferenceCode(externalReferenceCode);
PortalUtil.updateImageId(
organization, hasLogo, logoBytes, "logoId",
_userFileUploadsSettings.getImageMaxSize(),
_userFileUploadsSettings.getImageMaxHeight(),
_userFileUploadsSettings.getImageMaxWidth());
organization = organizationPersistence.update(organization);
}
else {
organization = updateOrganization(
user.getCompanyId(), organization.getOrganizationId(),
parentOrganizationId, name, type, regionId, countryId,
statusListTypeId, comments, hasLogo, logoBytes, site,
serviceContext);
}
return organization;
}
/**
* Assigns the password policy to the organizations, removing any other
* currently assigned password policies.
*
* @param passwordPolicyId the primary key of the password policy
* @param organizationIds the primary keys of the organizations
*/
@Override
public void addPasswordPolicyOrganizations(
long passwordPolicyId, long[] organizationIds) {
_passwordPolicyRelLocalService.addPasswordPolicyRels(
passwordPolicyId, Organization.class.getName(), organizationIds);
}
@Override
public void addUserOrganizationByEmailAddress(
String emailAddress, long organizationId)
throws PortalException {
Organization organization = organizationPersistence.findByPrimaryKey(
organizationId);
User user = _userPersistence.findByC_EA(
organization.getCompanyId(), emailAddress);
_userLocalService.addOrganizationUser(organizationId, user);
}
/**
* Deletes the organization's logo.
*
* @param organizationId the primary key of the organization
*/
@Override
public void deleteLogo(long organizationId) throws PortalException {
PortalUtil.updateImageId(
getOrganization(organizationId), false, null, "logoId", 0, 0, 0);
}
/**
* Deletes the organization. The organization's associated resources and
* assets are also deleted.
*
* @param organizationId the primary key of the organization
* @return the deleted organization
*/
@Override
public Organization deleteOrganization(long organizationId)
throws PortalException {
Organization organization = organizationPersistence.findByPrimaryKey(
organizationId);
return organizationLocalService.deleteOrganization(organization);
}
/**
* Deletes the organization. The organization's associated resources and
* assets are also deleted.
*
* @param organization the organization
* @return the deleted organization
*/
@Override
@SystemEvent(type = SystemEventConstants.TYPE_DELETE)
public Organization deleteOrganization(Organization organization)
throws PortalException {
if (!CompanyThreadLocal.isDeleteInProcess()) {
int count1 = organizationPersistence.countByC_P(
organization.getCompanyId(), organization.getOrganizationId());
int count2 = _userFinder.countByKeywords(
organization.getCompanyId(), null,
WorkflowConstants.STATUS_APPROVED,
LinkedHashMapBuilder.put(
"usersOrgs", Long.valueOf(organization.getOrganizationId())
).build());
if ((count1 > 0) || (count2 > 0)) {
throw new RequiredOrganizationException();
}
}
// Asset
_assetEntryLocalService.deleteEntry(
Organization.class.getName(), organization.getOrganizationId());
// Addresses
_addressLocalService.deleteAddresses(
organization.getCompanyId(), Organization.class.getName(),
organization.getOrganizationId());
// Email addresses
_emailAddressLocalService.deleteEmailAddresses(
organization.getCompanyId(), Organization.class.getName(),
organization.getOrganizationId());
// Expando
_expandoRowLocalService.deleteRows(organization.getOrganizationId());
// Password policy relation
_passwordPolicyRelLocalService.deletePasswordPolicyRel(
Organization.class.getName(), organization.getOrganizationId());
// Phone
_phoneLocalService.deletePhones(
organization.getCompanyId(), Organization.class.getName(),
organization.getOrganizationId());
// Website
_websiteLocalService.deleteWebsites(
organization.getCompanyId(), Organization.class.getName(),
organization.getOrganizationId());
// Group
Group group = organization.getGroup();
if (group.isSite()) {
group.setSite(false);
group = _groupPersistence.update(group);
}
_groupLocalService.deleteGroup(group);
// Resources
_resourceLocalService.deleteResource(
organization.getCompanyId(), Organization.class.getName(),
ResourceConstants.SCOPE_INDIVIDUAL,
organization.getOrganizationId());
// Organization
organizationPersistence.remove(organization);
return organization;
}
@Override
public void deleteUserOrganizationByEmailAddress(
String emailAddress, long organizationId)
throws PortalException {
Organization organization = organizationPersistence.findByPrimaryKey(
organizationId);
User user = _userPersistence.findByC_EA(
organization.getCompanyId(), emailAddress);
_userLocalService.unsetOrganizationUsers(
organizationId, new long[] {user.getUserId()});
}
/**
* Returns the organization with the name.
*
* @param companyId the primary key of the organization's company
* @param name the organization's name
* @return the organization with the name, or null if no
* organization could be found
*/
@Override
public Organization fetchOrganization(long companyId, String name) {
return organizationPersistence.fetchByC_N(companyId, name);
}
@Override
public String[] getChildrenTypes(String type) {
return _organizationTypesSettings.getChildrenTypes(type);
}
@Override
public List getGroupUserOrganizations(
long groupId, long userId)
throws PortalException {
long[] groupOrganizationIds =
_groupPersistence.getOrganizationPrimaryKeys(groupId);
if (groupOrganizationIds.length == 0) {
return Collections.emptyList();
}
long[] userOrganizationIds =
_userPersistence.getOrganizationPrimaryKeys(userId);
if (userOrganizationIds.length == 0) {
return Collections.emptyList();
}
Set organizationIds = SetUtil.intersect(
groupOrganizationIds, userOrganizationIds);
if (organizationIds.isEmpty()) {
return Collections.emptyList();
}
List organizations = new ArrayList<>(
organizationIds.size());
for (Long organizationId : organizationIds) {
organizations.add(
organizationPersistence.findByPrimaryKey(organizationId));
}
return organizations;
}
@Override
public List getNoAssetOrganizations() {
return organizationFinder.findO_ByNoAssets();
}
/**
* Returns the organization with the name.
*
* @param companyId the primary key of the organization's company
* @param name the organization's name
* @return the organization with the name
*/
@Override
public Organization getOrganization(long companyId, String name)
throws PortalException {
return organizationPersistence.findByC_N(companyId, name);
}
/**
* Returns the primary key of the organization with the name.
*
* @param companyId the primary key of the organization's company
* @param name the organization's name
* @return the primary key of the organization with the name, or
* 0 if the organization could not be found
*/
@Override
public long getOrganizationId(long companyId, String name) {
Organization organization = organizationPersistence.fetchByC_N(
companyId, name);
if (organization != null) {
return organization.getOrganizationId();
}
return 0;
}
@Override
public List getOrganizations(
long userId, int start, int end,
OrderByComparator orderByComparator)
throws PortalException {
User user = _userPersistence.findByPrimaryKey(userId);
List organizations = ListUtil.copy(
_userPersistence.getOrganizations(userId));
Iterator iterator = organizations.iterator();
while (iterator.hasNext()) {
Organization organization = iterator.next();
if (organization.getCompanyId() != user.getCompanyId()) {
iterator.remove();
}
}
if (organizations.isEmpty()) {
return organizations;
}
if (orderByComparator == null) {
orderByComparator = new OrganizationNameComparator(true);
}
Collections.sort(organizations, orderByComparator);
return ListUtil.subList(organizations, start, end);
}
/**
* Returns all the organizations belonging to the parent organization.
*
* @param companyId the primary key of the organization's company
* @param parentOrganizationId the primary key of the organization's parent
* organization
* @return the organizations belonging to the parent organization
*/
@Override
public List getOrganizations(
long companyId, long parentOrganizationId) {
return getOrganizations(
companyId, parentOrganizationId, QueryUtil.ALL_POS,
QueryUtil.ALL_POS);
}
/**
* Returns a range of all the organizations belonging to the parent
* organization.
*
*
* Useful when paginating results. Returns a maximum of end -
* start instances. start and end are not
* primary keys, they are indexes in the result set. Thus, 0
* refers to the first result in the set. Setting both start
* and end to {@link QueryUtil#ALL_POS} will return the full
* result set.
*
*
* @param companyId the primary key of the organization's company
* @param parentOrganizationId the primary key of the organization's parent
* organization
* @param start the lower bound of the range of organizations to return
* @param end the upper bound of the range of organizations to return (not
* inclusive)
* @return the range of organizations belonging to the parent organization
* @see com.liferay.portal.kernel.service.persistence.OrganizationPersistence#findByC_P(
* long, long, int, int)
*/
@Override
public List getOrganizations(
long companyId, long parentOrganizationId, int start, int end) {
if (parentOrganizationId ==
OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
return organizationPersistence.findByCompanyId(
companyId, start, end);
}
return organizationPersistence.findByC_P(
companyId, parentOrganizationId, start, end);
}
@Override
public List getOrganizations(
long companyId, long parentOrganizationId, String name, int start,
int end) {
if (Validator.isNull(name)) {
return organizationPersistence.findByC_P(
companyId, parentOrganizationId, start, end);
}
return organizationPersistence.findByC_P_LikeN(
companyId, parentOrganizationId, name, start, end);
}
@Override
public List getOrganizations(
long companyId, String treePath) {
return organizationPersistence.findByC_LikeT(companyId, treePath);
}
/**
* Returns the organizations with the primary keys.
*
* @param organizationIds the primary keys of the organizations
* @return the organizations with the primary keys
*/
@Override
public List getOrganizations(long[] organizationIds)
throws PortalException {
List organizations = new ArrayList<>(
organizationIds.length);
for (long organizationId : organizationIds) {
organizations.add(getOrganization(organizationId));
}
return organizations;
}
/**
* Returns all the organizations and users belonging to the parent
* organization.
*
* @param companyId the primary key of the organization and user's company
* @param parentOrganizationId the primary key of the organization and
* user's parent organization
* @param status the user's workflow status
* @param start the lower bound of the range of organizations and users to
* return
* @param end the upper bound of the range of organizations and users to
* return (not inclusive)
* @param orderByComparator the comparator to order the organizations and
* users (optionally null)
* @return the organizations and users belonging to the parent organization
*/
@Override
public List