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