1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.OrganizationTypeException;
21  import com.liferay.portal.PortalException;
22  import com.liferay.portal.RequiredOrganizationException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.configuration.Filter;
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
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.Group;
33  import com.liferay.portal.model.LayoutSet;
34  import com.liferay.portal.model.Organization;
35  import com.liferay.portal.model.OrganizationConstants;
36  import com.liferay.portal.model.ResourceConstants;
37  import com.liferay.portal.model.Role;
38  import com.liferay.portal.model.RoleConstants;
39  import com.liferay.portal.model.User;
40  import com.liferay.portal.model.impl.ListTypeImpl;
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         // Expando
135 
136         ExpandoBridge expandoBridge = organization.getExpandoBridge();
137 
138         expandoBridge.setAttributes(serviceContext);
139 
140         // Tags
141 
142         if (serviceContext != null) {
143             updateTagsAsset(
144                 userId, organization, serviceContext.getTagsCategories(),
145                 serviceContext.getTagsEntries());
146         }
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(privateLayoutSet, 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         // Tags
223 
224         tagsAssetLocalService.deleteAsset(
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 Organization updateOrganization(
633             long companyId, long organizationId, long parentOrganizationId,
634             String name, String type, boolean recursable, long regionId,
635             long countryId, int statusId, String comments,
636             ServiceContext serviceContext)
637         throws PortalException, SystemException {
638 
639         // Organization
640 
641         parentOrganizationId = getParentOrganizationId(
642             companyId, parentOrganizationId);
643         recursable = true;
644 
645         validate(
646             companyId, organizationId, parentOrganizationId, name, type,
647             countryId, statusId);
648 
649         Organization organization = organizationPersistence.findByPrimaryKey(
650             organizationId);
651 
652         organization.setParentOrganizationId(parentOrganizationId);
653         organization.setName(name);
654         organization.setType(type);
655         organization.setRecursable(recursable);
656         organization.setRegionId(regionId);
657         organization.setCountryId(countryId);
658         organization.setStatusId(statusId);
659         organization.setComments(comments);
660 
661         organizationPersistence.update(organization, false);
662 
663         // Expando
664 
665         ExpandoBridge expandoBridge = organization.getExpandoBridge();
666 
667         expandoBridge.setAttributes(serviceContext);
668 
669         // Tags
670 
671         if (serviceContext != null) {
672             updateTagsAsset(
673                 serviceContext.getUserId(), organization,
674                 serviceContext.getTagsCategories(),
675                 serviceContext.getTagsEntries());
676         }
677 
678         return organization;
679     }
680 
681     public void updateTagsAsset(
682             long userId, Organization organization, String[] tagsCategories,
683             String[] tagsEntries)
684         throws PortalException, SystemException {
685 
686         tagsAssetLocalService.updateAsset(
687             userId, 0, Organization.class.getName(),
688             organization.getOrganizationId(), tagsCategories, tagsEntries, true,
689             null, null, null, null, null, organization.getName(),
690             StringPool.BLANK, null, null, 0, 0, null, false);
691     }
692 
693     protected void addSuborganizations(
694             List<Organization> allSuborganizations,
695             List<Organization> organizations)
696         throws SystemException {
697 
698         for (Organization organization : organizations) {
699             if (!allSuborganizations.contains(organization)) {
700                 allSuborganizations.add(organization);
701 
702                 List<Organization> suborganizations =
703                     organizationPersistence.findByC_P(
704                         organization.getCompanyId(),
705                         organization.getOrganizationId());
706 
707                 addSuborganizations(allSuborganizations, suborganizations);
708             }
709         }
710     }
711 
712     protected long getParentOrganizationId(
713             long companyId, long parentOrganizationId)
714         throws SystemException {
715 
716         if (parentOrganizationId !=
717                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
718 
719             // Ensure parent organization exists and belongs to the proper
720             // company
721 
722             Organization parentOrganization =
723                 organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
724 
725             if ((parentOrganization == null) ||
726                 (companyId != parentOrganization.getCompanyId())) {
727 
728                 parentOrganizationId =
729                     OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
730             }
731         }
732 
733         return parentOrganizationId;
734     }
735 
736     protected List<Organization> getParentOrganizations(
737             Organization organization, boolean lastOrganization)
738         throws PortalException, SystemException {
739 
740         List<Organization> organizations = new ArrayList<Organization>();
741 
742         if (!lastOrganization) {
743             organizations.add(organization);
744         }
745 
746         long parentOrganizationId = organization.getParentOrganizationId();
747 
748         if (parentOrganizationId ==
749                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
750 
751             return organizations;
752         }
753 
754         Organization parentOrganization =
755             organizationPersistence.findByPrimaryKey(parentOrganizationId);
756 
757         List<Organization> parentOrganizatons = getParentOrganizations(
758             parentOrganization, false);
759 
760         organizations.addAll(parentOrganizatons);
761 
762         return organizations;
763     }
764 
765     protected boolean isParentOrganization(
766             long parentOrganizationId, long organizationId)
767         throws PortalException, SystemException {
768 
769         // Return true if parentOrganizationId is among the parent organizatons
770         // of organizationId
771 
772         Organization parentOrganization =
773             organizationPersistence.findByPrimaryKey(
774                 parentOrganizationId);
775 
776         List<Organization> parentOrganizations = getParentOrganizations(
777             organizationId);
778 
779         if (parentOrganizations.contains(parentOrganization)) {
780             return true;
781         }
782         else {
783             return false;
784         }
785     }
786 
787     protected void validate(
788             long companyId, long parentOrganizationId, String name, String type,
789             long countryId, int statusId)
790         throws PortalException, SystemException {
791 
792         validate(
793             companyId, 0, parentOrganizationId, name, type, countryId,
794             statusId);
795     }
796 
797     protected void validate(
798             long companyId, long organizationId, long parentOrganizationId,
799             String name, String type, long countryId, int statusId)
800         throws PortalException, SystemException {
801 
802         if (!ArrayUtil.contains(PropsValues.ORGANIZATIONS_TYPES, type)) {
803             throw new OrganizationTypeException(
804                 "Invalid organization type " + type);
805         }
806 
807         if ((parentOrganizationId ==
808                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
809 
810             if (!OrganizationImpl.isRootable(type)) {
811                 throw new OrganizationParentException(
812                     "Organization of type " + type + " cannot be a root");
813             }
814         }
815         else {
816             Organization parentOrganization =
817                 organizationPersistence.fetchByPrimaryKey(
818                     parentOrganizationId);
819 
820             if (parentOrganization == null) {
821                 throw new OrganizationParentException(
822                     "Organization " + parentOrganizationId + " doesn't exist");
823             }
824 
825             String[] childrenTypes = OrganizationImpl.getChildrenTypes(
826                 parentOrganization.getType());
827 
828             if (childrenTypes.length == 0) {
829                 throw new OrganizationParentException(
830                     "Organization of type " + type + " cannot have children");
831             }
832 
833             if ((companyId != parentOrganization.getCompanyId()) ||
834                 (parentOrganizationId == organizationId)) {
835 
836                 throw new OrganizationParentException();
837             }
838 
839             if (!ArrayUtil.contains(childrenTypes, type)) {
840                 throw new OrganizationParentException(
841                     "Type " + type + " not allowed as child of " +
842                         parentOrganization.getType());
843             }
844         }
845 
846         if ((organizationId > 0) &&
847             (parentOrganizationId !=
848                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
849 
850             // Prevent circular organizational references
851 
852             if (isParentOrganization(organizationId, parentOrganizationId)) {
853                 throw new OrganizationParentException();
854             }
855         }
856 
857         if (Validator.isNull(name)) {
858             throw new OrganizationNameException();
859         }
860         else {
861             Organization organization = organizationPersistence.fetchByC_N(
862                 companyId, name);
863 
864             if ((organization != null) &&
865                 (organization.getName().equalsIgnoreCase(name))) {
866 
867                 if ((organizationId <= 0) ||
868                     (organization.getOrganizationId() != organizationId)) {
869 
870                     throw new DuplicateOrganizationException();
871                 }
872             }
873         }
874 
875         boolean countryRequired = GetterUtil.getBoolean(
876             PropsUtil.get(
877                 PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
878 
879         if (countryRequired || (countryId > 0)) {
880             countryPersistence.findByPrimaryKey(countryId);
881         }
882 
883         listTypeService.validate(statusId, ListTypeImpl.ORGANIZATION_STATUS);
884     }
885 
886 }