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.impl;
016    
017    import com.liferay.portal.DuplicateOrganizationException;
018    import com.liferay.portal.OrganizationNameException;
019    import com.liferay.portal.OrganizationParentException;
020    import com.liferay.portal.OrganizationTypeException;
021    import com.liferay.portal.RequiredOrganizationException;
022    import com.liferay.portal.kernel.configuration.Filter;
023    import com.liferay.portal.kernel.dao.orm.QueryUtil;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.search.Hits;
027    import com.liferay.portal.kernel.search.Indexer;
028    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
029    import com.liferay.portal.kernel.search.QueryConfig;
030    import com.liferay.portal.kernel.search.SearchContext;
031    import com.liferay.portal.kernel.search.Sort;
032    import com.liferay.portal.kernel.util.ArrayUtil;
033    import com.liferay.portal.kernel.util.GetterUtil;
034    import com.liferay.portal.kernel.util.ListUtil;
035    import com.liferay.portal.kernel.util.OrderByComparator;
036    import com.liferay.portal.kernel.util.PropsKeys;
037    import com.liferay.portal.kernel.util.StringPool;
038    import com.liferay.portal.kernel.util.StringUtil;
039    import com.liferay.portal.kernel.util.Validator;
040    import com.liferay.portal.kernel.workflow.WorkflowConstants;
041    import com.liferay.portal.model.Company;
042    import com.liferay.portal.model.Group;
043    import com.liferay.portal.model.GroupConstants;
044    import com.liferay.portal.model.LayoutSet;
045    import com.liferay.portal.model.ListTypeConstants;
046    import com.liferay.portal.model.Organization;
047    import com.liferay.portal.model.OrganizationConstants;
048    import com.liferay.portal.model.ResourceConstants;
049    import com.liferay.portal.model.Role;
050    import com.liferay.portal.model.RoleConstants;
051    import com.liferay.portal.model.User;
052    import com.liferay.portal.model.UserGroupRole;
053    import com.liferay.portal.model.impl.OrganizationImpl;
054    import com.liferay.portal.security.permission.PermissionCacheUtil;
055    import com.liferay.portal.service.ServiceContext;
056    import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
057    import com.liferay.portal.util.PropsUtil;
058    import com.liferay.portal.util.PropsValues;
059    import com.liferay.portal.util.comparator.OrganizationNameComparator;
060    
061    import java.io.Serializable;
062    
063    import java.util.ArrayList;
064    import java.util.Collections;
065    import java.util.Date;
066    import java.util.HashMap;
067    import java.util.HashSet;
068    import java.util.Iterator;
069    import java.util.LinkedHashMap;
070    import java.util.List;
071    import java.util.Map;
072    import java.util.Set;
073    
074    /**
075     * Provides the local service for accessing, adding, deleting, and updating
076     * organizations.
077     *
078     * @author Brian Wing Shun Chan
079     * @author Jorge Ferrer
080     * @author Julio Camarero
081     * @author Hugo Huijser
082     * @author Juan Fern??ndez
083     */
084    public class OrganizationLocalServiceImpl
085            extends OrganizationLocalServiceBaseImpl {
086    
087            /**
088             * Adds the organizations to the group.
089             *
090             * @param  groupId the primary key of the group
091             * @param  organizationIds the primary keys of the organizations
092             * @throws PortalException if a group or organization with the primary key
093             *         could not be found
094             * @throws SystemException if a system exception occurred
095             */
096            @Override
097            public void addGroupOrganizations(long groupId, long[] organizationIds)
098                    throws PortalException, SystemException {
099    
100                    groupPersistence.addOrganizations(groupId, organizationIds);
101    
102                    PermissionCacheUtil.clearCache();
103            }
104    
105            /**
106             * Adds an organization.
107             *
108             * <p>
109             * This method handles the creation and bookkeeping of the organization
110             * including its resources, metadata, and internal data structures. It is
111             * not necessary to make a subsequent call to {@link
112             * #addOrganizationResources(long, Organization)}.
113             * </p>
114             *
115             * @param  userId the primary key of the creator/owner of the organization
116             * @param  parentOrganizationId the primary key of the organization's parent
117             *         organization
118             * @param  name the organization's name
119             * @param  site whether the organization is to be associated with a main
120             *         site
121             * @return the organization
122             * @throws PortalException if a creator or parent organization with the
123             *         primary key could not be found or if the organization's
124             *         information was invalid
125             * @throws SystemException if a system exception occurred
126             */
127            @Override
128            public Organization addOrganization(
129                            long userId, long parentOrganizationId, String name, boolean site)
130                    throws PortalException, SystemException {
131    
132                    return addOrganization(
133                            userId, parentOrganizationId, name,
134                            OrganizationConstants.TYPE_REGULAR_ORGANIZATION, 0, 0,
135                            ListTypeConstants.ORGANIZATION_STATUS_DEFAULT, StringPool.BLANK,
136                            site, null);
137            }
138    
139            /**
140             * Adds an organization.
141             *
142             * <p>
143             * This method handles the creation and bookkeeping of the organization
144             * including its resources, metadata, and internal data structures. It is
145             * not necessary to make a subsequent call to {@link
146             * #addOrganizationResources(long, Organization)}.
147             * </p>
148             *
149             * @param      userId the primary key of the creator/owner of the
150             *             organization
151             * @param      parentOrganizationId the primary key of the organization's
152             *             parent organization
153             * @param      name the organization's name
154             * @param      type the organization's type
155             * @param      recursable whether the permissions of the organization are to
156             *             be inherited by its suborganizations
157             * @param      regionId the primary key of the organization's region
158             * @param      countryId the primary key of the organization's country
159             * @param      statusId the organization's workflow status
160             * @param      comments the comments about the organization
161             * @param      site whether the organization is to be associated with a main
162             *             site
163             * @param      serviceContext the service context to be applied (optionally
164             *             <code>null</code>). Can set asset category IDs, asset tag
165             *             names, and expando bridge attributes for the organization.
166             * @return     the organization
167             * @throws     PortalException if a creator or parent organization with the
168             *             primary key could not be found or if the organization's
169             *             information was invalid
170             * @throws     SystemException if a system exception occurred
171             * @deprecated As of 6.2.0, replaced by {@link #addOrganization(long, long,
172             *             String, String, long, long, int, String, boolean,
173             *             ServiceContext)}
174             */
175            @Override
176            public Organization addOrganization(
177                            long userId, long parentOrganizationId, String name, String type,
178                            boolean recursable, long regionId, long countryId, int statusId,
179                            String comments, boolean site, ServiceContext serviceContext)
180                    throws PortalException, SystemException {
181    
182                    return addOrganization(
183                            userId, parentOrganizationId, name, type, regionId, countryId,
184                            statusId, comments, site, serviceContext);
185            }
186    
187            /**
188             * Adds an organization.
189             *
190             * <p>
191             * This method handles the creation and bookkeeping of the organization
192             * including its resources, metadata, and internal data structures. It is
193             * not necessary to make a subsequent call to {@link
194             * #addOrganizationResources(long, Organization)}.
195             * </p>
196             *
197             * @param  userId the primary key of the creator/owner of the organization
198             * @param  parentOrganizationId the primary key of the organization's parent
199             *         organization
200             * @param  name the organization's name
201             * @param  type the organization's type
202             * @param  regionId the primary key of the organization's region
203             * @param  countryId the primary key of the organization's country
204             * @param  statusId the organization's workflow status
205             * @param  comments the comments about the organization
206             * @param  site whether the organization is to be associated with a main
207             *         site
208             * @param  serviceContext the service context to be applied (optionally
209             *         <code>null</code>). Can set asset category IDs, asset tag names,
210             *         and expando bridge attributes for the organization.
211             * @return the organization
212             * @throws PortalException if a creator or parent organization with the
213             *         primary key could not be found or if the organization's
214             *         information was invalid
215             * @throws SystemException if a system exception occurred
216             */
217            @Override
218            public Organization addOrganization(
219                            long userId, long parentOrganizationId, String name, String type,
220                            long regionId, long countryId, int statusId, String comments,
221                            boolean site, ServiceContext serviceContext)
222                    throws PortalException, SystemException {
223    
224                    // Organization
225    
226                    User user = userPersistence.findByPrimaryKey(userId);
227                    parentOrganizationId = getParentOrganizationId(
228                            user.getCompanyId(), parentOrganizationId);
229                    Date now = new Date();
230    
231                    validate(
232                            user.getCompanyId(), parentOrganizationId, name, type, countryId,
233                            statusId);
234    
235                    long organizationId = counterLocalService.increment();
236    
237                    Organization organization = organizationPersistence.create(
238                            organizationId);
239    
240                    if (serviceContext != null) {
241                            organization.setUuid(serviceContext.getUuid());
242                    }
243    
244                    organization.setCompanyId(user.getCompanyId());
245                    organization.setUserId(user.getUserId());
246                    organization.setUserName(user.getFullName());
247    
248                    if (serviceContext != null) {
249                            organization.setCreateDate(serviceContext.getCreateDate(now));
250                            organization.setModifiedDate(serviceContext.getModifiedDate(now));
251                    }
252                    else {
253                            organization.setCreateDate(now);
254                            organization.setModifiedDate(now);
255                    }
256    
257                    organization.setParentOrganizationId(parentOrganizationId);
258                    organization.setTreePath(organization.buildTreePath());
259                    organization.setName(name);
260                    organization.setType(type);
261                    organization.setRecursable(true);
262                    organization.setRegionId(regionId);
263                    organization.setCountryId(countryId);
264                    organization.setStatusId(statusId);
265                    organization.setComments(comments);
266                    organization.setExpandoBridgeAttributes(serviceContext);
267    
268                    organizationPersistence.update(organization);
269    
270                    // Group
271    
272                    long parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
273    
274                    if (parentOrganizationId !=
275                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
276    
277                            Organization parentOrganization =
278                                    organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
279    
280                            if (parentOrganization != null) {
281                                    Group parentGroup = parentOrganization.getGroup();
282    
283                                    if (site && parentGroup.isSite()) {
284                                            parentGroupId = parentOrganization.getGroupId();
285                                    }
286                            }
287                    }
288    
289                    Group group = groupLocalService.addGroup(
290                            userId, parentGroupId, Organization.class.getName(), organizationId,
291                            GroupConstants.DEFAULT_LIVE_GROUP_ID, name, null,
292                            GroupConstants.TYPE_SITE_PRIVATE, false,
293                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null, site, true,
294                            null);
295    
296                    // Role
297    
298                    Role role = roleLocalService.getRole(
299                            organization.getCompanyId(), RoleConstants.ORGANIZATION_OWNER);
300    
301                    userGroupRoleLocalService.addUserGroupRoles(
302                            userId, group.getGroupId(), new long[] {role.getRoleId()});
303    
304                    // Resources
305    
306                    addOrganizationResources(userId, organization);
307    
308                    // Asset
309    
310                    if (serviceContext != null) {
311                            updateAsset(
312                                    userId, organization, serviceContext.getAssetCategoryIds(),
313                                    serviceContext.getAssetTagNames());
314                    }
315    
316                    // Indexer
317    
318                    if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
319                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
320                                    Organization.class);
321    
322                            indexer.reindex(organization);
323                    }
324    
325                    return organization;
326            }
327    
328            /**
329             * Adds a resource for each type of permission available on the
330             * organization.
331             *
332             * @param  userId the primary key of the creator/owner of the organization
333             * @param  organization the organization
334             * @throws PortalException if a portal exception occurred
335             * @throws SystemException if a system exception occurred
336             */
337            @Override
338            public void addOrganizationResources(long userId, Organization organization)
339                    throws PortalException, SystemException {
340    
341                    String name = Organization.class.getName();
342    
343                    resourceLocalService.addResources(
344                            organization.getCompanyId(), 0, userId, name,
345                            organization.getOrganizationId(), false, false, false);
346            }
347    
348            /**
349             * Assigns the password policy to the organizations, removing any other
350             * currently assigned password policies.
351             *
352             * @param  passwordPolicyId the primary key of the password policy
353             * @param  organizationIds the primary keys of the organizations
354             * @throws SystemException if a system exception occurred
355             */
356            @Override
357            public void addPasswordPolicyOrganizations(
358                            long passwordPolicyId, long[] organizationIds)
359                    throws SystemException {
360    
361                    passwordPolicyRelLocalService.addPasswordPolicyRels(
362                            passwordPolicyId, Organization.class.getName(), organizationIds);
363            }
364    
365            /**
366             * Deletes the logo of the organization.
367             *
368             * @param  organizationId the primary key of the organization
369             * @throws PortalException if an organization or parent organization with
370             *         the primary key could not be found or if the organization's logo
371             *         could not be found
372             * @throws SystemException if a system exception occurred
373             */
374            @Override
375            public void deleteLogo(long organizationId)
376                    throws PortalException, SystemException {
377    
378                    Organization organization = getOrganization(organizationId);
379    
380                    Group group = organization.getGroup();
381    
382                    LayoutSet publicLayoutSet = layoutSetLocalService.getLayoutSet(
383                            group.getGroupId(), false);
384    
385                    if (publicLayoutSet.isLogo()) {
386                            long logoId = publicLayoutSet.getLogoId();
387    
388                            publicLayoutSet.setLogo(false);
389                            publicLayoutSet.setLogoId(0);
390    
391                            layoutSetPersistence.update(publicLayoutSet);
392    
393                            imageLocalService.deleteImage(logoId);
394                    }
395    
396                    LayoutSet privateLayoutSet = layoutSetLocalService.getLayoutSet(
397                            group.getGroupId(), true);
398    
399                    if (privateLayoutSet.isLogo()) {
400                            long logoId = privateLayoutSet.getLogoId();
401    
402                            privateLayoutSet.setLogo(false);
403                            privateLayoutSet.setLogoId(0);
404    
405                            layoutSetPersistence.update(privateLayoutSet);
406    
407                            if (imageLocalService.getImage(logoId) != null) {
408                                    imageLocalService.deleteImage(logoId);
409                            }
410                    }
411            }
412    
413            /**
414             * Deletes the organization. The organization's associated resources and
415             * assets are also deleted.
416             *
417             * @param  organizationId the primary key of the organization
418             * @return the deleted organization
419             * @throws PortalException if an organization with the primary key could not
420             *         be found, if the organization had a workflow in approved status,
421             *         or if the organization was a parent organization
422             * @throws SystemException if a system exception occurred
423             */
424            @Override
425            public Organization deleteOrganization(long organizationId)
426                    throws PortalException, SystemException {
427    
428                    Organization organization = organizationPersistence.findByPrimaryKey(
429                            organizationId);
430    
431                    return deleteOrganization(organization);
432            }
433    
434            /**
435             * Deletes the organization. The organization's associated resources and
436             * assets are also deleted.
437             *
438             * @param  organization the organization
439             * @return the deleted organization
440             * @throws PortalException if the organization had a workflow in approved
441             *         status or if the organization was a parent organization
442             * @throws SystemException if a system exception occurred
443             */
444            @Override
445            public Organization deleteOrganization(Organization organization)
446                    throws PortalException, SystemException {
447    
448                    if ((userLocalService.getOrganizationUsersCount(
449                                    organization.getOrganizationId(),
450                                    WorkflowConstants.STATUS_APPROVED) > 0) ||
451                            (organizationPersistence.countByC_P(
452                                    organization.getCompanyId(),
453                                    organization.getOrganizationId()) > 0)) {
454    
455                            throw new RequiredOrganizationException();
456                    }
457    
458                    // Asset
459    
460                    assetEntryLocalService.deleteEntry(
461                            Organization.class.getName(), organization.getOrganizationId());
462    
463                    // Addresses
464    
465                    addressLocalService.deleteAddresses(
466                            organization.getCompanyId(), Organization.class.getName(),
467                            organization.getOrganizationId());
468    
469                    // Email addresses
470    
471                    emailAddressLocalService.deleteEmailAddresses(
472                            organization.getCompanyId(), Organization.class.getName(),
473                            organization.getOrganizationId());
474    
475                    // Expando
476    
477                    expandoRowLocalService.deleteRows(organization.getOrganizationId());
478    
479                    // Password policy relation
480    
481                    passwordPolicyRelLocalService.deletePasswordPolicyRel(
482                            Organization.class.getName(), organization.getOrganizationId());
483    
484                    // Phone
485    
486                    phoneLocalService.deletePhones(
487                            organization.getCompanyId(), Organization.class.getName(),
488                            organization.getOrganizationId());
489    
490                    // Website
491    
492                    websiteLocalService.deleteWebsites(
493                            organization.getCompanyId(), Organization.class.getName(),
494                            organization.getOrganizationId());
495    
496                    // Group
497    
498                    Group group = organization.getGroup();
499    
500                    if (group.isSite()) {
501                            group.setSite(false);
502    
503                            groupPersistence.update(group);
504                    }
505    
506                    groupLocalService.deleteGroup(group);
507    
508                    // Resources
509    
510                    String name = Organization.class.getName();
511    
512                    resourceLocalService.deleteResource(
513                            organization.getCompanyId(), name,
514                            ResourceConstants.SCOPE_INDIVIDUAL,
515                            organization.getOrganizationId());
516    
517                    // Organization
518    
519                    organizationPersistence.remove(organization);
520    
521                    // Permission cache
522    
523                    PermissionCacheUtil.clearCache();
524    
525                    return organization;
526            }
527    
528            @Override
529            public Organization fetchOrganization(long companyId, String name)
530                    throws SystemException {
531    
532                    return organizationPersistence.fetchByC_N(companyId, name);
533            }
534    
535            @Override
536            public List<Organization> getNoAssetOrganizations() throws SystemException {
537                    return organizationFinder.findByNoAssets();
538            }
539    
540            /**
541             * Returns the organization with the name.
542             *
543             * @param  companyId the primary key of the organization's company
544             * @param  name the organization's name
545             * @return the organization with the name
546             * @throws PortalException if the organization with the name could not be
547             *         found
548             * @throws SystemException if a system exception occurred
549             */
550            @Override
551            public Organization getOrganization(long companyId, String name)
552                    throws PortalException, SystemException {
553    
554                    return organizationPersistence.findByC_N(companyId, name);
555            }
556    
557            /**
558             * Returns the primary key of the organization with the name.
559             *
560             * @param  companyId the primary key of the organization's company
561             * @param  name the organization's name
562             * @return the primary key of the organization with the name, or
563             *         <code>0</code> if the organization could not be found
564             * @throws SystemException if a system exception occurred
565             */
566            @Override
567            public long getOrganizationId(long companyId, String name)
568                    throws SystemException {
569    
570                    Organization organization = organizationPersistence.fetchByC_N(
571                            companyId, name);
572    
573                    if (organization != null) {
574                            return organization.getOrganizationId();
575                    }
576                    else {
577                            return 0;
578                    }
579            }
580    
581            @Override
582            public List<Organization> getOrganizations(
583                            long userId, int start, int end, OrderByComparator obc)
584                    throws PortalException, SystemException {
585    
586                    User user = userPersistence.findByPrimaryKey(userId);
587    
588                    List<Organization> organizations = ListUtil.copy(
589                            userPersistence.getOrganizations(userId));
590    
591                    Iterator<Organization> iterator = organizations.iterator();
592    
593                    while (iterator.hasNext()) {
594                            Organization organization = iterator.next();
595    
596                            if (organization.getCompanyId() != user.getCompanyId()) {
597                                    iterator.remove();
598                            }
599                    }
600    
601                    if (organizations.isEmpty()) {
602                            return organizations;
603                    }
604    
605                    if (obc == null) {
606                            obc = new OrganizationNameComparator(true);
607                    }
608    
609                    Collections.sort(organizations, obc);
610    
611                    return ListUtil.subList(organizations, start, end);
612            }
613    
614            /**
615             * Returns all the organizations belonging to the parent organization.
616             *
617             * @param  companyId the primary key of the organization's company
618             * @param  parentOrganizationId the primary key of the organization's parent
619             *         organization
620             * @return the organizations belonging to the parent organization
621             * @throws SystemException if a system exception occurred
622             */
623            @Override
624            public List<Organization> getOrganizations(
625                            long companyId, long parentOrganizationId)
626                    throws SystemException {
627    
628                    return getOrganizations(
629                            companyId, parentOrganizationId, QueryUtil.ALL_POS,
630                            QueryUtil.ALL_POS);
631            }
632    
633            /**
634             * Returns a range of all the organizations belonging to the parent
635             * organization.
636             *
637             * <p>
638             * Useful when paginating results. Returns a maximum of <code>end -
639             * start</code> instances. <code>start</code> and <code>end</code> are not
640             * primary keys, they are indexes in the result set. Thus, <code>0</code>
641             * refers to the first result in the set. Setting both <code>start</code>
642             * and <code>end</code> to {@link
643             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
644             * result set.
645             * </p>
646             *
647             * @param  companyId the primary key of the organization's company
648             * @param  parentOrganizationId the primary key of the organization's parent
649             *         organization
650             * @param  start the lower bound of the range of organizations to return
651             * @param  end the upper bound of the range of organizations to return (not
652             *         inclusive)
653             * @return the range of organizations belonging to the parent organization
654             * @throws SystemException if a system exception occurred
655             * @see    com.liferay.portal.service.persistence.OrganizationPersistence#findByC_P(
656             *         long, long, int, int)
657             */
658            @Override
659            public List<Organization> getOrganizations(
660                            long companyId, long parentOrganizationId, int start, int end)
661                    throws SystemException {
662    
663                    if (parentOrganizationId ==
664                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
665    
666                            return organizationPersistence.findByCompanyId(
667                                    companyId, start, end);
668                    }
669                    else {
670                            return organizationPersistence.findByC_P(
671                                    companyId, parentOrganizationId, start, end);
672                    }
673            }
674    
675            /**
676             * Returns the organizations with the primary keys.
677             *
678             * @param  organizationIds the primary keys of the organizations
679             * @return the organizations with the primary keys
680             * @throws PortalException if any one of the organizations could not be
681             *         found
682             * @throws SystemException if a system exception occurred
683             */
684            @Override
685            public List<Organization> getOrganizations(long[] organizationIds)
686                    throws PortalException, SystemException {
687    
688                    List<Organization> organizations = new ArrayList<Organization>(
689                            organizationIds.length);
690    
691                    for (long organizationId : organizationIds) {
692                            Organization organization = getOrganization(organizationId);
693    
694                            organizations.add(organization);
695                    }
696    
697                    return organizations;
698            }
699    
700            /**
701             * Returns the number of organizations belonging to the parent organization.
702             *
703             * @param  companyId the primary key of the organization's company
704             * @param  parentOrganizationId the primary key of the organization's parent
705             *         organization
706             * @return the number of organizations belonging to the parent organization
707             * @throws SystemException if a system exception occurred
708             */
709            @Override
710            public int getOrganizationsCount(long companyId, long parentOrganizationId)
711                    throws SystemException {
712    
713                    if (parentOrganizationId ==
714                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
715    
716                            return organizationPersistence.countByCompanyId(companyId);
717                    }
718                    else {
719                            return organizationPersistence.countByC_P(
720                                    companyId, parentOrganizationId);
721                    }
722            }
723    
724            /**
725             * Returns the parent organizations in order by closest ancestor. The list
726             * starts with the organization itself.
727             *
728             * @param  organizationId the primary key of the organization
729             * @return the parent organizations in order by closest ancestor
730             * @throws PortalException if an organization with the primary key could not
731             *         be found
732             * @throws SystemException if a system exception occurred
733             */
734            @Override
735            public List<Organization> getParentOrganizations(long organizationId)
736                    throws PortalException, SystemException {
737    
738                    if (organizationId ==
739                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
740    
741                            return new ArrayList<Organization>();
742                    }
743    
744                    Organization organization = organizationPersistence.findByPrimaryKey(
745                            organizationId);
746    
747                    return organization.getAncestors();
748            }
749    
750            /**
751             * Returns the suborganizations of the organizations.
752             *
753             * @param  organizations the organizations from which to get
754             *         suborganizations
755             * @return the suborganizations of the organizations
756             * @throws SystemException if a system exception occurred
757             */
758            @Override
759            public List<Organization> getSuborganizations(
760                            List<Organization> organizations)
761                    throws SystemException {
762    
763                    List<Organization> allSuborganizations = new ArrayList<Organization>();
764    
765                    for (int i = 0; i < organizations.size(); i++) {
766                            Organization organization = organizations.get(i);
767    
768                            List<Organization> suborganizations =
769                                    organizationPersistence.findByC_P(
770                                            organization.getCompanyId(),
771                                            organization.getOrganizationId());
772    
773                            addSuborganizations(allSuborganizations, suborganizations);
774                    }
775    
776                    return allSuborganizations;
777            }
778    
779            /**
780             * Returns the suborganizations of the organization.
781             *
782             * @param  companyId the primary key of the organization's company
783             * @param  organizationId the primary key of the organization
784             * @return the suborganizations of the organization
785             * @throws SystemException if a system exception occurred
786             */
787            @Override
788            public List<Organization> getSuborganizations(
789                            long companyId, long organizationId)
790                    throws SystemException {
791    
792                    return organizationPersistence.findByC_P(companyId, organizationId);
793            }
794    
795            /**
796             * Returns the count of suborganizations of the organization.
797             *
798             * @param  companyId the primary key of the organization's company
799             * @param  organizationId the primary key of the organization
800             * @return the count of suborganizations of the organization
801             * @throws SystemException if a system exception occurred
802             */
803            @Override
804            public int getSuborganizationsCount(long companyId, long organizationId)
805                    throws SystemException {
806    
807                    return organizationPersistence.countByC_P(companyId, organizationId);
808            }
809    
810            /**
811             * Returns the intersection of <code>allOrganizations</code> and
812             * <code>availableOrganizations</code>.
813             *
814             * @param  allOrganizations the organizations to check for availability
815             * @param  availableOrganizations the available organizations
816             * @return the intersection of <code>allOrganizations</code> and
817             *         <code>availableOrganizations</code>
818             */
819            @Override
820            public List<Organization> getSubsetOrganizations(
821                    List<Organization> allOrganizations,
822                    List<Organization> availableOrganizations) {
823    
824                    List<Organization> subsetOrganizations = new ArrayList<Organization>();
825    
826                    for (Organization organization : allOrganizations) {
827                            if (availableOrganizations.contains(organization)) {
828                                    subsetOrganizations.add(organization);
829                            }
830                    }
831    
832                    return subsetOrganizations;
833            }
834    
835            /**
836             * Returns all the organizations associated with the user. If
837             * includeAdministrative is <code>true</code>, the result includes those
838             * organizations that are not directly associated to the user but he is an
839             * administrator or an owner of the organization.
840             *
841             * @param  userId the primary key of the user
842             * @param  includeAdministrative whether to includes organizations that are
843             *         indirectly associated to the user because he is an administrator
844             *         or an owner of the organization
845             * @return the organizations associated with the user
846             * @throws PortalException if a user with the primary key could not be found
847             * @throws SystemException if a system exception occurred
848             */
849            @Override
850            public List<Organization> getUserOrganizations(
851                            long userId, boolean includeAdministrative)
852                    throws PortalException, SystemException {
853    
854                    if (!includeAdministrative) {
855                            return getUserOrganizations(userId);
856                    }
857    
858                    Set<Organization> organizations = new HashSet<Organization>();
859    
860                    List<UserGroupRole> userGroupRoles =
861                            userGroupRoleLocalService.getUserGroupRoles(userId);
862    
863                    for (UserGroupRole userGroupRole : userGroupRoles) {
864                            Role role = userGroupRole.getRole();
865    
866                            String roleName = role.getName();
867    
868                            if (roleName.equals(RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
869                                    roleName.equals(RoleConstants.ORGANIZATION_OWNER)) {
870    
871                                    Group group = userGroupRole.getGroup();
872    
873                                    Organization organization =
874                                            organizationPersistence.findByPrimaryKey(
875                                                    group.getOrganizationId());
876    
877                                    organizations.add(organization);
878                            }
879                    }
880    
881                    organizations.addAll(getUserOrganizations(userId));
882    
883                    return new ArrayList<Organization>(organizations);
884            }
885    
886            /**
887             * Returns <code>true</code> if the password policy has been assigned to the
888             * organization.
889             *
890             * @param  passwordPolicyId the primary key of the password policy
891             * @param  organizationId the primary key of the organization
892             * @return <code>true</code> if the password policy has been assigned to the
893             *         organization; <code>false</code> otherwise
894             * @throws SystemException if a system exception occurred
895             */
896            @Override
897            public boolean hasPasswordPolicyOrganization(
898                            long passwordPolicyId, long organizationId)
899                    throws SystemException {
900    
901                    return passwordPolicyRelLocalService.hasPasswordPolicyRel(
902                            passwordPolicyId, Organization.class.getName(), organizationId);
903            }
904    
905            /**
906             * Returns <code>true</code> if the user is a member of the organization,
907             * optionally focusing on suborganizations or the specified organization.
908             * This method is usually called to determine if the user has view access to
909             * a resource belonging to the organization.
910             *
911             * <ol>
912             * <li>
913             * If <code>inheritSuborganizations=<code>false</code></code>:
914             * the method checks whether the user belongs to the organization specified
915             * by <code>organizationId</code>. The parameter
916             * <code>includeSpecifiedOrganization</code> is ignored.
917             * </li>
918             * <li>
919             * The parameter <code>includeSpecifiedOrganization</code> is
920             * ignored unless <code>inheritSuborganizations</code> is also
921             * <code>true</code>.
922             * </li>
923             * <li>
924             * If <code>inheritSuborganizations=<code>true</code></code> and
925             * <code>includeSpecifiedOrganization=<code>false</code></code>: the method
926             * checks
927             * whether the user belongs to one of the child organizations of the one
928             * specified by <code>organizationId</code>.
929             * </li>
930             * <li>
931             * If <code>inheritSuborganizations=<code>true</code></code> and
932             * <code>includeSpecifiedOrganization=<code>true</code></code>: the method
933             * checks whether
934             * the user belongs to the organization specified by
935             * <code>organizationId</code> or any of
936             * its child organizations.
937             * </li>
938             * </ol>
939             *
940             * @param  userId the primary key of the organization's user
941             * @param  organizationId the primary key of the organization
942             * @param  inheritSuborganizations if <code>true</code> suborganizations are
943             *         considered in the determination
944             * @param  includeSpecifiedOrganization if <code>true</code> the
945             *         organization specified by <code>organizationId</code> is
946             *         considered in the determination
947             * @return <code>true</code> if the user has access to the organization;
948             *         <code>false</code> otherwise
949             * @throws PortalException if an organization with the primary key could not
950             *         be found
951             * @throws SystemException if a system exception occurred
952             * @see    com.liferay.portal.service.persistence.OrganizationFinder
953             */
954            @Override
955            public boolean hasUserOrganization(
956                            long userId, long organizationId, boolean inheritSuborganizations,
957                            boolean includeSpecifiedOrganization)
958                    throws PortalException, SystemException {
959    
960                    if (!inheritSuborganizations) {
961                            return userPersistence.containsOrganization(userId, organizationId);
962                    }
963    
964                    LinkedHashMap<String, Object> params =
965                            new LinkedHashMap<String, Object>();
966    
967                    List<Organization> organizationsTree = new ArrayList<Organization>();
968    
969                    Organization organization = organizationPersistence.findByPrimaryKey(
970                            organizationId);
971    
972                    if (!includeSpecifiedOrganization) {
973                            organizationsTree.add(organization);
974                    }
975                    else {
976                            organizationsTree.add(organization.getParentOrganization());
977                    }
978    
979                    params.put("usersOrgsTree", organizationsTree);
980    
981                    if (userFinder.countByUser(userId, params) > 0) {
982                            return true;
983                    }
984    
985                    return false;
986            }
987    
988            /**
989             * Rebuilds the organizations tree.
990             *
991             * <p>
992             * Only call this method if the tree has become stale through operations
993             * other than normal CRUD. Under normal circumstances the tree is
994             * automatically rebuilt whenever necessary.
995             * </p>
996             *
997             * @param  companyId the primary key of the organization's company
998             * @throws PortalException if an organization with the primary key could not
999             *         be found
1000             * @throws SystemException if a system exception occurred
1001             */
1002            @Override
1003            public void rebuildTree(long companyId)
1004                    throws PortalException, SystemException {
1005    
1006                    List<Organization> organizations =
1007                            organizationPersistence.findByCompanyId(companyId);
1008    
1009                    for (Organization organization : organizations) {
1010                            organization.setTreePath(organization.buildTreePath());
1011    
1012                            organizationPersistence.update(organization);
1013                    }
1014            }
1015    
1016            /**
1017             * Returns a range of all the organizations of the company.
1018             *
1019             * <p>
1020             * Useful when paginating results. Returns a maximum of <code>end -
1021             * start</code> instances. <code>start</code> and <code>end</code> are not
1022             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1023             * refers to the first result in the set. Setting both <code>start</code>
1024             * and <code>end</code> to {@link
1025             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1026             * result set.
1027             * </p>
1028             *
1029             * @param  companyId the primary key of the company
1030             * @param  params the finder parameters (optionally <code>null</code>). For
1031             *         more information see {@link
1032             *         com.liferay.portlet.usersadmin.util.OrganizationIndexer}
1033             * @param  start the lower bound of the range of organizations to return
1034             * @param  end the upper bound of the range of organizations to return (not
1035             *         inclusive)
1036             * @return the range of all the organizations of the company
1037             * @throws SystemException if a system exception occurred
1038             */
1039            @Override
1040            public List<Organization> search(
1041                            long companyId, LinkedHashMap<String, Object> params, int start,
1042                            int end)
1043                    throws SystemException {
1044    
1045                    return organizationFinder.findByCompanyId(
1046                            companyId, params, start, end,
1047                            new OrganizationNameComparator(true));
1048            }
1049    
1050            /**
1051             * Returns an ordered range of all the organizations that match the
1052             * keywords, using the indexer. It is preferable to use this method instead
1053             * of the non-indexed version whenever possible for performance reasons.
1054             *
1055             * <p>
1056             * Useful when paginating results. Returns a maximum of <code>end -
1057             * start</code> instances. <code>start</code> and <code>end</code> are not
1058             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1059             * refers to the first result in the set. Setting both <code>start</code>
1060             * and <code>end</code> to {@link
1061             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1062             * result set.
1063             * </p>
1064             *
1065             * @param  companyId the primary key of the organization's company
1066             * @param  parentOrganizationId the primary key of the organization's parent
1067             *         organization
1068             * @param  keywords the keywords (space separated), which may occur in the
1069             *         organization's name, street, city, zipcode, type, region or
1070             *         country (optionally <code>null</code>)
1071             * @param  params the finder parameters (optionally <code>null</code>). For
1072             *         more information see {@link
1073             *         com.liferay.portlet.usersadmin.util.OrganizationIndexer}
1074             * @param  start the lower bound of the range of organizations to return
1075             * @param  end the upper bound of the range of organizations to return (not
1076             *         inclusive)
1077             * @param  sort the field and direction by which to sort (optionally
1078             *         <code>null</code>)
1079             * @return the matching organizations ordered by name
1080             * @throws SystemException if a system exception occurred
1081             * @see    com.liferay.portlet.usersadmin.util.OrganizationIndexer
1082             */
1083            @Override
1084            public Hits search(
1085                            long companyId, long parentOrganizationId, String keywords,
1086                            LinkedHashMap<String, Object> params, int start, int end, Sort sort)
1087                    throws SystemException {
1088    
1089                    String name = null;
1090                    String type = null;
1091                    String street = null;
1092                    String city = null;
1093                    String zip = null;
1094                    String region = null;
1095                    String country = null;
1096                    boolean andOperator = false;
1097    
1098                    if (Validator.isNotNull(keywords)) {
1099                            name = keywords;
1100                            type = keywords;
1101                            street = keywords;
1102                            city = keywords;
1103                            zip = keywords;
1104                            region = keywords;
1105                            country = keywords;
1106                    }
1107                    else {
1108                            andOperator = true;
1109                    }
1110    
1111                    if (params != null) {
1112                            params.put("keywords", keywords);
1113                    }
1114    
1115                    return search(
1116                            companyId, parentOrganizationId, name, type, street, city, zip,
1117                            region, country, params, andOperator, start, end, sort);
1118            }
1119    
1120            /**
1121             * Returns a name ordered range of all the organizations that match the
1122             * keywords, type, region, and country, without using the indexer. It is
1123             * preferable to use the indexed version {@link #search(long, long, String,
1124             * LinkedHashMap, int, int, Sort)} instead of this method wherever possible
1125             * for performance reasons.
1126             *
1127             * <p>
1128             * Useful when paginating results. Returns a maximum of <code>end -
1129             * start</code> instances. <code>start</code> and <code>end</code> are not
1130             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1131             * refers to the first result in the set. Setting both <code>start</code>
1132             * and <code>end</code> to {@link
1133             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1134             * result set.
1135             * </p>
1136             *
1137             * @param  companyId the primary key of the organization's company
1138             * @param  parentOrganizationId the primary key of the organization's parent
1139             *         organization
1140             * @param  keywords the keywords (space separated), which may occur in the
1141             *         organization's name, street, city, or zipcode (optionally
1142             *         <code>null</code>)
1143             * @param  type the organization's type (optionally <code>null</code>)
1144             * @param  regionId the primary key of the organization's region (optionally
1145             *         <code>null</code>)
1146             * @param  countryId the primary key of the organization's country
1147             *         (optionally <code>null</code>)
1148             * @param  params the finder params. For more information see {@link
1149             *         com.liferay.portal.service.persistence.OrganizationFinder}
1150             * @param  start the lower bound of the range of organizations to return
1151             * @param  end the upper bound of the range of organizations to return (not
1152             *         inclusive)
1153             * @return the matching organizations ordered by name
1154             * @throws SystemException if a system exception occurred
1155             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1156             */
1157            @Override
1158            public List<Organization> search(
1159                            long companyId, long parentOrganizationId, String keywords,
1160                            String type, Long regionId, Long countryId,
1161                            LinkedHashMap<String, Object> params, int start, int end)
1162                    throws SystemException {
1163    
1164                    return search(
1165                            companyId, parentOrganizationId, keywords, type, regionId,
1166                            countryId, params, start, end,
1167                            new OrganizationNameComparator(true));
1168            }
1169    
1170            /**
1171             * Returns an ordered range of all the organizations that match the
1172             * keywords, type, region, and country, without using the indexer. It is
1173             * preferable to use the indexed version {@link #search(long, long, String,
1174             * String, String, String, String, String, String, LinkedHashMap, boolean,
1175             * int, int, Sort)} instead of this method wherever possible for performance
1176             * reasons.
1177             *
1178             * <p>
1179             * Useful when paginating results. Returns a maximum of <code>end -
1180             * start</code> instances. <code>start</code> and <code>end</code> are not
1181             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1182             * refers to the first result in the set. Setting both <code>start</code>
1183             * and <code>end</code> to {@link
1184             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1185             * result set.
1186             * </p>
1187             *
1188             * @param  companyId the primary key of the organization's company
1189             * @param  parentOrganizationId the primary key of the organization's parent
1190             *         organization
1191             * @param  keywords the keywords (space separated), which may occur in the
1192             *         organization's name, street, city, or zipcode (optionally
1193             *         <code>null</code>)
1194             * @param  type the organization's type (optionally <code>null</code>)
1195             * @param  regionId the primary key of the organization's region (optionally
1196             *         <code>null</code>)
1197             * @param  countryId the primary key of the organization's country
1198             *         (optionally <code>null</code>)
1199             * @param  params the finder params. For more information see {@link
1200             *         com.liferay.portal.service.persistence.OrganizationFinder}
1201             * @param  start the lower bound of the range of organizations to return
1202             * @param  end the upper bound of the range of organizations to return (not
1203             *         inclusive)
1204             * @param  obc the comparator to order the organizations (optionally
1205             *         <code>null</code>)
1206             * @return the matching organizations ordered by comparator <code>obc</code>
1207             * @throws SystemException if a system exception occurred
1208             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1209             */
1210            @Override
1211            public List<Organization> search(
1212                            long companyId, long parentOrganizationId, String keywords,
1213                            String type, Long regionId, Long countryId,
1214                            LinkedHashMap<String, Object> params, int start, int end,
1215                            OrderByComparator obc)
1216                    throws SystemException {
1217    
1218                    String parentOrganizationIdComparator = StringPool.EQUAL;
1219    
1220                    if (parentOrganizationId ==
1221                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1222    
1223                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1224                    }
1225    
1226                    return organizationFinder.findByKeywords(
1227                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1228                            keywords, type, regionId, countryId, params, start, end, obc);
1229            }
1230    
1231            /**
1232             * Returns a name ordered range of all the organizations with the type,
1233             * region, and country, and whose name, street, city, and zipcode match the
1234             * keywords specified for them, without using the indexer. It is preferable
1235             * to use the indexed version {@link #search(long, long, String, String,
1236             * String, String, String, String, String, LinkedHashMap, boolean, int, int,
1237             * Sort)} instead of this method wherever possible for performance reasons.
1238             *
1239             * <p>
1240             * Useful when paginating results. Returns a maximum of <code>end -
1241             * start</code> instances. <code>start</code> and <code>end</code> are not
1242             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1243             * refers to the first result in the set. Setting both <code>start</code>
1244             * and <code>end</code> to {@link
1245             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1246             * result set.
1247             * </p>
1248             *
1249             * @param  companyId the primary key of the organization's company
1250             * @param  parentOrganizationId the primary key of the organization's parent
1251             * @param  name the name keywords (space separated, optionally
1252             *         <code>null</code>)
1253             * @param  type the organization's type (optionally <code>null</code>)
1254             * @param  street the street keywords (optionally <code>null</code>)
1255             * @param  city the city keywords (optionally <code>null</code>)
1256             * @param  zip the zipcode keywords (optionally <code>null</code>)
1257             * @param  regionId the primary key of the organization's region (optionally
1258             *         <code>null</code>)
1259             * @param  countryId the primary key of the organization's country
1260             *         (optionally <code>null</code>)
1261             * @param  params the finder parameters (optionally <code>null</code>). For
1262             *         more information see {@link
1263             *         com.liferay.portal.service.persistence.OrganizationFinder}
1264             * @param  andOperator whether every field must match its keywords, or just
1265             *         one field. For example, &quot;organizations with the name
1266             *         'Employees' and city 'Chicago'&quot; vs &quot;organizations with
1267             *         the name 'Employees' or the city 'Chicago'&quot;.
1268             * @param  start the lower bound of the range of organizations to return
1269             * @param  end the upper bound of the range of organizations to return (not
1270             *         inclusive)
1271             * @return the matching organizations ordered by name
1272             * @throws SystemException if a system exception occurred
1273             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1274             */
1275            @Override
1276            public List<Organization> search(
1277                            long companyId, long parentOrganizationId, String name, String type,
1278                            String street, String city, String zip, Long regionId,
1279                            Long countryId, LinkedHashMap<String, Object> params,
1280                            boolean andOperator, int start, int end)
1281                    throws SystemException {
1282    
1283                    return search(
1284                            companyId, parentOrganizationId, name, type, street, city, zip,
1285                            regionId, countryId, params, andOperator, start, end,
1286                            new OrganizationNameComparator(true));
1287            }
1288    
1289            /**
1290             * Returns an ordered range of all the organizations with the type, region,
1291             * and country, and whose name, street, city, and zipcode match the keywords
1292             * specified for them, without using the indexer. It is preferable to use
1293             * the indexed version {@link #search(long, long, String, String, String,
1294             * String, String, String, String, LinkedHashMap, boolean, int, int, Sort)}
1295             * instead of this method wherever possible for performance reasons.
1296             *
1297             * <p>
1298             * Useful when paginating results. Returns a maximum of <code>end -
1299             * start</code> instances. <code>start</code> and <code>end</code> are not
1300             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1301             * refers to the first result in the set. Setting both <code>start</code>
1302             * and <code>end</code> to {@link
1303             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1304             * result set.
1305             * </p>
1306             *
1307             * @param  companyId the primary key of the organization's company
1308             * @param  parentOrganizationId the primary key of the organization's parent
1309             *         organization
1310             * @param  name the name keywords (space separated, optionally
1311             *         <code>null</code>)
1312             * @param  type the organization's type (optionally <code>null</code>)
1313             * @param  street the street keywords (optionally <code>null</code>)
1314             * @param  city the city keywords (optionally <code>null</code>)
1315             * @param  zip the zipcode keywords (optionally <code>null</code>)
1316             * @param  regionId the primary key of the organization's region (optionally
1317             *         <code>null</code>)
1318             * @param  countryId the primary key of the organization's country
1319             *         (optionally <code>null</code>)
1320             * @param  params the finder parameters (optionally <code>null</code>). For
1321             *         more information see {@link
1322             *         com.liferay.portal.service.persistence.OrganizationFinder}
1323             * @param  andOperator whether every field must match its keywords, or just
1324             *         one field. For example, &quot;organizations with the name
1325             *         'Employees' and city 'Chicago'&quot; vs &quot;organizations with
1326             *         the name 'Employees' or the city 'Chicago'&quot;.
1327             * @param  start the lower bound of the range of organizations to return
1328             * @param  end the upper bound of the range of organizations to return (not
1329             *         inclusive)
1330             * @param  obc the comparator to order the organizations (optionally
1331             *         <code>null</code>)
1332             * @return the matching organizations ordered by comparator <code>obc</code>
1333             * @throws SystemException if a system exception occurred
1334             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1335             */
1336            @Override
1337            public List<Organization> search(
1338                            long companyId, long parentOrganizationId, String name, String type,
1339                            String street, String city, String zip, Long regionId,
1340                            Long countryId, LinkedHashMap<String, Object> params,
1341                            boolean andOperator, int start, int end, OrderByComparator obc)
1342                    throws SystemException {
1343    
1344                    String parentOrganizationIdComparator = StringPool.EQUAL;
1345    
1346                    if (parentOrganizationId ==
1347                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1348    
1349                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1350                    }
1351    
1352                    return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
1353                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1354                            name, type, street, city, zip, regionId, countryId, params,
1355                            andOperator, start, end, obc);
1356            }
1357    
1358            /**
1359             * Returns an ordered range of all the organizations whose name, type, or
1360             * location fields match the keywords specified for them, using the indexer.
1361             * It is preferable to use this method instead of the non-indexed version
1362             * whenever possible for performance reasons.
1363             *
1364             * <p>
1365             * Useful when paginating results. Returns a maximum of <code>end -
1366             * start</code> instances. <code>start</code> and <code>end</code> are not
1367             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1368             * refers to the first result in the set. Setting both <code>start</code>
1369             * and <code>end</code> to {@link
1370             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1371             * result set.
1372             * </p>
1373             *
1374             * @param  companyId the primary key of the organization's company
1375             * @param  parentOrganizationId the primary key of the organization's parent
1376             *         organization
1377             * @param  name the name keywords (space separated, optionally
1378             *         <code>null</code>)
1379             * @param  type the type keywords (optionally <code>null</code>)
1380             * @param  street the street keywords (optionally <code>null</code>)
1381             * @param  city the city keywords (optionally <code>null</code>)
1382             * @param  zip the zipcode keywords (optionally <code>null</code>)
1383             * @param  region the region keywords (optionally <code>null</code>)
1384             * @param  country the country keywords (optionally <code>null</code>)
1385             * @param  params the finder parameters (optionally <code>null</code>). For
1386             *         more information see {@link
1387             *         com.liferay.portlet.usersadmin.util.OrganizationIndexer}.
1388             * @param  andSearch whether every field must match its keywords or just one
1389             *         field
1390             * @param  start the lower bound of the range of organizations to return
1391             * @param  end the upper bound of the range of organizations to return (not
1392             *         inclusive)
1393             * @param  sort the field and direction by which to sort (optionally
1394             *         <code>null</code>)
1395             * @return the matching organizations ordered by <code>sort</code>
1396             * @throws SystemException if a system exception occurred
1397             * @see    com.liferay.portlet.usersadmin.util.OrganizationIndexer
1398             */
1399            @Override
1400            public Hits search(
1401                            long companyId, long parentOrganizationId, String name, String type,
1402                            String street, String city, String zip, String region,
1403                            String country, LinkedHashMap<String, Object> params,
1404                            boolean andSearch, int start, int end, Sort sort)
1405                    throws SystemException {
1406    
1407                    try {
1408                            SearchContext searchContext = new SearchContext();
1409    
1410                            searchContext.setAndSearch(andSearch);
1411    
1412                            Map<String, Serializable> attributes =
1413                                    new HashMap<String, Serializable>();
1414    
1415                            attributes.put("city", city);
1416                            attributes.put("country", country);
1417                            attributes.put("name", name);
1418                            attributes.put("params", params);
1419                            attributes.put(
1420                                    "parentOrganizationId", String.valueOf(parentOrganizationId));
1421                            attributes.put("region", region);
1422                            attributes.put("street", street);
1423                            attributes.put("type", type);
1424                            attributes.put("zip", zip);
1425    
1426                            searchContext.setAttributes(attributes);
1427    
1428                            searchContext.setCompanyId(companyId);
1429                            searchContext.setEnd(end);
1430    
1431                            if (params != null) {
1432                                    String keywords = (String)params.remove("keywords");
1433    
1434                                    if (Validator.isNotNull(keywords)) {
1435                                            searchContext.setKeywords(keywords);
1436                                    }
1437                            }
1438    
1439                            QueryConfig queryConfig = new QueryConfig();
1440    
1441                            queryConfig.setHighlightEnabled(false);
1442                            queryConfig.setScoreEnabled(false);
1443    
1444                            searchContext.setQueryConfig(queryConfig);
1445    
1446                            if (sort != null) {
1447                                    searchContext.setSorts(sort);
1448                            }
1449    
1450                            searchContext.setStart(start);
1451    
1452                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1453                                    Organization.class);
1454    
1455                            return indexer.search(searchContext);
1456                    }
1457                    catch (Exception e) {
1458                            throw new SystemException(e);
1459                    }
1460            }
1461    
1462            /**
1463             * Returns the number of organizations that match the keywords, type,
1464             * region, and country.
1465             *
1466             * @param  companyId the primary key of the organization's company
1467             * @param  parentOrganizationId the primary key of the organization's parent
1468             *         organization
1469             * @param  keywords the keywords (space separated), which may occur in the
1470             *         organization's name, street, city, or zipcode (optionally
1471             *         <code>null</code>)
1472             * @param  type the organization's type (optionally <code>null</code>)
1473             * @param  regionId the primary key of the organization's region (optionally
1474             *         <code>null</code>)
1475             * @param  countryId the primary key of the organization's country
1476             *         (optionally <code>null</code>)
1477             * @param  params the finder parameters (optionally <code>null</code>). For
1478             *         more information see {@link
1479             *         com.liferay.portal.service.persistence.OrganizationFinder}
1480             * @return the number of matching organizations
1481             * @throws SystemException if a system exception occurred
1482             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1483             */
1484            @Override
1485            public int searchCount(
1486                            long companyId, long parentOrganizationId, String keywords,
1487                            String type, Long regionId, Long countryId,
1488                            LinkedHashMap<String, Object> params)
1489                    throws SystemException {
1490    
1491                    String parentOrganizationIdComparator = StringPool.EQUAL;
1492    
1493                    if (parentOrganizationId ==
1494                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1495    
1496                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1497                    }
1498    
1499                    return organizationFinder.countByKeywords(
1500                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1501                            keywords, type, regionId, countryId, params);
1502            }
1503    
1504            /**
1505             * Returns the number of organizations with the type, region, and country,
1506             * and whose name, street, city, and zipcode match the keywords specified
1507             * for them.
1508             *
1509             * @param  companyId the primary key of the organization's company
1510             * @param  parentOrganizationId the primary key of the organization's parent
1511             *         organization
1512             * @param  name the name keywords (space separated, optionally
1513             *         <code>null</code>)
1514             * @param  type the organization's type (optionally <code>null</code>)
1515             * @param  street the street keywords (optionally <code>null</code>)
1516             * @param  city the city keywords (optionally <code>null</code>)
1517             * @param  zip the zipcode keywords (optionally <code>null</code>)
1518             * @param  regionId the primary key of the organization's region (optionally
1519             *         <code>null</code>)
1520             * @param  countryId the primary key of the organization's country
1521             *         (optionally <code>null</code>)
1522             * @param  params the finder parameters (optionally <code>null</code>). For
1523             *         more information see {@link
1524             *         com.liferay.portal.service.persistence.OrganizationFinder}
1525             * @param  andOperator whether every field must match its keywords, or just
1526             *         one field. For example, &quot;organizations with the name
1527             *         'Employees' and city 'Chicago'&quot; vs &quot;organizations with
1528             *         the name 'Employees' or the city 'Chicago'&quot;.
1529             * @return the number of matching organizations
1530             * @throws SystemException if a system exception occurred
1531             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1532             */
1533            @Override
1534            public int searchCount(
1535                            long companyId, long parentOrganizationId, String name, String type,
1536                            String street, String city, String zip, Long regionId,
1537                            Long countryId, LinkedHashMap<String, Object> params,
1538                            boolean andOperator)
1539                    throws SystemException {
1540    
1541                    String parentOrganizationIdComparator = StringPool.EQUAL;
1542    
1543                    if (parentOrganizationId ==
1544                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1545    
1546                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1547                    }
1548    
1549                    return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
1550                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1551                            name, type, street, city, zip, regionId, countryId, params,
1552                            andOperator);
1553            }
1554    
1555            /**
1556             * Sets the organizations in the group, removing and adding organizations to
1557             * the group as necessary.
1558             *
1559             * @param  groupId the primary key of the group
1560             * @param  organizationIds the primary keys of the organizations
1561             * @throws PortalException if a portal exception occurred
1562             * @throws SystemException if a system exception occurred
1563             */
1564            @Override
1565            public void setGroupOrganizations(long groupId, long[] organizationIds)
1566                    throws PortalException, SystemException {
1567    
1568                    groupPersistence.setOrganizations(groupId, organizationIds);
1569    
1570                    PermissionCacheUtil.clearCache();
1571            }
1572    
1573            /**
1574             * Removes the organizations from the group.
1575             *
1576             * @param  groupId the primary key of the group
1577             * @param  organizationIds the primary keys of the organizations
1578             * @throws PortalException if a portal exception occurred
1579             * @throws SystemException if a system exception occurred
1580             */
1581            @Override
1582            public void unsetGroupOrganizations(long groupId, long[] organizationIds)
1583                    throws PortalException, SystemException {
1584    
1585                    groupPersistence.removeOrganizations(groupId, organizationIds);
1586    
1587                    PermissionCacheUtil.clearCache();
1588            }
1589    
1590            /**
1591             * Removes the organizations from the password policy.
1592             *
1593             * @param  passwordPolicyId the primary key of the password policy
1594             * @param  organizationIds the primary keys of the organizations
1595             * @throws SystemException if a system exception occurred
1596             */
1597            @Override
1598            public void unsetPasswordPolicyOrganizations(
1599                            long passwordPolicyId, long[] organizationIds)
1600                    throws SystemException {
1601    
1602                    passwordPolicyRelLocalService.deletePasswordPolicyRels(
1603                            passwordPolicyId, Organization.class.getName(), organizationIds);
1604            }
1605    
1606            /**
1607             * Updates the organization's asset with the new asset categories and tag
1608             * names, removing and adding asset categories and tag names as necessary.
1609             *
1610             * @param  userId the primary key of the user
1611             * @param  organization the organization
1612             * @param  assetCategoryIds the primary keys of the asset categories
1613             * @param  assetTagNames the asset tag names
1614             * @throws PortalException if a user with the primary key could not be found
1615             * @throws SystemException if a system exception occurred
1616             */
1617            @Override
1618            public void updateAsset(
1619                            long userId, Organization organization, long[] assetCategoryIds,
1620                            String[] assetTagNames)
1621                    throws PortalException, SystemException {
1622    
1623                    User user = userPersistence.findByPrimaryKey(userId);
1624    
1625                    Company company = companyPersistence.findByPrimaryKey(
1626                            user.getCompanyId());
1627    
1628                    Group companyGroup = company.getGroup();
1629    
1630                    assetEntryLocalService.updateEntry(
1631                            userId, companyGroup.getGroupId(), null, null,
1632                            Organization.class.getName(), organization.getOrganizationId(),
1633                            organization.getUuid(), 0, assetCategoryIds, assetTagNames, false,
1634                            null, null, null, null, organization.getName(), StringPool.BLANK,
1635                            null, null, null, 0, 0, null, false);
1636            }
1637    
1638            /**
1639             * Updates the organization.
1640             *
1641             * @param      companyId the primary key of the organization's company
1642             * @param      organizationId the primary key of the organization
1643             * @param      parentOrganizationId the primary key of organization's parent
1644             *             organization
1645             * @param      name the organization's name
1646             * @param      type the organization's type
1647             * @param      recursable whether permissions of the organization are to be
1648             *             inherited by its suborganizations
1649             * @param      regionId the primary key of the organization's region
1650             * @param      countryId the primary key of the organization's country
1651             * @param      statusId the organization's workflow status
1652             * @param      comments the comments about the organization
1653             * @param      site whether the organization is to be associated with a main
1654             *             site
1655             * @param      serviceContext the service context to be applied (optionally
1656             *             <code>null</code>). Can set asset category IDs and asset tag
1657             *             names for the organization, and merge expando bridge
1658             *             attributes for the organization.
1659             * @return     the organization
1660             * @throws     PortalException if an organization or parent organization
1661             *             with the primary key could not be found or if the new
1662             *             information was invalid
1663             * @throws     SystemException if a system exception occurred
1664             * @deprecated As of 6.2.0, replaced by {@link #updateOrganization(long,
1665             *             long, long, String, String, long, long, int, String, boolean,
1666             *             ServiceContext)}
1667             */
1668            @Override
1669            public Organization updateOrganization(
1670                            long companyId, long organizationId, long parentOrganizationId,
1671                            String name, String type, boolean recursable, long regionId,
1672                            long countryId, int statusId, String comments, boolean site,
1673                            ServiceContext serviceContext)
1674                    throws PortalException, SystemException {
1675    
1676                    return updateOrganization(
1677                            companyId, organizationId, parentOrganizationId, name, type,
1678                            regionId, countryId, statusId, comments, site, serviceContext);
1679            }
1680    
1681            /**
1682             * Updates the organization.
1683             *
1684             * @param  companyId the primary key of the organization's company
1685             * @param  organizationId the primary key of the organization
1686             * @param  parentOrganizationId the primary key of organization's parent
1687             *         organization
1688             * @param  name the organization's name
1689             * @param  type the organization's type
1690             * @param  regionId the primary key of the organization's region
1691             * @param  countryId the primary key of the organization's country
1692             * @param  statusId the organization's workflow status
1693             * @param  comments the comments about the organization
1694             * @param  site whether the organization is to be associated with a main
1695             *         site
1696             * @param  serviceContext the service context to be applied (optionally
1697             *         <code>null</code>). Can set asset category IDs and asset tag
1698             *         names for the organization, and merge expando bridge attributes
1699             *         for the organization.
1700             * @return the organization
1701             * @throws PortalException if an organization or parent organization with
1702             *         the primary key could not be found or if the new information was
1703             *         invalid
1704             * @throws SystemException if a system exception occurred
1705             */
1706            @Override
1707            public Organization updateOrganization(
1708                            long companyId, long organizationId, long parentOrganizationId,
1709                            String name, String type, long regionId, long countryId,
1710                            int statusId, String comments, boolean site,
1711                            ServiceContext serviceContext)
1712                    throws PortalException, SystemException {
1713    
1714                    // Organization
1715    
1716                    parentOrganizationId = getParentOrganizationId(
1717                            companyId, parentOrganizationId);
1718    
1719                    validate(
1720                            companyId, organizationId, parentOrganizationId, name, type,
1721                            countryId, statusId);
1722    
1723                    Organization organization = organizationPersistence.findByPrimaryKey(
1724                            organizationId);
1725    
1726                    long oldParentOrganizationId = organization.getParentOrganizationId();
1727                    String oldName = organization.getName();
1728    
1729                    organization.setModifiedDate(new Date());
1730                    organization.setParentOrganizationId(parentOrganizationId);
1731                    organization.setTreePath(organization.buildTreePath());
1732                    organization.setName(name);
1733                    organization.setType(type);
1734                    organization.setRecursable(true);
1735                    organization.setRegionId(regionId);
1736                    organization.setCountryId(countryId);
1737                    organization.setStatusId(statusId);
1738                    organization.setComments(comments);
1739                    organization.setExpandoBridgeAttributes(serviceContext);
1740    
1741                    organizationPersistence.update(organization);
1742    
1743                    // Group
1744    
1745                    Group group = organization.getGroup();
1746    
1747                    long parentGroupId = group.getParentGroupId();
1748    
1749                    boolean organizationGroup = isOrganizationGroup(
1750                            oldParentOrganizationId, group.getParentGroupId());
1751    
1752                    if (organizationGroup) {
1753                            if (parentOrganizationId !=
1754                                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1755    
1756                                    Organization parentOrganization =
1757                                            organizationPersistence.fetchByPrimaryKey(
1758                                                    parentOrganizationId);
1759    
1760                                    Group parentGroup = parentOrganization.getGroup();
1761    
1762                                    if (site && parentGroup.isSite()) {
1763                                            parentGroupId = parentOrganization.getGroupId();
1764                                    }
1765                                    else {
1766                                            parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
1767                                    }
1768                            }
1769                            else {
1770                                    parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
1771                            }
1772                    }
1773    
1774                    if (!oldName.equals(name) || organizationGroup) {
1775                            groupLocalService.updateGroup(
1776                                    group.getGroupId(), parentGroupId, name, group.getDescription(),
1777                                    group.getType(), group.isManualMembership(),
1778                                    group.getMembershipRestriction(), group.getFriendlyURL(),
1779                                    group.isActive(), null);
1780                    }
1781    
1782                    if (group.isSite() != site) {
1783                            groupLocalService.updateSite(group.getGroupId(), site);
1784                    }
1785    
1786                    // Asset
1787    
1788                    if (serviceContext != null) {
1789                            updateAsset(
1790                                    serviceContext.getUserId(), organization,
1791                                    serviceContext.getAssetCategoryIds(),
1792                                    serviceContext.getAssetTagNames());
1793                    }
1794    
1795                    // Indexer
1796    
1797                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1798                            Organization.class);
1799    
1800                    if (oldParentOrganizationId != parentOrganizationId) {
1801                            long[] organizationIds = getReindexOrganizationIds(organization);
1802    
1803                            indexer.reindex(organizationIds);
1804                    }
1805                    else {
1806                            indexer.reindex(organization);
1807                    }
1808    
1809                    return organization;
1810            }
1811    
1812            protected void addSuborganizations(
1813                            List<Organization> allSuborganizations,
1814                            List<Organization> organizations)
1815                    throws SystemException {
1816    
1817                    for (Organization organization : organizations) {
1818                            if (!allSuborganizations.contains(organization)) {
1819                                    allSuborganizations.add(organization);
1820    
1821                                    List<Organization> suborganizations =
1822                                            organizationPersistence.findByC_P(
1823                                                    organization.getCompanyId(),
1824                                                    organization.getOrganizationId());
1825    
1826                                    addSuborganizations(allSuborganizations, suborganizations);
1827                            }
1828                    }
1829            }
1830    
1831            protected long getParentOrganizationId(
1832                            long companyId, long parentOrganizationId)
1833                    throws SystemException {
1834    
1835                    if (parentOrganizationId !=
1836                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1837    
1838                            // Ensure parent organization exists and belongs to the proper
1839                            // company
1840    
1841                            Organization parentOrganization =
1842                                    organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
1843    
1844                            if ((parentOrganization == null) ||
1845                                    (companyId != parentOrganization.getCompanyId())) {
1846    
1847                                    parentOrganizationId =
1848                                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
1849                            }
1850                    }
1851    
1852                    return parentOrganizationId;
1853            }
1854    
1855            protected long[] getReindexOrganizationIds(Organization organization)
1856                    throws PortalException, SystemException {
1857    
1858                    List<Organization> organizationsTree = new ArrayList<Organization>();
1859    
1860                    organizationsTree.add(organization);
1861    
1862                    LinkedHashMap<String, Object> params =
1863                            new LinkedHashMap<String, Object>();
1864    
1865                    params.put("organizationsTree", organizationsTree);
1866    
1867                    List<Organization> organizations = search(
1868                            organization.getCompanyId(), params, QueryUtil.ALL_POS,
1869                            QueryUtil.ALL_POS);
1870    
1871                    long[] organizationIds = new long[organizations.size()];
1872    
1873                    for (int i = 0; i < organizations.size(); i++) {
1874                            Organization curOrganization = organizations.get(i);
1875    
1876                            curOrganization.setTreePath(curOrganization.buildTreePath());
1877    
1878                            organizationPersistence.update(curOrganization);
1879    
1880                            organizationIds[i] = curOrganization.getOrganizationId();
1881                    }
1882    
1883                    if (!ArrayUtil.contains(
1884                                    organizationIds, organization.getOrganizationId())) {
1885    
1886                            organizationIds = ArrayUtil.append(
1887                                    organizationIds, organization.getOrganizationId());
1888                    }
1889    
1890                    return organizationIds;
1891            }
1892    
1893            protected boolean isOrganizationGroup(long organizationId, long groupId)
1894                    throws SystemException {
1895    
1896                    if ((organizationId ==
1897                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) &&
1898                            (groupId == GroupConstants.DEFAULT_PARENT_GROUP_ID)) {
1899    
1900                            return true;
1901                    }
1902    
1903                    if (organizationId !=
1904                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1905    
1906                            Organization organization =
1907                                    organizationPersistence.fetchByPrimaryKey(organizationId);
1908    
1909                            if (organization.getGroupId() == groupId) {
1910                                    return true;
1911                            }
1912                    }
1913    
1914                    return false;
1915            }
1916    
1917            protected boolean isParentOrganization(
1918                            long parentOrganizationId, long organizationId)
1919                    throws PortalException, SystemException {
1920    
1921                    // Return true if parentOrganizationId is among the parent organizatons
1922                    // of organizationId
1923    
1924                    if (organizationId ==
1925                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1926    
1927                            return false;
1928                    }
1929    
1930                    Organization organization = organizationPersistence.findByPrimaryKey(
1931                            organizationId);
1932    
1933                    String treePath = organization.getTreePath();
1934    
1935                    if (treePath.contains(
1936                                    StringPool.SLASH + parentOrganizationId + StringPool.SLASH)) {
1937    
1938                            return true;
1939                    }
1940                    else {
1941                            return false;
1942                    }
1943            }
1944    
1945            protected void validate(
1946                            long companyId, long organizationId, long parentOrganizationId,
1947                            String name, String type, long countryId, int statusId)
1948                    throws PortalException, SystemException {
1949    
1950                    if (!ArrayUtil.contains(PropsValues.ORGANIZATIONS_TYPES, type)) {
1951                            throw new OrganizationTypeException(
1952                                    "Invalid organization type " + type);
1953                    }
1954    
1955                    if (parentOrganizationId ==
1956                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1957    
1958                            if (!OrganizationImpl.isRootable(type)) {
1959                                    throw new OrganizationParentException(
1960                                            "Organization of type " + type + " cannot be a root");
1961                            }
1962                    }
1963                    else {
1964                            Organization parentOrganization =
1965                                    organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
1966    
1967                            if (parentOrganization == null) {
1968                                    throw new OrganizationParentException(
1969                                            "Organization " + parentOrganizationId + " doesn't exist");
1970                            }
1971    
1972                            String[] childrenTypes = OrganizationImpl.getChildrenTypes(
1973                                    parentOrganization.getType());
1974    
1975                            if (childrenTypes.length == 0) {
1976                                    throw new OrganizationParentException(
1977                                            "Organization of type " + type + " cannot have children");
1978                            }
1979    
1980                            if ((companyId != parentOrganization.getCompanyId()) ||
1981                                    (parentOrganizationId == organizationId)) {
1982    
1983                                    throw new OrganizationParentException();
1984                            }
1985    
1986                            if (!ArrayUtil.contains(childrenTypes, type)) {
1987                                    throw new OrganizationParentException(
1988                                            "Type " + type + " not allowed as child of " +
1989                                                    parentOrganization.getType());
1990                            }
1991                    }
1992    
1993                    if ((organizationId > 0) &&
1994                            (parentOrganizationId !=
1995                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
1996    
1997                            // Prevent circular organizational references
1998    
1999                            if (isParentOrganization(organizationId, parentOrganizationId)) {
2000                                    throw new OrganizationParentException();
2001                            }
2002                    }
2003    
2004                    if (Validator.isNull(name)) {
2005                            throw new OrganizationNameException();
2006                    }
2007                    else {
2008                            Organization organization = organizationPersistence.fetchByC_N(
2009                                    companyId, name);
2010    
2011                            if ((organization != null) &&
2012                                    StringUtil.equalsIgnoreCase(organization.getName(), name)) {
2013    
2014                                    if ((organizationId <= 0) ||
2015                                            (organization.getOrganizationId() != organizationId)) {
2016    
2017                                            throw new DuplicateOrganizationException(
2018                                                    "There is another organization named " + name);
2019                                    }
2020                            }
2021                    }
2022    
2023                    boolean countryRequired = GetterUtil.getBoolean(
2024                            PropsUtil.get(
2025                                    PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
2026    
2027                    if (countryRequired || (countryId > 0)) {
2028                            countryPersistence.findByPrimaryKey(countryId);
2029                    }
2030    
2031                    listTypeService.validate(
2032                            statusId, ListTypeConstants.ORGANIZATION_STATUS);
2033            }
2034    
2035            protected void validate(
2036                            long companyId, long parentOrganizationId, String name, String type,
2037                            long countryId, int statusId)
2038                    throws PortalException, SystemException {
2039    
2040                    validate(
2041                            companyId, 0, parentOrganizationId, name, type, countryId,
2042                            statusId);
2043            }
2044    
2045    }