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