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