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                                    (organization.getParentOrganization() == null)) {
598    
599                                    iterator.remove();
600                            }
601                    }
602    
603                    if (organizations.isEmpty()) {
604                            return organizations;
605                    }
606    
607                    if (obc == null) {
608                            obc = new OrganizationNameComparator(true);
609                    }
610    
611                    Collections.sort(organizations, obc);
612    
613                    return ListUtil.subList(organizations, start, end);
614            }
615    
616            /**
617             * Returns all the organizations belonging to the parent organization.
618             *
619             * @param  companyId the primary key of the organization's company
620             * @param  parentOrganizationId the primary key of the organization's parent
621             *         organization
622             * @return the organizations belonging to the parent organization
623             * @throws SystemException if a system exception occurred
624             */
625            @Override
626            public List<Organization> getOrganizations(
627                            long companyId, long parentOrganizationId)
628                    throws SystemException {
629    
630                    return getOrganizations(
631                            companyId, parentOrganizationId, QueryUtil.ALL_POS,
632                            QueryUtil.ALL_POS);
633            }
634    
635            /**
636             * Returns a range of all the organizations belonging to the parent
637             * organization.
638             *
639             * <p>
640             * Useful when paginating results. Returns a maximum of <code>end -
641             * start</code> instances. <code>start</code> and <code>end</code> are not
642             * primary keys, they are indexes in the result set. Thus, <code>0</code>
643             * refers to the first result in the set. Setting both <code>start</code>
644             * and <code>end</code> to {@link
645             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
646             * result set.
647             * </p>
648             *
649             * @param  companyId the primary key of the organization's company
650             * @param  parentOrganizationId the primary key of the organization's parent
651             *         organization
652             * @param  start the lower bound of the range of organizations to return
653             * @param  end the upper bound of the range of organizations to return (not
654             *         inclusive)
655             * @return the range of organizations belonging to the parent organization
656             * @throws SystemException if a system exception occurred
657             * @see    com.liferay.portal.service.persistence.OrganizationPersistence#findByC_P(
658             *         long, long, int, int)
659             */
660            @Override
661            public List<Organization> getOrganizations(
662                            long companyId, long parentOrganizationId, int start, int end)
663                    throws SystemException {
664    
665                    if (parentOrganizationId ==
666                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
667    
668                            return organizationPersistence.findByCompanyId(
669                                    companyId, start, end);
670                    }
671                    else {
672                            return organizationPersistence.findByC_P(
673                                    companyId, parentOrganizationId, start, end);
674                    }
675            }
676    
677            /**
678             * Returns the organizations with the primary keys.
679             *
680             * @param  organizationIds the primary keys of the organizations
681             * @return the organizations with the primary keys
682             * @throws PortalException if any one of the organizations could not be
683             *         found
684             * @throws SystemException if a system exception occurred
685             */
686            @Override
687            public List<Organization> getOrganizations(long[] organizationIds)
688                    throws PortalException, SystemException {
689    
690                    List<Organization> organizations = new ArrayList<Organization>(
691                            organizationIds.length);
692    
693                    for (long organizationId : organizationIds) {
694                            Organization organization = getOrganization(organizationId);
695    
696                            organizations.add(organization);
697                    }
698    
699                    return organizations;
700            }
701    
702            /**
703             * Returns the number of organizations belonging to the parent organization.
704             *
705             * @param  companyId the primary key of the organization's company
706             * @param  parentOrganizationId the primary key of the organization's parent
707             *         organization
708             * @return the number of organizations belonging to the parent organization
709             * @throws SystemException if a system exception occurred
710             */
711            @Override
712            public int getOrganizationsCount(long companyId, long parentOrganizationId)
713                    throws SystemException {
714    
715                    if (parentOrganizationId ==
716                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
717    
718                            return organizationPersistence.countByCompanyId(companyId);
719                    }
720                    else {
721                            return organizationPersistence.countByC_P(
722                                    companyId, parentOrganizationId);
723                    }
724            }
725    
726            /**
727             * Returns the parent organizations in order by closest ancestor. The list
728             * starts with the organization itself.
729             *
730             * @param  organizationId the primary key of the organization
731             * @return the parent organizations in order by closest ancestor
732             * @throws PortalException if an organization with the primary key could not
733             *         be found
734             * @throws SystemException if a system exception occurred
735             */
736            @Override
737            public List<Organization> getParentOrganizations(long organizationId)
738                    throws PortalException, SystemException {
739    
740                    if (organizationId ==
741                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
742    
743                            return new ArrayList<Organization>();
744                    }
745    
746                    Organization organization = organizationPersistence.findByPrimaryKey(
747                            organizationId);
748    
749                    return organization.getAncestors();
750            }
751    
752            /**
753             * Returns the suborganizations of the organizations.
754             *
755             * @param  organizations the organizations from which to get
756             *         suborganizations
757             * @return the suborganizations of the organizations
758             * @throws SystemException if a system exception occurred
759             */
760            @Override
761            public List<Organization> getSuborganizations(
762                            List<Organization> organizations)
763                    throws SystemException {
764    
765                    List<Organization> allSuborganizations = new ArrayList<Organization>();
766    
767                    for (int i = 0; i < organizations.size(); i++) {
768                            Organization organization = organizations.get(i);
769    
770                            List<Organization> suborganizations =
771                                    organizationPersistence.findByC_P(
772                                            organization.getCompanyId(),
773                                            organization.getOrganizationId());
774    
775                            addSuborganizations(allSuborganizations, suborganizations);
776                    }
777    
778                    return allSuborganizations;
779            }
780    
781            /**
782             * Returns the suborganizations of the organization.
783             *
784             * @param  companyId the primary key of the organization's company
785             * @param  organizationId the primary key of the organization
786             * @return the suborganizations of the organization
787             * @throws SystemException if a system exception occurred
788             */
789            @Override
790            public List<Organization> getSuborganizations(
791                            long companyId, long organizationId)
792                    throws SystemException {
793    
794                    return organizationPersistence.findByC_P(companyId, organizationId);
795            }
796    
797            /**
798             * Returns the count of suborganizations of the organization.
799             *
800             * @param  companyId the primary key of the organization's company
801             * @param  organizationId the primary key of the organization
802             * @return the count of suborganizations of the organization
803             * @throws SystemException if a system exception occurred
804             */
805            @Override
806            public int getSuborganizationsCount(long companyId, long organizationId)
807                    throws SystemException {
808    
809                    return organizationPersistence.countByC_P(companyId, organizationId);
810            }
811    
812            /**
813             * Returns the intersection of <code>allOrganizations</code> and
814             * <code>availableOrganizations</code>.
815             *
816             * @param  allOrganizations the organizations to check for availability
817             * @param  availableOrganizations the available organizations
818             * @return the intersection of <code>allOrganizations</code> and
819             *         <code>availableOrganizations</code>
820             */
821            @Override
822            public List<Organization> getSubsetOrganizations(
823                    List<Organization> allOrganizations,
824                    List<Organization> availableOrganizations) {
825    
826                    List<Organization> subsetOrganizations = new ArrayList<Organization>();
827    
828                    for (Organization organization : allOrganizations) {
829                            if (availableOrganizations.contains(organization)) {
830                                    subsetOrganizations.add(organization);
831                            }
832                    }
833    
834                    return subsetOrganizations;
835            }
836    
837            /**
838             * Returns all the organizations associated with the user. If
839             * includeAdministrative is <code>true</code>, the result includes those
840             * organizations that are not directly associated to the user but he is an
841             * administrator or an owner of the organization.
842             *
843             * @param  userId the primary key of the user
844             * @param  includeAdministrative whether to includes organizations that are
845             *         indirectly associated to the user because he is an administrator
846             *         or an owner of the organization
847             * @return the organizations associated with the user
848             * @throws PortalException if a user with the primary key could not be found
849             * @throws SystemException if a system exception occurred
850             */
851            @Override
852            public List<Organization> getUserOrganizations(
853                            long userId, boolean includeAdministrative)
854                    throws PortalException, SystemException {
855    
856                    if (!includeAdministrative) {
857                            return getUserOrganizations(userId);
858                    }
859    
860                    Set<Organization> organizations = new HashSet<Organization>();
861    
862                    List<UserGroupRole> userGroupRoles =
863                            userGroupRoleLocalService.getUserGroupRoles(userId);
864    
865                    for (UserGroupRole userGroupRole : userGroupRoles) {
866                            Role role = userGroupRole.getRole();
867    
868                            String roleName = role.getName();
869    
870                            if (roleName.equals(RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
871                                    roleName.equals(RoleConstants.ORGANIZATION_OWNER)) {
872    
873                                    Group group = userGroupRole.getGroup();
874    
875                                    Organization organization =
876                                            organizationPersistence.findByPrimaryKey(
877                                                    group.getOrganizationId());
878    
879                                    organizations.add(organization);
880                            }
881                    }
882    
883                    organizations.addAll(getUserOrganizations(userId));
884    
885                    return new ArrayList<Organization>(organizations);
886            }
887    
888            /**
889             * Returns <code>true</code> if the password policy has been assigned to the
890             * organization.
891             *
892             * @param  passwordPolicyId the primary key of the password policy
893             * @param  organizationId the primary key of the organization
894             * @return <code>true</code> if the password policy has been assigned to the
895             *         organization; <code>false</code> otherwise
896             * @throws SystemException if a system exception occurred
897             */
898            @Override
899            public boolean hasPasswordPolicyOrganization(
900                            long passwordPolicyId, long organizationId)
901                    throws SystemException {
902    
903                    return passwordPolicyRelLocalService.hasPasswordPolicyRel(
904                            passwordPolicyId, Organization.class.getName(), organizationId);
905            }
906    
907            /**
908             * Returns <code>true</code> if the user is a member of the organization,
909             * optionally focusing on suborganizations or the specified organization.
910             * This method is usually called to determine if the user has view access to
911             * a resource belonging to the organization.
912             *
913             * <ol>
914             * <li>
915             * If <code>inheritSuborganizations=<code>false</code></code>:
916             * the method checks whether the user belongs to the organization specified
917             * by <code>organizationId</code>. The parameter
918             * <code>includeSpecifiedOrganization</code> is ignored.
919             * </li>
920             * <li>
921             * The parameter <code>includeSpecifiedOrganization</code> is
922             * ignored unless <code>inheritSuborganizations</code> is also
923             * <code>true</code>.
924             * </li>
925             * <li>
926             * If <code>inheritSuborganizations=<code>true</code></code> and
927             * <code>includeSpecifiedOrganization=<code>false</code></code>: the method
928             * checks
929             * whether the user belongs to one of the child organizations of the one
930             * specified by <code>organizationId</code>.
931             * </li>
932             * <li>
933             * If <code>inheritSuborganizations=<code>true</code></code> and
934             * <code>includeSpecifiedOrganization=<code>true</code></code>: the method
935             * checks whether
936             * the user belongs to the organization specified by
937             * <code>organizationId</code> or any of
938             * its child organizations.
939             * </li>
940             * </ol>
941             *
942             * @param  userId the primary key of the organization's user
943             * @param  organizationId the primary key of the organization
944             * @param  inheritSuborganizations if <code>true</code> suborganizations are
945             *         considered in the determination
946             * @param  includeSpecifiedOrganization if <code>true</code> the
947             *         organization specified by <code>organizationId</code> is
948             *         considered in the determination
949             * @return <code>true</code> if the user has access to the organization;
950             *         <code>false</code> otherwise
951             * @throws PortalException if an organization with the primary key could not
952             *         be found
953             * @throws SystemException if a system exception occurred
954             * @see    com.liferay.portal.service.persistence.OrganizationFinder
955             */
956            @Override
957            public boolean hasUserOrganization(
958                            long userId, long organizationId, boolean inheritSuborganizations,
959                            boolean includeSpecifiedOrganization)
960                    throws PortalException, SystemException {
961    
962                    if (!inheritSuborganizations) {
963                            return userPersistence.containsOrganization(userId, organizationId);
964                    }
965    
966                    LinkedHashMap<String, Object> params =
967                            new LinkedHashMap<String, Object>();
968    
969                    List<Organization> organizationsTree = new ArrayList<Organization>();
970    
971                    Organization organization = organizationPersistence.findByPrimaryKey(
972                            organizationId);
973    
974                    if (!includeSpecifiedOrganization) {
975                            organizationsTree.add(organization);
976                    }
977                    else {
978                            organizationsTree.add(organization.getParentOrganization());
979                    }
980    
981                    params.put("usersOrgsTree", organizationsTree);
982    
983                    if (userFinder.countByUser(userId, params) > 0) {
984                            return true;
985                    }
986    
987                    return false;
988            }
989    
990            /**
991             * Rebuilds the organizations tree.
992             *
993             * <p>
994             * Only call this method if the tree has become stale through operations
995             * other than normal CRUD. Under normal circumstances the tree is
996             * automatically rebuilt whenever necessary.
997             * </p>
998             *
999             * @param  companyId the primary key of the organization's company
1000             * @throws PortalException if an organization with the primary key could not
1001             *         be found
1002             * @throws SystemException if a system exception occurred
1003             */
1004            @Override
1005            public void rebuildTree(long companyId)
1006                    throws PortalException, SystemException {
1007    
1008                    List<Organization> organizations =
1009                            organizationPersistence.findByCompanyId(companyId);
1010    
1011                    for (Organization organization : organizations) {
1012                            organization.setTreePath(organization.buildTreePath());
1013    
1014                            organizationPersistence.update(organization);
1015                    }
1016            }
1017    
1018            /**
1019             * Returns a range of all the organizations of the company.
1020             *
1021             * <p>
1022             * Useful when paginating results. Returns a maximum of <code>end -
1023             * start</code> instances. <code>start</code> and <code>end</code> are not
1024             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1025             * refers to the first result in the set. Setting both <code>start</code>
1026             * and <code>end</code> to {@link
1027             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1028             * result set.
1029             * </p>
1030             *
1031             * @param  companyId the primary key of the company
1032             * @param  params the finder parameters (optionally <code>null</code>). For
1033             *         more information see {@link
1034             *         com.liferay.portlet.usersadmin.util.OrganizationIndexer}
1035             * @param  start the lower bound of the range of organizations to return
1036             * @param  end the upper bound of the range of organizations to return (not
1037             *         inclusive)
1038             * @return the range of all the organizations of the company
1039             * @throws SystemException if a system exception occurred
1040             */
1041            @Override
1042            public List<Organization> search(
1043                            long companyId, LinkedHashMap<String, Object> params, int start,
1044                            int end)
1045                    throws SystemException {
1046    
1047                    return organizationFinder.findByCompanyId(
1048                            companyId, params, start, end,
1049                            new OrganizationNameComparator(true));
1050            }
1051    
1052            /**
1053             * Returns an ordered range of all the organizations that match the
1054             * keywords, using the indexer. It is preferable to use this method instead
1055             * of the non-indexed version whenever possible for performance reasons.
1056             *
1057             * <p>
1058             * Useful when paginating results. Returns a maximum of <code>end -
1059             * start</code> instances. <code>start</code> and <code>end</code> are not
1060             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1061             * refers to the first result in the set. Setting both <code>start</code>
1062             * and <code>end</code> to {@link
1063             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1064             * result set.
1065             * </p>
1066             *
1067             * @param  companyId the primary key of the organization's company
1068             * @param  parentOrganizationId the primary key of the organization's parent
1069             *         organization
1070             * @param  keywords the keywords (space separated), which may occur in the
1071             *         organization's name, street, city, zipcode, type, region or
1072             *         country (optionally <code>null</code>)
1073             * @param  params the finder parameters (optionally <code>null</code>). For
1074             *         more information see {@link
1075             *         com.liferay.portlet.usersadmin.util.OrganizationIndexer}
1076             * @param  start the lower bound of the range of organizations to return
1077             * @param  end the upper bound of the range of organizations to return (not
1078             *         inclusive)
1079             * @param  sort the field and direction by which to sort (optionally
1080             *         <code>null</code>)
1081             * @return the matching organizations ordered by name
1082             * @throws SystemException if a system exception occurred
1083             * @see    com.liferay.portlet.usersadmin.util.OrganizationIndexer
1084             */
1085            @Override
1086            public Hits search(
1087                            long companyId, long parentOrganizationId, String keywords,
1088                            LinkedHashMap<String, Object> params, int start, int end, Sort sort)
1089                    throws SystemException {
1090    
1091                    String name = null;
1092                    String type = null;
1093                    String street = null;
1094                    String city = null;
1095                    String zip = null;
1096                    String region = null;
1097                    String country = null;
1098                    boolean andOperator = false;
1099    
1100                    if (Validator.isNotNull(keywords)) {
1101                            name = keywords;
1102                            type = keywords;
1103                            street = keywords;
1104                            city = keywords;
1105                            zip = keywords;
1106                            region = keywords;
1107                            country = keywords;
1108                    }
1109                    else {
1110                            andOperator = true;
1111                    }
1112    
1113                    if (params != null) {
1114                            params.put("keywords", keywords);
1115                    }
1116    
1117                    return search(
1118                            companyId, parentOrganizationId, name, type, street, city, zip,
1119                            region, country, params, andOperator, start, end, sort);
1120            }
1121    
1122            /**
1123             * Returns a name ordered range of all the organizations that match the
1124             * keywords, type, region, and country, without using the indexer. It is
1125             * preferable to use the indexed version {@link #search(long, long, String,
1126             * LinkedHashMap, int, int, Sort)} instead of this method wherever possible
1127             * for performance reasons.
1128             *
1129             * <p>
1130             * Useful when paginating results. Returns a maximum of <code>end -
1131             * start</code> instances. <code>start</code> and <code>end</code> are not
1132             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1133             * refers to the first result in the set. Setting both <code>start</code>
1134             * and <code>end</code> to {@link
1135             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1136             * result set.
1137             * </p>
1138             *
1139             * @param  companyId the primary key of the organization's company
1140             * @param  parentOrganizationId the primary key of the organization's parent
1141             *         organization
1142             * @param  keywords the keywords (space separated), which may occur in the
1143             *         organization's name, street, city, or zipcode (optionally
1144             *         <code>null</code>)
1145             * @param  type the organization's type (optionally <code>null</code>)
1146             * @param  regionId the primary key of the organization's region (optionally
1147             *         <code>null</code>)
1148             * @param  countryId the primary key of the organization's country
1149             *         (optionally <code>null</code>)
1150             * @param  params the finder params. For more information see {@link
1151             *         com.liferay.portal.service.persistence.OrganizationFinder}
1152             * @param  start the lower bound of the range of organizations to return
1153             * @param  end the upper bound of the range of organizations to return (not
1154             *         inclusive)
1155             * @return the matching organizations ordered by name
1156             * @throws SystemException if a system exception occurred
1157             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1158             */
1159            @Override
1160            public List<Organization> search(
1161                            long companyId, long parentOrganizationId, String keywords,
1162                            String type, Long regionId, Long countryId,
1163                            LinkedHashMap<String, Object> params, int start, int end)
1164                    throws SystemException {
1165    
1166                    return search(
1167                            companyId, parentOrganizationId, keywords, type, regionId,
1168                            countryId, params, start, end,
1169                            new OrganizationNameComparator(true));
1170            }
1171    
1172            /**
1173             * Returns an ordered range of all the organizations that match the
1174             * keywords, type, region, and country, without using the indexer. It is
1175             * preferable to use the indexed version {@link #search(long, long, String,
1176             * String, String, String, String, String, String, LinkedHashMap, boolean,
1177             * int, int, Sort)} instead of this method wherever possible for performance
1178             * reasons.
1179             *
1180             * <p>
1181             * Useful when paginating results. Returns a maximum of <code>end -
1182             * start</code> instances. <code>start</code> and <code>end</code> are not
1183             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1184             * refers to the first result in the set. Setting both <code>start</code>
1185             * and <code>end</code> to {@link
1186             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1187             * result set.
1188             * </p>
1189             *
1190             * @param  companyId the primary key of the organization's company
1191             * @param  parentOrganizationId the primary key of the organization's parent
1192             *         organization
1193             * @param  keywords the keywords (space separated), which may occur in the
1194             *         organization's name, street, city, or zipcode (optionally
1195             *         <code>null</code>)
1196             * @param  type the organization's type (optionally <code>null</code>)
1197             * @param  regionId the primary key of the organization's region (optionally
1198             *         <code>null</code>)
1199             * @param  countryId the primary key of the organization's country
1200             *         (optionally <code>null</code>)
1201             * @param  params the finder params. For more information see {@link
1202             *         com.liferay.portal.service.persistence.OrganizationFinder}
1203             * @param  start the lower bound of the range of organizations to return
1204             * @param  end the upper bound of the range of organizations to return (not
1205             *         inclusive)
1206             * @param  obc the comparator to order the organizations (optionally
1207             *         <code>null</code>)
1208             * @return the matching organizations ordered by comparator <code>obc</code>
1209             * @throws SystemException if a system exception occurred
1210             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1211             */
1212            @Override
1213            public List<Organization> search(
1214                            long companyId, long parentOrganizationId, String keywords,
1215                            String type, Long regionId, Long countryId,
1216                            LinkedHashMap<String, Object> params, int start, int end,
1217                            OrderByComparator obc)
1218                    throws SystemException {
1219    
1220                    String parentOrganizationIdComparator = StringPool.EQUAL;
1221    
1222                    if (parentOrganizationId ==
1223                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1224    
1225                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1226                    }
1227    
1228                    return organizationFinder.findByKeywords(
1229                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1230                            keywords, type, regionId, countryId, params, start, end, obc);
1231            }
1232    
1233            /**
1234             * Returns a name ordered range of all the organizations with the type,
1235             * region, and country, and whose name, street, city, and zipcode match the
1236             * keywords specified for them, without using the indexer. It is preferable
1237             * to use the indexed version {@link #search(long, long, String, String,
1238             * String, String, String, String, String, LinkedHashMap, boolean, int, int,
1239             * Sort)} instead of this method wherever possible for performance reasons.
1240             *
1241             * <p>
1242             * Useful when paginating results. Returns a maximum of <code>end -
1243             * start</code> instances. <code>start</code> and <code>end</code> are not
1244             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1245             * refers to the first result in the set. Setting both <code>start</code>
1246             * and <code>end</code> to {@link
1247             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1248             * result set.
1249             * </p>
1250             *
1251             * @param  companyId the primary key of the organization's company
1252             * @param  parentOrganizationId the primary key of the organization's parent
1253             * @param  name the name keywords (space separated, optionally
1254             *         <code>null</code>)
1255             * @param  type the organization's type (optionally <code>null</code>)
1256             * @param  street the street keywords (optionally <code>null</code>)
1257             * @param  city the city keywords (optionally <code>null</code>)
1258             * @param  zip the zipcode keywords (optionally <code>null</code>)
1259             * @param  regionId the primary key of the organization's region (optionally
1260             *         <code>null</code>)
1261             * @param  countryId the primary key of the organization's country
1262             *         (optionally <code>null</code>)
1263             * @param  params the finder parameters (optionally <code>null</code>). For
1264             *         more information see {@link
1265             *         com.liferay.portal.service.persistence.OrganizationFinder}
1266             * @param  andOperator whether every field must match its keywords, or just
1267             *         one field. For example, &quot;organizations with the name
1268             *         'Employees' and city 'Chicago'&quot; vs &quot;organizations with
1269             *         the name 'Employees' or the city 'Chicago'&quot;.
1270             * @param  start the lower bound of the range of organizations to return
1271             * @param  end the upper bound of the range of organizations to return (not
1272             *         inclusive)
1273             * @return the matching organizations ordered by name
1274             * @throws SystemException if a system exception occurred
1275             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1276             */
1277            @Override
1278            public List<Organization> search(
1279                            long companyId, long parentOrganizationId, String name, String type,
1280                            String street, String city, String zip, Long regionId,
1281                            Long countryId, LinkedHashMap<String, Object> params,
1282                            boolean andOperator, int start, int end)
1283                    throws SystemException {
1284    
1285                    return search(
1286                            companyId, parentOrganizationId, name, type, street, city, zip,
1287                            regionId, countryId, params, andOperator, start, end,
1288                            new OrganizationNameComparator(true));
1289            }
1290    
1291            /**
1292             * Returns an ordered range of all the organizations with the type, region,
1293             * and country, and whose name, street, city, and zipcode match the keywords
1294             * specified for them, without using the indexer. It is preferable to use
1295             * the indexed version {@link #search(long, long, String, String, String,
1296             * String, String, String, String, LinkedHashMap, boolean, int, int, Sort)}
1297             * instead of this method wherever possible for performance reasons.
1298             *
1299             * <p>
1300             * Useful when paginating results. Returns a maximum of <code>end -
1301             * start</code> instances. <code>start</code> and <code>end</code> are not
1302             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1303             * refers to the first result in the set. Setting both <code>start</code>
1304             * and <code>end</code> to {@link
1305             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1306             * result set.
1307             * </p>
1308             *
1309             * @param  companyId the primary key of the organization's company
1310             * @param  parentOrganizationId the primary key of the organization's parent
1311             *         organization
1312             * @param  name the name keywords (space separated, optionally
1313             *         <code>null</code>)
1314             * @param  type the organization's type (optionally <code>null</code>)
1315             * @param  street the street keywords (optionally <code>null</code>)
1316             * @param  city the city keywords (optionally <code>null</code>)
1317             * @param  zip the zipcode keywords (optionally <code>null</code>)
1318             * @param  regionId the primary key of the organization's region (optionally
1319             *         <code>null</code>)
1320             * @param  countryId the primary key of the organization's country
1321             *         (optionally <code>null</code>)
1322             * @param  params the finder parameters (optionally <code>null</code>). For
1323             *         more information see {@link
1324             *         com.liferay.portal.service.persistence.OrganizationFinder}
1325             * @param  andOperator whether every field must match its keywords, or just
1326             *         one field. For example, &quot;organizations with the name
1327             *         'Employees' and city 'Chicago'&quot; vs &quot;organizations with
1328             *         the name 'Employees' or the city 'Chicago'&quot;.
1329             * @param  start the lower bound of the range of organizations to return
1330             * @param  end the upper bound of the range of organizations to return (not
1331             *         inclusive)
1332             * @param  obc the comparator to order the organizations (optionally
1333             *         <code>null</code>)
1334             * @return the matching organizations ordered by comparator <code>obc</code>
1335             * @throws SystemException if a system exception occurred
1336             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1337             */
1338            @Override
1339            public List<Organization> search(
1340                            long companyId, long parentOrganizationId, String name, String type,
1341                            String street, String city, String zip, Long regionId,
1342                            Long countryId, LinkedHashMap<String, Object> params,
1343                            boolean andOperator, int start, int end, OrderByComparator obc)
1344                    throws SystemException {
1345    
1346                    String parentOrganizationIdComparator = StringPool.EQUAL;
1347    
1348                    if (parentOrganizationId ==
1349                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1350    
1351                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1352                    }
1353    
1354                    return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
1355                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1356                            name, type, street, city, zip, regionId, countryId, params,
1357                            andOperator, start, end, obc);
1358            }
1359    
1360            /**
1361             * Returns an ordered range of all the organizations whose name, type, or
1362             * location fields match the keywords specified for them, using the indexer.
1363             * It is preferable to use this method instead of the non-indexed version
1364             * whenever possible for performance reasons.
1365             *
1366             * <p>
1367             * Useful when paginating results. Returns a maximum of <code>end -
1368             * start</code> instances. <code>start</code> and <code>end</code> are not
1369             * primary keys, they are indexes in the result set. Thus, <code>0</code>
1370             * refers to the first result in the set. Setting both <code>start</code>
1371             * and <code>end</code> to {@link
1372             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
1373             * result set.
1374             * </p>
1375             *
1376             * @param  companyId the primary key of the organization's company
1377             * @param  parentOrganizationId the primary key of the organization's parent
1378             *         organization
1379             * @param  name the name keywords (space separated, optionally
1380             *         <code>null</code>)
1381             * @param  type the type keywords (optionally <code>null</code>)
1382             * @param  street the street keywords (optionally <code>null</code>)
1383             * @param  city the city keywords (optionally <code>null</code>)
1384             * @param  zip the zipcode keywords (optionally <code>null</code>)
1385             * @param  region the region keywords (optionally <code>null</code>)
1386             * @param  country the country keywords (optionally <code>null</code>)
1387             * @param  params the finder parameters (optionally <code>null</code>). For
1388             *         more information see {@link
1389             *         com.liferay.portlet.usersadmin.util.OrganizationIndexer}.
1390             * @param  andSearch whether every field must match its keywords or just one
1391             *         field
1392             * @param  start the lower bound of the range of organizations to return
1393             * @param  end the upper bound of the range of organizations to return (not
1394             *         inclusive)
1395             * @param  sort the field and direction by which to sort (optionally
1396             *         <code>null</code>)
1397             * @return the matching organizations ordered by <code>sort</code>
1398             * @throws SystemException if a system exception occurred
1399             * @see    com.liferay.portlet.usersadmin.util.OrganizationIndexer
1400             */
1401            @Override
1402            public Hits search(
1403                            long companyId, long parentOrganizationId, String name, String type,
1404                            String street, String city, String zip, String region,
1405                            String country, LinkedHashMap<String, Object> params,
1406                            boolean andSearch, int start, int end, Sort sort)
1407                    throws SystemException {
1408    
1409                    try {
1410                            SearchContext searchContext = new SearchContext();
1411    
1412                            searchContext.setAndSearch(andSearch);
1413    
1414                            Map<String, Serializable> attributes =
1415                                    new HashMap<String, Serializable>();
1416    
1417                            attributes.put("city", city);
1418                            attributes.put("country", country);
1419                            attributes.put("name", name);
1420                            attributes.put("params", params);
1421                            attributes.put(
1422                                    "parentOrganizationId", String.valueOf(parentOrganizationId));
1423                            attributes.put("region", region);
1424                            attributes.put("street", street);
1425                            attributes.put("type", type);
1426                            attributes.put("zip", zip);
1427    
1428                            searchContext.setAttributes(attributes);
1429    
1430                            searchContext.setCompanyId(companyId);
1431                            searchContext.setEnd(end);
1432    
1433                            if (params != null) {
1434                                    String keywords = (String)params.remove("keywords");
1435    
1436                                    if (Validator.isNotNull(keywords)) {
1437                                            searchContext.setKeywords(keywords);
1438                                    }
1439                            }
1440    
1441                            QueryConfig queryConfig = new QueryConfig();
1442    
1443                            queryConfig.setHighlightEnabled(false);
1444                            queryConfig.setScoreEnabled(false);
1445    
1446                            searchContext.setQueryConfig(queryConfig);
1447    
1448                            if (sort != null) {
1449                                    searchContext.setSorts(sort);
1450                            }
1451    
1452                            searchContext.setStart(start);
1453    
1454                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1455                                    Organization.class);
1456    
1457                            return indexer.search(searchContext);
1458                    }
1459                    catch (Exception e) {
1460                            throw new SystemException(e);
1461                    }
1462            }
1463    
1464            /**
1465             * Returns the number of organizations that match the keywords, type,
1466             * region, and country.
1467             *
1468             * @param  companyId the primary key of the organization's company
1469             * @param  parentOrganizationId the primary key of the organization's parent
1470             *         organization
1471             * @param  keywords the keywords (space separated), which may occur in the
1472             *         organization's name, street, city, or zipcode (optionally
1473             *         <code>null</code>)
1474             * @param  type the organization's type (optionally <code>null</code>)
1475             * @param  regionId the primary key of the organization's region (optionally
1476             *         <code>null</code>)
1477             * @param  countryId the primary key of the organization's country
1478             *         (optionally <code>null</code>)
1479             * @param  params the finder parameters (optionally <code>null</code>). For
1480             *         more information see {@link
1481             *         com.liferay.portal.service.persistence.OrganizationFinder}
1482             * @return the number of matching organizations
1483             * @throws SystemException if a system exception occurred
1484             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1485             */
1486            @Override
1487            public int searchCount(
1488                            long companyId, long parentOrganizationId, String keywords,
1489                            String type, Long regionId, Long countryId,
1490                            LinkedHashMap<String, Object> params)
1491                    throws SystemException {
1492    
1493                    String parentOrganizationIdComparator = StringPool.EQUAL;
1494    
1495                    if (parentOrganizationId ==
1496                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1497    
1498                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1499                    }
1500    
1501                    return organizationFinder.countByKeywords(
1502                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1503                            keywords, type, regionId, countryId, params);
1504            }
1505    
1506            /**
1507             * Returns the number of organizations with the type, region, and country,
1508             * and whose name, street, city, and zipcode match the keywords specified
1509             * for them.
1510             *
1511             * @param  companyId the primary key of the organization's company
1512             * @param  parentOrganizationId the primary key of the organization's parent
1513             *         organization
1514             * @param  name the name keywords (space separated, optionally
1515             *         <code>null</code>)
1516             * @param  type the organization's type (optionally <code>null</code>)
1517             * @param  street the street keywords (optionally <code>null</code>)
1518             * @param  city the city keywords (optionally <code>null</code>)
1519             * @param  zip the zipcode keywords (optionally <code>null</code>)
1520             * @param  regionId the primary key of the organization's region (optionally
1521             *         <code>null</code>)
1522             * @param  countryId the primary key of the organization's country
1523             *         (optionally <code>null</code>)
1524             * @param  params the finder parameters (optionally <code>null</code>). For
1525             *         more information see {@link
1526             *         com.liferay.portal.service.persistence.OrganizationFinder}
1527             * @param  andOperator whether every field must match its keywords, or just
1528             *         one field. For example, &quot;organizations with the name
1529             *         'Employees' and city 'Chicago'&quot; vs &quot;organizations with
1530             *         the name 'Employees' or the city 'Chicago'&quot;.
1531             * @return the number of matching organizations
1532             * @throws SystemException if a system exception occurred
1533             * @see    com.liferay.portal.service.persistence.OrganizationFinder
1534             */
1535            @Override
1536            public int searchCount(
1537                            long companyId, long parentOrganizationId, String name, String type,
1538                            String street, String city, String zip, Long regionId,
1539                            Long countryId, LinkedHashMap<String, Object> params,
1540                            boolean andOperator)
1541                    throws SystemException {
1542    
1543                    String parentOrganizationIdComparator = StringPool.EQUAL;
1544    
1545                    if (parentOrganizationId ==
1546                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
1547    
1548                            parentOrganizationIdComparator = StringPool.NOT_EQUAL;
1549                    }
1550    
1551                    return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
1552                            companyId, parentOrganizationId, parentOrganizationIdComparator,
1553                            name, type, street, city, zip, regionId, countryId, params,
1554                            andOperator);
1555            }
1556    
1557            /**
1558             * Sets the organizations in the group, removing and adding organizations to
1559             * the group as necessary.
1560             *
1561             * @param  groupId the primary key of the group
1562             * @param  organizationIds the primary keys of the organizations
1563             * @throws PortalException if a portal exception occurred
1564             * @throws SystemException if a system exception occurred
1565             */
1566            @Override
1567            public void setGroupOrganizations(long groupId, long[] organizationIds)
1568                    throws PortalException, SystemException {
1569    
1570                    groupPersistence.setOrganizations(groupId, organizationIds);
1571    
1572                    PermissionCacheUtil.clearCache();
1573            }
1574    
1575            /**
1576             * Removes the organizations from the group.
1577             *
1578             * @param  groupId the primary key of the group
1579             * @param  organizationIds the primary keys of the organizations
1580             * @throws PortalException if a portal exception occurred
1581             * @throws SystemException if a system exception occurred
1582             */
1583            @Override
1584            public void unsetGroupOrganizations(long groupId, long[] organizationIds)
1585                    throws PortalException, SystemException {
1586    
1587                    groupPersistence.removeOrganizations(groupId, organizationIds);
1588    
1589                    PermissionCacheUtil.clearCache();
1590            }
1591    
1592            /**
1593             * Removes the organizations from the password policy.
1594             *
1595             * @param  passwordPolicyId the primary key of the password policy
1596             * @param  organizationIds the primary keys of the organizations
1597             * @throws SystemException if a system exception occurred
1598             */
1599            @Override
1600            public void unsetPasswordPolicyOrganizations(
1601                            long passwordPolicyId, long[] organizationIds)
1602                    throws SystemException {
1603    
1604                    passwordPolicyRelLocalService.deletePasswordPolicyRels(
1605                            passwordPolicyId, Organization.class.getName(), organizationIds);
1606            }
1607    
1608            /**
1609             * Updates the organization's asset with the new asset categories and tag
1610             * names, removing and adding asset categories and tag names as necessary.
1611             *
1612             * @param  userId the primary key of the user
1613             * @param  organization the organization
1614             * @param  assetCategoryIds the primary keys of the asset categories
1615             * @param  assetTagNames the asset tag names
1616             * @throws PortalException if a user with the primary key could not be found
1617             * @throws SystemException if a system exception occurred
1618             */
1619            @Override
1620            public void updateAsset(
1621                            long userId, Organization organization, long[] assetCategoryIds,
1622                            String[] assetTagNames)
1623                    throws PortalException, SystemException {
1624    
1625                    User user = userPersistence.findByPrimaryKey(userId);
1626    
1627                    Company company = companyPersistence.findByPrimaryKey(
1628                            user.getCompanyId());
1629    
1630                    Group companyGroup = company.getGroup();
1631    
1632                    assetEntryLocalService.updateEntry(
1633                            userId, companyGroup.getGroupId(), null, null,
1634                            Organization.class.getName(), organization.getOrganizationId(),
1635                            organization.getUuid(), 0, assetCategoryIds, assetTagNames, false,
1636                            null, null, null, null, organization.getName(), StringPool.BLANK,
1637                            null, null, null, 0, 0, null, false);
1638            }
1639    
1640            /**
1641             * Updates the organization.
1642             *
1643             * @param      companyId the primary key of the organization's company
1644             * @param      organizationId the primary key of the organization
1645             * @param      parentOrganizationId the primary key of organization's parent
1646             *             organization
1647             * @param      name the organization's name
1648             * @param      type the organization's type
1649             * @param      recursable whether permissions of the organization are to be
1650             *             inherited by its suborganizations
1651             * @param      regionId the primary key of the organization's region
1652             * @param      countryId the primary key of the organization's country
1653             * @param      statusId the organization's workflow status
1654             * @param      comments the comments about the organization
1655             * @param      site whether the organization is to be associated with a main
1656             *             site
1657             * @param      serviceContext the service context to be applied (optionally
1658             *             <code>null</code>). Can set asset category IDs and asset tag
1659             *             names for the organization, and merge expando bridge
1660             *             attributes for the organization.
1661             * @return     the organization
1662             * @throws     PortalException if an organization or parent organization
1663             *             with the primary key could not be found or if the new
1664             *             information was invalid
1665             * @throws     SystemException if a system exception occurred
1666             * @deprecated As of 6.2.0, replaced by {@link #updateOrganization(long,
1667             *             long, long, String, String, long, long, int, String, boolean,
1668             *             ServiceContext)}
1669             */
1670            @Override
1671            public Organization updateOrganization(
1672                            long companyId, long organizationId, long parentOrganizationId,
1673                            String name, String type, boolean recursable, long regionId,
1674                            long countryId, int statusId, String comments, boolean site,
1675                            ServiceContext serviceContext)
1676                    throws PortalException, SystemException {
1677    
1678                    return updateOrganization(
1679                            companyId, organizationId, parentOrganizationId, name, type,
1680                            regionId, countryId, statusId, comments, site, serviceContext);
1681            }
1682    
1683            /**
1684             * Updates the organization.
1685             *
1686             * @param  companyId the primary key of the organization's company
1687             * @param  organizationId the primary key of the organization
1688             * @param  parentOrganizationId the primary key of organization's parent
1689             *         organization
1690             * @param  name the organization's name
1691             * @param  type the organization's type
1692             * @param  regionId the primary key of the organization's region
1693             * @param  countryId the primary key of the organization's country
1694             * @param  statusId the organization's workflow status
1695             * @param  comments the comments about the organization
1696             * @param  site whether the organization is to be associated with a main
1697             *         site
1698             * @param  serviceContext the service context to be applied (optionally
1699             *         <code>null</code>). Can set asset category IDs and asset tag
1700             *         names for the organization, and merge expando bridge attributes
1701             *         for the organization.
1702             * @return the organization
1703             * @throws PortalException if an organization or parent organization with
1704             *         the primary key could not be found or if the new information was
1705             *         invalid
1706             * @throws SystemException if a system exception occurred
1707             */
1708            @Override
1709            public Organization updateOrganization(
1710                            long companyId, long organizationId, long parentOrganizationId,
1711                            String name, String type, long regionId, long countryId,
1712                            int statusId, String comments, boolean site,
1713                            ServiceContext serviceContext)
1714                    throws PortalException, SystemException {
1715    
1716                    // Organization
1717    
1718                    parentOrganizationId = getParentOrganizationId(
1719                            companyId, parentOrganizationId);
1720    
1721                    validate(
1722                            companyId, organizationId, parentOrganizationId, name, type,
1723                            countryId, statusId);
1724    
1725                    Organization organization = organizationPersistence.findByPrimaryKey(
1726                            organizationId);
1727    
1728                    long oldParentOrganizationId = organization.getParentOrganizationId();
1729                    String oldName = organization.getName();
1730    
1731                    organization.setModifiedDate(new Date());
1732                    organization.setParentOrganizationId(parentOrganizationId);
1733                    organization.setTreePath(organization.buildTreePath());
1734                    organization.setName(name);
1735                    organization.setType(type);
1736                    organization.setRecursable(true);
1737                    organization.setRegionId(regionId);
1738                    organization.setCountryId(countryId);
1739                    organization.setStatusId(statusId);
1740                    organization.setComments(comments);
1741                    organization.setExpandoBridgeAttributes(serviceContext);
1742    
1743                    organizationPersistence.update(organization);
1744    
1745                    // Group
1746    
1747                    Group group = organization.getGroup();
1748    
1749                    long parentGroupId = group.getParentGroupId();
1750    
1751                    boolean organizationGroup = isOrganizationGroup(
1752                            oldParentOrganizationId, group.getParentGroupId());
1753    
1754                    if (organizationGroup) {
1755                            if (parentOrganizationId !=
1756                                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1757    
1758                                    Organization parentOrganization =
1759                                            organizationPersistence.fetchByPrimaryKey(
1760                                                    parentOrganizationId);
1761    
1762                                    Group parentGroup = parentOrganization.getGroup();
1763    
1764                                    if (site && parentGroup.isSite()) {
1765                                            parentGroupId = parentOrganization.getGroupId();
1766                                    }
1767                                    else {
1768                                            parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
1769                                    }
1770                            }
1771                            else {
1772                                    parentGroupId = GroupConstants.DEFAULT_PARENT_GROUP_ID;
1773                            }
1774                    }
1775    
1776                    if (!oldName.equals(name) || organizationGroup) {
1777                            groupLocalService.updateGroup(
1778                                    group.getGroupId(), parentGroupId, name, group.getDescription(),
1779                                    group.getType(), group.isManualMembership(),
1780                                    group.getMembershipRestriction(), group.getFriendlyURL(),
1781                                    group.isActive(), null);
1782                    }
1783    
1784                    if (group.isSite() != site) {
1785                            groupLocalService.updateSite(group.getGroupId(), site);
1786                    }
1787    
1788                    // Asset
1789    
1790                    if (serviceContext != null) {
1791                            updateAsset(
1792                                    serviceContext.getUserId(), organization,
1793                                    serviceContext.getAssetCategoryIds(),
1794                                    serviceContext.getAssetTagNames());
1795                    }
1796    
1797                    // Indexer
1798    
1799                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1800                            Organization.class);
1801    
1802                    if (oldParentOrganizationId != parentOrganizationId) {
1803                            long[] organizationIds = getReindexOrganizationIds(organization);
1804    
1805                            indexer.reindex(organizationIds);
1806                    }
1807                    else {
1808                            indexer.reindex(organization);
1809                    }
1810    
1811                    return organization;
1812            }
1813    
1814            protected void addSuborganizations(
1815                            List<Organization> allSuborganizations,
1816                            List<Organization> organizations)
1817                    throws SystemException {
1818    
1819                    for (Organization organization : organizations) {
1820                            if (!allSuborganizations.contains(organization)) {
1821                                    allSuborganizations.add(organization);
1822    
1823                                    List<Organization> suborganizations =
1824                                            organizationPersistence.findByC_P(
1825                                                    organization.getCompanyId(),
1826                                                    organization.getOrganizationId());
1827    
1828                                    addSuborganizations(allSuborganizations, suborganizations);
1829                            }
1830                    }
1831            }
1832    
1833            protected long getParentOrganizationId(
1834                            long companyId, long parentOrganizationId)
1835                    throws SystemException {
1836    
1837                    if (parentOrganizationId !=
1838                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1839    
1840                            // Ensure parent organization exists and belongs to the proper
1841                            // company
1842    
1843                            Organization parentOrganization =
1844                                    organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
1845    
1846                            if ((parentOrganization == null) ||
1847                                    (companyId != parentOrganization.getCompanyId())) {
1848    
1849                                    parentOrganizationId =
1850                                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
1851                            }
1852                    }
1853    
1854                    return parentOrganizationId;
1855            }
1856    
1857            protected long[] getReindexOrganizationIds(Organization organization)
1858                    throws PortalException, SystemException {
1859    
1860                    List<Organization> organizationsTree = new ArrayList<Organization>();
1861    
1862                    organizationsTree.add(organization);
1863    
1864                    LinkedHashMap<String, Object> params =
1865                            new LinkedHashMap<String, Object>();
1866    
1867                    params.put("organizationsTree", organizationsTree);
1868    
1869                    List<Organization> organizations = search(
1870                            organization.getCompanyId(), params, QueryUtil.ALL_POS,
1871                            QueryUtil.ALL_POS);
1872    
1873                    long[] organizationIds = new long[organizations.size()];
1874    
1875                    for (int i = 0; i < organizations.size(); i++) {
1876                            Organization curOrganization = organizations.get(i);
1877    
1878                            curOrganization.setTreePath(curOrganization.buildTreePath());
1879    
1880                            organizationPersistence.update(curOrganization);
1881    
1882                            organizationIds[i] = curOrganization.getOrganizationId();
1883                    }
1884    
1885                    if (!ArrayUtil.contains(
1886                                    organizationIds, organization.getOrganizationId())) {
1887    
1888                            organizationIds = ArrayUtil.append(
1889                                    organizationIds, organization.getOrganizationId());
1890                    }
1891    
1892                    return organizationIds;
1893            }
1894    
1895            protected boolean isOrganizationGroup(long organizationId, long groupId)
1896                    throws SystemException {
1897    
1898                    if ((organizationId ==
1899                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) &&
1900                            (groupId == GroupConstants.DEFAULT_PARENT_GROUP_ID)) {
1901    
1902                            return true;
1903                    }
1904    
1905                    if (organizationId !=
1906                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1907    
1908                            Organization organization =
1909                                    organizationPersistence.fetchByPrimaryKey(organizationId);
1910    
1911                            if (organization.getGroupId() == groupId) {
1912                                    return true;
1913                            }
1914                    }
1915    
1916                    return false;
1917            }
1918    
1919            protected boolean isParentOrganization(
1920                            long parentOrganizationId, long organizationId)
1921                    throws PortalException, SystemException {
1922    
1923                    // Return true if parentOrganizationId is among the parent organizatons
1924                    // of organizationId
1925    
1926                    if (organizationId ==
1927                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1928    
1929                            return false;
1930                    }
1931    
1932                    Organization organization = organizationPersistence.findByPrimaryKey(
1933                            organizationId);
1934    
1935                    String treePath = organization.getTreePath();
1936    
1937                    if (treePath.contains(
1938                                    StringPool.SLASH + parentOrganizationId + StringPool.SLASH)) {
1939    
1940                            return true;
1941                    }
1942                    else {
1943                            return false;
1944                    }
1945            }
1946    
1947            protected void validate(
1948                            long companyId, long organizationId, long parentOrganizationId,
1949                            String name, String type, long countryId, int statusId)
1950                    throws PortalException, SystemException {
1951    
1952                    if (!ArrayUtil.contains(PropsValues.ORGANIZATIONS_TYPES, type)) {
1953                            throw new OrganizationTypeException(
1954                                    "Invalid organization type " + type);
1955                    }
1956    
1957                    if (parentOrganizationId ==
1958                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
1959    
1960                            if (!OrganizationImpl.isRootable(type)) {
1961                                    throw new OrganizationParentException(
1962                                            "Organization of type " + type + " cannot be a root");
1963                            }
1964                    }
1965                    else {
1966                            Organization parentOrganization =
1967                                    organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
1968    
1969                            if (parentOrganization == null) {
1970                                    throw new OrganizationParentException(
1971                                            "Organization " + parentOrganizationId + " doesn't exist");
1972                            }
1973    
1974                            String[] childrenTypes = OrganizationImpl.getChildrenTypes(
1975                                    parentOrganization.getType());
1976    
1977                            if (childrenTypes.length == 0) {
1978                                    throw new OrganizationParentException(
1979                                            "Organization of type " + type + " cannot have children");
1980                            }
1981    
1982                            if ((companyId != parentOrganization.getCompanyId()) ||
1983                                    (parentOrganizationId == organizationId)) {
1984    
1985                                    throw new OrganizationParentException();
1986                            }
1987    
1988                            if (!ArrayUtil.contains(childrenTypes, type)) {
1989                                    throw new OrganizationParentException(
1990                                            "Type " + type + " not allowed as child of " +
1991                                                    parentOrganization.getType());
1992                            }
1993                    }
1994    
1995                    if ((organizationId > 0) &&
1996                            (parentOrganizationId !=
1997                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
1998    
1999                            // Prevent circular organizational references
2000    
2001                            if (isParentOrganization(organizationId, parentOrganizationId)) {
2002                                    throw new OrganizationParentException();
2003                            }
2004                    }
2005    
2006                    if (Validator.isNull(name)) {
2007                            throw new OrganizationNameException();
2008                    }
2009                    else {
2010                            Organization organization = organizationPersistence.fetchByC_N(
2011                                    companyId, name);
2012    
2013                            if ((organization != null) &&
2014                                    StringUtil.equalsIgnoreCase(organization.getName(), name)) {
2015    
2016                                    if ((organizationId <= 0) ||
2017                                            (organization.getOrganizationId() != organizationId)) {
2018    
2019                                            throw new DuplicateOrganizationException(
2020                                                    "There is another organization named " + name);
2021                                    }
2022                            }
2023                    }
2024    
2025                    boolean countryRequired = GetterUtil.getBoolean(
2026                            PropsUtil.get(
2027                                    PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
2028    
2029                    if (countryRequired || (countryId > 0)) {
2030                            countryPersistence.findByPrimaryKey(countryId);
2031                    }
2032    
2033                    listTypeService.validate(
2034                            statusId, ListTypeConstants.ORGANIZATION_STATUS);
2035            }
2036    
2037            protected void validate(
2038                            long companyId, long parentOrganizationId, String name, String type,
2039                            long countryId, int statusId)
2040                    throws PortalException, SystemException {
2041    
2042                    validate(
2043                            companyId, 0, parentOrganizationId, name, type, countryId,
2044                            statusId);
2045            }
2046    
2047    }