1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.DuplicateOrganizationException;
18  import com.liferay.portal.OrganizationNameException;
19  import com.liferay.portal.OrganizationParentException;
20  import com.liferay.portal.RequiredOrganizationException;
21  import com.liferay.portal.kernel.cache.ThreadLocalCachable;
22  import com.liferay.portal.kernel.configuration.Filter;
23  import com.liferay.portal.kernel.dao.orm.QueryUtil;
24  import com.liferay.portal.kernel.exception.PortalException;
25  import com.liferay.portal.kernel.exception.SystemException;
26  import com.liferay.portal.kernel.util.ArrayUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.PropsKeys;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Company;
33  import com.liferay.portal.model.Group;
34  import com.liferay.portal.model.LayoutSet;
35  import com.liferay.portal.model.ListTypeConstants;
36  import com.liferay.portal.model.Organization;
37  import com.liferay.portal.model.OrganizationConstants;
38  import com.liferay.portal.model.ResourceConstants;
39  import com.liferay.portal.model.Role;
40  import com.liferay.portal.model.RoleConstants;
41  import com.liferay.portal.model.User;
42  import com.liferay.portal.model.impl.OrganizationImpl;
43  import com.liferay.portal.security.permission.PermissionCacheUtil;
44  import com.liferay.portal.service.ServiceContext;
45  import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
46  import com.liferay.portal.util.PropsUtil;
47  import com.liferay.portal.util.PropsValues;
48  import com.liferay.portal.util.comparator.OrganizationNameComparator;
49  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
50  import com.liferay.portlet.expando.model.ExpandoBridge;
51  
52  import java.util.ArrayList;
53  import java.util.Iterator;
54  import java.util.LinkedHashMap;
55  import java.util.List;
56  
57  /**
58   * <a href="OrganizationLocalServiceImpl.java.html"><b><i>View Source</i></b>
59   * </a>
60   *
61   * @author Brian Wing Shun Chan
62   * @author Jorge Ferrer
63   * @author Julio Camarero
64   */
65  public class OrganizationLocalServiceImpl
66      extends OrganizationLocalServiceBaseImpl {
67  
68      public void addGroupOrganizations(long groupId, long[] organizationIds)
69          throws SystemException {
70  
71          groupPersistence.addOrganizations(groupId, organizationIds);
72  
73          PermissionCacheUtil.clearCache();
74      }
75  
76      public Organization addOrganization(
77              long userId, long parentOrganizationId, String name,
78              String type, boolean recursable, long regionId, long countryId,
79              int statusId, String comments, ServiceContext serviceContext)
80          throws PortalException, SystemException {
81  
82          // Organization
83  
84          User user = userPersistence.findByPrimaryKey(userId);
85          parentOrganizationId = getParentOrganizationId(
86              user.getCompanyId(), parentOrganizationId);
87          recursable = true;
88  
89          validate(
90              user.getCompanyId(), parentOrganizationId, name, type, countryId,
91              statusId);
92  
93          long organizationId = counterLocalService.increment();
94  
95          Organization organization = organizationPersistence.create(
96              organizationId);
97  
98          organization.setCompanyId(user.getCompanyId());
99          organization.setParentOrganizationId(parentOrganizationId);
100         organization.setName(name);
101         organization.setType(type);
102         organization.setRecursable(recursable);
103         organization.setRegionId(regionId);
104         organization.setCountryId(countryId);
105         organization.setStatusId(statusId);
106         organization.setComments(comments);
107 
108         organizationPersistence.update(organization, false);
109 
110         // Group
111 
112         Group group = groupLocalService.addGroup(
113             userId, Organization.class.getName(), organizationId, null, null, 0,
114             null, true, null);
115 
116         if (PropsValues.ORGANIZATIONS_ASSIGNMENT_AUTO) {
117 
118             // Role
119 
120             Role role = roleLocalService.getRole(
121                 organization.getCompanyId(), RoleConstants.ORGANIZATION_OWNER);
122 
123             userGroupRoleLocalService.addUserGroupRoles(
124                 userId, group.getGroupId(), new long[] {role.getRoleId()});
125 
126             // User
127 
128             userPersistence.addOrganization(userId, organizationId);
129         }
130 
131         // Resources
132 
133         addOrganizationResources(userId, organization);
134 
135         // Asset
136 
137         if (serviceContext != null) {
138             updateAsset(
139                 userId, organization, serviceContext.getAssetCategoryIds(),
140                 serviceContext.getAssetTagNames());
141         }
142 
143         // Expando
144 
145         ExpandoBridge expandoBridge = organization.getExpandoBridge();
146 
147         expandoBridge.setAttributes(serviceContext);
148 
149         return organization;
150     }
151 
152     public void addOrganizationResources(long userId, Organization organization)
153         throws PortalException, SystemException {
154 
155         String name = Organization.class.getName();
156 
157         resourceLocalService.addResources(
158             organization.getCompanyId(), 0, userId, name,
159             organization.getOrganizationId(), false, false, false);
160     }
161 
162     public void addPasswordPolicyOrganizations(
163             long passwordPolicyId, long[] organizationIds)
164         throws SystemException {
165 
166         passwordPolicyRelLocalService.addPasswordPolicyRels(
167             passwordPolicyId, Organization.class.getName(), organizationIds);
168     }
169 
170     public void deleteLogo(long organizationId)
171         throws PortalException, SystemException {
172 
173         Organization organization = getOrganization(organizationId);
174 
175         Group group = organization.getGroup();
176 
177         LayoutSet publicLayoutSet = layoutSetLocalService.getLayoutSet(
178             group.getGroupId(), false);
179 
180         if (publicLayoutSet.isLogo()) {
181             long logoId = publicLayoutSet.getLogoId();
182 
183             publicLayoutSet.setLogo(false);
184             publicLayoutSet.setLogoId(0);
185 
186             layoutSetPersistence.update(publicLayoutSet, false);
187 
188             imageLocalService.deleteImage(logoId);
189         }
190 
191         LayoutSet privateLayoutSet = layoutSetLocalService.getLayoutSet(
192             group.getGroupId(), true);
193 
194         if (privateLayoutSet.isLogo()) {
195             long logoId = privateLayoutSet.getLogoId();
196 
197             privateLayoutSet.setLogo(false);
198             privateLayoutSet.setLogoId(0);
199 
200             layoutSetPersistence.update(publicLayoutSet, false);
201 
202             if (imageLocalService.getImage(logoId) != null) {
203                 imageLocalService.deleteImage(logoId);
204             }
205         }
206     }
207 
208     public void deleteOrganization(long organizationId)
209         throws PortalException, SystemException {
210 
211         Organization organization = organizationPersistence.findByPrimaryKey(
212             organizationId);
213 
214         if ((userLocalService.getOrganizationUsersCount(
215                 organization.getOrganizationId(), true) > 0) ||
216             (organizationPersistence.countByC_P(
217                 organization.getCompanyId(),
218                 organization.getOrganizationId()) > 0)) {
219 
220             throw new RequiredOrganizationException();
221         }
222 
223         // Asset
224 
225         assetEntryLocalService.deleteEntry(
226             Organization.class.getName(), organization.getOrganizationId());
227 
228         // Addresses
229 
230         addressLocalService.deleteAddresses(
231             organization.getCompanyId(), Organization.class.getName(),
232             organization.getOrganizationId());
233 
234         // Email addresses
235 
236         emailAddressLocalService.deleteEmailAddresses(
237             organization.getCompanyId(), Organization.class.getName(),
238             organization.getOrganizationId());
239 
240         // Expando
241 
242         expandoValueLocalService.deleteValues(
243             Organization.class.getName(), organization.getOrganizationId());
244 
245         // Password policy relation
246 
247         passwordPolicyRelLocalService.deletePasswordPolicyRel(
248             Organization.class.getName(), organization.getOrganizationId());
249 
250         // Phone
251 
252         phoneLocalService.deletePhones(
253             organization.getCompanyId(), Organization.class.getName(),
254             organization.getOrganizationId());
255 
256         // Website
257 
258         websiteLocalService.deleteWebsites(
259             organization.getCompanyId(), Organization.class.getName(),
260             organization.getOrganizationId());
261 
262         // Group
263 
264         Group group = organization.getGroup();
265 
266         groupLocalService.deleteGroup(group.getGroupId());
267 
268         // Resources
269 
270         String name = Organization.class.getName();
271 
272         resourceLocalService.deleteResource(
273             organization.getCompanyId(), name,
274             ResourceConstants.SCOPE_INDIVIDUAL,
275             organization.getOrganizationId());
276 
277         // Organization
278 
279         organizationPersistence.remove(organization);
280 
281         // Permission cache
282 
283         PermissionCacheUtil.clearCache();
284     }
285 
286     public List<Organization> getGroupOrganizations(long groupId)
287         throws SystemException {
288 
289         return groupPersistence.getOrganizations(groupId);
290     }
291 
292     public Organization getOrganization(long organizationId)
293         throws PortalException, SystemException {
294 
295         return organizationPersistence.findByPrimaryKey(organizationId);
296     }
297 
298     public Organization getOrganization(long companyId, String name)
299         throws PortalException, SystemException {
300 
301         return organizationPersistence.findByC_N(companyId, name);
302     }
303 
304     public long getOrganizationId(long companyId, String name)
305         throws SystemException {
306 
307         Organization organization = organizationPersistence.fetchByC_N(
308             companyId, name);
309 
310         if (organization != null) {
311             return organization.getOrganizationId();
312         }
313         else {
314             return 0;
315         }
316     }
317 
318     public List<Organization> getOrganizations(long[] organizationIds)
319         throws PortalException, SystemException {
320 
321         List<Organization> organizations = new ArrayList<Organization>(
322             organizationIds.length);
323 
324         for (long organizationId : organizationIds) {
325             Organization organization = getOrganization(organizationId);
326 
327             organizations.add(organization);
328         }
329 
330         return organizations;
331     }
332 
333     public List<Organization> getParentOrganizations(long organizationId)
334         throws PortalException, SystemException {
335 
336         if (organizationId ==
337                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
338 
339             return new ArrayList<Organization>();
340         }
341 
342         Organization organization =
343             organizationPersistence.findByPrimaryKey(organizationId);
344 
345         return getParentOrganizations(organization, true);
346     }
347 
348     public List<Organization> getSuborganizations(
349             List<Organization> organizations)
350         throws SystemException {
351 
352         List<Organization> allSuborganizations = new ArrayList<Organization>();
353 
354         for (int i = 0; i < organizations.size(); i++) {
355             Organization organization = organizations.get(i);
356 
357             List<Organization> suborganizations =
358                 organizationPersistence.findByC_P(
359                     organization.getCompanyId(),
360                     organization.getOrganizationId());
361 
362             addSuborganizations(allSuborganizations, suborganizations);
363         }
364 
365         return allSuborganizations;
366     }
367 
368     public List<Organization> getSubsetOrganizations(
369         List<Organization> allOrganizations,
370         List<Organization> availableOrganizations) {
371 
372         List<Organization> subsetOrganizations = new ArrayList<Organization>();
373 
374         Iterator<Organization> itr = allOrganizations.iterator();
375 
376         while (itr.hasNext()) {
377             Organization organization = itr.next();
378 
379             if (availableOrganizations.contains(organization)) {
380                 subsetOrganizations.add(organization);
381             }
382         }
383 
384         return subsetOrganizations;
385     }
386 
387     public List<Organization> getUserOrganizations(long userId)
388         throws PortalException, SystemException {
389 
390         return getUserOrganizations(userId, false);
391     }
392 
393     public List<Organization> getUserOrganizations(
394             long userId, boolean inheritUserGroups)
395         throws PortalException, SystemException {
396 
397         return getUserOrganizations(
398             userId, inheritUserGroups, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
399     }
400 
401     public List<Organization> getUserOrganizations(
402             long userId, int start, int end)
403         throws PortalException, SystemException {
404 
405         return getUserOrganizations(userId, false, start, end);
406     }
407 
408     public List<Organization> getUserOrganizations(
409             long userId, boolean inheritUserGroups, int start, int end)
410         throws PortalException, SystemException {
411 
412         if (inheritUserGroups &&
413             PropsValues.ORGANIZATIONS_USER_GROUP_MEMBERSHIP_ENABLED) {
414 
415             User user = userPersistence.findByPrimaryKey(userId);
416 
417             LinkedHashMap<String, Object> organizationParams =
418                 new LinkedHashMap<String, Object>();
419 
420             organizationParams.put("usersOrgs", new Long(userId));
421 
422             return search(
423                 user.getCompanyId(),
424                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID, null, null,
425                 null, null, organizationParams, start, end);
426         }
427         else {
428             return userPersistence.getOrganizations(userId, start, end);
429         }
430     }
431 
432     @ThreadLocalCachable
433     public int getUserOrganizationsCount(long userId) throws SystemException {
434         return userPersistence.getOrganizationsSize(userId);
435     }
436 
437     public boolean hasGroupOrganization(long groupId, long organizationId)
438         throws SystemException {
439 
440         return groupPersistence.containsOrganization(groupId, organizationId);
441     }
442 
443     public boolean hasUserOrganization(long userId, long organizationId)
444         throws SystemException {
445 
446         return userPersistence.containsOrganization(userId, organizationId);
447     }
448 
449     public boolean hasUserOrganization(
450             long userId, long organizationId, boolean inheritSuborganizations,
451             boolean inheritUserGroups, boolean includeSpecifiedOrganization)
452         throws PortalException, SystemException {
453 
454         if (!inheritSuborganizations && !inheritUserGroups) {
455             return userPersistence.containsOrganization(userId, organizationId);
456         }
457 
458         if (inheritSuborganizations) {
459             LinkedHashMap<String, Object> params =
460                 new LinkedHashMap<String, Object>();
461 
462             Long[][] leftAndRightOrganizationIds =
463                 EnterpriseAdminUtil.getLeftAndRightOrganizationIds(
464                     organizationId);
465 
466             if (!includeSpecifiedOrganization) {
467                 leftAndRightOrganizationIds[0][0] =
468                     leftAndRightOrganizationIds[0][0].longValue() + 1;
469             }
470 
471             params.put("usersOrgsTree", leftAndRightOrganizationIds);
472 
473             if (userFinder.countByUser(userId, params) > 0) {
474                 return true;
475             }
476         }
477 
478         if (inheritUserGroups) {
479             if (organizationFinder.countByO_U(organizationId, userId) > 0) {
480                 return true;
481             }
482         }
483 
484         return false;
485     }
486 
487     public boolean hasPasswordPolicyOrganization(
488             long passwordPolicyId, long organizationId)
489         throws SystemException {
490 
491         return passwordPolicyRelLocalService.hasPasswordPolicyRel(
492             passwordPolicyId, Organization.class.getName(), organizationId);
493     }
494 
495     public void rebuildTree(long companyId, boolean force)
496         throws SystemException {
497 
498         organizationPersistence.rebuildTree(companyId, force);
499     }
500 
501     public List<Organization> search(
502             long companyId, long parentOrganizationId, String keywords,
503             String type, Long regionId, Long countryId,
504             LinkedHashMap<String, Object> params,
505             int start, int end)
506         throws SystemException {
507 
508         return search(
509             companyId, parentOrganizationId, keywords, type, regionId,
510             countryId, params, start, end,
511             new OrganizationNameComparator(true));
512     }
513 
514     public List<Organization> search(
515             long companyId, long parentOrganizationId, String keywords,
516             String type, Long regionId, Long countryId,
517             LinkedHashMap<String, Object> params,
518             int start, int end, OrderByComparator obc)
519         throws SystemException {
520 
521         String parentOrganizationIdComparator = StringPool.EQUAL;
522 
523         if (parentOrganizationId ==
524                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
525 
526             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
527         }
528 
529         return organizationFinder.findByKeywords(
530             companyId, parentOrganizationId, parentOrganizationIdComparator,
531             keywords, type, regionId, countryId, params, start, end,
532             obc);
533     }
534 
535     public List<Organization> search(
536             long companyId, long parentOrganizationId, String name, String type,
537             String street, String city, String zip,
538             Long regionId, Long countryId,
539             LinkedHashMap<String, Object> params, boolean andOperator,
540             int start, int end)
541         throws SystemException {
542 
543         return search(
544             companyId, parentOrganizationId, name, type, street, city, zip,
545             regionId, countryId, params, andOperator, start, end,
546             new OrganizationNameComparator(true));
547     }
548 
549     public List<Organization> search(
550             long companyId, long parentOrganizationId, String name, String type,
551             String street, String city, String zip,
552             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
553             boolean andOperator, int start, int end, OrderByComparator obc)
554         throws SystemException {
555 
556         String parentOrganizationIdComparator = StringPool.EQUAL;
557 
558         if (parentOrganizationId ==
559                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
560 
561             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
562         }
563 
564         return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
565             companyId, parentOrganizationId, parentOrganizationIdComparator,
566             name, type, street, city, zip, regionId, countryId, params,
567             andOperator, start, end, obc);
568     }
569 
570     public int searchCount(
571             long companyId, long parentOrganizationId, String keywords,
572             String type, Long regionId, Long countryId,
573             LinkedHashMap<String, Object> params)
574         throws SystemException {
575 
576         String parentOrganizationIdComparator = StringPool.EQUAL;
577 
578         if (parentOrganizationId ==
579                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
580 
581             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
582         }
583 
584         return organizationFinder.countByKeywords(
585             companyId, parentOrganizationId, parentOrganizationIdComparator,
586             keywords, type, regionId, countryId, params);
587     }
588 
589     public int searchCount(
590             long companyId, long parentOrganizationId, String name, String type,
591             String street, String city, String zip,
592             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
593             boolean andOperator)
594         throws SystemException {
595 
596         String parentOrganizationIdComparator = StringPool.EQUAL;
597 
598         if (parentOrganizationId ==
599                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
600 
601             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
602         }
603 
604         return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
605             companyId, parentOrganizationId, parentOrganizationIdComparator,
606             name, type, street, city, zip, regionId, countryId, params,
607             andOperator);
608     }
609 
610     public void setGroupOrganizations(long groupId, long[] organizationIds)
611         throws SystemException {
612 
613         groupPersistence.setOrganizations(groupId, organizationIds);
614 
615         PermissionCacheUtil.clearCache();
616     }
617 
618     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
619         throws SystemException {
620 
621         groupPersistence.removeOrganizations(groupId, organizationIds);
622 
623         PermissionCacheUtil.clearCache();
624     }
625 
626     public void unsetPasswordPolicyOrganizations(
627             long passwordPolicyId, long[] organizationIds)
628         throws SystemException {
629 
630         passwordPolicyRelLocalService.deletePasswordPolicyRels(
631             passwordPolicyId, Organization.class.getName(), organizationIds);
632     }
633 
634     public void updateAsset(
635             long userId, Organization organization, long[] assetCategoryIds,
636             String[] assetTagNames)
637         throws PortalException, SystemException {
638 
639         User user = userPersistence.findByPrimaryKey(userId);
640 
641         Company company = companyPersistence.findByPrimaryKey(
642             user.getCompanyId());
643 
644         Group companyGroup = company.getGroup();
645 
646         assetEntryLocalService.updateEntry(
647             userId, companyGroup.getGroupId(), Organization.class.getName(),
648             organization.getOrganizationId(), assetCategoryIds, assetTagNames,
649             false, null, null, null, null, null, organization.getName(),
650             StringPool.BLANK, null, null, 0, 0, null, false);
651     }
652 
653     public Organization updateOrganization(
654             long companyId, long organizationId, long parentOrganizationId,
655             String name, String type, boolean recursable, long regionId,
656             long countryId, int statusId, String comments,
657             ServiceContext serviceContext)
658         throws PortalException, SystemException {
659 
660         // Organization
661 
662         parentOrganizationId = getParentOrganizationId(
663             companyId, parentOrganizationId);
664         recursable = true;
665 
666         validate(
667             companyId, organizationId, parentOrganizationId, name, type,
668             countryId, statusId);
669 
670         Organization organization = organizationPersistence.findByPrimaryKey(
671             organizationId);
672 
673         organization.setParentOrganizationId(parentOrganizationId);
674         organization.setName(name);
675         organization.setType(type);
676         organization.setRecursable(recursable);
677         organization.setRegionId(regionId);
678         organization.setCountryId(countryId);
679         organization.setStatusId(statusId);
680         organization.setComments(comments);
681 
682         organizationPersistence.update(organization, false);
683 
684         // Asset
685 
686         if (serviceContext != null) {
687             updateAsset(
688                 serviceContext.getUserId(), organization,
689                 serviceContext.getAssetCategoryIds(),
690                 serviceContext.getAssetTagNames());
691         }
692 
693         // Expando
694 
695         ExpandoBridge expandoBridge = organization.getExpandoBridge();
696 
697         expandoBridge.setAttributes(serviceContext);
698 
699         return organization;
700     }
701 
702     protected void addSuborganizations(
703             List<Organization> allSuborganizations,
704             List<Organization> organizations)
705         throws SystemException {
706 
707         for (Organization organization : organizations) {
708             if (!allSuborganizations.contains(organization)) {
709                 allSuborganizations.add(organization);
710 
711                 List<Organization> suborganizations =
712                     organizationPersistence.findByC_P(
713                         organization.getCompanyId(),
714                         organization.getOrganizationId());
715 
716                 addSuborganizations(allSuborganizations, suborganizations);
717             }
718         }
719     }
720 
721     protected long getParentOrganizationId(
722             long companyId, long parentOrganizationId)
723         throws SystemException {
724 
725         if (parentOrganizationId !=
726                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
727 
728             // Ensure parent organization exists and belongs to the proper
729             // company
730 
731             Organization parentOrganization =
732                 organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
733 
734             if ((parentOrganization == null) ||
735                 (companyId != parentOrganization.getCompanyId())) {
736 
737                 parentOrganizationId =
738                     OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
739             }
740         }
741 
742         return parentOrganizationId;
743     }
744 
745     protected List<Organization> getParentOrganizations(
746             Organization organization, boolean lastOrganization)
747         throws PortalException, SystemException {
748 
749         List<Organization> organizations = new ArrayList<Organization>();
750 
751         if (!lastOrganization) {
752             organizations.add(organization);
753         }
754 
755         long parentOrganizationId = organization.getParentOrganizationId();
756 
757         if (parentOrganizationId ==
758                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
759 
760             return organizations;
761         }
762 
763         Organization parentOrganization =
764             organizationPersistence.findByPrimaryKey(parentOrganizationId);
765 
766         List<Organization> parentOrganizatons = getParentOrganizations(
767             parentOrganization, false);
768 
769         organizations.addAll(parentOrganizatons);
770 
771         return organizations;
772     }
773 
774     protected boolean isParentOrganization(
775             long parentOrganizationId, long organizationId)
776         throws PortalException, SystemException {
777 
778         // Return true if parentOrganizationId is among the parent organizatons
779         // of organizationId
780 
781         Organization parentOrganization =
782             organizationPersistence.findByPrimaryKey(
783                 parentOrganizationId);
784 
785         List<Organization> parentOrganizations = getParentOrganizations(
786             organizationId);
787 
788         if (parentOrganizations.contains(parentOrganization)) {
789             return true;
790         }
791         else {
792             return false;
793         }
794     }
795 
796     protected void validate(
797             long companyId, long parentOrganizationId, String name, String type,
798             long countryId, int statusId)
799         throws PortalException, SystemException {
800 
801         validate(
802             companyId, 0, parentOrganizationId, name, type, countryId,
803             statusId);
804     }
805 
806     protected void validate(
807             long companyId, long organizationId, long parentOrganizationId,
808             String name, String type, long countryId, int statusId)
809         throws PortalException, SystemException {
810 
811         if ((parentOrganizationId ==
812                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
813 
814             if (!OrganizationImpl.isRootable(type)) {
815                 throw new OrganizationParentException(
816                     "Organization of type " + type + " cannot be a root");
817             }
818         }
819         else {
820             Organization parentOrganization =
821                 organizationPersistence.fetchByPrimaryKey(
822                     parentOrganizationId);
823 
824             if (parentOrganization == null) {
825                 throw new OrganizationParentException(
826                     "Organization " + parentOrganizationId + " doesn't exist");
827             }
828 
829             String[] childrenTypes = OrganizationImpl.getChildrenTypes(
830                 parentOrganization.getType());
831 
832             if (childrenTypes.length == 0) {
833                 throw new OrganizationParentException(
834                     "Organization of type " + type + " cannot have children");
835             }
836 
837             if ((companyId != parentOrganization.getCompanyId()) ||
838                 (parentOrganizationId == organizationId)) {
839 
840                 throw new OrganizationParentException();
841             }
842 
843             if (!ArrayUtil.contains(childrenTypes, type)) {
844                 throw new OrganizationParentException(
845                     "Type " + type + " not allowed as child of " +
846                         parentOrganization.getType());
847             }
848         }
849 
850         if ((organizationId > 0) &&
851             (parentOrganizationId !=
852                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
853 
854             // Prevent circular organizational references
855 
856             if (isParentOrganization(organizationId, parentOrganizationId)) {
857                 throw new OrganizationParentException();
858             }
859         }
860 
861         if (Validator.isNull(name)) {
862             throw new OrganizationNameException();
863         }
864         else {
865             Organization organization = organizationPersistence.fetchByC_N(
866                 companyId, name);
867 
868             if ((organization != null) &&
869                 (organization.getName().equalsIgnoreCase(name))) {
870 
871                 if ((organizationId <= 0) ||
872                     (organization.getOrganizationId() != organizationId)) {
873 
874                     throw new DuplicateOrganizationException();
875                 }
876             }
877         }
878 
879         boolean countryRequired = GetterUtil.getBoolean(
880             PropsUtil.get(
881                 PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
882 
883         if (countryRequired || (countryId > 0)) {
884             countryPersistence.findByPrimaryKey(countryId);
885         }
886 
887         listTypeService.validate(
888             statusId, ListTypeConstants.ORGANIZATION_STATUS);
889     }
890 
891 }