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