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