001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.util;
016    
017    import com.liferay.portal.NoSuchOrganizationException;
018    import com.liferay.portal.NoSuchUserException;
019    import com.liferay.portal.NoSuchUserGroupException;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.Hits;
025    import com.liferay.portal.kernel.search.Indexer;
026    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.ListUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.ParamUtil;
033    import com.liferay.portal.kernel.util.StringUtil;
034    import com.liferay.portal.kernel.util.Tuple;
035    import com.liferay.portal.kernel.util.UniqueList;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.Address;
038    import com.liferay.portal.model.EmailAddress;
039    import com.liferay.portal.model.Group;
040    import com.liferay.portal.model.OrgLabor;
041    import com.liferay.portal.model.Organization;
042    import com.liferay.portal.model.Phone;
043    import com.liferay.portal.model.Role;
044    import com.liferay.portal.model.RoleConstants;
045    import com.liferay.portal.model.User;
046    import com.liferay.portal.model.UserGroup;
047    import com.liferay.portal.model.UserGroupRole;
048    import com.liferay.portal.model.Website;
049    import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
050    import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
051    import com.liferay.portal.security.permission.ActionKeys;
052    import com.liferay.portal.security.permission.PermissionChecker;
053    import com.liferay.portal.service.AddressLocalServiceUtil;
054    import com.liferay.portal.service.AddressServiceUtil;
055    import com.liferay.portal.service.EmailAddressLocalServiceUtil;
056    import com.liferay.portal.service.EmailAddressServiceUtil;
057    import com.liferay.portal.service.GroupLocalServiceUtil;
058    import com.liferay.portal.service.OrgLaborLocalServiceUtil;
059    import com.liferay.portal.service.OrgLaborServiceUtil;
060    import com.liferay.portal.service.OrganizationLocalServiceUtil;
061    import com.liferay.portal.service.PhoneLocalServiceUtil;
062    import com.liferay.portal.service.PhoneServiceUtil;
063    import com.liferay.portal.service.RoleLocalServiceUtil;
064    import com.liferay.portal.service.UserGroupLocalServiceUtil;
065    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
066    import com.liferay.portal.service.UserLocalServiceUtil;
067    import com.liferay.portal.service.WebsiteLocalServiceUtil;
068    import com.liferay.portal.service.WebsiteServiceUtil;
069    import com.liferay.portal.service.permission.GroupPermissionUtil;
070    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
071    import com.liferay.portal.service.permission.RolePermissionUtil;
072    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
073    import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
074    import com.liferay.portal.service.persistence.UserGroupRolePK;
075    import com.liferay.portal.util.PortalUtil;
076    import com.liferay.portal.util.PropsValues;
077    import com.liferay.portal.util.comparator.GroupNameComparator;
078    import com.liferay.portal.util.comparator.GroupTypeComparator;
079    import com.liferay.portal.util.comparator.OrganizationNameComparator;
080    import com.liferay.portal.util.comparator.OrganizationTypeComparator;
081    import com.liferay.portal.util.comparator.RoleDescriptionComparator;
082    import com.liferay.portal.util.comparator.RoleNameComparator;
083    import com.liferay.portal.util.comparator.RoleTypeComparator;
084    import com.liferay.portal.util.comparator.UserEmailAddressComparator;
085    import com.liferay.portal.util.comparator.UserFirstNameComparator;
086    import com.liferay.portal.util.comparator.UserGroupDescriptionComparator;
087    import com.liferay.portal.util.comparator.UserGroupNameComparator;
088    import com.liferay.portal.util.comparator.UserJobTitleComparator;
089    import com.liferay.portal.util.comparator.UserLastNameComparator;
090    import com.liferay.portal.util.comparator.UserScreenNameComparator;
091    
092    import java.util.ArrayList;
093    import java.util.Collections;
094    import java.util.HashSet;
095    import java.util.Iterator;
096    import java.util.List;
097    import java.util.Set;
098    
099    import javax.portlet.ActionRequest;
100    import javax.portlet.PortletRequest;
101    import javax.portlet.PortletURL;
102    import javax.portlet.RenderResponse;
103    
104    import javax.servlet.http.HttpServletRequest;
105    
106    /**
107     * @author Brian Wing Shun Chan
108     * @author Jorge Ferrer
109     * @author Julio Camarero
110     */
111    @DoPrivileged
112    public class UsersAdminImpl implements UsersAdmin {
113    
114            public void addPortletBreadcrumbEntries(
115                            Organization organization, HttpServletRequest request,
116                            RenderResponse renderResponse)
117                    throws Exception {
118    
119                    PortletURL portletURL = renderResponse.createRenderURL();
120    
121                    portletURL.setParameter("struts_action", "/users_admin/view");
122    
123                    List<Organization> ancestorOrganizations = organization.getAncestors();
124    
125                    Collections.reverse(ancestorOrganizations);
126    
127                    for (Organization ancestorOrganization : ancestorOrganizations) {
128                            portletURL.setParameter(
129                                    "organizationId",
130                                    String.valueOf(ancestorOrganization.getOrganizationId()));
131    
132                            PortalUtil.addPortletBreadcrumbEntry(
133                                    request, ancestorOrganization.getName(), portletURL.toString());
134                    }
135    
136                    Organization unescapedOrganization = organization.toUnescapedModel();
137    
138                    portletURL.setParameter(
139                            "organizationId",
140                            String.valueOf(unescapedOrganization.getOrganizationId()));
141    
142                    PortalUtil.addPortletBreadcrumbEntry(
143                            request, unescapedOrganization.getName(), portletURL.toString());
144            }
145    
146            public long[] addRequiredRoles(long userId, long[] roleIds)
147                    throws PortalException, SystemException {
148    
149                    User user = UserLocalServiceUtil.getUser(userId);
150    
151                    return addRequiredRoles(user, roleIds);
152            }
153    
154            public long[] addRequiredRoles(User user, long[] roleIds)
155                    throws PortalException, SystemException {
156    
157                    if (user.isDefaultUser()) {
158                            return removeRequiredRoles(user, roleIds);
159                    }
160    
161                    Role administratorRole = RoleLocalServiceUtil.getRole(
162                            user.getCompanyId(), RoleConstants.ADMINISTRATOR);
163    
164                    long[] administratorUserIds = UserLocalServiceUtil.getRoleUserIds(
165                            administratorRole.getRoleId());
166    
167                    if (ArrayUtil.contains(administratorUserIds, user.getUserId()) &&
168                            !ArrayUtil.contains(roleIds, administratorRole.getRoleId()) &&
169                            (administratorUserIds.length == 1)) {
170    
171                            roleIds = ArrayUtil.append(roleIds, administratorRole.getRoleId());
172                    }
173    
174                    Role userRole = RoleLocalServiceUtil.getRole(
175                            user.getCompanyId(), RoleConstants.USER);
176    
177                    if (!ArrayUtil.contains(roleIds, userRole.getRoleId())) {
178                            roleIds = ArrayUtil.append(roleIds, userRole.getRoleId());
179                    }
180    
181                    return roleIds;
182            }
183    
184            public List<Role> filterGroupRoles(
185                            PermissionChecker permissionChecker, long groupId, List<Role> roles)
186                    throws PortalException, SystemException {
187    
188                    List<Role> filteredGroupRoles = ListUtil.copy(roles);
189    
190                    Iterator<Role> itr = filteredGroupRoles.iterator();
191    
192                    while (itr.hasNext()) {
193                            Role groupRole = itr.next();
194    
195                            String name = groupRole.getName();
196    
197                            if (name.equals(RoleConstants.ORGANIZATION_USER) ||
198                                    name.equals(RoleConstants.SITE_MEMBER)) {
199    
200                                    itr.remove();
201                            }
202                    }
203    
204                    if (permissionChecker.isCompanyAdmin() ||
205                            permissionChecker.isGroupOwner(groupId)) {
206    
207                            return filteredGroupRoles;
208                    }
209    
210                    itr = filteredGroupRoles.iterator();
211    
212                    while (itr.hasNext()) {
213                            Role groupRole = itr.next();
214    
215                            String groupRoleName = groupRole.getName();
216    
217                            if (groupRoleName.equals(
218                                            RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
219                                    groupRoleName.equals(RoleConstants.ORGANIZATION_OWNER) ||
220                                    groupRoleName.equals(RoleConstants.SITE_ADMINISTRATOR) ||
221                                    groupRoleName.equals(RoleConstants.SITE_OWNER)) {
222    
223                                    itr.remove();
224                            }
225                    }
226    
227                    Group group = GroupLocalServiceUtil.getGroup(groupId);
228    
229                    if (GroupPermissionUtil.contains(
230                                    permissionChecker, groupId, ActionKeys.ASSIGN_USER_ROLES) ||
231                            OrganizationPermissionUtil.contains(
232                                    permissionChecker, group.getOrganizationId(),
233                                    ActionKeys.ASSIGN_USER_ROLES)) {
234    
235                            return filteredGroupRoles;
236                    }
237    
238                    itr = filteredGroupRoles.iterator();
239    
240                    while (itr.hasNext()) {
241                            Role role = itr.next();
242    
243                            if (!RolePermissionUtil.contains(
244                                            permissionChecker, groupId, role.getRoleId(),
245                                            ActionKeys.ASSIGN_MEMBERS)) {
246    
247                                    itr.remove();
248                            }
249                    }
250    
251                    return filteredGroupRoles;
252            }
253    
254            public List<Group> filterGroups(
255                            PermissionChecker permissionChecker, List<Group> groups)
256                    throws PortalException, SystemException {
257    
258                    if (permissionChecker.isCompanyAdmin()) {
259                            return groups;
260                    }
261    
262                    List<Group> filteredGroups = ListUtil.copy(groups);
263    
264                    Iterator<Group> itr = filteredGroups.iterator();
265    
266                    while (itr.hasNext()) {
267                            Group group = itr.next();
268    
269                            if (!GroupPermissionUtil.contains(
270                                            permissionChecker, group.getGroupId(),
271                                            ActionKeys.ASSIGN_MEMBERS)) {
272    
273                                    itr.remove();
274                            }
275                    }
276    
277                    return filteredGroups;
278            }
279    
280            public List<Organization> filterOrganizations(
281                            PermissionChecker permissionChecker,
282                            List<Organization> organizations)
283                    throws PortalException, SystemException {
284    
285                    if (permissionChecker.isCompanyAdmin()) {
286                            return organizations;
287                    }
288    
289                    List<Organization> filteredOrganizations = ListUtil.copy(organizations);
290    
291                    Iterator<Organization> itr = filteredOrganizations.iterator();
292    
293                    while (itr.hasNext()) {
294                            Organization organization = itr.next();
295    
296                            if (!OrganizationPermissionUtil.contains(
297                                            permissionChecker, organization.getOrganizationId(),
298                                            ActionKeys.ASSIGN_MEMBERS)) {
299    
300                                    itr.remove();
301                            }
302                    }
303    
304                    return filteredOrganizations;
305            }
306    
307            public List<Role> filterRoles(
308                    PermissionChecker permissionChecker, List<Role> roles) {
309    
310                    List<Role> filteredRoles = ListUtil.copy(roles);
311    
312                    Iterator<Role> itr = filteredRoles.iterator();
313    
314                    while (itr.hasNext()) {
315                            Role role = itr.next();
316    
317                            String name = role.getName();
318    
319                            if (name.equals(RoleConstants.GUEST) ||
320                                    name.equals(RoleConstants.ORGANIZATION_USER) ||
321                                    name.equals(RoleConstants.OWNER) ||
322                                    name.equals(RoleConstants.SITE_MEMBER) ||
323                                    name.equals(RoleConstants.USER)) {
324    
325                                    itr.remove();
326                            }
327                    }
328    
329                    if (permissionChecker.isCompanyAdmin()) {
330                            return filteredRoles;
331                    }
332    
333                    itr = filteredRoles.iterator();
334    
335                    while (itr.hasNext()) {
336                            Role role = itr.next();
337    
338                            if (!RolePermissionUtil.contains(
339                                            permissionChecker, role.getRoleId(),
340                                            ActionKeys.ASSIGN_MEMBERS)) {
341    
342                                    itr.remove();
343                            }
344                    }
345    
346                    return filteredRoles;
347            }
348    
349            public long[] filterUnsetGroupUserIds(
350                            PermissionChecker permissionChecker, long groupId, long[] userIds)
351                    throws PortalException, SystemException {
352    
353                    long[] filteredUserIds = userIds;
354    
355                    for (long userId : userIds) {
356                            if (SiteMembershipPolicyUtil.isMembershipProtected(
357                                            permissionChecker, userId, groupId)) {
358    
359                                    filteredUserIds = ArrayUtil.remove(filteredUserIds, userId);
360                            }
361                    }
362    
363                    return filteredUserIds;
364            }
365    
366            public long[] filterUnsetOrganizationUserIds(
367                            PermissionChecker permissionChecker, long organizationId,
368                            long[] userIds)
369                    throws PortalException, SystemException {
370    
371                    long[] filteredUserIds = userIds;
372    
373                    for (long userId : userIds) {
374                            if (OrganizationMembershipPolicyUtil.isMembershipProtected(
375                                            permissionChecker, userId, organizationId)) {
376    
377                                    filteredUserIds = ArrayUtil.remove(filteredUserIds, userId);
378                            }
379                    }
380    
381                    return filteredUserIds;
382            }
383    
384            public List<UserGroupRole> filterUserGroupRoles(
385                            PermissionChecker permissionChecker,
386                            List<UserGroupRole> userGroupRoles)
387                    throws PortalException, SystemException {
388    
389                    List<UserGroupRole> filteredUserGroupRoles = ListUtil.copy(
390                            userGroupRoles);
391    
392                    Iterator<UserGroupRole> itr = filteredUserGroupRoles.iterator();
393    
394                    while (itr.hasNext()) {
395                            UserGroupRole userGroupRole = itr.next();
396    
397                            Role role = userGroupRole.getRole();
398    
399                            String name = role.getName();
400    
401                            if (name.equals(RoleConstants.ORGANIZATION_USER) ||
402                                    name.equals(RoleConstants.SITE_MEMBER)) {
403    
404                                    itr.remove();
405                            }
406                    }
407    
408                    if (permissionChecker.isCompanyAdmin()) {
409                            return filteredUserGroupRoles;
410                    }
411    
412                    itr = filteredUserGroupRoles.iterator();
413    
414                    while (itr.hasNext()) {
415                            UserGroupRole userGroupRole = itr.next();
416    
417                            if (!UserGroupRolePermissionUtil.contains(
418                                            permissionChecker, userGroupRole.getGroupId(),
419                                            userGroupRole.getRoleId())) {
420    
421                                    itr.remove();
422                            }
423                    }
424    
425                    return filteredUserGroupRoles;
426            }
427    
428            public List<UserGroup> filterUserGroups(
429                    PermissionChecker permissionChecker, List<UserGroup> userGroups) {
430    
431                    if (permissionChecker.isCompanyAdmin()) {
432                            return userGroups;
433                    }
434    
435                    List<UserGroup> filteredUserGroups = ListUtil.copy(userGroups);
436    
437                    Iterator<UserGroup> itr = filteredUserGroups.iterator();
438    
439                    while (itr.hasNext()) {
440                            UserGroup userGroup = itr.next();
441    
442                            if (!UserGroupPermissionUtil.contains(
443                                            permissionChecker, userGroup.getUserGroupId(),
444                                            ActionKeys.ASSIGN_MEMBERS)) {
445    
446                                    itr.remove();
447                            }
448                    }
449    
450                    return filteredUserGroups;
451            }
452    
453            public List<Address> getAddresses(ActionRequest actionRequest) {
454                    return getAddresses(actionRequest, Collections.<Address>emptyList());
455            }
456    
457            public List<Address> getAddresses(
458                    ActionRequest actionRequest, List<Address> defaultAddresses) {
459    
460                    String addressesIndexesString = actionRequest.getParameter(
461                            "addressesIndexes");
462    
463                    if (addressesIndexesString == null) {
464                            return defaultAddresses;
465                    }
466    
467                    List<Address> addresses = new ArrayList<Address>();
468    
469                    int[] addressesIndexes = StringUtil.split(addressesIndexesString, 0);
470    
471                    int addressPrimary = ParamUtil.getInteger(
472                            actionRequest, "addressPrimary");
473    
474                    for (int addressesIndex : addressesIndexes) {
475                            long addressId = ParamUtil.getLong(
476                                    actionRequest, "addressId" + addressesIndex);
477    
478                            String street1 = ParamUtil.getString(
479                                    actionRequest, "addressStreet1_" + addressesIndex);
480                            String street2 = ParamUtil.getString(
481                                    actionRequest, "addressStreet2_" + addressesIndex);
482                            String street3 = ParamUtil.getString(
483                                    actionRequest, "addressStreet3_" + addressesIndex);
484                            String city = ParamUtil.getString(
485                                    actionRequest, "addressCity" + addressesIndex);
486                            String zip = ParamUtil.getString(
487                                    actionRequest, "addressZip" + addressesIndex);
488    
489                            if (Validator.isNull(street1) && Validator.isNull(street2) &&
490                                    Validator.isNull(street3) && Validator.isNull(city) &&
491                                    Validator.isNull(zip)) {
492    
493                                    continue;
494                            }
495    
496                            long regionId = ParamUtil.getLong(
497                                    actionRequest, "addressRegionId" + addressesIndex);
498                            long countryId = ParamUtil.getLong(
499                                    actionRequest, "addressCountryId" + addressesIndex);
500                            int typeId = ParamUtil.getInteger(
501                                    actionRequest, "addressTypeId" + addressesIndex);
502                            boolean mailing = ParamUtil.getBoolean(
503                                    actionRequest, "addressMailing" + addressesIndex);
504    
505                            boolean primary = false;
506    
507                            if (addressesIndex == addressPrimary) {
508                                    primary = true;
509                            }
510    
511                            Address address = AddressLocalServiceUtil.createAddress(addressId);
512    
513                            address.setStreet1(street1);
514                            address.setStreet2(street2);
515                            address.setStreet3(street3);
516                            address.setCity(city);
517                            address.setZip(zip);
518                            address.setRegionId(regionId);
519                            address.setCountryId(countryId);
520                            address.setTypeId(typeId);
521                            address.setMailing(mailing);
522                            address.setPrimary(primary);
523    
524                            addresses.add(address);
525                    }
526    
527                    return addresses;
528            }
529    
530            public List<EmailAddress> getEmailAddresses(ActionRequest actionRequest) {
531                    return getEmailAddresses(
532                            actionRequest, Collections.<EmailAddress>emptyList());
533            }
534    
535            public List<EmailAddress> getEmailAddresses(
536                    ActionRequest actionRequest, List<EmailAddress> defaultEmailAddresses) {
537    
538                    String emailAddressesIndexesString = actionRequest.getParameter(
539                            "emailAddressesIndexes");
540    
541                    if (emailAddressesIndexesString == null) {
542                            return defaultEmailAddresses;
543                    }
544    
545                    List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
546    
547                    int[] emailAddressesIndexes = StringUtil.split(
548                            emailAddressesIndexesString, 0);
549    
550                    int emailAddressPrimary = ParamUtil.getInteger(
551                            actionRequest, "emailAddressPrimary");
552    
553                    for (int emailAddressesIndex : emailAddressesIndexes) {
554                            long emailAddressId = ParamUtil.getLong(
555                                    actionRequest, "emailAddressId" + emailAddressesIndex);
556    
557                            String address = ParamUtil.getString(
558                                    actionRequest, "emailAddressAddress" + emailAddressesIndex);
559    
560                            if (Validator.isNull(address)) {
561                                    continue;
562                            }
563    
564                            int typeId = ParamUtil.getInteger(
565                                    actionRequest, "emailAddressTypeId" + emailAddressesIndex);
566    
567                            boolean primary = false;
568    
569                            if (emailAddressesIndex == emailAddressPrimary) {
570                                    primary = true;
571                            }
572    
573                            EmailAddress emailAddress =
574                                    EmailAddressLocalServiceUtil.createEmailAddress(emailAddressId);
575    
576                            emailAddress.setAddress(address);
577                            emailAddress.setTypeId(typeId);
578                            emailAddress.setPrimary(primary);
579    
580                            emailAddresses.add(emailAddress);
581                    }
582    
583                    return emailAddresses;
584            }
585    
586            public OrderByComparator getGroupOrderByComparator(
587                    String orderByCol, String orderByType) {
588    
589                    boolean orderByAsc = false;
590    
591                    if (orderByType.equals("asc")) {
592                            orderByAsc = true;
593                    }
594    
595                    OrderByComparator orderByComparator = null;
596    
597                    if (orderByCol.equals("name")) {
598                            orderByComparator = new GroupNameComparator(orderByAsc);
599                    }
600                    else if (orderByCol.equals("type")) {
601                            orderByComparator = new GroupTypeComparator(orderByAsc);
602                    }
603                    else {
604                            orderByComparator = new GroupNameComparator(orderByAsc);
605                    }
606    
607                    return orderByComparator;
608            }
609    
610            public Long[] getOrganizationIds(List<Organization> organizations) {
611                    if ((organizations == null) || organizations.isEmpty()) {
612                            return new Long[0];
613                    }
614    
615                    Long[] organizationIds = new Long[organizations.size()];
616    
617                    for (int i = 0; i < organizations.size(); i++) {
618                            Organization organization = organizations.get(i);
619    
620                            organizationIds[i] = new Long(organization.getOrganizationId());
621                    }
622    
623                    return organizationIds;
624            }
625    
626            public OrderByComparator getOrganizationOrderByComparator(
627                    String orderByCol, String orderByType) {
628    
629                    boolean orderByAsc = false;
630    
631                    if (orderByType.equals("asc")) {
632                            orderByAsc = true;
633                    }
634    
635                    OrderByComparator orderByComparator = null;
636    
637                    if (orderByCol.equals("name")) {
638                            orderByComparator = new OrganizationNameComparator(orderByAsc);
639                    }
640                    else if (orderByCol.equals("type")) {
641                            orderByComparator = new OrganizationTypeComparator(orderByAsc);
642                    }
643                    else {
644                            orderByComparator = new OrganizationNameComparator(orderByAsc);
645                    }
646    
647                    return orderByComparator;
648            }
649    
650            public Tuple getOrganizations(Hits hits)
651                    throws PortalException, SystemException {
652    
653                    List<Organization> organizations = new ArrayList<Organization>();
654                    boolean corruptIndex = false;
655    
656                    List<Document> documents = hits.toList();
657    
658                    for (Document document : documents) {
659                            long organizationId = GetterUtil.getLong(
660                                    document.get(Field.ORGANIZATION_ID));
661    
662                            try {
663                                    Organization organization =
664                                            OrganizationLocalServiceUtil.getOrganization(
665                                                    organizationId);
666    
667                                    organizations.add(organization);
668                            }
669                            catch (NoSuchOrganizationException nsoe) {
670                                    corruptIndex = true;
671    
672                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
673                                            Organization.class);
674    
675                                    long companyId = GetterUtil.getLong(
676                                            document.get(Field.COMPANY_ID));
677    
678                                    indexer.delete(companyId, document.getUID());
679                            }
680                    }
681    
682                    return new Tuple(organizations, corruptIndex);
683            }
684    
685            public List<OrgLabor> getOrgLabors(ActionRequest actionRequest) {
686                    List<OrgLabor> orgLabors = new ArrayList<OrgLabor>();
687    
688                    int[] orgLaborsIndexes = StringUtil.split(
689                            ParamUtil.getString(actionRequest, "orgLaborsIndexes"), 0);
690    
691                    for (int orgLaborsIndex : orgLaborsIndexes) {
692                            long orgLaborId = ParamUtil.getLong(
693                                    actionRequest, "orgLaborId" + orgLaborsIndex);
694    
695                            int typeId = ParamUtil.getInteger(
696                                    actionRequest, "orgLaborTypeId" + orgLaborsIndex, -1);
697    
698                            if (typeId == -1) {
699                                    continue;
700                            }
701    
702                            int sunOpen = ParamUtil.getInteger(
703                                    actionRequest, "sunOpen" + orgLaborsIndex, -1);
704                            int sunClose = ParamUtil.getInteger(
705                                    actionRequest, "sunClose" + orgLaborsIndex, -1);
706                            int monOpen = ParamUtil.getInteger(
707                                    actionRequest, "monOpen" + orgLaborsIndex, -1);
708                            int monClose = ParamUtil.getInteger(
709                                    actionRequest, "monClose" + orgLaborsIndex, -1);
710                            int tueOpen = ParamUtil.getInteger(
711                                    actionRequest, "tueOpen" + orgLaborsIndex, -1);
712                            int tueClose = ParamUtil.getInteger(
713                                    actionRequest, "tueClose" + orgLaborsIndex, -1);
714                            int wedOpen = ParamUtil.getInteger(
715                                    actionRequest, "wedOpen" + orgLaborsIndex, -1);
716                            int wedClose = ParamUtil.getInteger(
717                                    actionRequest, "wedClose" + orgLaborsIndex, -1);
718                            int thuOpen = ParamUtil.getInteger(
719                                    actionRequest, "thuOpen" + orgLaborsIndex, -1);
720                            int thuClose = ParamUtil.getInteger(
721                                    actionRequest, "thuClose" + orgLaborsIndex, -1);
722                            int friOpen = ParamUtil.getInteger(
723                                    actionRequest, "friOpen" + orgLaborsIndex, -1);
724                            int friClose = ParamUtil.getInteger(
725                                    actionRequest, "friClose" + orgLaborsIndex, -1);
726                            int satOpen = ParamUtil.getInteger(
727                                    actionRequest, "satOpen" + orgLaborsIndex, -1);
728                            int satClose = ParamUtil.getInteger(
729                                    actionRequest, "satClose" + orgLaborsIndex, -1);
730    
731                            OrgLabor orgLabor = OrgLaborLocalServiceUtil.createOrgLabor(
732                                    orgLaborId);
733    
734                            orgLabor.setTypeId(typeId);
735                            orgLabor.setSunOpen(sunOpen);
736                            orgLabor.setSunClose(sunClose);
737                            orgLabor.setMonOpen(monOpen);
738                            orgLabor.setMonClose(monClose);
739                            orgLabor.setTueOpen(tueOpen);
740                            orgLabor.setTueClose(tueClose);
741                            orgLabor.setWedOpen(wedOpen);
742                            orgLabor.setWedClose(wedClose);
743                            orgLabor.setThuOpen(thuOpen);
744                            orgLabor.setThuClose(thuClose);
745                            orgLabor.setFriOpen(friOpen);
746                            orgLabor.setFriClose(friClose);
747                            orgLabor.setSatOpen(satOpen);
748                            orgLabor.setSatClose(satClose);
749    
750                            orgLabors.add(orgLabor);
751                    }
752    
753                    return orgLabors;
754            }
755    
756            public List<Phone> getPhones(ActionRequest actionRequest) {
757                    return getPhones(actionRequest, Collections.<Phone>emptyList());
758            }
759    
760            public List<Phone> getPhones(
761                    ActionRequest actionRequest, List<Phone> defaultPhones) {
762    
763                    String phonesIndexesString = actionRequest.getParameter(
764                            "phonesIndexes");
765    
766                    if (phonesIndexesString == null) {
767                            return defaultPhones;
768                    }
769    
770                    List<Phone> phones = new ArrayList<Phone>();
771    
772                    int[] phonesIndexes = StringUtil.split(phonesIndexesString, 0);
773    
774                    int phonePrimary = ParamUtil.getInteger(actionRequest, "phonePrimary");
775    
776                    for (int phonesIndex : phonesIndexes) {
777                            long phoneId = ParamUtil.getLong(
778                                    actionRequest, "phoneId" + phonesIndex);
779    
780                            String number = ParamUtil.getString(
781                                    actionRequest, "phoneNumber" + phonesIndex);
782                            String extension = ParamUtil.getString(
783                                    actionRequest, "phoneExtension" + phonesIndex);
784    
785                            if (Validator.isNull(number) && Validator.isNull(extension)) {
786                                    continue;
787                            }
788    
789                            int typeId = ParamUtil.getInteger(
790                                    actionRequest, "phoneTypeId" + phonesIndex);
791    
792                            boolean primary = false;
793    
794                            if (phonesIndex == phonePrimary) {
795                                    primary = true;
796                            }
797    
798                            Phone phone = PhoneLocalServiceUtil.createPhone(phoneId);
799    
800                            phone.setNumber(number);
801                            phone.setExtension(extension);
802                            phone.setTypeId(typeId);
803                            phone.setPrimary(primary);
804    
805                            phones.add(phone);
806                    }
807    
808                    return phones;
809            }
810    
811            public OrderByComparator getRoleOrderByComparator(
812                    String orderByCol, String orderByType) {
813    
814                    boolean orderByAsc = false;
815    
816                    if (orderByType.equals("asc")) {
817                            orderByAsc = true;
818                    }
819    
820                    OrderByComparator orderByComparator = null;
821    
822                    if (orderByCol.equals("name")) {
823                            orderByComparator = new RoleNameComparator(orderByAsc);
824                    }
825                    else if (orderByCol.equals("description")) {
826                            orderByComparator = new RoleDescriptionComparator(orderByAsc);
827                    }
828                    else if (orderByCol.equals("type")) {
829                            orderByComparator = new RoleTypeComparator(orderByAsc);
830                    }
831                    else {
832                            orderByComparator = new RoleNameComparator(orderByAsc);
833                    }
834    
835                    return orderByComparator;
836            }
837    
838            public OrderByComparator getUserGroupOrderByComparator(
839                    String orderByCol, String orderByType) {
840    
841                    boolean orderByAsc = false;
842    
843                    if (orderByType.equals("asc")) {
844                            orderByAsc = true;
845                    }
846    
847                    OrderByComparator orderByComparator = null;
848    
849                    if (orderByCol.equals("name")) {
850                            orderByComparator = new UserGroupNameComparator(orderByAsc);
851                    }
852                    else if (orderByCol.equals("description")) {
853                            orderByComparator = new UserGroupDescriptionComparator(orderByAsc);
854                    }
855                    else {
856                            orderByComparator = new UserGroupNameComparator(orderByAsc);
857                    }
858    
859                    return orderByComparator;
860            }
861    
862            public List<UserGroupRole> getUserGroupRoles(PortletRequest portletRequest)
863                    throws PortalException, SystemException {
864    
865                    List<UserGroupRole> userGroupRoles = new UniqueList<UserGroupRole>();
866    
867                    long[] groupRolesRoleIds= StringUtil.split(
868                            ParamUtil.getString(portletRequest, "groupRolesRoleIds"), 0L);
869                    long[] groupRolesGroupIds= StringUtil.split(
870                            ParamUtil.getString(portletRequest, "groupRolesGroupIds"), 0L);
871    
872                    if (groupRolesGroupIds.length != groupRolesRoleIds.length) {
873                            return userGroupRoles;
874                    }
875    
876                    User user = PortalUtil.getSelectedUser(portletRequest);
877    
878                    long userId = 0;
879    
880                    if (user != null) {
881                            userId = user.getUserId();
882                    }
883    
884                    for (int i = 0; i < groupRolesGroupIds.length; i++) {
885                            if ((groupRolesGroupIds[i] == 0) || (groupRolesRoleIds[i] == 0)) {
886                                    continue;
887                            }
888    
889                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
890                                    userId, groupRolesGroupIds[i], groupRolesRoleIds[i]);
891    
892                            UserGroupRole userGroupRole =
893                                    UserGroupRoleLocalServiceUtil.createUserGroupRole(
894                                            userGroupRolePK);
895    
896                            userGroupRoles.add(userGroupRole);
897                    }
898    
899                    return userGroupRoles;
900            }
901    
902            public Tuple getUserGroups(Hits hits)
903                    throws PortalException, SystemException {
904    
905                    List<UserGroup> userGroups = new ArrayList<UserGroup>();
906                    boolean corruptIndex = false;
907    
908                    List<Document> documents = hits.toList();
909    
910                    for (Document document : documents) {
911                            long userGroupId = GetterUtil.getLong(
912                                    document.get(Field.USER_GROUP_ID));
913    
914                            try {
915                                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
916                                            userGroupId);
917    
918                                    userGroups.add(userGroup);
919                            }
920                            catch (NoSuchUserGroupException nsuge) {
921                                    corruptIndex = true;
922    
923                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
924                                            UserGroup.class);
925    
926                                    long companyId = GetterUtil.getLong(
927                                            document.get(Field.COMPANY_ID));
928    
929                                    indexer.delete(companyId, document.getUID());
930                            }
931                    }
932    
933                    return new Tuple(userGroups, corruptIndex);
934            }
935    
936            public OrderByComparator getUserOrderByComparator(
937                    String orderByCol, String orderByType) {
938    
939                    boolean orderByAsc = false;
940    
941                    if (orderByType.equals("asc")) {
942                            orderByAsc = true;
943                    }
944    
945                    OrderByComparator orderByComparator = null;
946    
947                    if (orderByCol.equals("email-address")) {
948                            orderByComparator = new UserEmailAddressComparator(orderByAsc);
949                    }
950                    else if (orderByCol.equals("first-name")) {
951                            orderByComparator = new UserFirstNameComparator(orderByAsc);
952                    }
953                    else if (orderByCol.equals("job-title")) {
954                            orderByComparator = new UserJobTitleComparator(orderByAsc);
955                    }
956                    else if (orderByCol.equals("last-name")) {
957                            orderByComparator = new UserLastNameComparator(orderByAsc);
958                    }
959                    else if (orderByCol.equals("screen-name")) {
960                            orderByComparator = new UserScreenNameComparator(orderByAsc);
961                    }
962                    else {
963                            orderByComparator = new UserLastNameComparator(orderByAsc);
964                    }
965    
966                    return orderByComparator;
967            }
968    
969            public Tuple getUsers(Hits hits) throws PortalException, SystemException {
970                    List<User> users = new ArrayList<User>();
971                    boolean corruptIndex = false;
972    
973                    List<Document> documents = hits.toList();
974    
975                    for (Document document : documents) {
976                            long userId = GetterUtil.getLong(document.get(Field.USER_ID));
977    
978                            try {
979                                    User user = UserLocalServiceUtil.getUser(userId);
980    
981                                    users.add(user);
982                            }
983                            catch (NoSuchUserException nsue) {
984                                    corruptIndex = true;
985    
986                                    Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
987    
988                                    long companyId = GetterUtil.getLong(
989                                            document.get(Field.COMPANY_ID));
990    
991                                    indexer.delete(companyId, document.getUID());
992                            }
993                    }
994    
995                    return new Tuple(users, corruptIndex);
996            }
997    
998            public List<Website> getWebsites(ActionRequest actionRequest) {
999                    return getWebsites(actionRequest, Collections.<Website>emptyList());
1000            }
1001    
1002            public List<Website> getWebsites(
1003                    ActionRequest actionRequest, List<Website> defaultWebsites) {
1004    
1005                    String websitesIndexesString = actionRequest.getParameter(
1006                            "websitesIndexes");
1007    
1008                    if (websitesIndexesString == null) {
1009                            return defaultWebsites;
1010                    }
1011    
1012                    List<Website> websites = new ArrayList<Website>();
1013    
1014                    int[] websitesIndexes = StringUtil.split(websitesIndexesString, 0);
1015    
1016                    int websitePrimary = ParamUtil.getInteger(
1017                            actionRequest, "websitePrimary");
1018    
1019                    for (int websitesIndex : websitesIndexes) {
1020                            long websiteId = ParamUtil.getLong(
1021                                    actionRequest, "websiteId" + websitesIndex);
1022    
1023                            String url = ParamUtil.getString(
1024                                    actionRequest, "websiteUrl" + websitesIndex);
1025    
1026                            if (Validator.isNull(url)) {
1027                                    continue;
1028                            }
1029    
1030                            int typeId = ParamUtil.getInteger(
1031                                    actionRequest, "websiteTypeId" + websitesIndex);
1032    
1033                            boolean primary = false;
1034    
1035                            if (websitesIndex == websitePrimary) {
1036                                    primary = true;
1037                            }
1038    
1039                            Website website = WebsiteLocalServiceUtil.createWebsite(websiteId);
1040    
1041                            website.setUrl(url);
1042                            website.setTypeId(typeId);
1043                            website.setPrimary(primary);
1044    
1045                            websites.add(website);
1046                    }
1047    
1048                    return websites;
1049            }
1050    
1051            public boolean hasUpdateEmailAddress(
1052                            PermissionChecker permissionChecker, User user)
1053                    throws PortalException, SystemException {
1054    
1055                    String[] fieldEditiableUserEmailAddress =
1056                            PropsValues.
1057                                    FIELD_EDITABLE_COM_LIFERAY_PORTAL_MODEL_USER_EMAILADDRESS;
1058    
1059                    if (ArrayUtil.contains(
1060                                    fieldEditiableUserEmailAddress, "administrator") &&
1061                            permissionChecker.isCompanyAdmin()) {
1062    
1063                            return true;
1064                    }
1065    
1066                    if (ArrayUtil.contains(
1067                                    fieldEditiableUserEmailAddress, "user-with-mx") &&
1068                            user.hasCompanyMx()) {
1069    
1070                            return true;
1071                    }
1072    
1073                    if (ArrayUtil.contains(
1074                                    fieldEditiableUserEmailAddress, "user-without-mx") &&
1075                            !user.hasCompanyMx()) {
1076    
1077                            return true;
1078                    }
1079    
1080                    return false;
1081            }
1082    
1083            public boolean hasUpdateScreenName(
1084                            PermissionChecker permissionChecker, User user)
1085                    throws PortalException, SystemException {
1086    
1087                    String[] fieldEditiableUserScreenName =
1088                            PropsValues.
1089                                    FIELD_EDITABLE_COM_LIFERAY_PORTAL_MODEL_USER_SCREENNAME;
1090    
1091                    if (ArrayUtil.contains(
1092                                    fieldEditiableUserScreenName, "administrator") &&
1093                            permissionChecker.isCompanyAdmin()) {
1094    
1095                            return true;
1096                    }
1097    
1098                    if (ArrayUtil.contains(
1099                                    fieldEditiableUserScreenName, "user-with-mx") &&
1100                            user.hasCompanyMx()) {
1101    
1102                            return true;
1103                    }
1104    
1105                    if (ArrayUtil.contains(
1106                                    fieldEditiableUserScreenName, "user-without-mx") &&
1107                            !user.hasCompanyMx()) {
1108    
1109                            return true;
1110                    }
1111    
1112                    return false;
1113            }
1114    
1115            public long[] removeRequiredRoles(long userId, long[] roleIds)
1116                    throws PortalException, SystemException {
1117    
1118                    User user = UserLocalServiceUtil.getUser(userId);
1119    
1120                    return removeRequiredRoles(user, roleIds);
1121            }
1122    
1123            public long[] removeRequiredRoles(User user, long[] roleIds)
1124                    throws PortalException, SystemException {
1125    
1126                    Role role = RoleLocalServiceUtil.getRole(
1127                            user.getCompanyId(), RoleConstants.USER);
1128    
1129                    roleIds = ArrayUtil.remove(roleIds, role.getRoleId());
1130    
1131                    return roleIds;
1132            }
1133    
1134            public void updateAddresses(
1135                            String className, long classPK, List<Address> addresses)
1136                    throws PortalException, SystemException {
1137    
1138                    Set<Long> addressIds = new HashSet<Long>();
1139    
1140                    for (Address address : addresses) {
1141                            long addressId = address.getAddressId();
1142    
1143                            String street1 = address.getStreet1();
1144                            String street2 = address.getStreet2();
1145                            String street3 = address.getStreet3();
1146                            String city = address.getCity();
1147                            String zip = address.getZip();
1148                            long regionId = address.getRegionId();
1149                            long countryId = address.getCountryId();
1150                            int typeId = address.getTypeId();
1151                            boolean mailing = address.isMailing();
1152                            boolean primary = address.isPrimary();
1153    
1154                            if (addressId <= 0) {
1155                                    address = AddressServiceUtil.addAddress(
1156                                            className, classPK, street1, street2, street3, city, zip,
1157                                            regionId, countryId, typeId, mailing, primary);
1158    
1159                                    addressId = address.getAddressId();
1160                            }
1161                            else {
1162                                    AddressServiceUtil.updateAddress(
1163                                            addressId, street1, street2, street3, city, zip, regionId,
1164                                            countryId, typeId, mailing, primary);
1165                            }
1166    
1167                            addressIds.add(addressId);
1168                    }
1169    
1170                    addresses = AddressServiceUtil.getAddresses(className, classPK);
1171    
1172                    for (Address address : addresses) {
1173                            if (!addressIds.contains(address.getAddressId())) {
1174                                    AddressServiceUtil.deleteAddress(address.getAddressId());
1175                            }
1176                    }
1177            }
1178    
1179            public void updateEmailAddresses(
1180                            String className, long classPK, List<EmailAddress> emailAddresses)
1181                    throws PortalException, SystemException {
1182    
1183                    Set<Long> emailAddressIds = new HashSet<Long>();
1184    
1185                    for (EmailAddress emailAddress : emailAddresses) {
1186                            long emailAddressId = emailAddress.getEmailAddressId();
1187    
1188                            String address = emailAddress.getAddress();
1189                            int typeId = emailAddress.getTypeId();
1190                            boolean primary = emailAddress.isPrimary();
1191    
1192                            if (emailAddressId <= 0) {
1193                                    emailAddress = EmailAddressServiceUtil.addEmailAddress(
1194                                            className, classPK, address, typeId, primary);
1195    
1196                                    emailAddressId = emailAddress.getEmailAddressId();
1197                            }
1198                            else {
1199                                    EmailAddressServiceUtil.updateEmailAddress(
1200                                            emailAddressId, address, typeId, primary);
1201                            }
1202    
1203                            emailAddressIds.add(emailAddressId);
1204                    }
1205    
1206                    emailAddresses = EmailAddressServiceUtil.getEmailAddresses(
1207                            className, classPK);
1208    
1209                    for (EmailAddress emailAddress : emailAddresses) {
1210                            if (!emailAddressIds.contains(emailAddress.getEmailAddressId())) {
1211                                    EmailAddressServiceUtil.deleteEmailAddress(
1212                                            emailAddress.getEmailAddressId());
1213                            }
1214                    }
1215            }
1216    
1217            public void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
1218                    throws PortalException, SystemException {
1219    
1220                    Set<Long> orgLaborsIds = new HashSet<Long>();
1221    
1222                    for (OrgLabor orgLabor : orgLabors) {
1223                            long orgLaborId = orgLabor.getOrgLaborId();
1224    
1225                            int typeId = orgLabor.getTypeId();
1226                            int sunOpen = orgLabor.getSunOpen();
1227                            int sunClose = orgLabor.getSunClose();
1228                            int monOpen = orgLabor.getMonOpen();
1229                            int monClose = orgLabor.getMonClose();
1230                            int tueOpen = orgLabor.getTueOpen();
1231                            int tueClose = orgLabor.getTueClose();
1232                            int wedOpen = orgLabor.getWedOpen();
1233                            int wedClose = orgLabor.getWedClose();
1234                            int thuOpen = orgLabor.getThuOpen();
1235                            int thuClose = orgLabor.getThuClose();
1236                            int friOpen = orgLabor.getFriOpen();
1237                            int friClose = orgLabor.getFriClose();
1238                            int satOpen = orgLabor.getSatOpen();
1239                            int satClose = orgLabor.getSatClose();
1240    
1241                            if (orgLaborId <= 0) {
1242                                    orgLabor = OrgLaborServiceUtil.addOrgLabor(
1243                                            classPK, typeId, sunOpen, sunClose, monOpen, monClose,
1244                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1245                                            friOpen, friClose, satOpen, satClose);
1246    
1247                                    orgLaborId = orgLabor.getOrgLaborId();
1248                            }
1249                            else {
1250                                    OrgLaborServiceUtil.updateOrgLabor(
1251                                            orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
1252                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1253                                            friOpen, friClose, satOpen, satClose);
1254                            }
1255    
1256                            orgLaborsIds.add(orgLaborId);
1257                    }
1258    
1259                    orgLabors = OrgLaborServiceUtil.getOrgLabors(classPK);
1260    
1261                    for (OrgLabor orgLabor : orgLabors) {
1262                            if (!orgLaborsIds.contains(orgLabor.getOrgLaborId())) {
1263                                    OrgLaborServiceUtil.deleteOrgLabor(orgLabor.getOrgLaborId());
1264                            }
1265                    }
1266            }
1267    
1268            public void updatePhones(String className, long classPK, List<Phone> phones)
1269                    throws PortalException, SystemException {
1270    
1271                    Set<Long> phoneIds = new HashSet<Long>();
1272    
1273                    for (Phone phone : phones) {
1274                            long phoneId = phone.getPhoneId();
1275    
1276                            String number = phone.getNumber();
1277                            String extension = phone.getExtension();
1278                            int typeId = phone.getTypeId();
1279                            boolean primary = phone.isPrimary();
1280    
1281                            if (phoneId <= 0) {
1282                                    phone = PhoneServiceUtil.addPhone(
1283                                            className, classPK, number, extension, typeId, primary);
1284    
1285                                    phoneId = phone.getPhoneId();
1286                            }
1287                            else {
1288                                    PhoneServiceUtil.updatePhone(
1289                                            phoneId, number, extension, typeId, primary);
1290                            }
1291    
1292                            phoneIds.add(phoneId);
1293                    }
1294    
1295                    phones = PhoneServiceUtil.getPhones(className, classPK);
1296    
1297                    for (Phone phone : phones) {
1298                            if (!phoneIds.contains(phone.getPhoneId())) {
1299                                    PhoneServiceUtil.deletePhone(phone.getPhoneId());
1300                            }
1301                    }
1302            }
1303    
1304            public void updateWebsites(
1305                            String className, long classPK, List<Website> websites)
1306                    throws PortalException, SystemException {
1307    
1308                    Set<Long> websiteIds = new HashSet<Long>();
1309    
1310                    for (Website website : websites) {
1311                            long websiteId = website.getWebsiteId();
1312    
1313                            String url = website.getUrl();
1314                            int typeId = website.getTypeId();
1315                            boolean primary = website.isPrimary();
1316    
1317                            if (websiteId <= 0) {
1318                                    website = WebsiteServiceUtil.addWebsite(
1319                                            className, classPK, url, typeId, primary);
1320    
1321                                    websiteId = website.getWebsiteId();
1322                            }
1323                            else {
1324                                    WebsiteServiceUtil.updateWebsite(
1325                                            websiteId, url, typeId, primary);
1326                            }
1327    
1328                            websiteIds.add(websiteId);
1329                    }
1330    
1331                    websites = WebsiteServiceUtil.getWebsites(className, classPK);
1332    
1333                    for (Website website : websites) {
1334                            if (!websiteIds.contains(website.getWebsiteId())) {
1335                                    WebsiteServiceUtil.deleteWebsite(website.getWebsiteId());
1336                            }
1337                    }
1338            }
1339    
1340    }