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