001    /**
002     * Copyright (c) 2000-2013 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.NoSuchOrganizationException;
018    import com.liferay.portal.NoSuchUserGroupException;
019    import com.liferay.portal.kernel.configuration.Filter;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.Hits;
025    import com.liferay.portal.kernel.search.Indexer;
026    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.ListUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.ParamUtil;
033    import com.liferay.portal.kernel.util.PropsKeys;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.Tuple;
036    import com.liferay.portal.kernel.util.UniqueList;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.Address;
039    import com.liferay.portal.model.EmailAddress;
040    import com.liferay.portal.model.Group;
041    import com.liferay.portal.model.OrgLabor;
042    import com.liferay.portal.model.Organization;
043    import com.liferay.portal.model.Phone;
044    import com.liferay.portal.model.Role;
045    import com.liferay.portal.model.RoleConstants;
046    import com.liferay.portal.model.User;
047    import com.liferay.portal.model.UserGroup;
048    import com.liferay.portal.model.UserGroupRole;
049    import com.liferay.portal.model.Website;
050    import com.liferay.portal.security.auth.PrincipalThreadLocal;
051    import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
052    import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
053    import com.liferay.portal.security.permission.ActionKeys;
054    import com.liferay.portal.security.permission.PermissionChecker;
055    import com.liferay.portal.security.permission.PermissionThreadLocal;
056    import com.liferay.portal.service.AddressLocalServiceUtil;
057    import com.liferay.portal.service.AddressServiceUtil;
058    import com.liferay.portal.service.EmailAddressLocalServiceUtil;
059    import com.liferay.portal.service.EmailAddressServiceUtil;
060    import com.liferay.portal.service.GroupLocalServiceUtil;
061    import com.liferay.portal.service.OrgLaborLocalServiceUtil;
062    import com.liferay.portal.service.OrgLaborServiceUtil;
063    import com.liferay.portal.service.OrganizationLocalServiceUtil;
064    import com.liferay.portal.service.PhoneLocalServiceUtil;
065    import com.liferay.portal.service.PhoneServiceUtil;
066    import com.liferay.portal.service.RoleLocalServiceUtil;
067    import com.liferay.portal.service.ServiceContext;
068    import com.liferay.portal.service.UserGroupLocalServiceUtil;
069    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
070    import com.liferay.portal.service.UserLocalServiceUtil;
071    import com.liferay.portal.service.WebsiteLocalServiceUtil;
072    import com.liferay.portal.service.WebsiteServiceUtil;
073    import com.liferay.portal.service.permission.GroupPermissionUtil;
074    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
075    import com.liferay.portal.service.permission.RolePermissionUtil;
076    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
077    import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
078    import com.liferay.portal.service.permission.UserPermissionUtil;
079    import com.liferay.portal.service.persistence.UserGroupRolePK;
080    import com.liferay.portal.util.PortalUtil;
081    import com.liferay.portal.util.PropsUtil;
082    import com.liferay.portal.util.PropsValues;
083    import com.liferay.portal.util.comparator.GroupNameComparator;
084    import com.liferay.portal.util.comparator.GroupTypeComparator;
085    import com.liferay.portal.util.comparator.OrganizationNameComparator;
086    import com.liferay.portal.util.comparator.OrganizationTypeComparator;
087    import com.liferay.portal.util.comparator.RoleDescriptionComparator;
088    import com.liferay.portal.util.comparator.RoleNameComparator;
089    import com.liferay.portal.util.comparator.RoleTypeComparator;
090    import com.liferay.portal.util.comparator.UserEmailAddressComparator;
091    import com.liferay.portal.util.comparator.UserFirstNameComparator;
092    import com.liferay.portal.util.comparator.UserGroupDescriptionComparator;
093    import com.liferay.portal.util.comparator.UserGroupNameComparator;
094    import com.liferay.portal.util.comparator.UserJobTitleComparator;
095    import com.liferay.portal.util.comparator.UserLastNameComparator;
096    import com.liferay.portal.util.comparator.UserScreenNameComparator;
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.Set;
104    
105    import javax.portlet.ActionRequest;
106    import javax.portlet.PortletRequest;
107    import javax.portlet.PortletURL;
108    import javax.portlet.RenderResponse;
109    
110    import javax.servlet.http.HttpServletRequest;
111    
112    /**
113     * @author Brian Wing Shun Chan
114     * @author Jorge Ferrer
115     * @author Julio Camarero
116     */
117    @DoPrivileged
118    public class UsersAdminImpl implements UsersAdmin {
119    
120            @Override
121            public void addPortletBreadcrumbEntries(
122                            Organization organization, HttpServletRequest request,
123                            RenderResponse renderResponse)
124                    throws Exception {
125    
126                    PortletURL portletURL = renderResponse.createRenderURL();
127    
128                    portletURL.setParameter("struts_action", "/users_admin/view");
129    
130                    List<Organization> ancestorOrganizations = organization.getAncestors();
131    
132                    Collections.reverse(ancestorOrganizations);
133    
134                    for (Organization ancestorOrganization : ancestorOrganizations) {
135                            portletURL.setParameter(
136                                    "organizationId",
137                                    String.valueOf(ancestorOrganization.getOrganizationId()));
138    
139                            PortalUtil.addPortletBreadcrumbEntry(
140                                    request, ancestorOrganization.getName(), portletURL.toString());
141                    }
142    
143                    Organization unescapedOrganization = organization.toUnescapedModel();
144    
145                    portletURL.setParameter(
146                            "organizationId",
147                            String.valueOf(unescapedOrganization.getOrganizationId()));
148    
149                    PortalUtil.addPortletBreadcrumbEntry(
150                            request, unescapedOrganization.getName(), portletURL.toString());
151            }
152    
153            @Override
154            public long[] addRequiredRoles(long userId, long[] roleIds)
155                    throws PortalException, SystemException {
156    
157                    User user = UserLocalServiceUtil.getUser(userId);
158    
159                    return addRequiredRoles(user, roleIds);
160            }
161    
162            @Override
163            public long[] addRequiredRoles(User user, long[] roleIds)
164                    throws PortalException, SystemException {
165    
166                    if (user.isDefaultUser()) {
167                            return removeRequiredRoles(user, roleIds);
168                    }
169    
170                    Role administratorRole = RoleLocalServiceUtil.getRole(
171                            user.getCompanyId(), RoleConstants.ADMINISTRATOR);
172    
173                    long[] administratorUserIds = UserLocalServiceUtil.getRoleUserIds(
174                            administratorRole.getRoleId());
175    
176                    if (ArrayUtil.contains(administratorUserIds, user.getUserId()) &&
177                            !ArrayUtil.contains(roleIds, administratorRole.getRoleId()) &&
178                            (administratorUserIds.length == 1)) {
179    
180                            roleIds = ArrayUtil.append(roleIds, administratorRole.getRoleId());
181                    }
182    
183                    Role userRole = RoleLocalServiceUtil.getRole(
184                            user.getCompanyId(), RoleConstants.USER);
185    
186                    if (!ArrayUtil.contains(roleIds, userRole.getRoleId())) {
187                            roleIds = ArrayUtil.append(roleIds, userRole.getRoleId());
188                    }
189    
190                    return roleIds;
191            }
192    
193            @Override
194            public List<Role> filterGroupRoles(
195                            PermissionChecker permissionChecker, long groupId, List<Role> roles)
196                    throws PortalException, SystemException {
197    
198                    List<Role> filteredGroupRoles = ListUtil.copy(roles);
199    
200                    Iterator<Role> itr = filteredGroupRoles.iterator();
201    
202                    while (itr.hasNext()) {
203                            Role groupRole = itr.next();
204    
205                            String roleName = groupRole.getName();
206    
207                            if (roleName.equals(RoleConstants.ORGANIZATION_USER) ||
208                                    roleName.equals(RoleConstants.SITE_MEMBER)) {
209    
210                                    itr.remove();
211                            }
212                    }
213    
214                    if (permissionChecker.isCompanyAdmin() ||
215                            permissionChecker.isGroupOwner(groupId)) {
216    
217                            return filteredGroupRoles;
218                    }
219    
220                    Group group = GroupLocalServiceUtil.getGroup(groupId);
221    
222                    if (!GroupPermissionUtil.contains(
223                                    permissionChecker, group, ActionKeys.ASSIGN_USER_ROLES) &&
224                            !OrganizationPermissionUtil.contains(
225                                    permissionChecker, group.getOrganizationId(),
226                                    ActionKeys.ASSIGN_USER_ROLES)) {
227    
228                            return Collections.emptyList();
229                    }
230    
231                    itr = filteredGroupRoles.iterator();
232    
233                    while (itr.hasNext()) {
234                            Role groupRole = itr.next();
235    
236                            String roleName = groupRole.getName();
237    
238                            if (roleName.equals(
239                                            RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
240                                    roleName.equals(RoleConstants.ORGANIZATION_OWNER) ||
241                                    roleName.equals(RoleConstants.SITE_ADMINISTRATOR) ||
242                                    roleName.equals(RoleConstants.SITE_OWNER) ||
243                                    !RolePermissionUtil.contains(
244                                            permissionChecker, groupId, groupRole.getRoleId(),
245                                            ActionKeys.ASSIGN_MEMBERS)) {
246    
247                                    itr.remove();
248                            }
249                    }
250    
251                    return filteredGroupRoles;
252            }
253    
254            @Override
255            public List<Group> filterGroups(
256                            PermissionChecker permissionChecker, List<Group> groups)
257                    throws PortalException, SystemException {
258    
259                    if (permissionChecker.isCompanyAdmin()) {
260                            return groups;
261                    }
262    
263                    List<Group> filteredGroups = ListUtil.copy(groups);
264    
265                    Iterator<Group> itr = filteredGroups.iterator();
266    
267                    while (itr.hasNext()) {
268                            Group group = itr.next();
269    
270                            if (!GroupPermissionUtil.contains(
271                                            permissionChecker, group.getGroupId(),
272                                            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, SystemException {
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.getOrganizationId(),
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, SystemException {
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, SystemException {
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, SystemException {
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<Address>();
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                            int typeId = ParamUtil.getInteger(
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<EmailAddress>();
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                            int typeId = ParamUtil.getInteger(
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 OrderByComparator getGroupOrderByComparator(
599                    String orderByCol, String orderByType) {
600    
601                    boolean orderByAsc = false;
602    
603                    if (orderByType.equals("asc")) {
604                            orderByAsc = true;
605                    }
606    
607                    OrderByComparator orderByComparator = null;
608    
609                    if (orderByCol.equals("name")) {
610                            orderByComparator = new GroupNameComparator(orderByAsc);
611                    }
612                    else if (orderByCol.equals("type")) {
613                            orderByComparator = new GroupTypeComparator(orderByAsc);
614                    }
615                    else {
616                            orderByComparator = new GroupNameComparator(orderByAsc);
617                    }
618    
619                    return orderByComparator;
620            }
621    
622            @Override
623            public Long[] getOrganizationIds(List<Organization> organizations) {
624                    if ((organizations == null) || organizations.isEmpty()) {
625                            return new Long[0];
626                    }
627    
628                    Long[] organizationIds = new Long[organizations.size()];
629    
630                    for (int i = 0; i < organizations.size(); i++) {
631                            Organization organization = organizations.get(i);
632    
633                            organizationIds[i] = new Long(organization.getOrganizationId());
634                    }
635    
636                    return organizationIds;
637            }
638    
639            @Override
640            public OrderByComparator getOrganizationOrderByComparator(
641                    String orderByCol, String orderByType) {
642    
643                    boolean orderByAsc = false;
644    
645                    if (orderByType.equals("asc")) {
646                            orderByAsc = true;
647                    }
648    
649                    OrderByComparator orderByComparator = null;
650    
651                    if (orderByCol.equals("name")) {
652                            orderByComparator = new OrganizationNameComparator(orderByAsc);
653                    }
654                    else if (orderByCol.equals("type")) {
655                            orderByComparator = new OrganizationTypeComparator(orderByAsc);
656                    }
657                    else {
658                            orderByComparator = new OrganizationNameComparator(orderByAsc);
659                    }
660    
661                    return orderByComparator;
662            }
663    
664            @Override
665            public Tuple getOrganizations(Hits hits)
666                    throws PortalException, SystemException {
667    
668                    List<Organization> organizations = new ArrayList<Organization>();
669                    boolean corruptIndex = false;
670    
671                    List<Document> documents = hits.toList();
672    
673                    for (Document document : documents) {
674                            long organizationId = GetterUtil.getLong(
675                                    document.get(Field.ORGANIZATION_ID));
676    
677                            try {
678                                    Organization organization =
679                                            OrganizationLocalServiceUtil.getOrganization(
680                                                    organizationId);
681    
682                                    organizations.add(organization);
683                            }
684                            catch (NoSuchOrganizationException nsoe) {
685                                    corruptIndex = true;
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                    }
696    
697                    return new Tuple(organizations, corruptIndex);
698            }
699    
700            @Override
701            public List<OrgLabor> getOrgLabors(ActionRequest actionRequest) {
702                    List<OrgLabor> orgLabors = new ArrayList<OrgLabor>();
703    
704                    int[] orgLaborsIndexes = StringUtil.split(
705                            ParamUtil.getString(actionRequest, "orgLaborsIndexes"), 0);
706    
707                    for (int orgLaborsIndex : orgLaborsIndexes) {
708                            long orgLaborId = ParamUtil.getLong(
709                                    actionRequest, "orgLaborId" + orgLaborsIndex);
710    
711                            int typeId = ParamUtil.getInteger(
712                                    actionRequest, "orgLaborTypeId" + orgLaborsIndex, -1);
713    
714                            if (typeId == -1) {
715                                    continue;
716                            }
717    
718                            int sunOpen = ParamUtil.getInteger(
719                                    actionRequest, "sunOpen" + orgLaborsIndex, -1);
720                            int sunClose = ParamUtil.getInteger(
721                                    actionRequest, "sunClose" + orgLaborsIndex, -1);
722                            int monOpen = ParamUtil.getInteger(
723                                    actionRequest, "monOpen" + orgLaborsIndex, -1);
724                            int monClose = ParamUtil.getInteger(
725                                    actionRequest, "monClose" + orgLaborsIndex, -1);
726                            int tueOpen = ParamUtil.getInteger(
727                                    actionRequest, "tueOpen" + orgLaborsIndex, -1);
728                            int tueClose = ParamUtil.getInteger(
729                                    actionRequest, "tueClose" + orgLaborsIndex, -1);
730                            int wedOpen = ParamUtil.getInteger(
731                                    actionRequest, "wedOpen" + orgLaborsIndex, -1);
732                            int wedClose = ParamUtil.getInteger(
733                                    actionRequest, "wedClose" + orgLaborsIndex, -1);
734                            int thuOpen = ParamUtil.getInteger(
735                                    actionRequest, "thuOpen" + orgLaborsIndex, -1);
736                            int thuClose = ParamUtil.getInteger(
737                                    actionRequest, "thuClose" + orgLaborsIndex, -1);
738                            int friOpen = ParamUtil.getInteger(
739                                    actionRequest, "friOpen" + orgLaborsIndex, -1);
740                            int friClose = ParamUtil.getInteger(
741                                    actionRequest, "friClose" + orgLaborsIndex, -1);
742                            int satOpen = ParamUtil.getInteger(
743                                    actionRequest, "satOpen" + orgLaborsIndex, -1);
744                            int satClose = ParamUtil.getInteger(
745                                    actionRequest, "satClose" + orgLaborsIndex, -1);
746    
747                            OrgLabor orgLabor = OrgLaborLocalServiceUtil.createOrgLabor(
748                                    orgLaborId);
749    
750                            orgLabor.setTypeId(typeId);
751                            orgLabor.setSunOpen(sunOpen);
752                            orgLabor.setSunClose(sunClose);
753                            orgLabor.setMonOpen(monOpen);
754                            orgLabor.setMonClose(monClose);
755                            orgLabor.setTueOpen(tueOpen);
756                            orgLabor.setTueClose(tueClose);
757                            orgLabor.setWedOpen(wedOpen);
758                            orgLabor.setWedClose(wedClose);
759                            orgLabor.setThuOpen(thuOpen);
760                            orgLabor.setThuClose(thuClose);
761                            orgLabor.setFriOpen(friOpen);
762                            orgLabor.setFriClose(friClose);
763                            orgLabor.setSatOpen(satOpen);
764                            orgLabor.setSatClose(satClose);
765    
766                            orgLabors.add(orgLabor);
767                    }
768    
769                    return orgLabors;
770            }
771    
772            @Override
773            public List<Phone> getPhones(ActionRequest actionRequest) {
774                    return getPhones(actionRequest, Collections.<Phone>emptyList());
775            }
776    
777            @Override
778            public List<Phone> getPhones(
779                    ActionRequest actionRequest, List<Phone> defaultPhones) {
780    
781                    String phonesIndexesString = actionRequest.getParameter(
782                            "phonesIndexes");
783    
784                    if (phonesIndexesString == null) {
785                            return defaultPhones;
786                    }
787    
788                    List<Phone> phones = new ArrayList<Phone>();
789    
790                    int[] phonesIndexes = StringUtil.split(phonesIndexesString, 0);
791    
792                    int phonePrimary = ParamUtil.getInteger(actionRequest, "phonePrimary");
793    
794                    for (int phonesIndex : phonesIndexes) {
795                            long phoneId = ParamUtil.getLong(
796                                    actionRequest, "phoneId" + phonesIndex);
797    
798                            String number = ParamUtil.getString(
799                                    actionRequest, "phoneNumber" + phonesIndex);
800                            String extension = ParamUtil.getString(
801                                    actionRequest, "phoneExtension" + phonesIndex);
802    
803                            if (Validator.isNull(number) && Validator.isNull(extension)) {
804                                    continue;
805                            }
806    
807                            int typeId = ParamUtil.getInteger(
808                                    actionRequest, "phoneTypeId" + phonesIndex);
809    
810                            boolean primary = false;
811    
812                            if (phonesIndex == phonePrimary) {
813                                    primary = true;
814                            }
815    
816                            Phone phone = PhoneLocalServiceUtil.createPhone(phoneId);
817    
818                            phone.setNumber(number);
819                            phone.setExtension(extension);
820                            phone.setTypeId(typeId);
821                            phone.setPrimary(primary);
822    
823                            phones.add(phone);
824                    }
825    
826                    return phones;
827            }
828    
829            @Override
830            public OrderByComparator getRoleOrderByComparator(
831                    String orderByCol, String orderByType) {
832    
833                    boolean orderByAsc = false;
834    
835                    if (orderByType.equals("asc")) {
836                            orderByAsc = true;
837                    }
838    
839                    OrderByComparator orderByComparator = null;
840    
841                    if (orderByCol.equals("name")) {
842                            orderByComparator = new RoleNameComparator(orderByAsc);
843                    }
844                    else if (orderByCol.equals("description")) {
845                            orderByComparator = new RoleDescriptionComparator(orderByAsc);
846                    }
847                    else if (orderByCol.equals("type")) {
848                            orderByComparator = new RoleTypeComparator(orderByAsc);
849                    }
850                    else {
851                            orderByComparator = new RoleNameComparator(orderByAsc);
852                    }
853    
854                    return orderByComparator;
855            }
856    
857            @Override
858            public OrderByComparator getUserGroupOrderByComparator(
859                    String orderByCol, String orderByType) {
860    
861                    boolean orderByAsc = false;
862    
863                    if (orderByType.equals("asc")) {
864                            orderByAsc = true;
865                    }
866    
867                    OrderByComparator orderByComparator = null;
868    
869                    if (orderByCol.equals("name")) {
870                            orderByComparator = new UserGroupNameComparator(orderByAsc);
871                    }
872                    else if (orderByCol.equals("description")) {
873                            orderByComparator = new UserGroupDescriptionComparator(orderByAsc);
874                    }
875                    else {
876                            orderByComparator = new UserGroupNameComparator(orderByAsc);
877                    }
878    
879                    return orderByComparator;
880            }
881    
882            @Override
883            public List<UserGroupRole> getUserGroupRoles(PortletRequest portletRequest)
884                    throws PortalException, SystemException {
885    
886                    User user = PortalUtil.getSelectedUser(portletRequest);
887    
888                    if (user == null) {
889                            return Collections.emptyList();
890                    }
891    
892                    Set<UserGroupRole> userGroupRoles =
893                            new HashSet<UserGroupRole>(
894                                    UserGroupRoleLocalServiceUtil.getUserGroupRoles(
895                                            user.getUserId()));
896    
897                    userGroupRoles.addAll(
898                            getUserGroupRoles(
899                                    portletRequest, user, "addGroupRolesGroupIds",
900                                    "addGroupRolesRoleIds"));
901                    userGroupRoles.removeAll(
902                            getUserGroupRoles(
903                                    portletRequest, user, "deleteGroupRolesGroupIds",
904                                    "deleteGroupRolesRoleIds"));
905    
906                    return new ArrayList<UserGroupRole>(userGroupRoles);
907            }
908    
909            @Override
910            public Tuple getUserGroups(Hits hits)
911                    throws PortalException, SystemException {
912    
913                    List<UserGroup> userGroups = new ArrayList<UserGroup>();
914                    boolean corruptIndex = false;
915    
916                    List<Document> documents = hits.toList();
917    
918                    for (Document document : documents) {
919                            long userGroupId = GetterUtil.getLong(
920                                    document.get(Field.USER_GROUP_ID));
921    
922                            try {
923                                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
924                                            userGroupId);
925    
926                                    userGroups.add(userGroup);
927                            }
928                            catch (NoSuchUserGroupException nsuge) {
929                                    corruptIndex = true;
930    
931                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
932                                            UserGroup.class);
933    
934                                    long companyId = GetterUtil.getLong(
935                                            document.get(Field.COMPANY_ID));
936    
937                                    indexer.delete(companyId, document.getUID());
938                            }
939                    }
940    
941                    return new Tuple(userGroups, corruptIndex);
942            }
943    
944            @Override
945            public OrderByComparator getUserOrderByComparator(
946                    String orderByCol, String orderByType) {
947    
948                    boolean orderByAsc = false;
949    
950                    if (orderByType.equals("asc")) {
951                            orderByAsc = true;
952                    }
953    
954                    OrderByComparator 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 Tuple getUsers(Hits hits) throws PortalException, SystemException {
980                    List<User> users = new ArrayList<User>();
981                    boolean corruptIndex = false;
982    
983                    List<Document> documents = hits.toList();
984    
985                    for (Document document : documents) {
986                            long userId = GetterUtil.getLong(document.get(Field.USER_ID));
987    
988                            User user = UserLocalServiceUtil.fetchUser(userId);
989    
990                            if (user != null) {
991                                    users.add(user);
992                            }
993                            else {
994                                    corruptIndex = true;
995    
996                                    Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
997    
998                                    long companyId = GetterUtil.getLong(
999                                            document.get(Field.COMPANY_ID));
1000    
1001                                    indexer.delete(companyId, document.getUID());
1002                            }
1003                    }
1004    
1005                    return new Tuple(users, corruptIndex);
1006            }
1007    
1008            @Override
1009            public List<Website> getWebsites(ActionRequest actionRequest) {
1010                    return getWebsites(actionRequest, Collections.<Website>emptyList());
1011            }
1012    
1013            @Override
1014            public List<Website> getWebsites(
1015                    ActionRequest actionRequest, List<Website> defaultWebsites) {
1016    
1017                    String websitesIndexesString = actionRequest.getParameter(
1018                            "websitesIndexes");
1019    
1020                    if (websitesIndexesString == null) {
1021                            return defaultWebsites;
1022                    }
1023    
1024                    List<Website> websites = new ArrayList<Website>();
1025    
1026                    int[] websitesIndexes = StringUtil.split(websitesIndexesString, 0);
1027    
1028                    int websitePrimary = ParamUtil.getInteger(
1029                            actionRequest, "websitePrimary");
1030    
1031                    for (int websitesIndex : websitesIndexes) {
1032                            long websiteId = ParamUtil.getLong(
1033                                    actionRequest, "websiteId" + websitesIndex);
1034    
1035                            String url = ParamUtil.getString(
1036                                    actionRequest, "websiteUrl" + websitesIndex);
1037    
1038                            if (Validator.isNull(url)) {
1039                                    continue;
1040                            }
1041    
1042                            int typeId = ParamUtil.getInteger(
1043                                    actionRequest, "websiteTypeId" + websitesIndex);
1044    
1045                            boolean primary = false;
1046    
1047                            if (websitesIndex == websitePrimary) {
1048                                    primary = true;
1049                            }
1050    
1051                            Website website = WebsiteLocalServiceUtil.createWebsite(websiteId);
1052    
1053                            website.setUrl(url);
1054                            website.setTypeId(typeId);
1055                            website.setPrimary(primary);
1056    
1057                            websites.add(website);
1058                    }
1059    
1060                    return websites;
1061            }
1062    
1063            /**
1064             * @deprecated As of 6.2.0, replaced by {@link
1065             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1066             *             String)}
1067             */
1068            @Override
1069            public boolean hasUpdateEmailAddress(
1070                            PermissionChecker permissionChecker, User user)
1071                    throws PortalException, SystemException {
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, SystemException {
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            @Override
1154            public boolean hasUpdateFieldPermission(User user, String field)
1155                    throws PortalException, SystemException {
1156    
1157                    PermissionChecker permissionChecker =
1158                            PermissionThreadLocal.getPermissionChecker();
1159    
1160                    return hasUpdateFieldPermission(permissionChecker, null, user, field);
1161            }
1162    
1163            /**
1164             * @deprecated As of 6.2.0, replaced by {@link
1165             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1166             *             String)}
1167             */
1168            @Deprecated
1169            @Override
1170            public boolean hasUpdateScreenName(
1171                            PermissionChecker permissionChecker, User user)
1172                    throws PortalException, SystemException {
1173    
1174                    return hasUpdateFieldPermission(
1175                            permissionChecker, null, user, "screenName");
1176            }
1177    
1178            @Override
1179            public long[] removeRequiredRoles(long userId, long[] roleIds)
1180                    throws PortalException, SystemException {
1181    
1182                    User user = UserLocalServiceUtil.getUser(userId);
1183    
1184                    return removeRequiredRoles(user, roleIds);
1185            }
1186    
1187            @Override
1188            public long[] removeRequiredRoles(User user, long[] roleIds)
1189                    throws PortalException, SystemException {
1190    
1191                    Role role = RoleLocalServiceUtil.getRole(
1192                            user.getCompanyId(), RoleConstants.USER);
1193    
1194                    roleIds = ArrayUtil.remove(roleIds, role.getRoleId());
1195    
1196                    return roleIds;
1197            }
1198    
1199            @Override
1200            public void updateAddresses(
1201                            String className, long classPK, List<Address> addresses)
1202                    throws PortalException, SystemException {
1203    
1204                    Set<Long> addressIds = new HashSet<Long>();
1205    
1206                    for (Address address : addresses) {
1207                            long addressId = address.getAddressId();
1208    
1209                            String street1 = address.getStreet1();
1210                            String street2 = address.getStreet2();
1211                            String street3 = address.getStreet3();
1212                            String city = address.getCity();
1213                            String zip = address.getZip();
1214                            long regionId = address.getRegionId();
1215                            long countryId = address.getCountryId();
1216                            int typeId = address.getTypeId();
1217                            boolean mailing = address.isMailing();
1218                            boolean primary = address.isPrimary();
1219    
1220                            if (addressId <= 0) {
1221                                    address = AddressServiceUtil.addAddress(
1222                                            className, classPK, street1, street2, street3, city, zip,
1223                                            regionId, countryId, typeId, mailing, primary,
1224                                            new ServiceContext());
1225    
1226                                    addressId = address.getAddressId();
1227                            }
1228                            else {
1229                                    AddressServiceUtil.updateAddress(
1230                                            addressId, street1, street2, street3, city, zip, regionId,
1231                                            countryId, typeId, mailing, primary);
1232                            }
1233    
1234                            addressIds.add(addressId);
1235                    }
1236    
1237                    addresses = AddressServiceUtil.getAddresses(className, classPK);
1238    
1239                    for (Address address : addresses) {
1240                            if (!addressIds.contains(address.getAddressId())) {
1241                                    AddressServiceUtil.deleteAddress(address.getAddressId());
1242                            }
1243                    }
1244            }
1245    
1246            @Override
1247            public void updateEmailAddresses(
1248                            String className, long classPK, List<EmailAddress> emailAddresses)
1249                    throws PortalException, SystemException {
1250    
1251                    Set<Long> emailAddressIds = new HashSet<Long>();
1252    
1253                    for (EmailAddress emailAddress : emailAddresses) {
1254                            long emailAddressId = emailAddress.getEmailAddressId();
1255    
1256                            String address = emailAddress.getAddress();
1257                            int typeId = emailAddress.getTypeId();
1258                            boolean primary = emailAddress.isPrimary();
1259    
1260                            if (emailAddressId <= 0) {
1261                                    emailAddress = EmailAddressServiceUtil.addEmailAddress(
1262                                            className, classPK, address, typeId, primary,
1263                                            new ServiceContext());
1264    
1265                                    emailAddressId = emailAddress.getEmailAddressId();
1266                            }
1267                            else {
1268                                    EmailAddressServiceUtil.updateEmailAddress(
1269                                            emailAddressId, address, typeId, primary);
1270                            }
1271    
1272                            emailAddressIds.add(emailAddressId);
1273                    }
1274    
1275                    emailAddresses = EmailAddressServiceUtil.getEmailAddresses(
1276                            className, classPK);
1277    
1278                    for (EmailAddress emailAddress : emailAddresses) {
1279                            if (!emailAddressIds.contains(emailAddress.getEmailAddressId())) {
1280                                    EmailAddressServiceUtil.deleteEmailAddress(
1281                                            emailAddress.getEmailAddressId());
1282                            }
1283                    }
1284            }
1285    
1286            @Override
1287            public void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
1288                    throws PortalException, SystemException {
1289    
1290                    Set<Long> orgLaborsIds = new HashSet<Long>();
1291    
1292                    for (OrgLabor orgLabor : orgLabors) {
1293                            long orgLaborId = orgLabor.getOrgLaborId();
1294    
1295                            int typeId = orgLabor.getTypeId();
1296                            int sunOpen = orgLabor.getSunOpen();
1297                            int sunClose = orgLabor.getSunClose();
1298                            int monOpen = orgLabor.getMonOpen();
1299                            int monClose = orgLabor.getMonClose();
1300                            int tueOpen = orgLabor.getTueOpen();
1301                            int tueClose = orgLabor.getTueClose();
1302                            int wedOpen = orgLabor.getWedOpen();
1303                            int wedClose = orgLabor.getWedClose();
1304                            int thuOpen = orgLabor.getThuOpen();
1305                            int thuClose = orgLabor.getThuClose();
1306                            int friOpen = orgLabor.getFriOpen();
1307                            int friClose = orgLabor.getFriClose();
1308                            int satOpen = orgLabor.getSatOpen();
1309                            int satClose = orgLabor.getSatClose();
1310    
1311                            if (orgLaborId <= 0) {
1312                                    orgLabor = OrgLaborServiceUtil.addOrgLabor(
1313                                            classPK, typeId, sunOpen, sunClose, monOpen, monClose,
1314                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1315                                            friOpen, friClose, satOpen, satClose);
1316    
1317                                    orgLaborId = orgLabor.getOrgLaborId();
1318                            }
1319                            else {
1320                                    OrgLaborServiceUtil.updateOrgLabor(
1321                                            orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
1322                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1323                                            friOpen, friClose, satOpen, satClose);
1324                            }
1325    
1326                            orgLaborsIds.add(orgLaborId);
1327                    }
1328    
1329                    orgLabors = OrgLaborServiceUtil.getOrgLabors(classPK);
1330    
1331                    for (OrgLabor orgLabor : orgLabors) {
1332                            if (!orgLaborsIds.contains(orgLabor.getOrgLaborId())) {
1333                                    OrgLaborServiceUtil.deleteOrgLabor(orgLabor.getOrgLaborId());
1334                            }
1335                    }
1336            }
1337    
1338            @Override
1339            public void updatePhones(String className, long classPK, List<Phone> phones)
1340                    throws PortalException, SystemException {
1341    
1342                    Set<Long> phoneIds = new HashSet<Long>();
1343    
1344                    for (Phone phone : phones) {
1345                            long phoneId = phone.getPhoneId();
1346    
1347                            String number = phone.getNumber();
1348                            String extension = phone.getExtension();
1349                            int typeId = phone.getTypeId();
1350                            boolean primary = phone.isPrimary();
1351    
1352                            if (phoneId <= 0) {
1353                                    phone = PhoneServiceUtil.addPhone(
1354                                            className, classPK, number, extension, typeId, primary,
1355                                            new ServiceContext());
1356    
1357                                    phoneId = phone.getPhoneId();
1358                            }
1359                            else {
1360                                    PhoneServiceUtil.updatePhone(
1361                                            phoneId, number, extension, typeId, primary);
1362                            }
1363    
1364                            phoneIds.add(phoneId);
1365                    }
1366    
1367                    phones = PhoneServiceUtil.getPhones(className, classPK);
1368    
1369                    for (Phone phone : phones) {
1370                            if (!phoneIds.contains(phone.getPhoneId())) {
1371                                    PhoneServiceUtil.deletePhone(phone.getPhoneId());
1372                            }
1373                    }
1374            }
1375    
1376            @Override
1377            public void updateWebsites(
1378                            String className, long classPK, List<Website> websites)
1379                    throws PortalException, SystemException {
1380    
1381                    Set<Long> websiteIds = new HashSet<Long>();
1382    
1383                    for (Website website : websites) {
1384                            long websiteId = website.getWebsiteId();
1385    
1386                            String url = website.getUrl();
1387                            int typeId = website.getTypeId();
1388                            boolean primary = website.isPrimary();
1389    
1390                            if (websiteId <= 0) {
1391                                    website = WebsiteServiceUtil.addWebsite(
1392                                            className, classPK, url, typeId, primary,
1393                                            new ServiceContext());
1394    
1395                                    websiteId = website.getWebsiteId();
1396                            }
1397                            else {
1398                                    WebsiteServiceUtil.updateWebsite(
1399                                            websiteId, url, typeId, primary);
1400                            }
1401    
1402                            websiteIds.add(websiteId);
1403                    }
1404    
1405                    websites = WebsiteServiceUtil.getWebsites(className, classPK);
1406    
1407                    for (Website website : websites) {
1408                            if (!websiteIds.contains(website.getWebsiteId())) {
1409                                    WebsiteServiceUtil.deleteWebsite(website.getWebsiteId());
1410                            }
1411                    }
1412            }
1413    
1414            protected List<UserGroupRole> getUserGroupRoles(
1415                    PortletRequest portletRequest, User user, String groupIdsParam,
1416                    String roleIdsParam) {
1417    
1418                    List<UserGroupRole> userGroupRoles = new UniqueList<UserGroupRole>();
1419    
1420                    long[] groupRolesGroupIds = StringUtil.split(
1421                            ParamUtil.getString(portletRequest, groupIdsParam), 0L);
1422                    long[] groupRolesRoleIds = StringUtil.split(
1423                            ParamUtil.getString(portletRequest, roleIdsParam), 0L);
1424    
1425                    if (groupRolesGroupIds.length != groupRolesRoleIds.length) {
1426                            return userGroupRoles;
1427                    }
1428    
1429                    long userId = 0;
1430    
1431                    if (user != null) {
1432                            userId = user.getUserId();
1433                    }
1434    
1435                    for (int i = 0; i < groupRolesGroupIds.length; i++) {
1436                            if ((groupRolesGroupIds[i] == 0) || (groupRolesRoleIds[i] == 0)) {
1437                                    continue;
1438                            }
1439    
1440                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
1441                                    userId, groupRolesGroupIds[i], groupRolesRoleIds[i]);
1442    
1443                            UserGroupRole userGroupRole =
1444                                    UserGroupRoleLocalServiceUtil.createUserGroupRole(
1445                                            userGroupRolePK);
1446    
1447                            userGroupRoles.add(userGroupRole);
1448                    }
1449    
1450                    return userGroupRoles;
1451            }
1452    
1453    }