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