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