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