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