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.pacl.DoPrivileged;
026    import com.liferay.portal.kernel.util.Accessor;
027    import com.liferay.portal.kernel.util.ArrayUtil;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.ListUtil;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.ParamUtil;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.SetUtil;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
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.auth.PrincipalThreadLocal;
050    import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
051    import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
052    import com.liferay.portal.security.permission.ActionKeys;
053    import com.liferay.portal.security.permission.PermissionChecker;
054    import com.liferay.portal.security.permission.PermissionThreadLocal;
055    import com.liferay.portal.service.AddressLocalServiceUtil;
056    import com.liferay.portal.service.AddressServiceUtil;
057    import com.liferay.portal.service.EmailAddressLocalServiceUtil;
058    import com.liferay.portal.service.EmailAddressServiceUtil;
059    import com.liferay.portal.service.GroupLocalServiceUtil;
060    import com.liferay.portal.service.OrgLaborLocalServiceUtil;
061    import com.liferay.portal.service.OrgLaborServiceUtil;
062    import com.liferay.portal.service.OrganizationLocalServiceUtil;
063    import com.liferay.portal.service.PhoneLocalServiceUtil;
064    import com.liferay.portal.service.PhoneServiceUtil;
065    import com.liferay.portal.service.RoleLocalServiceUtil;
066    import com.liferay.portal.service.ServiceContext;
067    import com.liferay.portal.service.UserGroupLocalServiceUtil;
068    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
069    import com.liferay.portal.service.UserLocalServiceUtil;
070    import com.liferay.portal.service.WebsiteLocalServiceUtil;
071    import com.liferay.portal.service.WebsiteServiceUtil;
072    import com.liferay.portal.service.permission.GroupPermissionUtil;
073    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
074    import com.liferay.portal.service.permission.RolePermissionUtil;
075    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
076    import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
077    import com.liferay.portal.service.permission.UserPermissionUtil;
078    import com.liferay.portal.service.persistence.UserGroupRolePK;
079    import com.liferay.portal.util.PortalUtil;
080    import com.liferay.portal.util.PropsUtil;
081    import com.liferay.portal.util.PropsValues;
082    import com.liferay.portal.util.comparator.GroupNameComparator;
083    import com.liferay.portal.util.comparator.GroupTypeComparator;
084    import com.liferay.portal.util.comparator.OrganizationNameComparator;
085    import com.liferay.portal.util.comparator.OrganizationTypeComparator;
086    import com.liferay.portal.util.comparator.RoleDescriptionComparator;
087    import com.liferay.portal.util.comparator.RoleNameComparator;
088    import com.liferay.portal.util.comparator.RoleTypeComparator;
089    import com.liferay.portal.util.comparator.UserEmailAddressComparator;
090    import com.liferay.portal.util.comparator.UserFirstNameComparator;
091    import com.liferay.portal.util.comparator.UserGroupDescriptionComparator;
092    import com.liferay.portal.util.comparator.UserGroupNameComparator;
093    import com.liferay.portal.util.comparator.UserJobTitleComparator;
094    import com.liferay.portal.util.comparator.UserLastNameComparator;
095    import com.liferay.portal.util.comparator.UserScreenNameComparator;
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            /**
1141             * @deprecated As of 6.2.0, replaced by {@link
1142             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1143             *             String)}
1144             */
1145            @Deprecated
1146            @Override
1147            public boolean hasUpdateEmailAddress(
1148                            PermissionChecker permissionChecker, User user)
1149                    throws PortalException {
1150    
1151                    return hasUpdateFieldPermission(
1152                            permissionChecker, null, user, "emailAddress");
1153            }
1154    
1155            @Override
1156            public boolean hasUpdateFieldPermission(
1157                            PermissionChecker permissionChecker, User updatingUser,
1158                            User updatedUser, String field)
1159                    throws PortalException {
1160    
1161                    if (updatedUser == null) {
1162                            return true;
1163                    }
1164    
1165                    if (updatingUser == null) {
1166                            long updatingUserId = PrincipalThreadLocal.getUserId();
1167    
1168                            if (updatingUserId > 0) {
1169                                    updatingUser = UserLocalServiceUtil.fetchUserById(
1170                                            updatingUserId);
1171                            }
1172                    }
1173    
1174                    if ((updatingUser != null) && !updatingUser.equals(updatedUser) &&
1175                            UserPermissionUtil.contains(
1176                                    permissionChecker, updatingUser.getUserId(),
1177                                    ActionKeys.UPDATE_USER)) {
1178    
1179                            return true;
1180                    }
1181    
1182                    for (String userType : PropsValues.FIELD_EDITABLE_USER_TYPES) {
1183                            if (userType.equals("user-with-mx") && updatedUser.hasCompanyMx()) {
1184                                    return true;
1185                            }
1186    
1187                            if (userType.equals("user-without-mx") &&
1188                                    !updatedUser.hasCompanyMx()) {
1189    
1190                                    return true;
1191                            }
1192                    }
1193    
1194                    for (String roleName : PropsValues.FIELD_EDITABLE_ROLES) {
1195                            Role role = RoleLocalServiceUtil.fetchRole(
1196                                    updatedUser.getCompanyId(), roleName);
1197    
1198                            if ((role != null) &&
1199                                    RoleLocalServiceUtil.hasUserRole(
1200                                            updatedUser.getUserId(), role.getRoleId())) {
1201    
1202                                    return true;
1203                            }
1204                    }
1205    
1206                    String emailAddress = updatedUser.getEmailAddress();
1207    
1208                    for (String domainName : PropsValues.FIELD_EDITABLE_DOMAINS) {
1209                            if (emailAddress.endsWith(domainName)) {
1210                                    return true;
1211                            }
1212                    }
1213    
1214                    String[] fieldEditableDomainNames = PropsUtil.getArray(
1215                            PropsKeys.FIELD_EDITABLE_DOMAINS, new Filter(field));
1216    
1217                    for (String domainName : fieldEditableDomainNames) {
1218                            if (domainName.equals(StringPool.STAR) ||
1219                                    emailAddress.endsWith(domainName)) {
1220    
1221                                    return true;
1222                            }
1223                    }
1224    
1225                    return false;
1226            }
1227    
1228            /**
1229             * @deprecated As of 6.2.0, replaced by {@link
1230             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1231             *             String)}
1232             */
1233            @Deprecated
1234            @Override
1235            public boolean hasUpdateFieldPermission(User user, String field)
1236                    throws PortalException {
1237    
1238                    PermissionChecker permissionChecker =
1239                            PermissionThreadLocal.getPermissionChecker();
1240    
1241                    return hasUpdateFieldPermission(permissionChecker, null, user, field);
1242            }
1243    
1244            /**
1245             * @deprecated As of 6.2.0, replaced by {@link
1246             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1247             *             String)}
1248             */
1249            @Deprecated
1250            @Override
1251            public boolean hasUpdateScreenName(
1252                            PermissionChecker permissionChecker, User user)
1253                    throws PortalException {
1254    
1255                    return hasUpdateFieldPermission(
1256                            permissionChecker, null, user, "screenName");
1257            }
1258    
1259            @Override
1260            public long[] removeRequiredRoles(long userId, long[] roleIds)
1261                    throws PortalException {
1262    
1263                    User user = UserLocalServiceUtil.getUser(userId);
1264    
1265                    return removeRequiredRoles(user, roleIds);
1266            }
1267    
1268            @Override
1269            public long[] removeRequiredRoles(User user, long[] roleIds)
1270                    throws PortalException {
1271    
1272                    Role role = RoleLocalServiceUtil.getRole(
1273                            user.getCompanyId(), RoleConstants.USER);
1274    
1275                    roleIds = ArrayUtil.remove(roleIds, role.getRoleId());
1276    
1277                    return roleIds;
1278            }
1279    
1280            @Override
1281            public void updateAddresses(
1282                            String className, long classPK, List<Address> addresses)
1283                    throws PortalException {
1284    
1285                    Set<Long> addressIds = new HashSet<>();
1286    
1287                    for (Address address : addresses) {
1288                            long addressId = address.getAddressId();
1289    
1290                            String street1 = address.getStreet1();
1291                            String street2 = address.getStreet2();
1292                            String street3 = address.getStreet3();
1293                            String city = address.getCity();
1294                            String zip = address.getZip();
1295                            long regionId = address.getRegionId();
1296                            long countryId = address.getCountryId();
1297                            long typeId = address.getTypeId();
1298                            boolean mailing = address.isMailing();
1299                            boolean primary = address.isPrimary();
1300    
1301                            if (addressId <= 0) {
1302                                    address = AddressServiceUtil.addAddress(
1303                                            className, classPK, street1, street2, street3, city, zip,
1304                                            regionId, countryId, typeId, mailing, primary,
1305                                            new ServiceContext());
1306    
1307                                    addressId = address.getAddressId();
1308                            }
1309                            else {
1310                                    AddressServiceUtil.updateAddress(
1311                                            addressId, street1, street2, street3, city, zip, regionId,
1312                                            countryId, typeId, mailing, primary);
1313                            }
1314    
1315                            addressIds.add(addressId);
1316                    }
1317    
1318                    addresses = AddressServiceUtil.getAddresses(className, classPK);
1319    
1320                    for (Address address : addresses) {
1321                            if (!addressIds.contains(address.getAddressId())) {
1322                                    AddressServiceUtil.deleteAddress(address.getAddressId());
1323                            }
1324                    }
1325            }
1326    
1327            @Override
1328            public void updateEmailAddresses(
1329                            String className, long classPK, List<EmailAddress> emailAddresses)
1330                    throws PortalException {
1331    
1332                    Set<Long> emailAddressIds = new HashSet<>();
1333    
1334                    for (EmailAddress emailAddress : emailAddresses) {
1335                            long emailAddressId = emailAddress.getEmailAddressId();
1336    
1337                            String address = emailAddress.getAddress();
1338                            long typeId = emailAddress.getTypeId();
1339                            boolean primary = emailAddress.isPrimary();
1340    
1341                            if (emailAddressId <= 0) {
1342                                    emailAddress = EmailAddressServiceUtil.addEmailAddress(
1343                                            className, classPK, address, typeId, primary,
1344                                            new ServiceContext());
1345    
1346                                    emailAddressId = emailAddress.getEmailAddressId();
1347                            }
1348                            else {
1349                                    EmailAddressServiceUtil.updateEmailAddress(
1350                                            emailAddressId, address, typeId, primary);
1351                            }
1352    
1353                            emailAddressIds.add(emailAddressId);
1354                    }
1355    
1356                    emailAddresses = EmailAddressServiceUtil.getEmailAddresses(
1357                            className, classPK);
1358    
1359                    for (EmailAddress emailAddress : emailAddresses) {
1360                            if (!emailAddressIds.contains(emailAddress.getEmailAddressId())) {
1361                                    EmailAddressServiceUtil.deleteEmailAddress(
1362                                            emailAddress.getEmailAddressId());
1363                            }
1364                    }
1365            }
1366    
1367            @Override
1368            public void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
1369                    throws PortalException {
1370    
1371                    Set<Long> orgLaborsIds = new HashSet<>();
1372    
1373                    for (OrgLabor orgLabor : orgLabors) {
1374                            long orgLaborId = orgLabor.getOrgLaborId();
1375    
1376                            long typeId = orgLabor.getTypeId();
1377                            int sunOpen = orgLabor.getSunOpen();
1378                            int sunClose = orgLabor.getSunClose();
1379                            int monOpen = orgLabor.getMonOpen();
1380                            int monClose = orgLabor.getMonClose();
1381                            int tueOpen = orgLabor.getTueOpen();
1382                            int tueClose = orgLabor.getTueClose();
1383                            int wedOpen = orgLabor.getWedOpen();
1384                            int wedClose = orgLabor.getWedClose();
1385                            int thuOpen = orgLabor.getThuOpen();
1386                            int thuClose = orgLabor.getThuClose();
1387                            int friOpen = orgLabor.getFriOpen();
1388                            int friClose = orgLabor.getFriClose();
1389                            int satOpen = orgLabor.getSatOpen();
1390                            int satClose = orgLabor.getSatClose();
1391    
1392                            if (orgLaborId <= 0) {
1393                                    orgLabor = OrgLaborServiceUtil.addOrgLabor(
1394                                            classPK, typeId, sunOpen, sunClose, monOpen, monClose,
1395                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1396                                            friOpen, friClose, satOpen, satClose);
1397    
1398                                    orgLaborId = orgLabor.getOrgLaborId();
1399                            }
1400                            else {
1401                                    OrgLaborServiceUtil.updateOrgLabor(
1402                                            orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
1403                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1404                                            friOpen, friClose, satOpen, satClose);
1405                            }
1406    
1407                            orgLaborsIds.add(orgLaborId);
1408                    }
1409    
1410                    orgLabors = OrgLaborServiceUtil.getOrgLabors(classPK);
1411    
1412                    for (OrgLabor orgLabor : orgLabors) {
1413                            if (!orgLaborsIds.contains(orgLabor.getOrgLaborId())) {
1414                                    OrgLaborServiceUtil.deleteOrgLabor(orgLabor.getOrgLaborId());
1415                            }
1416                    }
1417            }
1418    
1419            @Override
1420            public void updatePhones(String className, long classPK, List<Phone> phones)
1421                    throws PortalException {
1422    
1423                    Set<Long> phoneIds = new HashSet<>();
1424    
1425                    for (Phone phone : phones) {
1426                            long phoneId = phone.getPhoneId();
1427    
1428                            String number = phone.getNumber();
1429                            String extension = phone.getExtension();
1430                            long typeId = phone.getTypeId();
1431                            boolean primary = phone.isPrimary();
1432    
1433                            if (phoneId <= 0) {
1434                                    phone = PhoneServiceUtil.addPhone(
1435                                            className, classPK, number, extension, typeId, primary,
1436                                            new ServiceContext());
1437    
1438                                    phoneId = phone.getPhoneId();
1439                            }
1440                            else {
1441                                    PhoneServiceUtil.updatePhone(
1442                                            phoneId, number, extension, typeId, primary);
1443                            }
1444    
1445                            phoneIds.add(phoneId);
1446                    }
1447    
1448                    phones = PhoneServiceUtil.getPhones(className, classPK);
1449    
1450                    for (Phone phone : phones) {
1451                            if (!phoneIds.contains(phone.getPhoneId())) {
1452                                    PhoneServiceUtil.deletePhone(phone.getPhoneId());
1453                            }
1454                    }
1455            }
1456    
1457            @Override
1458            public void updateWebsites(
1459                            String className, long classPK, List<Website> websites)
1460                    throws PortalException {
1461    
1462                    Set<Long> websiteIds = new HashSet<>();
1463    
1464                    for (Website website : websites) {
1465                            long websiteId = website.getWebsiteId();
1466    
1467                            String url = website.getUrl();
1468                            long typeId = website.getTypeId();
1469                            boolean primary = website.isPrimary();
1470    
1471                            if (websiteId <= 0) {
1472                                    website = WebsiteServiceUtil.addWebsite(
1473                                            className, classPK, url, typeId, primary,
1474                                            new ServiceContext());
1475    
1476                                    websiteId = website.getWebsiteId();
1477                            }
1478                            else {
1479                                    WebsiteServiceUtil.updateWebsite(
1480                                            websiteId, url, typeId, primary);
1481                            }
1482    
1483                            websiteIds.add(websiteId);
1484                    }
1485    
1486                    websites = WebsiteServiceUtil.getWebsites(className, classPK);
1487    
1488                    for (Website website : websites) {
1489                            if (!websiteIds.contains(website.getWebsiteId())) {
1490                                    WebsiteServiceUtil.deleteWebsite(website.getWebsiteId());
1491                            }
1492                    }
1493            }
1494    
1495            protected long[] getRequestPrimaryKeys(
1496                    PortletRequest portletRequest, long[] currentPKs, String addParam,
1497                    String deleteParam) {
1498    
1499                    Set<Long> primaryKeys = SetUtil.fromArray(currentPKs);
1500    
1501                    long[] addPrimaryKeys = StringUtil.split(
1502                            ParamUtil.getString(portletRequest, addParam), 0L);
1503                    long[] deletePrimaryKeys = StringUtil.split(
1504                            ParamUtil.getString(portletRequest, deleteParam), 0L);
1505    
1506                    for (long addPrimaryKey : addPrimaryKeys) {
1507                            primaryKeys.add(addPrimaryKey);
1508                    }
1509    
1510                    for (long deletePrimaryKey : deletePrimaryKeys) {
1511                            primaryKeys.remove(deletePrimaryKey);
1512                    }
1513    
1514                    return ArrayUtil.toLongArray(primaryKeys);
1515            }
1516    
1517            protected Set<UserGroupRole> getUserGroupRoles(
1518                    PortletRequest portletRequest, User user, String groupIdsParam,
1519                    String roleIdsParam) {
1520    
1521                    Set<UserGroupRole> userGroupRoles = new HashSet<>();
1522    
1523                    long[] groupRolesGroupIds = StringUtil.split(
1524                            ParamUtil.getString(portletRequest, groupIdsParam), 0L);
1525                    long[] groupRolesRoleIds = StringUtil.split(
1526                            ParamUtil.getString(portletRequest, roleIdsParam), 0L);
1527    
1528                    if (groupRolesGroupIds.length != groupRolesRoleIds.length) {
1529                            return userGroupRoles;
1530                    }
1531    
1532                    long userId = 0;
1533    
1534                    if (user != null) {
1535                            userId = user.getUserId();
1536                    }
1537    
1538                    for (int i = 0; i < groupRolesGroupIds.length; i++) {
1539                            if ((groupRolesGroupIds[i] == 0) || (groupRolesRoleIds[i] == 0)) {
1540                                    continue;
1541                            }
1542    
1543                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
1544                                    userId, groupRolesGroupIds[i], groupRolesRoleIds[i]);
1545    
1546                            UserGroupRole userGroupRole =
1547                                    UserGroupRoleLocalServiceUtil.createUserGroupRole(
1548                                            userGroupRolePK);
1549    
1550                            userGroupRoles.add(userGroupRole);
1551                    }
1552    
1553                    return userGroupRoles;
1554            }
1555    
1556    }