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