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