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