001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 specify the organization's asset category
101             *         IDs, asset tag names, and expando bridge attributes.
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 specify the organization's asset category
190             *         IDs, asset tag names, and expando bridge attributes.
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(
425                            long companyId, long parentOrganizationId)
426                    throws SystemException {
427    
428                    return organizationPersistence.filterCountByC_P(
429                            companyId, parentOrganizationId);
430            }
431    
432            /**
433             * Returns all the organizations associated with the user.
434             *
435             * @param  userId the primary key of the user
436             * @return the organizations associated with the user
437             * @throws PortalException if a user with the primary key could not be found
438             * @throws SystemException if a system exception occurred
439             */
440            public List<Organization> getUserOrganizations(long userId)
441                    throws PortalException, SystemException {
442    
443                    return organizationLocalService.getUserOrganizations(userId);
444            }
445    
446            /**
447             * Returns all the organizations associated with the user, optionally
448             * including the organizations associated with the user groups to which the
449             * user belongs.
450             *
451             * @param  userId the primary key of the user
452             * @param  inheritUserGroups whether to include organizations associated
453             *         with the user groups to which the user belongs
454             * @return the organizations associated with the user
455             * @throws PortalException if a user with the primary key could not be found
456             * @throws SystemException if a system exception occurred
457             */
458            public List<Organization> getUserOrganizations(
459                            long userId, boolean inheritUserGroups)
460                    throws PortalException, SystemException {
461    
462                    return organizationLocalService.getUserOrganizations(
463                            userId, inheritUserGroups);
464            }
465    
466            /**
467             * Sets the organizations in the group, removing and adding organizations to
468             * the group as necessary.
469             *
470             * @param  groupId the primary key of the group
471             * @param  organizationIds the primary keys of the organizations
472             * @throws PortalException if a group or organization with the primary key
473             *         could not be found or if the user did not have permission to
474             *         assign group members
475             * @throws SystemException if a system exception occurred
476             */
477            public void setGroupOrganizations(long groupId, long[] organizationIds)
478                    throws PortalException, SystemException {
479    
480                    GroupPermissionUtil.check(
481                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
482    
483                    organizationLocalService.setGroupOrganizations(
484                            groupId, organizationIds);
485            }
486    
487            /**
488             * Removes the organizations from the group.
489             *
490             * @param  groupId the primary key of the group
491             * @param  organizationIds the primary keys of the organizations
492             * @throws PortalException if a group or organization with the primary key
493             *         could not be found or if the user did not have permission to
494             *         assign group members
495             * @throws SystemException if a system exception occurred
496             */
497            public void unsetGroupOrganizations(long groupId, long[] organizationIds)
498                    throws PortalException, SystemException {
499    
500                    GroupPermissionUtil.check(
501                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
502    
503                    organizationLocalService.unsetGroupOrganizations(
504                            groupId, organizationIds);
505            }
506    
507            /**
508             * Removes the organizations from the password policy.
509             *
510             * @param  passwordPolicyId the primary key of the password policy
511             * @param  organizationIds the primary keys of the organizations
512             * @throws PortalException if a password policy or organization with the
513             *         primary key could not be found, or if the user did not have
514             *         permission to update the password policy.
515             * @throws SystemException if a system exception occurred
516             */
517            public void unsetPasswordPolicyOrganizations(
518                            long passwordPolicyId, long[] organizationIds)
519                    throws PortalException, SystemException {
520    
521                    PasswordPolicyPermissionUtil.check(
522                            getPermissionChecker(), passwordPolicyId, ActionKeys.UPDATE);
523    
524                    organizationLocalService.unsetPasswordPolicyOrganizations(
525                            passwordPolicyId, organizationIds);
526            }
527    
528            /**
529             * Updates the organization with additional parameters.
530             *
531             * @param  organizationId the primary key of the organization
532             * @param  parentOrganizationId the primary key of the organization's parent
533             *         organization
534             * @param  name the organization's name
535             * @param  type the organization's type
536             * @param  recursable whether the permissions of the organization are to be
537             *         inherited by its sub-organizations
538             * @param  regionId the primary key of the organization's region
539             * @param  countryId the primary key of the organization's country
540             * @param  statusId the organization's workflow status
541             * @param  comments the comments about the organization
542             * @param  site whether the organization is to be associated with a main
543             *         site
544             * @param  addresses the organization's addresses
545             * @param  emailAddresses the organization's email addresses
546             * @param  orgLabors the organization's hours of operation
547             * @param  phones the organization's phone numbers
548             * @param  websites the organization's websites
549             * @param  serviceContext the organization's service context (optionally
550             *         <code>null</code>). Can specify the organization's replacement
551             *         asset category IDs, replacement asset tag names, and new expando
552             *         bridge attributes.
553             * @return the organization
554             * @throws PortalException if an organization or parent organization with
555             *         the primary key could not be found, if the user did not have
556             *         permission to update the organization information, or if the new
557             *         information was invalid
558             * @throws SystemException if a system exception occurred
559             */
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                    UsersAdminUtil.updateAddresses(
570                            Organization.class.getName(), organizationId, addresses);
571    
572                    UsersAdminUtil.updateEmailAddresses(
573                            Organization.class.getName(), organizationId, emailAddresses);
574    
575                    UsersAdminUtil.updateOrgLabors(organizationId, orgLabors);
576    
577                    UsersAdminUtil.updatePhones(
578                            Organization.class.getName(), organizationId, phones);
579    
580                    UsersAdminUtil.updateWebsites(
581                            Organization.class.getName(), organizationId, websites);
582    
583                    Organization organization = updateOrganization(
584                            organizationId, parentOrganizationId, name, type, recursable,
585                            regionId, countryId, statusId, comments, site, serviceContext);
586    
587                    return organization;
588            }
589    
590            /**
591             * Updates the organization.
592             *
593             * @param  organizationId the primary key of the organization
594             * @param  parentOrganizationId the primary key of the organization's parent
595             *         organization
596             * @param  name the organization's name
597             * @param  type the organization's type
598             * @param  recursable whether permissions of the organization are to be
599             *         inherited by its sub-organizations
600             * @param  regionId the primary key of the organization's region
601             * @param  countryId the primary key of the organization's country
602             * @param  statusId the organization's workflow status
603             * @param  comments the comments about the organization
604             * @param  site whether the organization is to be associated with a main
605             *         site
606             * @param  serviceContext the organization's service context (optionally
607             *         <code>null</code>). Can specify the organization's replacement
608             *         asset category IDs, replacement asset tag names, and new expando
609             *         bridge attributes.
610             * @return the organization
611             * @throws PortalException if an organization or parent organization with
612             *         the primary key could not be found, if the user did not have
613             *         permission to update the organization, or if the new information
614             *         was invalid
615             * @throws SystemException if a system exception occurred
616             */
617            public Organization updateOrganization(
618                            long organizationId, long parentOrganizationId, String name,
619                            String type, boolean recursable, long regionId, long countryId,
620                            int statusId, String comments, boolean site,
621                            ServiceContext serviceContext)
622                    throws PortalException, SystemException {
623    
624                    OrganizationPermissionUtil.check(
625                            getPermissionChecker(), organizationId, ActionKeys.UPDATE);
626    
627                    User user = getUser();
628    
629                    return organizationLocalService.updateOrganization(
630                            user.getCompanyId(), organizationId, parentOrganizationId,
631                            name, type, recursable, regionId, countryId, statusId, comments,
632                            site, serviceContext);
633            }
634    
635    }