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