1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.ArrayUtil;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.model.Group;
38  import com.liferay.portal.model.LayoutSet;
39  import com.liferay.portal.model.Organization;
40  import com.liferay.portal.model.OrganizationConstants;
41  import com.liferay.portal.model.ResourceConstants;
42  import com.liferay.portal.model.Role;
43  import com.liferay.portal.model.RoleConstants;
44  import com.liferay.portal.model.User;
45  import com.liferay.portal.model.impl.ListTypeImpl;
46  import com.liferay.portal.model.impl.OrganizationImpl;
47  import com.liferay.portal.security.permission.PermissionCacheUtil;
48  import com.liferay.portal.service.ServiceContext;
49  import com.liferay.portal.service.base.OrganizationLocalServiceBaseImpl;
50  import com.liferay.portal.util.PropsKeys;
51  import com.liferay.portal.util.PropsUtil;
52  import com.liferay.portal.util.PropsValues;
53  import com.liferay.portal.util.comparator.OrganizationNameComparator;
54  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
55  import com.liferay.portlet.expando.model.ExpandoBridge;
56  
57  import java.util.ArrayList;
58  import java.util.Iterator;
59  import java.util.LinkedHashMap;
60  import java.util.List;
61  
62  /**
63   * <a href="OrganizationLocalServiceImpl.java.html"><b><i>View Source</i></b>
64   * </a>
65   *
66   * @author Brian Wing Shun Chan
67   * @author Jorge Ferrer
68   * @author Julio Camarero
69   *
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 SystemException {
390 
391         return userPersistence.getOrganizations(userId);
392     }
393 
394     public List<Organization> getUserOrganizations(
395             long userId, int start, int end)
396         throws SystemException {
397 
398         return userPersistence.getOrganizations(userId, start, end);
399     }
400 
401     public int getUserOrganizationsCount(long userId) throws SystemException {
402         return userPersistence.getOrganizationsSize(userId);
403     }
404 
405     public boolean hasGroupOrganization(long groupId, long organizationId)
406         throws SystemException {
407 
408         return groupPersistence.containsOrganization(groupId, organizationId);
409     }
410 
411     public boolean hasUserOrganization(long userId, long organizationId)
412         throws SystemException {
413 
414         return userPersistence.containsOrganization(userId, organizationId);
415     }
416 
417     public boolean hasUserOrganization(
418             long userId, long organizationId, boolean inherited,
419             boolean includeSpecifiedOrganization)
420         throws PortalException, SystemException {
421 
422         if (inherited) {
423             LinkedHashMap<String, Object> params =
424                 new LinkedHashMap<String, Object>();
425 
426             Long[][] leftAndRightOrganizationIds =
427                 EnterpriseAdminUtil.getLeftAndRightOrganizationIds(
428                     organizationId);
429 
430             if (!includeSpecifiedOrganization) {
431                 leftAndRightOrganizationIds[0][0] =
432                     leftAndRightOrganizationIds[0][0].longValue() + 1;
433             }
434 
435             params.put("usersOrgsTree", leftAndRightOrganizationIds);
436 
437             int count = userFinder.countByUser(userId, params);
438 
439             if (count > 0) {
440                 return true;
441             }
442             else {
443                 return false;
444             }
445         }
446         else {
447             return userPersistence.containsOrganization(userId, organizationId);
448         }
449     }
450 
451     public boolean hasPasswordPolicyOrganization(
452             long passwordPolicyId, long organizationId)
453         throws SystemException {
454 
455         return passwordPolicyRelLocalService.hasPasswordPolicyRel(
456             passwordPolicyId, Organization.class.getName(), organizationId);
457     }
458 
459     public void rebuildTree(long companyId, boolean force)
460         throws SystemException {
461 
462         organizationPersistence.rebuildTree(companyId, force);
463     }
464 
465     public List<Organization> search(
466             long companyId, long parentOrganizationId, String keywords,
467             String type, Long regionId, Long countryId,
468             LinkedHashMap<String, Object> params,
469             int start, int end)
470         throws SystemException {
471 
472         return search(
473             companyId, parentOrganizationId, keywords, type, regionId,
474             countryId, params, start, end,
475             new OrganizationNameComparator(true));
476     }
477 
478     public List<Organization> search(
479             long companyId, long parentOrganizationId, String keywords,
480             String type, Long regionId, Long countryId,
481             LinkedHashMap<String, Object> params,
482             int start, int end, OrderByComparator obc)
483         throws SystemException {
484 
485         String parentOrganizationIdComparator = StringPool.EQUAL;
486 
487         if (parentOrganizationId ==
488                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
489 
490             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
491         }
492 
493         return organizationFinder.findByKeywords(
494             companyId, parentOrganizationId, parentOrganizationIdComparator,
495             keywords, type, regionId, countryId, params, start, end,
496             obc);
497     }
498 
499     public List<Organization> search(
500             long companyId, long parentOrganizationId, String name, String type,
501             String street, String city, String zip,
502             Long regionId, Long countryId,
503             LinkedHashMap<String, Object> params, boolean andOperator,
504             int start, int end)
505         throws SystemException {
506 
507         return search(
508             companyId, parentOrganizationId, name, type, street, city, zip,
509             regionId, countryId, params, andOperator, start, end,
510             new OrganizationNameComparator(true));
511     }
512 
513     public List<Organization> search(
514             long companyId, long parentOrganizationId, String name, String type,
515             String street, String city, String zip,
516             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
517             boolean andOperator, int start, int end, OrderByComparator obc)
518         throws SystemException {
519 
520         String parentOrganizationIdComparator = StringPool.EQUAL;
521 
522         if (parentOrganizationId ==
523                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
524 
525             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
526         }
527 
528         return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
529             companyId, parentOrganizationId, parentOrganizationIdComparator,
530             name, type, street, city, zip, regionId, countryId, params,
531             andOperator, start, end, obc);
532     }
533 
534     public int searchCount(
535             long companyId, long parentOrganizationId, String keywords,
536             String type, Long regionId, Long countryId,
537             LinkedHashMap<String, Object> params)
538         throws SystemException {
539 
540         String parentOrganizationIdComparator = StringPool.EQUAL;
541 
542         if (parentOrganizationId ==
543                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
544 
545             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
546         }
547 
548         return organizationFinder.countByKeywords(
549             companyId, parentOrganizationId, parentOrganizationIdComparator,
550             keywords, type, regionId, countryId, params);
551     }
552 
553     public int searchCount(
554             long companyId, long parentOrganizationId, String name, String type,
555             String street, String city, String zip,
556             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
557             boolean andOperator)
558         throws SystemException {
559 
560         String parentOrganizationIdComparator = StringPool.EQUAL;
561 
562         if (parentOrganizationId ==
563                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
564 
565             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
566         }
567 
568         return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
569             companyId, parentOrganizationId, parentOrganizationIdComparator,
570             name, type, street, city, zip, regionId, countryId, params,
571             andOperator);
572     }
573 
574     public void setGroupOrganizations(long groupId, long[] organizationIds)
575         throws SystemException {
576 
577         groupPersistence.setOrganizations(groupId, organizationIds);
578 
579         PermissionCacheUtil.clearCache();
580     }
581 
582     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
583         throws SystemException {
584 
585         groupPersistence.removeOrganizations(groupId, organizationIds);
586 
587         PermissionCacheUtil.clearCache();
588     }
589 
590     public void unsetPasswordPolicyOrganizations(
591             long passwordPolicyId, long[] organizationIds)
592         throws SystemException {
593 
594         passwordPolicyRelLocalService.deletePasswordPolicyRels(
595             passwordPolicyId, Organization.class.getName(), organizationIds);
596     }
597 
598     public Organization updateOrganization(
599             long companyId, long organizationId, long parentOrganizationId,
600             String name, String type, boolean recursable, long regionId,
601             long countryId, int statusId, String comments,
602             ServiceContext serviceContext)
603         throws PortalException, SystemException {
604 
605         // Organization
606 
607         parentOrganizationId = getParentOrganizationId(
608             companyId, parentOrganizationId);
609         recursable = true;
610 
611         validate(
612             companyId, organizationId, parentOrganizationId, name, type,
613             countryId, statusId);
614 
615         Organization organization = organizationPersistence.findByPrimaryKey(
616             organizationId);
617 
618         organization.setParentOrganizationId(parentOrganizationId);
619         organization.setName(name);
620         organization.setType(type);
621         organization.setRecursable(recursable);
622         organization.setRegionId(regionId);
623         organization.setCountryId(countryId);
624         organization.setStatusId(statusId);
625         organization.setComments(comments);
626 
627         organizationPersistence.update(organization, false);
628 
629         // Expando
630 
631         ExpandoBridge expandoBridge = organization.getExpandoBridge();
632 
633         expandoBridge.setAttributes(serviceContext);
634 
635         // Tags
636 
637         if (serviceContext != null) {
638             updateTagsAsset(
639                 serviceContext.getUserId(), organization,
640                 serviceContext.getTagsCategories(),
641                 serviceContext.getTagsEntries());
642         }
643 
644         return organization;
645     }
646 
647     public void updateTagsAsset(
648             long userId, Organization organization, String[] tagsCategories,
649             String[] tagsEntries)
650         throws PortalException, SystemException {
651 
652         tagsAssetLocalService.updateAsset(
653             userId, 0, Organization.class.getName(),
654             organization.getOrganizationId(), tagsCategories, tagsEntries, true,
655             null, null, null, null, null, organization.getName(),
656             StringPool.BLANK, null, null, 0, 0, null, false);
657     }
658 
659     protected void addSuborganizations(
660             List<Organization> allSuborganizations,
661             List<Organization> organizations)
662         throws SystemException {
663 
664         for (Organization organization : organizations) {
665             if (!allSuborganizations.contains(organization)) {
666                 allSuborganizations.add(organization);
667 
668                 List<Organization> suborganizations =
669                     organizationPersistence.findByC_P(
670                         organization.getCompanyId(),
671                         organization.getOrganizationId());
672 
673                 addSuborganizations(allSuborganizations, suborganizations);
674             }
675         }
676     }
677 
678     protected long getParentOrganizationId(
679             long companyId, long parentOrganizationId)
680         throws SystemException {
681 
682         if (parentOrganizationId !=
683                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
684 
685             // Ensure parent organization exists and belongs to the proper
686             // company
687 
688             Organization parentOrganization =
689                 organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
690 
691             if ((parentOrganization == null) ||
692                 (companyId != parentOrganization.getCompanyId())) {
693 
694                 parentOrganizationId =
695                     OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
696             }
697         }
698 
699         return parentOrganizationId;
700     }
701 
702     protected List<Organization> getParentOrganizations(
703             Organization organization, boolean lastOrganization)
704         throws PortalException, SystemException {
705 
706         List<Organization> organizations = new ArrayList<Organization>();
707 
708         if (!lastOrganization) {
709             organizations.add(organization);
710         }
711 
712         long parentOrganizationId = organization.getParentOrganizationId();
713 
714         if (parentOrganizationId ==
715                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
716 
717             return organizations;
718         }
719 
720         Organization parentOrganization =
721             organizationPersistence.findByPrimaryKey(parentOrganizationId);
722 
723         List<Organization> parentOrganizatons = getParentOrganizations(
724             parentOrganization, false);
725 
726         organizations.addAll(parentOrganizatons);
727 
728         return organizations;
729     }
730 
731     protected boolean isParentOrganization(
732             long parentOrganizationId, long organizationId)
733         throws PortalException, SystemException {
734 
735         // Return true if parentOrganizationId is among the parent organizatons
736         // of organizationId
737 
738         Organization parentOrganization =
739             organizationPersistence.findByPrimaryKey(
740                 parentOrganizationId);
741 
742         List<Organization> parentOrganizations = getParentOrganizations(
743             organizationId);
744 
745         if (parentOrganizations.contains(parentOrganization)) {
746             return true;
747         }
748         else {
749             return false;
750         }
751     }
752 
753     protected void validate(
754             long companyId, long parentOrganizationId, String name, String type,
755             long countryId, int statusId)
756         throws PortalException, SystemException {
757 
758         validate(
759             companyId, 0, parentOrganizationId, name, type, countryId,
760             statusId);
761     }
762 
763     protected void validate(
764             long companyId, long organizationId, long parentOrganizationId,
765             String name, String type, long countryId, int statusId)
766         throws PortalException, SystemException {
767 
768         if ((parentOrganizationId ==
769                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
770 
771             if (!OrganizationImpl.isRootable(type)) {
772                 throw new OrganizationParentException(
773                     "Organization of type " + type + " cannot be a root");
774             }
775         }
776         else {
777             Organization parentOrganization =
778                 organizationPersistence.fetchByPrimaryKey(
779                     parentOrganizationId);
780 
781             if (parentOrganization == null) {
782                 throw new OrganizationParentException(
783                     "Organization " + parentOrganizationId + " doesn't exist");
784             }
785 
786             String[] childrenTypes = OrganizationImpl.getChildrenTypes(
787                 parentOrganization.getType());
788 
789             if (childrenTypes.length == 0) {
790                 throw new OrganizationParentException(
791                     "Organization of type " + type + " cannot have children");
792             }
793 
794             if ((companyId != parentOrganization.getCompanyId()) ||
795                 (parentOrganizationId == organizationId)) {
796 
797                 throw new OrganizationParentException();
798             }
799 
800             if (!ArrayUtil.contains(childrenTypes, type)) {
801                 throw new OrganizationParentException(
802                     "Type " + type + " not allowed as child of " +
803                         parentOrganization.getType());
804             }
805         }
806 
807         if ((organizationId > 0) &&
808             (parentOrganizationId !=
809                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
810 
811             // Prevent circular organizational references
812 
813             if (isParentOrganization(organizationId, parentOrganizationId)) {
814                 throw new OrganizationParentException();
815             }
816         }
817 
818         if (Validator.isNull(name)) {
819             throw new OrganizationNameException();
820         }
821         else {
822             Organization organization = organizationPersistence.fetchByC_N(
823                 companyId, name);
824 
825             if ((organization != null) &&
826                 (organization.getName().equalsIgnoreCase(name))) {
827 
828                 if ((organizationId <= 0) ||
829                     (organization.getOrganizationId() != organizationId)) {
830 
831                     throw new DuplicateOrganizationException();
832                 }
833             }
834         }
835 
836         boolean countryRequired = GetterUtil.getBoolean(
837             PropsUtil.get(
838                 PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
839 
840         if (countryRequired || (countryId > 0)) {
841             countryPersistence.findByPrimaryKey(countryId);
842         }
843 
844         listTypeService.validate(statusId, ListTypeImpl.ORGANIZATION_STATUS);
845     }
846 
847 }