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