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.LayoutSetLocalServiceUtil;
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.expando.model.ExpandoBridge;
56  import com.liferay.util.UniqueList;
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);
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         return organization;
149     }
150 
151     public void addOrganizationResources(long userId, Organization organization)
152         throws PortalException, SystemException {
153 
154         String name = Organization.class.getName();
155 
156         resourceLocalService.addResources(
157             organization.getCompanyId(), 0, userId, name,
158             organization.getOrganizationId(), false, false, false);
159     }
160 
161     public void addPasswordPolicyOrganizations(
162             long passwordPolicyId, long[] organizationIds)
163         throws SystemException {
164 
165         passwordPolicyRelLocalService.addPasswordPolicyRels(
166             passwordPolicyId, Organization.class.getName(), organizationIds);
167     }
168 
169     public void deleteLogo(long organizationId)
170         throws PortalException, SystemException {
171 
172         Organization organization = getOrganization(organizationId);
173 
174         Group group = organization.getGroup();
175 
176         LayoutSet publicLayoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
177             group.getGroupId(), false);
178 
179         if (publicLayoutSet.isLogo()) {
180             long logoId = publicLayoutSet.getLogoId();
181 
182             publicLayoutSet.setLogo(false);
183             publicLayoutSet.setLogoId(0);
184 
185             layoutSetPersistence.update(publicLayoutSet, false);
186 
187             imageLocalService.deleteImage(logoId);
188         }
189 
190         LayoutSet privateLayoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
191             group.getGroupId(), true);
192 
193         if (privateLayoutSet.isLogo()) {
194             long logoId = privateLayoutSet.getLogoId();
195 
196             privateLayoutSet.setLogo(false);
197             privateLayoutSet.setLogoId(0);
198 
199             layoutSetPersistence.update(publicLayoutSet, false);
200 
201             if (imageLocalService.getImage(logoId) != null) {
202                 imageLocalService.deleteImage(logoId);
203             }
204         }
205     }
206 
207     public void deleteOrganization(long organizationId)
208         throws PortalException, SystemException {
209 
210         Organization organization = organizationPersistence.findByPrimaryKey(
211             organizationId);
212 
213         if ((userLocalService.getOrganizationUsersCount(
214                 organization.getOrganizationId(), true) > 0) ||
215             (organizationPersistence.countByC_P(
216                 organization.getCompanyId(),
217                 organization.getOrganizationId()) > 0)) {
218 
219             throw new RequiredOrganizationException();
220         }
221 
222         // Addresses
223 
224         addressLocalService.deleteAddresses(
225             organization.getCompanyId(), Organization.class.getName(),
226             organization.getOrganizationId());
227 
228         // Email addresses
229 
230         emailAddressLocalService.deleteEmailAddresses(
231             organization.getCompanyId(), Organization.class.getName(),
232             organization.getOrganizationId());
233 
234         // Password policy relation
235 
236         passwordPolicyRelLocalService.deletePasswordPolicyRel(
237             Organization.class.getName(), organization.getOrganizationId());
238 
239         // Phone
240 
241         phoneLocalService.deletePhones(
242             organization.getCompanyId(), Organization.class.getName(),
243             organization.getOrganizationId());
244 
245         // Website
246 
247         websiteLocalService.deleteWebsites(
248             organization.getCompanyId(), Organization.class.getName(),
249             organization.getOrganizationId());
250 
251         // Group
252 
253         Group group = organization.getGroup();
254 
255         groupLocalService.deleteGroup(group.getGroupId());
256 
257         // Resources
258 
259         String name = Organization.class.getName();
260 
261         resourceLocalService.deleteResource(
262             organization.getCompanyId(), name,
263             ResourceConstants.SCOPE_INDIVIDUAL,
264             organization.getOrganizationId());
265 
266         // Organization
267 
268         organizationPersistence.remove(organization);
269 
270         // Permission cache
271 
272         PermissionCacheUtil.clearCache();
273     }
274 
275     public List<Organization> getGroupOrganizations(long groupId)
276         throws SystemException {
277 
278         return groupPersistence.getOrganizations(groupId);
279     }
280 
281     /**
282      * Gets a list of organizations that a user has access to administrate. This
283      * includes organizations that a user belongs to and all suborganizations of
284      * those organizations.
285      *
286      * @param       userId the user id of the user
287      * @return      a list of organizations
288      */
289     public List<Organization> getManageableOrganizations(long userId)
290         throws SystemException {
291 
292         List<Organization> manageableOrganizations =
293             new UniqueList<Organization>();
294 
295         List<Organization> userOrganizations = userPersistence.getOrganizations(
296             userId);
297 
298         manageableOrganizations.addAll(userOrganizations);
299         manageableOrganizations.addAll(getSuborganizations(userOrganizations));
300 
301         return manageableOrganizations;
302     }
303 
304     public Organization getOrganization(long organizationId)
305         throws PortalException, SystemException {
306 
307         return organizationPersistence.findByPrimaryKey(organizationId);
308     }
309 
310     public Organization getOrganization(long companyId, String name)
311         throws PortalException, SystemException {
312 
313         return organizationPersistence.findByC_N(companyId, name);
314     }
315 
316     public long getOrganizationId(long companyId, String name)
317         throws SystemException {
318 
319         Organization organization = organizationPersistence.fetchByC_N(
320             companyId, name);
321 
322         if (organization != null) {
323             return organization.getOrganizationId();
324         }
325         else {
326             return 0;
327         }
328     }
329 
330     public List<Organization> getOrganizations(long[] organizationIds)
331         throws PortalException, SystemException {
332 
333         List<Organization> organizations = new ArrayList<Organization>(
334             organizationIds.length);
335 
336         for (long organizationId : organizationIds) {
337             Organization organization = getOrganization(organizationId);
338 
339             organizations.add(organization);
340         }
341 
342         return organizations;
343     }
344 
345     public List<Organization> getParentOrganizations(long organizationId)
346         throws PortalException, SystemException {
347 
348         if (organizationId ==
349                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
350 
351             return new ArrayList<Organization>();
352         }
353 
354         Organization organization =
355             organizationPersistence.findByPrimaryKey(organizationId);
356 
357         return getParentOrganizations(organization, true);
358     }
359 
360     public List<Organization> getSuborganizations(
361             List<Organization> organizations)
362         throws SystemException {
363 
364         List<Organization> allSuborganizations = new ArrayList<Organization>();
365 
366         for (int i = 0; i < organizations.size(); i++) {
367             Organization organization = organizations.get(i);
368 
369             List<Organization> suborganizations =
370                 organizationPersistence.findByC_P(
371                     organization.getCompanyId(),
372                     organization.getOrganizationId());
373 
374             addSuborganizations(allSuborganizations, suborganizations);
375         }
376 
377         return allSuborganizations;
378     }
379 
380     public List<Organization> getSubsetOrganizations(
381         List<Organization> allOrganizations,
382         List<Organization> availableOrganizations) {
383 
384         List<Organization> subsetOrganizations = new ArrayList<Organization>();
385 
386         Iterator<Organization> itr = allOrganizations.iterator();
387 
388         while (itr.hasNext()) {
389             Organization organization = itr.next();
390 
391             if (availableOrganizations.contains(organization)) {
392                 subsetOrganizations.add(organization);
393             }
394         }
395 
396         return subsetOrganizations;
397     }
398 
399     public List<Organization> getUserOrganizations(long userId)
400         throws SystemException {
401 
402         return userPersistence.getOrganizations(userId);
403     }
404 
405     public List<Organization> getUserOrganizations(
406             long userId, int start, int end)
407         throws SystemException {
408 
409         return userPersistence.getOrganizations(userId, start, end);
410     }
411 
412     public int getUserOrganizationsCount(long userId) throws SystemException {
413         return userPersistence.getOrganizationsSize(userId);
414     }
415 
416     public boolean hasGroupOrganization(long groupId, long organizationId)
417         throws SystemException {
418 
419         return groupPersistence.containsOrganization(groupId, organizationId);
420     }
421 
422     public boolean hasUserOrganization(long userId, long organizationId)
423         throws SystemException {
424 
425         return userPersistence.containsOrganization(userId, organizationId);
426     }
427 
428     public boolean hasPasswordPolicyOrganization(
429             long passwordPolicyId, long organizationId)
430         throws SystemException {
431 
432         return passwordPolicyRelLocalService.hasPasswordPolicyRel(
433             passwordPolicyId, Organization.class.getName(), organizationId);
434     }
435 
436     public List<Organization> search(
437             long companyId, long parentOrganizationId, String keywords,
438             String type, Long regionId, Long countryId,
439             LinkedHashMap<String, Object> params,
440             int start, int end)
441         throws SystemException {
442 
443         return search(
444             companyId, parentOrganizationId, keywords, type, regionId,
445             countryId, params, start, end,
446             new OrganizationNameComparator(true));
447     }
448 
449     public List<Organization> search(
450             long companyId, long parentOrganizationId, String keywords,
451             String type, Long regionId, Long countryId,
452             LinkedHashMap<String, Object> params,
453             int start, int end, OrderByComparator obc)
454         throws SystemException {
455 
456         String parentOrganizationIdComparator = StringPool.EQUAL;
457 
458         if (parentOrganizationId ==
459                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
460 
461             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
462         }
463 
464         return organizationFinder.findByKeywords(
465             companyId, parentOrganizationId, parentOrganizationIdComparator,
466             keywords, type, regionId, countryId, params, start, end,
467             obc);
468     }
469 
470     public List<Organization> search(
471             long companyId, long parentOrganizationId, String name, String type,
472             String street, String city, String zip,
473             Long regionId, Long countryId,
474             LinkedHashMap<String, Object> params, boolean andOperator,
475             int start, int end)
476         throws SystemException {
477 
478         return search(
479             companyId, parentOrganizationId, name, type, street, city, zip,
480             regionId, countryId, params, andOperator, start, end,
481             new OrganizationNameComparator(true));
482     }
483 
484     public List<Organization> search(
485             long companyId, long parentOrganizationId, String name, String type,
486             String street, String city, String zip,
487             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
488             boolean andOperator, int start, int end, OrderByComparator obc)
489         throws SystemException {
490 
491         String parentOrganizationIdComparator = StringPool.EQUAL;
492 
493         if (parentOrganizationId ==
494                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
495 
496             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
497         }
498 
499         return organizationFinder.findByC_PO_N_T_S_C_Z_R_C(
500             companyId, parentOrganizationId, parentOrganizationIdComparator,
501             name, type, street, city, zip, regionId, countryId, params,
502             andOperator, start, end, obc);
503     }
504 
505     public int searchCount(
506             long companyId, long parentOrganizationId, String keywords,
507             String type, Long regionId, Long countryId,
508             LinkedHashMap<String, Object> params)
509         throws SystemException {
510 
511         String parentOrganizationIdComparator = StringPool.EQUAL;
512 
513         if (parentOrganizationId ==
514                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
515 
516             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
517         }
518 
519         return organizationFinder.countByKeywords(
520             companyId, parentOrganizationId, parentOrganizationIdComparator,
521             keywords, type, regionId, countryId, params);
522     }
523 
524     public int searchCount(
525             long companyId, long parentOrganizationId, String name, String type,
526             String street, String city, String zip,
527             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
528             boolean andOperator)
529         throws SystemException {
530 
531         String parentOrganizationIdComparator = StringPool.EQUAL;
532 
533         if (parentOrganizationId ==
534                 OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
535 
536             parentOrganizationIdComparator = StringPool.NOT_EQUAL;
537         }
538 
539         return organizationFinder.countByC_PO_N_T_S_C_Z_R_C(
540             companyId, parentOrganizationId, parentOrganizationIdComparator,
541             name, type, street, city, zip, regionId, countryId, params,
542             andOperator);
543     }
544 
545     public void setGroupOrganizations(long groupId, long[] organizationIds)
546         throws SystemException {
547 
548         groupPersistence.setOrganizations(groupId, organizationIds);
549 
550         PermissionCacheUtil.clearCache();
551     }
552 
553     public void unsetGroupOrganizations(long groupId, long[] organizationIds)
554         throws SystemException {
555 
556         groupPersistence.removeOrganizations(groupId, organizationIds);
557 
558         PermissionCacheUtil.clearCache();
559     }
560 
561     public void unsetPasswordPolicyOrganizations(
562             long passwordPolicyId, long[] organizationIds)
563         throws SystemException {
564 
565         passwordPolicyRelLocalService.deletePasswordPolicyRels(
566             passwordPolicyId, Organization.class.getName(), organizationIds);
567     }
568 
569     public Organization updateOrganization(
570             long companyId, long organizationId, long parentOrganizationId,
571             String name, String type, boolean recursable, long regionId,
572             long countryId, int statusId, String comments,
573             ServiceContext serviceContext)
574         throws PortalException, SystemException {
575 
576         // Organization
577 
578         parentOrganizationId = getParentOrganizationId(
579             companyId, parentOrganizationId);
580         recursable = true;
581 
582         validate(
583             companyId, organizationId, parentOrganizationId, name, type,
584             countryId, statusId);
585 
586         Organization organization = organizationPersistence.findByPrimaryKey(
587             organizationId);
588 
589         organization.setParentOrganizationId(parentOrganizationId);
590         organization.setName(name);
591         organization.setType(type);
592         organization.setRecursable(recursable);
593         organization.setRegionId(regionId);
594         organization.setCountryId(countryId);
595         organization.setStatusId(statusId);
596         organization.setComments(comments);
597 
598         organizationPersistence.update(organization, false);
599 
600         // Expando
601 
602         ExpandoBridge expandoBridge = organization.getExpandoBridge();
603 
604         expandoBridge.setAttributes(serviceContext);
605 
606         return organization;
607     }
608 
609     protected void addSuborganizations(
610             List<Organization> allSuborganizations,
611             List<Organization> organizations)
612         throws SystemException {
613 
614         for (Organization organization : organizations) {
615             if (!allSuborganizations.contains(organization)) {
616                 allSuborganizations.add(organization);
617 
618                 List<Organization> suborganizations =
619                     organizationPersistence.findByC_P(
620                         organization.getCompanyId(),
621                         organization.getOrganizationId());
622 
623                 addSuborganizations(allSuborganizations, suborganizations);
624             }
625         }
626     }
627 
628     protected long getParentOrganizationId(
629             long companyId, long parentOrganizationId)
630         throws SystemException {
631 
632         if (parentOrganizationId !=
633                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
634 
635             // Ensure parent organization exists and belongs to the proper
636             // company
637 
638             Organization parentOrganization =
639                 organizationPersistence.fetchByPrimaryKey(parentOrganizationId);
640 
641             if ((parentOrganization == null) ||
642                 (companyId != parentOrganization.getCompanyId())) {
643 
644                 parentOrganizationId =
645                     OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID;
646             }
647         }
648 
649         return parentOrganizationId;
650     }
651 
652     protected List<Organization> getParentOrganizations(
653             Organization organization, boolean lastOrganization)
654         throws PortalException, SystemException {
655 
656         List<Organization> organizations = new ArrayList<Organization>();
657 
658         if (!lastOrganization) {
659             organizations.add(organization);
660         }
661 
662         long parentOrganizationId = organization.getParentOrganizationId();
663 
664         if (parentOrganizationId ==
665                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
666 
667             return organizations;
668         }
669 
670         Organization parentOrganization =
671             organizationPersistence.findByPrimaryKey(parentOrganizationId);
672 
673         List<Organization> parentOrganizatons = getParentOrganizations(
674             parentOrganization, false);
675 
676         organizations.addAll(parentOrganizatons);
677 
678         return organizations;
679     }
680 
681     protected boolean isParentOrganization(
682             long parentOrganizationId, long organizationId)
683         throws PortalException, SystemException {
684 
685         // Return true if parentOrganizationId is among the parent organizatons
686         // of organizationId
687 
688         Organization parentOrganization =
689             organizationPersistence.findByPrimaryKey(
690                 parentOrganizationId);
691 
692         List<Organization> parentOrganizations = getParentOrganizations(
693             organizationId);
694 
695         if (parentOrganizations.contains(parentOrganization)) {
696             return true;
697         }
698         else {
699             return false;
700         }
701     }
702 
703     protected void validate(
704             long companyId, long parentOrganizationId, String name, String type,
705             long countryId, int statusId)
706         throws PortalException, SystemException {
707 
708         validate(
709             companyId, 0, parentOrganizationId, name, type, countryId,
710             statusId);
711     }
712 
713     protected void validate(
714             long companyId, long organizationId, long parentOrganizationId,
715             String name, String type, long countryId, int statusId)
716         throws PortalException, SystemException {
717 
718         if ((parentOrganizationId ==
719                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
720 
721             if (!OrganizationImpl.isRootable(type)) {
722                 throw new OrganizationParentException(
723                     "Organization of type " + type + " cannot be a root");
724             }
725         }
726         else {
727             Organization parentOrganization =
728                 organizationPersistence.fetchByPrimaryKey(
729                     parentOrganizationId);
730 
731             if (parentOrganization == null) {
732                 throw new OrganizationParentException(
733                     "Organization " + parentOrganizationId + " doesn't exist");
734             }
735 
736             String[] childrenTypes = OrganizationImpl.getChildrenTypes(
737                 parentOrganization.getType());
738 
739             if (childrenTypes.length == 0) {
740                 throw new OrganizationParentException(
741                     "Organization of type " + type + " cannot have children");
742             }
743 
744             if ((companyId != parentOrganization.getCompanyId()) ||
745                 (parentOrganizationId == organizationId)) {
746 
747                 throw new OrganizationParentException();
748             }
749 
750             if (!ArrayUtil.contains(childrenTypes, type)) {
751                 throw new OrganizationParentException(
752                     "Type " + type + " not allowed as child of " +
753                         parentOrganization.getType());
754             }
755         }
756 
757         if ((organizationId > 0) &&
758             (parentOrganizationId !=
759                 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID)) {
760 
761             // Prevent circular organizational references
762 
763             if (isParentOrganization(organizationId, parentOrganizationId)) {
764                 throw new OrganizationParentException();
765             }
766         }
767 
768         if (Validator.isNull(name)) {
769             throw new OrganizationNameException();
770         }
771         else {
772             Organization organization = organizationPersistence.fetchByC_N(
773                 companyId, name);
774 
775             if ((organization != null) &&
776                 (organization.getName().equalsIgnoreCase(name))) {
777 
778                 if ((organizationId <= 0) ||
779                     (organization.getOrganizationId() != organizationId)) {
780 
781                     throw new DuplicateOrganizationException();
782                 }
783             }
784         }
785 
786         boolean countryRequired = GetterUtil.getBoolean(
787             PropsUtil.get(
788                 PropsKeys.ORGANIZATIONS_COUNTRY_REQUIRED, new Filter(type)));
789 
790         if (countryRequired || (countryId > 0)) {
791             countryPersistence.findByPrimaryKey(countryId);
792         }
793 
794         listTypeService.validate(statusId, ListTypeImpl.ORGANIZATION_STATUS);
795     }
796 
797 }