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