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