001    /**
002     * Copyright (c) 2000-present 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.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.search.Indexer;
019    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
020    import com.liferay.portal.kernel.security.membershippolicy.OrganizationMembershipPolicyUtil;
021    import com.liferay.portal.kernel.security.permission.ActionKeys;
022    import com.liferay.portal.model.Address;
023    import com.liferay.portal.model.EmailAddress;
024    import com.liferay.portal.model.OrgLabor;
025    import com.liferay.portal.model.Organization;
026    import com.liferay.portal.model.OrganizationConstants;
027    import com.liferay.portal.model.Phone;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.model.Website;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.base.OrganizationServiceBaseImpl;
032    import com.liferay.portal.service.permission.GroupPermissionUtil;
033    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
034    import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
035    import com.liferay.portal.service.permission.PortalPermissionUtil;
036    import com.liferay.portal.service.permission.UserPermissionUtil;
037    import com.liferay.portlet.asset.model.AssetCategory;
038    import com.liferay.portlet.asset.model.AssetTag;
039    import com.liferay.portlet.expando.model.ExpandoBridge;
040    import com.liferay.users.admin.kernel.util.UsersAdminUtil;
041    
042    import java.io.Serializable;
043    
044    import java.util.List;
045    import java.util.Map;
046    
047    /**
048     * Provides the remote service for accessing, adding, deleting, and updating
049     * organizations. Its methods include permission checks.
050     *
051     * @author Brian Wing Shun Chan
052     * @author Jorge Ferrer
053     * @author Julio Camarero
054     */
055    public class OrganizationServiceImpl extends OrganizationServiceBaseImpl {
056    
057            /**
058             * Adds the organizations to the group.
059             *
060             * @param groupId the primary key of the group
061             * @param organizationIds the primary keys of the organizations
062             */
063            @Override
064            public void addGroupOrganizations(long groupId, long[] organizationIds)
065                    throws PortalException {
066    
067                    GroupPermissionUtil.check(
068                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
069    
070                    organizationLocalService.addGroupOrganizations(
071                            groupId, organizationIds);
072            }
073    
074            /**
075             * Adds an organization with additional parameters.
076             *
077             * <p>
078             * This method handles the creation and bookkeeping of the organization
079             * including its resources, metadata, and internal data structures.
080             * </p>
081             *
082             * @param  parentOrganizationId the primary key of the organization's parent
083             *         organization
084             * @param  name the organization's name
085             * @param  type the organization's type
086             * @param  regionId the primary key of the organization's region
087             * @param  countryId the primary key of the organization's country
088             * @param  statusId the organization's workflow status
089             * @param  comments the comments about the organization
090             * @param  site whether the organization is to be associated with a main
091             *         site
092             * @param  addresses the organization's addresses
093             * @param  emailAddresses the organization's email addresses
094             * @param  orgLabors the organization's hours of operation
095             * @param  phones the organization's phone numbers
096             * @param  websites the organization's websites
097             * @param  serviceContext the service context to be applied (optionally
098             *         <code>null</code>). Can set asset category IDs, asset tag names,
099             *         and expando bridge attributes for the organization.
100             * @return the organization
101             */
102            @Override
103            public Organization addOrganization(
104                            long parentOrganizationId, String name, String type, long regionId,
105                            long countryId, long statusId, String comments, boolean site,
106                            List<Address> addresses, List<EmailAddress> emailAddresses,
107                            List<OrgLabor> orgLabors, List<Phone> phones,
108                            List<Website> websites, ServiceContext serviceContext)
109                    throws PortalException {
110    
111                    boolean indexingEnabled = true;
112    
113                    if (serviceContext != null) {
114                            indexingEnabled = serviceContext.isIndexingEnabled();
115    
116                            serviceContext.setIndexingEnabled(false);
117                    }
118    
119                    try {
120                            Organization organization = addOrganization(
121                                    parentOrganizationId, name, type, regionId, countryId, statusId,
122                                    comments, site, serviceContext);
123    
124                            UsersAdminUtil.updateAddresses(
125                                    Organization.class.getName(), organization.getOrganizationId(),
126                                    addresses);
127    
128                            UsersAdminUtil.updateEmailAddresses(
129                                    Organization.class.getName(), organization.getOrganizationId(),
130                                    emailAddresses);
131    
132                            UsersAdminUtil.updateOrgLabors(
133                                    organization.getOrganizationId(), orgLabors);
134    
135                            UsersAdminUtil.updatePhones(
136                                    Organization.class.getName(), organization.getOrganizationId(),
137                                    phones);
138    
139                            UsersAdminUtil.updateWebsites(
140                                    Organization.class.getName(), organization.getOrganizationId(),
141                                    websites);
142    
143                            if (indexingEnabled) {
144                                    Indexer<Organization> indexer =
145                                            IndexerRegistryUtil.nullSafeGetIndexer(Organization.class);
146    
147                                    indexer.reindex(organization);
148                            }
149    
150                            return organization;
151                    }
152                    finally {
153                            if (serviceContext != null) {
154                                    serviceContext.setIndexingEnabled(indexingEnabled);
155                            }
156                    }
157            }
158    
159            /**
160             * Adds an organization.
161             *
162             * <p>
163             * This method handles the creation and bookkeeping of the organization
164             * including its resources, metadata, and internal data structures.
165             * </p>
166             *
167             * @param  parentOrganizationId the primary key of the organization's parent
168             *         organization
169             * @param  name the organization's name
170             * @param  type the organization's type
171             * @param  regionId the primary key of the organization's region
172             * @param  countryId the primary key of the organization's country
173             * @param  statusId the organization's workflow status
174             * @param  comments the comments about the organization
175             * @param  site whether the organization is to be associated with a main
176             *         site
177             * @param  serviceContext the service context to be applied (optionally
178             *         <code>null</code>). Can set asset category IDs, asset tag names,
179             *         and expando bridge attributes for the organization.
180             * @return the organization
181             */
182            @Override
183            public Organization addOrganization(
184                            long parentOrganizationId, String name, String type, long regionId,
185                            long countryId, long statusId, String comments, boolean site,
186                            ServiceContext serviceContext)
187                    throws PortalException {
188    
189                    if (parentOrganizationId ==
190                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
191    
192                            PortalPermissionUtil.check(
193                                    getPermissionChecker(), ActionKeys.ADD_ORGANIZATION);
194                    }
195                    else {
196                            OrganizationPermissionUtil.check(
197                                    getPermissionChecker(), parentOrganizationId,
198                                    ActionKeys.ADD_ORGANIZATION);
199                    }
200    
201                    Organization organization = organizationLocalService.addOrganization(
202                            getUserId(), parentOrganizationId, name, type, regionId, countryId,
203                            statusId, comments, site, serviceContext);
204    
205                    OrganizationMembershipPolicyUtil.verifyPolicy(organization);
206    
207                    return organization;
208            }
209    
210            /**
211             * Assigns the password policy to the organizations, removing any other
212             * currently assigned password policies.
213             *
214             * @param passwordPolicyId the primary key of the password policy
215             * @param organizationIds the primary keys of the organizations
216             */
217            @Override
218            public void addPasswordPolicyOrganizations(
219                            long passwordPolicyId, long[] organizationIds)
220                    throws PortalException {
221    
222                    PasswordPolicyPermissionUtil.check(
223                            getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
224    
225                    organizationLocalService.addPasswordPolicyOrganizations(
226                            passwordPolicyId, organizationIds);
227            }
228    
229            /**
230             * Deletes the organization's logo.
231             *
232             * @param organizationId the primary key of the organization
233             */
234            @Override
235            public void deleteLogo(long organizationId) throws PortalException {
236                    OrganizationPermissionUtil.check(
237                            getPermissionChecker(), organizationId, ActionKeys.UPDATE);
238    
239                    organizationLocalService.deleteLogo(organizationId);
240            }
241    
242            /**
243             * Deletes the organization. The organization's associated resources and
244             * assets are also deleted.
245             *
246             * @param organizationId the primary key of the organization
247             */
248            @Override
249            public void deleteOrganization(long organizationId) throws PortalException {
250                    OrganizationPermissionUtil.check(
251                            getPermissionChecker(), organizationId, ActionKeys.DELETE);
252    
253                    organizationLocalService.deleteOrganization(organizationId);
254            }
255    
256            /**
257             * Returns the organization with the primary key.
258             *
259             * @param  organizationId the primary key of the organization
260             * @return the organization with the primary key, or <code>null</code> if an
261             *         organization with the primary key could not be found or if the
262             *         user did not have permission to view the organization
263             */
264            @Override
265            public Organization fetchOrganization(long organizationId)
266                    throws PortalException {
267    
268                    Organization organization = organizationLocalService.fetchOrganization(
269                            organizationId);
270    
271                    if (organization != null) {
272                            OrganizationPermissionUtil.check(
273                                    getPermissionChecker(), organization, ActionKeys.VIEW);
274                    }
275    
276                    return organization;
277            }
278    
279            /**
280             * Returns the organization with the primary key.
281             *
282             * @param  organizationId the primary key of the organization
283             * @return the organization with the primary key
284             */
285            @Override
286            public Organization getOrganization(long organizationId)
287                    throws PortalException {
288    
289                    Organization organization = organizationLocalService.getOrganization(
290                            organizationId);
291    
292                    OrganizationPermissionUtil.check(
293                            getPermissionChecker(), organization, ActionKeys.VIEW);
294    
295                    return organization;
296            }
297    
298            /**
299             * Returns the primary key of the organization with the name.
300             *
301             * @param  companyId the primary key of the organization's company
302             * @param  name the organization's name
303             * @return the primary key of the organization with the name, or
304             *         <code>0</code> if the organization could not be found
305             */
306            @Override
307            public long getOrganizationId(long companyId, String name)
308                    throws PortalException {
309    
310                    long organizationId = organizationLocalService.getOrganizationId(
311                            companyId, name);
312    
313                    OrganizationPermissionUtil.check(
314                            getPermissionChecker(), organizationId, ActionKeys.VIEW);
315    
316                    return organizationId;
317            }
318    
319            /**
320             * Returns all the organizations belonging to the parent organization.
321             *
322             * @param  companyId the primary key of the organizations' company
323             * @param  parentOrganizationId the primary key of the organizations' parent
324             *         organization
325             * @return the organizations belonging to the parent organization
326             */
327            @Override
328            public List<Organization> getOrganizations(
329                    long companyId, long parentOrganizationId) {
330    
331                    if (parentOrganizationId ==
332                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
333    
334                            return organizationPersistence.filterFindByCompanyId(companyId);
335                    }
336    
337                    return organizationPersistence.filterFindByC_P(
338                            companyId, parentOrganizationId);
339            }
340    
341            /**
342             * Returns a range of all the organizations belonging to the parent
343             * organization.
344             *
345             * <p>
346             * Useful when paginating results. Returns a maximum of <code>end -
347             * start</code> instances. <code>start</code> and <code>end</code> are not
348             * primary keys, they are indexes in the result set. Thus, <code>0</code>
349             * refers to the first result in the set. Setting both <code>start</code>
350             * and <code>end</code> to {@link
351             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
352             * result set.
353             * </p>
354             *
355             * @param  companyId the primary key of the organizations' company
356             * @param  parentOrganizationId the primary key of the organizations' parent
357             *         organization
358             * @param  start the lower bound of the range of organizations to return
359             * @param  end the upper bound of the range of organizations to return (not
360             *         inclusive)
361             * @return the range of organizations belonging to the parent organization
362             */
363            @Override
364            public List<Organization> getOrganizations(
365                    long companyId, long parentOrganizationId, int start, int end) {
366    
367                    if (parentOrganizationId ==
368                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
369    
370                            return organizationPersistence.filterFindByCompanyId(
371                                    companyId, start, end);
372                    }
373    
374                    return organizationPersistence.filterFindByC_P(
375                            companyId, parentOrganizationId, start, end);
376            }
377    
378            /**
379             * Returns the number of organizations belonging to the parent organization.
380             *
381             * @param  companyId the primary key of the organizations' company
382             * @param  parentOrganizationId the primary key of the organizations' parent
383             *         organization
384             * @return the number of organizations belonging to the parent organization
385             */
386            @Override
387            public int getOrganizationsCount(
388                    long companyId, long parentOrganizationId) {
389    
390                    if (parentOrganizationId ==
391                                    OrganizationConstants.ANY_PARENT_ORGANIZATION_ID) {
392    
393                            return organizationPersistence.filterCountByCompanyId(companyId);
394                    }
395    
396                    return organizationPersistence.filterCountByC_P(
397                            companyId, parentOrganizationId);
398            }
399    
400            /**
401             * Returns all the organizations with which the user is explicitly
402             * associated.
403             *
404             * <p>
405             * A user is considered to be <i>explicitly</i> associated with an
406             * organization if his account is individually created within the
407             * organization or if the user is later added as a member.
408             * </p>
409             *
410             * @param  userId the primary key of the user
411             * @return the organizations with which the user is explicitly associated
412             */
413            @Override
414            public List<Organization> getUserOrganizations(long userId)
415                    throws PortalException {
416    
417                    UserPermissionUtil.check(
418                            getPermissionChecker(), userId, ActionKeys.VIEW);
419    
420                    return organizationLocalService.getUserOrganizations(userId);
421            }
422    
423            /**
424             * Sets the organizations in the group, removing and adding organizations to
425             * the group as necessary.
426             *
427             * @param groupId the primary key of the group
428             * @param organizationIds the primary keys of the organizations
429             */
430            @Override
431            public void setGroupOrganizations(long groupId, long[] organizationIds)
432                    throws PortalException {
433    
434                    GroupPermissionUtil.check(
435                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
436    
437                    organizationLocalService.setGroupOrganizations(
438                            groupId, organizationIds);
439            }
440    
441            /**
442             * Removes the organizations from the group.
443             *
444             * @param groupId the primary key of the group
445             * @param organizationIds the primary keys of the organizations
446             */
447            @Override
448            public void unsetGroupOrganizations(long groupId, long[] organizationIds)
449                    throws PortalException {
450    
451                    GroupPermissionUtil.check(
452                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
453    
454                    organizationLocalService.unsetGroupOrganizations(
455                            groupId, organizationIds);
456            }
457    
458            /**
459             * Removes the organizations from the password policy.
460             *
461             * @param passwordPolicyId the primary key of the password policy
462             * @param organizationIds the primary keys of the organizations
463             */
464            @Override
465            public void unsetPasswordPolicyOrganizations(
466                            long passwordPolicyId, long[] organizationIds)
467                    throws PortalException {
468    
469                    PasswordPolicyPermissionUtil.check(
470                            getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
471    
472                    organizationLocalService.unsetPasswordPolicyOrganizations(
473                            passwordPolicyId, organizationIds);
474            }
475    
476            /**
477             * Updates the organization with additional parameters.
478             *
479             * @param  organizationId the primary key of the organization
480             * @param  parentOrganizationId the primary key of the organization's parent
481             *         organization
482             * @param  name the organization's name
483             * @param  type the organization's type
484             * @param  regionId the primary key of the organization's region
485             * @param  countryId the primary key of the organization's country
486             * @param  statusId the organization's workflow status
487             * @param  comments the comments about the organization
488             * @param  logo whether to update the ogranization's logo
489             * @param  logoBytes the new logo image data
490             * @param  site whether the organization is to be associated with a main
491             *         site
492             * @param  addresses the organization's addresses
493             * @param  emailAddresses the organization's email addresses
494             * @param  orgLabors the organization's hours of operation
495             * @param  phones the organization's phone numbers
496             * @param  websites the organization's websites
497             * @param  serviceContext the service context to be applied (optionally
498             *         <code>null</code>). Can set asset category IDs and asset tag
499             *         names for the organization, and merge expando bridge attributes
500             *         for the organization.
501             * @return the organization
502             */
503            @Override
504            public Organization updateOrganization(
505                            long organizationId, long parentOrganizationId, String name,
506                            String type, long regionId, long countryId, long statusId,
507                            String comments, boolean logo, byte[] logoBytes, boolean site,
508                            List<Address> addresses, List<EmailAddress> emailAddresses,
509                            List<OrgLabor> orgLabors, List<Phone> phones,
510                            List<Website> websites, ServiceContext serviceContext)
511                    throws PortalException {
512    
513                    Organization organization = organizationPersistence.findByPrimaryKey(
514                            organizationId);
515    
516                    OrganizationPermissionUtil.check(
517                            getPermissionChecker(), organization, ActionKeys.UPDATE);
518    
519                    if (organization.getParentOrganizationId() != parentOrganizationId) {
520                            if (parentOrganizationId ==
521                                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
522    
523                                    PortalPermissionUtil.check(
524                                            getPermissionChecker(), ActionKeys.ADD_ORGANIZATION);
525                            }
526                            else {
527                                    OrganizationPermissionUtil.check(
528                                            getPermissionChecker(), parentOrganizationId,
529                                            ActionKeys.ADD_ORGANIZATION);
530                            }
531                    }
532    
533                    if (addresses != null) {
534                            UsersAdminUtil.updateAddresses(
535                                    Organization.class.getName(), organizationId, addresses);
536                    }
537    
538                    if (emailAddresses != null) {
539                            UsersAdminUtil.updateEmailAddresses(
540                                    Organization.class.getName(), organizationId, emailAddresses);
541                    }
542    
543                    if (orgLabors != null) {
544                            UsersAdminUtil.updateOrgLabors(organizationId, orgLabors);
545                    }
546    
547                    if (phones != null) {
548                            UsersAdminUtil.updatePhones(
549                                    Organization.class.getName(), organizationId, phones);
550                    }
551    
552                    if (websites != null) {
553                            UsersAdminUtil.updateWebsites(
554                                    Organization.class.getName(), organizationId, websites);
555                    }
556    
557                    User user = getUser();
558    
559                    Organization oldOrganization = organization;
560    
561                    List<AssetCategory> oldAssetCategories =
562                            assetCategoryLocalService.getCategories(
563                                    Organization.class.getName(), organizationId);
564    
565                    List<AssetTag> oldAssetTags = assetTagLocalService.getTags(
566                            Organization.class.getName(), organizationId);
567    
568                    ExpandoBridge oldExpandoBridge = oldOrganization.getExpandoBridge();
569    
570                    Map<String, Serializable> oldExpandoAttributes =
571                            oldExpandoBridge.getAttributes();
572    
573                    organization = organizationLocalService.updateOrganization(
574                            user.getCompanyId(), organizationId, parentOrganizationId, name,
575                            type, regionId, countryId, statusId, comments, logo, logoBytes,
576                            site, serviceContext);
577    
578                    OrganizationMembershipPolicyUtil.verifyPolicy(
579                            organization, oldOrganization, oldAssetCategories, oldAssetTags,
580                            oldExpandoAttributes);
581    
582                    return organization;
583            }
584    
585            /**
586             * Updates the organization with additional parameters.
587             *
588             * @param      organizationId the primary key of the organization
589             * @param      parentOrganizationId the primary key of the organization's
590             *             parent organization
591             * @param      name the organization's name
592             * @param      type the organization's type
593             * @param      regionId the primary key of the organization's region
594             * @param      countryId the primary key of the organization's country
595             * @param      statusId the organization's workflow status
596             * @param      comments the comments about the organization
597             * @param      site whether the organization is to be associated with a main
598             *             site
599             * @param      addresses the organization's addresses
600             * @param      emailAddresses the organization's email addresses
601             * @param      orgLabors the organization's hours of operation
602             * @param      phones the organization's phone numbers
603             * @param      websites the organization's websites
604             * @param      serviceContext the service context to be applied (optionally
605             *             <code>null</code>). Can set asset category IDs and asset tag
606             *             names for the organization, and merge expando bridge
607             *             attributes for the organization.
608             * @return     the organization
609             * @deprecated As of 7.0.0, replaced by {@link #updateOrganization(long,
610             *             long, String, String, long, long, long, String, boolean,
611             *             byte[], boolean, List, List, List, List, List,
612             *             ServiceContext)}
613             */
614            @Deprecated
615            @Override
616            public Organization updateOrganization(
617                            long organizationId, long parentOrganizationId, String name,
618                            String type, long regionId, long countryId, long statusId,
619                            String comments, boolean site, List<Address> addresses,
620                            List<EmailAddress> emailAddresses, List<OrgLabor> orgLabors,
621                            List<Phone> phones, List<Website> websites,
622                            ServiceContext serviceContext)
623                    throws PortalException {
624    
625                    return updateOrganization(
626                            organizationId, parentOrganizationId, name, type, regionId,
627                            countryId, statusId, comments, true, null, site, addresses,
628                            emailAddresses, orgLabors, phones, websites, serviceContext);
629            }
630    
631            /**
632             * Updates the organization.
633             *
634             * @param  organizationId the primary key of the organization
635             * @param  parentOrganizationId the primary key of the organization's parent
636             *         organization
637             * @param  name the organization's name
638             * @param  type the organization's type
639             * @param  regionId the primary key of the organization's region
640             * @param  countryId the primary key of the organization's country
641             * @param  statusId the organization's workflow status
642             * @param  comments the comments about the organization
643             * @param  site whether the organization is to be associated with a main
644             *         site
645             * @param  serviceContext the service context to be applied (optionally
646             *         <code>null</code>). Can set asset category IDs and asset tag
647             *         names for the organization, and merge expando bridge attributes
648             *         for the organization.
649             * @return the organization
650             */
651            @Override
652            public Organization updateOrganization(
653                            long organizationId, long parentOrganizationId, String name,
654                            String type, long regionId, long countryId, long statusId,
655                            String comments, boolean site, ServiceContext serviceContext)
656                    throws PortalException {
657    
658                    return updateOrganization(
659                            organizationId, parentOrganizationId, name, type, regionId,
660                            countryId, statusId, comments, true, null, site, null, null, null,
661                            null, null, serviceContext);
662            }
663    
664    }