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