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                    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                    List<UserGroupRole> userGroupRoles = new UniqueList<UserGroupRole>();
897    
898                    long[] groupRolesRoleIds = StringUtil.split(
899                            ParamUtil.getString(portletRequest, "groupRolesRoleIds"), 0L);
900                    long[] groupRolesGroupIds = StringUtil.split(
901                            ParamUtil.getString(portletRequest, "groupRolesGroupIds"), 0L);
902    
903                    if (groupRolesGroupIds.length != groupRolesRoleIds.length) {
904                            return userGroupRoles;
905                    }
906    
907                    User user = PortalUtil.getSelectedUser(portletRequest);
908    
909                    long userId = 0;
910    
911                    if (user != null) {
912                            userId = user.getUserId();
913                    }
914    
915                    for (int i = 0; i < groupRolesGroupIds.length; i++) {
916                            if ((groupRolesGroupIds[i] == 0) || (groupRolesRoleIds[i] == 0)) {
917                                    continue;
918                            }
919    
920                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
921                                    userId, groupRolesGroupIds[i], groupRolesRoleIds[i]);
922    
923                            UserGroupRole userGroupRole =
924                                    UserGroupRoleLocalServiceUtil.createUserGroupRole(
925                                            userGroupRolePK);
926    
927                            userGroupRoles.add(userGroupRole);
928                    }
929    
930                    return userGroupRoles;
931            }
932    
933            @Override
934            public Tuple getUserGroups(Hits hits)
935                    throws PortalException, SystemException {
936    
937                    List<UserGroup> userGroups = new ArrayList<UserGroup>();
938                    boolean corruptIndex = false;
939    
940                    List<Document> documents = hits.toList();
941    
942                    for (Document document : documents) {
943                            long userGroupId = GetterUtil.getLong(
944                                    document.get(Field.USER_GROUP_ID));
945    
946                            try {
947                                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
948                                            userGroupId);
949    
950                                    userGroups.add(userGroup);
951                            }
952                            catch (NoSuchUserGroupException nsuge) {
953                                    corruptIndex = true;
954    
955                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
956                                            UserGroup.class);
957    
958                                    long companyId = GetterUtil.getLong(
959                                            document.get(Field.COMPANY_ID));
960    
961                                    indexer.delete(companyId, document.getUID());
962                            }
963                    }
964    
965                    return new Tuple(userGroups, corruptIndex);
966            }
967    
968            @Override
969            public OrderByComparator getUserOrderByComparator(
970                    String orderByCol, String orderByType) {
971    
972                    boolean orderByAsc = false;
973    
974                    if (orderByType.equals("asc")) {
975                            orderByAsc = true;
976                    }
977    
978                    OrderByComparator orderByComparator = null;
979    
980                    if (orderByCol.equals("email-address")) {
981                            orderByComparator = new UserEmailAddressComparator(orderByAsc);
982                    }
983                    else if (orderByCol.equals("first-name")) {
984                            orderByComparator = new UserFirstNameComparator(orderByAsc);
985                    }
986                    else if (orderByCol.equals("job-title")) {
987                            orderByComparator = new UserJobTitleComparator(orderByAsc);
988                    }
989                    else if (orderByCol.equals("last-name")) {
990                            orderByComparator = new UserLastNameComparator(orderByAsc);
991                    }
992                    else if (orderByCol.equals("screen-name")) {
993                            orderByComparator = new UserScreenNameComparator(orderByAsc);
994                    }
995                    else {
996                            orderByComparator = new UserLastNameComparator(orderByAsc);
997                    }
998    
999                    return orderByComparator;
1000            }
1001    
1002            @Override
1003            public Tuple getUsers(Hits hits) throws PortalException, SystemException {
1004                    List<User> users = new ArrayList<User>();
1005                    boolean corruptIndex = false;
1006    
1007                    List<Document> documents = hits.toList();
1008    
1009                    for (Document document : documents) {
1010                            long userId = GetterUtil.getLong(document.get(Field.USER_ID));
1011    
1012                            User user = UserLocalServiceUtil.fetchUser(userId);
1013    
1014                            if (user != null) {
1015                                    users.add(user);
1016                            }
1017                            else {
1018                                    corruptIndex = true;
1019    
1020                                    Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
1021    
1022                                    long companyId = GetterUtil.getLong(
1023                                            document.get(Field.COMPANY_ID));
1024    
1025                                    indexer.delete(companyId, document.getUID());
1026                            }
1027                    }
1028    
1029                    return new Tuple(users, corruptIndex);
1030            }
1031    
1032            @Override
1033            public List<Website> getWebsites(ActionRequest actionRequest) {
1034                    return getWebsites(actionRequest, Collections.<Website>emptyList());
1035            }
1036    
1037            @Override
1038            public List<Website> getWebsites(
1039                    ActionRequest actionRequest, List<Website> defaultWebsites) {
1040    
1041                    String websitesIndexesString = actionRequest.getParameter(
1042                            "websitesIndexes");
1043    
1044                    if (websitesIndexesString == null) {
1045                            return defaultWebsites;
1046                    }
1047    
1048                    List<Website> websites = new ArrayList<Website>();
1049    
1050                    int[] websitesIndexes = StringUtil.split(websitesIndexesString, 0);
1051    
1052                    int websitePrimary = ParamUtil.getInteger(
1053                            actionRequest, "websitePrimary");
1054    
1055                    for (int websitesIndex : websitesIndexes) {
1056                            long websiteId = ParamUtil.getLong(
1057                                    actionRequest, "websiteId" + websitesIndex);
1058    
1059                            String url = ParamUtil.getString(
1060                                    actionRequest, "websiteUrl" + websitesIndex);
1061    
1062                            if (Validator.isNull(url)) {
1063                                    continue;
1064                            }
1065    
1066                            int typeId = ParamUtil.getInteger(
1067                                    actionRequest, "websiteTypeId" + websitesIndex);
1068    
1069                            boolean primary = false;
1070    
1071                            if (websitesIndex == websitePrimary) {
1072                                    primary = true;
1073                            }
1074    
1075                            Website website = WebsiteLocalServiceUtil.createWebsite(websiteId);
1076    
1077                            website.setUrl(url);
1078                            website.setTypeId(typeId);
1079                            website.setPrimary(primary);
1080    
1081                            websites.add(website);
1082                    }
1083    
1084                    return websites;
1085            }
1086    
1087            /**
1088             * @deprecated As of 6.2.0, replaced by {@link
1089             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1090             *             String)}
1091             */
1092            @Override
1093            public boolean hasUpdateEmailAddress(
1094                            PermissionChecker permissionChecker, User user)
1095                    throws PortalException, SystemException {
1096    
1097                    return hasUpdateFieldPermission(
1098                            permissionChecker, null, user, "emailAddress");
1099            }
1100    
1101            @Override
1102            public boolean hasUpdateFieldPermission(
1103                            PermissionChecker permissionChecker, User updatingUser,
1104                            User updatedUser, String field)
1105                    throws PortalException, SystemException {
1106    
1107                    if (updatedUser == null) {
1108                            return true;
1109                    }
1110    
1111                    if (updatingUser == null) {
1112                            long updatingUserId = PrincipalThreadLocal.getUserId();
1113    
1114                            if (updatingUserId > 0) {
1115                                    updatingUser = UserLocalServiceUtil.fetchUserById(
1116                                            updatingUserId);
1117                            }
1118                    }
1119    
1120                    if ((updatingUser != null) && !updatingUser.equals(updatedUser) &&
1121                            UserPermissionUtil.contains(
1122                                    permissionChecker, updatingUser.getUserId(),
1123                                    ActionKeys.UPDATE_USER)) {
1124    
1125                            return true;
1126                    }
1127    
1128                    for (String userType : PropsValues.FIELD_EDITABLE_USER_TYPES) {
1129                            if (userType.equals("user-with-mx") && updatedUser.hasCompanyMx()) {
1130                                    return true;
1131                            }
1132    
1133                            if (userType.equals("user-without-mx") &&
1134                                    !updatedUser.hasCompanyMx()) {
1135    
1136                                    return true;
1137                            }
1138                    }
1139    
1140                    for (String roleName : PropsValues.FIELD_EDITABLE_ROLES) {
1141                            Role role = RoleLocalServiceUtil.fetchRole(
1142                                    updatedUser.getCompanyId(), roleName);
1143    
1144                            if ((role != null) &&
1145                                    RoleLocalServiceUtil.hasUserRole(
1146                                            updatedUser.getUserId(), role.getRoleId())) {
1147    
1148                                    return true;
1149                            }
1150                    }
1151    
1152                    String emailAddress = updatedUser.getEmailAddress();
1153    
1154                    for (String domainName : PropsValues.FIELD_EDITABLE_DOMAINS) {
1155                            if (emailAddress.endsWith(domainName)) {
1156                                    return true;
1157                            }
1158                    }
1159    
1160                    String[] fieldEditableDomainNames = PropsUtil.getArray(
1161                            PropsKeys.FIELD_EDITABLE_DOMAINS, new Filter(field));
1162    
1163                    for (String domainName : fieldEditableDomainNames) {
1164                            if (emailAddress.endsWith(domainName)) {
1165                                    return true;
1166                            }
1167                    }
1168    
1169                    return false;
1170            }
1171    
1172            /**
1173             * @deprecated As of 6.2.0, replaced by {@link
1174             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1175             *             String)}
1176             */
1177            @Override
1178            public boolean hasUpdateFieldPermission(User user, String field)
1179                    throws PortalException, SystemException {
1180    
1181                    PermissionChecker permissionChecker =
1182                            PermissionThreadLocal.getPermissionChecker();
1183    
1184                    return hasUpdateFieldPermission(permissionChecker, null, user, field);
1185            }
1186    
1187            /**
1188             * @deprecated As of 6.2.0, replaced by {@link
1189             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1190             *             String)}
1191             */
1192            @Deprecated
1193            @Override
1194            public boolean hasUpdateScreenName(
1195                            PermissionChecker permissionChecker, User user)
1196                    throws PortalException, SystemException {
1197    
1198                    return hasUpdateFieldPermission(
1199                            permissionChecker, null, user, "screenName");
1200            }
1201    
1202            @Override
1203            public long[] removeRequiredRoles(long userId, long[] roleIds)
1204                    throws PortalException, SystemException {
1205    
1206                    User user = UserLocalServiceUtil.getUser(userId);
1207    
1208                    return removeRequiredRoles(user, roleIds);
1209            }
1210    
1211            @Override
1212            public long[] removeRequiredRoles(User user, long[] roleIds)
1213                    throws PortalException, SystemException {
1214    
1215                    Role role = RoleLocalServiceUtil.getRole(
1216                            user.getCompanyId(), RoleConstants.USER);
1217    
1218                    roleIds = ArrayUtil.remove(roleIds, role.getRoleId());
1219    
1220                    return roleIds;
1221            }
1222    
1223            @Override
1224            public void updateAddresses(
1225                            String className, long classPK, List<Address> addresses)
1226                    throws PortalException, SystemException {
1227    
1228                    Set<Long> addressIds = new HashSet<Long>();
1229    
1230                    for (Address address : addresses) {
1231                            long addressId = address.getAddressId();
1232    
1233                            String street1 = address.getStreet1();
1234                            String street2 = address.getStreet2();
1235                            String street3 = address.getStreet3();
1236                            String city = address.getCity();
1237                            String zip = address.getZip();
1238                            long regionId = address.getRegionId();
1239                            long countryId = address.getCountryId();
1240                            int typeId = address.getTypeId();
1241                            boolean mailing = address.isMailing();
1242                            boolean primary = address.isPrimary();
1243    
1244                            if (addressId <= 0) {
1245                                    address = AddressServiceUtil.addAddress(
1246                                            className, classPK, street1, street2, street3, city, zip,
1247                                            regionId, countryId, typeId, mailing, primary,
1248                                            new ServiceContext());
1249    
1250                                    addressId = address.getAddressId();
1251                            }
1252                            else {
1253                                    AddressServiceUtil.updateAddress(
1254                                            addressId, street1, street2, street3, city, zip, regionId,
1255                                            countryId, typeId, mailing, primary);
1256                            }
1257    
1258                            addressIds.add(addressId);
1259                    }
1260    
1261                    addresses = AddressServiceUtil.getAddresses(className, classPK);
1262    
1263                    for (Address address : addresses) {
1264                            if (!addressIds.contains(address.getAddressId())) {
1265                                    AddressServiceUtil.deleteAddress(address.getAddressId());
1266                            }
1267                    }
1268            }
1269    
1270            @Override
1271            public void updateEmailAddresses(
1272                            String className, long classPK, List<EmailAddress> emailAddresses)
1273                    throws PortalException, SystemException {
1274    
1275                    Set<Long> emailAddressIds = new HashSet<Long>();
1276    
1277                    for (EmailAddress emailAddress : emailAddresses) {
1278                            long emailAddressId = emailAddress.getEmailAddressId();
1279    
1280                            String address = emailAddress.getAddress();
1281                            int typeId = emailAddress.getTypeId();
1282                            boolean primary = emailAddress.isPrimary();
1283    
1284                            if (emailAddressId <= 0) {
1285                                    emailAddress = EmailAddressServiceUtil.addEmailAddress(
1286                                            className, classPK, address, typeId, primary,
1287                                            new ServiceContext());
1288    
1289                                    emailAddressId = emailAddress.getEmailAddressId();
1290                            }
1291                            else {
1292                                    EmailAddressServiceUtil.updateEmailAddress(
1293                                            emailAddressId, address, typeId, primary);
1294                            }
1295    
1296                            emailAddressIds.add(emailAddressId);
1297                    }
1298    
1299                    emailAddresses = EmailAddressServiceUtil.getEmailAddresses(
1300                            className, classPK);
1301    
1302                    for (EmailAddress emailAddress : emailAddresses) {
1303                            if (!emailAddressIds.contains(emailAddress.getEmailAddressId())) {
1304                                    EmailAddressServiceUtil.deleteEmailAddress(
1305                                            emailAddress.getEmailAddressId());
1306                            }
1307                    }
1308            }
1309    
1310            @Override
1311            public void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
1312                    throws PortalException, SystemException {
1313    
1314                    Set<Long> orgLaborsIds = new HashSet<Long>();
1315    
1316                    for (OrgLabor orgLabor : orgLabors) {
1317                            long orgLaborId = orgLabor.getOrgLaborId();
1318    
1319                            int typeId = orgLabor.getTypeId();
1320                            int sunOpen = orgLabor.getSunOpen();
1321                            int sunClose = orgLabor.getSunClose();
1322                            int monOpen = orgLabor.getMonOpen();
1323                            int monClose = orgLabor.getMonClose();
1324                            int tueOpen = orgLabor.getTueOpen();
1325                            int tueClose = orgLabor.getTueClose();
1326                            int wedOpen = orgLabor.getWedOpen();
1327                            int wedClose = orgLabor.getWedClose();
1328                            int thuOpen = orgLabor.getThuOpen();
1329                            int thuClose = orgLabor.getThuClose();
1330                            int friOpen = orgLabor.getFriOpen();
1331                            int friClose = orgLabor.getFriClose();
1332                            int satOpen = orgLabor.getSatOpen();
1333                            int satClose = orgLabor.getSatClose();
1334    
1335                            if (orgLaborId <= 0) {
1336                                    orgLabor = OrgLaborServiceUtil.addOrgLabor(
1337                                            classPK, typeId, sunOpen, sunClose, monOpen, monClose,
1338                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1339                                            friOpen, friClose, satOpen, satClose);
1340    
1341                                    orgLaborId = orgLabor.getOrgLaborId();
1342                            }
1343                            else {
1344                                    OrgLaborServiceUtil.updateOrgLabor(
1345                                            orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
1346                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1347                                            friOpen, friClose, satOpen, satClose);
1348                            }
1349    
1350                            orgLaborsIds.add(orgLaborId);
1351                    }
1352    
1353                    orgLabors = OrgLaborServiceUtil.getOrgLabors(classPK);
1354    
1355                    for (OrgLabor orgLabor : orgLabors) {
1356                            if (!orgLaborsIds.contains(orgLabor.getOrgLaborId())) {
1357                                    OrgLaborServiceUtil.deleteOrgLabor(orgLabor.getOrgLaborId());
1358                            }
1359                    }
1360            }
1361    
1362            @Override
1363            public void updatePhones(String className, long classPK, List<Phone> phones)
1364                    throws PortalException, SystemException {
1365    
1366                    Set<Long> phoneIds = new HashSet<Long>();
1367    
1368                    for (Phone phone : phones) {
1369                            long phoneId = phone.getPhoneId();
1370    
1371                            String number = phone.getNumber();
1372                            String extension = phone.getExtension();
1373                            int typeId = phone.getTypeId();
1374                            boolean primary = phone.isPrimary();
1375    
1376                            if (phoneId <= 0) {
1377                                    phone = PhoneServiceUtil.addPhone(
1378                                            className, classPK, number, extension, typeId, primary,
1379                                            new ServiceContext());
1380    
1381                                    phoneId = phone.getPhoneId();
1382                            }
1383                            else {
1384                                    PhoneServiceUtil.updatePhone(
1385                                            phoneId, number, extension, typeId, primary);
1386                            }
1387    
1388                            phoneIds.add(phoneId);
1389                    }
1390    
1391                    phones = PhoneServiceUtil.getPhones(className, classPK);
1392    
1393                    for (Phone phone : phones) {
1394                            if (!phoneIds.contains(phone.getPhoneId())) {
1395                                    PhoneServiceUtil.deletePhone(phone.getPhoneId());
1396                            }
1397                    }
1398            }
1399    
1400            @Override
1401            public void updateWebsites(
1402                            String className, long classPK, List<Website> websites)
1403                    throws PortalException, SystemException {
1404    
1405                    Set<Long> websiteIds = new HashSet<Long>();
1406    
1407                    for (Website website : websites) {
1408                            long websiteId = website.getWebsiteId();
1409    
1410                            String url = website.getUrl();
1411                            int typeId = website.getTypeId();
1412                            boolean primary = website.isPrimary();
1413    
1414                            if (websiteId <= 0) {
1415                                    website = WebsiteServiceUtil.addWebsite(
1416                                            className, classPK, url, typeId, primary,
1417                                            new ServiceContext());
1418    
1419                                    websiteId = website.getWebsiteId();
1420                            }
1421                            else {
1422                                    WebsiteServiceUtil.updateWebsite(
1423                                            websiteId, url, typeId, primary);
1424                            }
1425    
1426                            websiteIds.add(websiteId);
1427                    }
1428    
1429                    websites = WebsiteServiceUtil.getWebsites(className, classPK);
1430    
1431                    for (Website website : websites) {
1432                            if (!websiteIds.contains(website.getWebsiteId())) {
1433                                    WebsiteServiceUtil.deleteWebsite(website.getWebsiteId());
1434                            }
1435                    }
1436            }
1437    
1438    }