001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.util;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.search.Document;
021    import com.liferay.portal.kernel.search.Field;
022    import com.liferay.portal.kernel.search.Hits;
023    import com.liferay.portal.kernel.search.Indexer;
024    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
025    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
026    import com.liferay.portal.kernel.util.Accessor;
027    import com.liferay.portal.kernel.util.ArrayUtil;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.ListUtil;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.ParamUtil;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.SetUtil;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.Address;
038    import com.liferay.portal.model.EmailAddress;
039    import com.liferay.portal.model.Group;
040    import com.liferay.portal.model.OrgLabor;
041    import com.liferay.portal.model.Organization;
042    import com.liferay.portal.model.Phone;
043    import com.liferay.portal.model.Role;
044    import com.liferay.portal.model.RoleConstants;
045    import com.liferay.portal.model.User;
046    import com.liferay.portal.model.UserGroup;
047    import com.liferay.portal.model.UserGroupRole;
048    import com.liferay.portal.model.Website;
049    import com.liferay.portal.security.auth.PrincipalThreadLocal;
050    import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
051    import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
052    import com.liferay.portal.security.permission.ActionKeys;
053    import com.liferay.portal.security.permission.PermissionChecker;
054    import com.liferay.portal.security.permission.PermissionThreadLocal;
055    import com.liferay.portal.service.AddressLocalServiceUtil;
056    import com.liferay.portal.service.AddressServiceUtil;
057    import com.liferay.portal.service.EmailAddressLocalServiceUtil;
058    import com.liferay.portal.service.EmailAddressServiceUtil;
059    import com.liferay.portal.service.GroupLocalServiceUtil;
060    import com.liferay.portal.service.OrgLaborLocalServiceUtil;
061    import com.liferay.portal.service.OrgLaborServiceUtil;
062    import com.liferay.portal.service.OrganizationLocalServiceUtil;
063    import com.liferay.portal.service.PhoneLocalServiceUtil;
064    import com.liferay.portal.service.PhoneServiceUtil;
065    import com.liferay.portal.service.RoleLocalServiceUtil;
066    import com.liferay.portal.service.ServiceContext;
067    import com.liferay.portal.service.UserGroupLocalServiceUtil;
068    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
069    import com.liferay.portal.service.UserLocalServiceUtil;
070    import com.liferay.portal.service.WebsiteLocalServiceUtil;
071    import com.liferay.portal.service.WebsiteServiceUtil;
072    import com.liferay.portal.service.permission.GroupPermissionUtil;
073    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
074    import com.liferay.portal.service.permission.RolePermissionUtil;
075    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
076    import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
077    import com.liferay.portal.service.permission.UserPermissionUtil;
078    import com.liferay.portal.service.persistence.UserGroupRolePK;
079    import com.liferay.portal.util.PortalUtil;
080    import com.liferay.portal.util.PropsUtil;
081    import com.liferay.portal.util.PropsValues;
082    import com.liferay.portal.util.comparator.GroupNameComparator;
083    import com.liferay.portal.util.comparator.GroupTypeComparator;
084    import com.liferay.portal.util.comparator.OrganizationNameComparator;
085    import com.liferay.portal.util.comparator.OrganizationTypeComparator;
086    import com.liferay.portal.util.comparator.RoleDescriptionComparator;
087    import com.liferay.portal.util.comparator.RoleNameComparator;
088    import com.liferay.portal.util.comparator.RoleTypeComparator;
089    import com.liferay.portal.util.comparator.UserEmailAddressComparator;
090    import com.liferay.portal.util.comparator.UserFirstNameComparator;
091    import com.liferay.portal.util.comparator.UserGroupDescriptionComparator;
092    import com.liferay.portal.util.comparator.UserGroupNameComparator;
093    import com.liferay.portal.util.comparator.UserJobTitleComparator;
094    import com.liferay.portal.util.comparator.UserLastNameComparator;
095    import com.liferay.portal.util.comparator.UserScreenNameComparator;
096    
097    import java.util.ArrayList;
098    import java.util.Collections;
099    import java.util.HashSet;
100    import java.util.Iterator;
101    import java.util.List;
102    import java.util.Locale;
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 {
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 {
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 {
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, group, 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 {
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, ActionKeys.ASSIGN_MEMBERS)) {
282    
283                                    itr.remove();
284                            }
285                    }
286    
287                    return filteredGroups;
288            }
289    
290            @Override
291            public List<Organization> filterOrganizations(
292                            PermissionChecker permissionChecker,
293                            List<Organization> organizations)
294                    throws PortalException {
295    
296                    if (permissionChecker.isCompanyAdmin()) {
297                            return organizations;
298                    }
299    
300                    List<Organization> filteredOrganizations = ListUtil.copy(organizations);
301    
302                    Iterator<Organization> itr = filteredOrganizations.iterator();
303    
304                    while (itr.hasNext()) {
305                            Organization organization = itr.next();
306    
307                            if (!OrganizationPermissionUtil.contains(
308                                            permissionChecker, organization,
309                                            ActionKeys.ASSIGN_MEMBERS)) {
310    
311                                    itr.remove();
312                            }
313                    }
314    
315                    return filteredOrganizations;
316            }
317    
318            @Override
319            public List<Role> filterRoles(
320                    PermissionChecker permissionChecker, List<Role> roles) {
321    
322                    List<Role> filteredRoles = ListUtil.copy(roles);
323    
324                    Iterator<Role> itr = filteredRoles.iterator();
325    
326                    while (itr.hasNext()) {
327                            Role role = itr.next();
328    
329                            String roleName = role.getName();
330    
331                            if (roleName.equals(RoleConstants.GUEST) ||
332                                    roleName.equals(RoleConstants.ORGANIZATION_USER) ||
333                                    roleName.equals(RoleConstants.OWNER) ||
334                                    roleName.equals(RoleConstants.SITE_MEMBER) ||
335                                    roleName.equals(RoleConstants.USER)) {
336    
337                                    itr.remove();
338                            }
339                    }
340    
341                    if (permissionChecker.isCompanyAdmin()) {
342                            return filteredRoles;
343                    }
344    
345                    itr = filteredRoles.iterator();
346    
347                    while (itr.hasNext()) {
348                            Role role = itr.next();
349    
350                            if (!RolePermissionUtil.contains(
351                                            permissionChecker, role.getRoleId(),
352                                            ActionKeys.ASSIGN_MEMBERS)) {
353    
354                                    itr.remove();
355                            }
356                    }
357    
358                    return filteredRoles;
359            }
360    
361            @Override
362            public long[] filterUnsetGroupUserIds(
363                            PermissionChecker permissionChecker, long groupId, long[] userIds)
364                    throws PortalException {
365    
366                    long[] filteredUserIds = userIds;
367    
368                    for (long userId : userIds) {
369                            if (SiteMembershipPolicyUtil.isMembershipProtected(
370                                            permissionChecker, userId, groupId)) {
371    
372                                    filteredUserIds = ArrayUtil.remove(filteredUserIds, userId);
373                            }
374                    }
375    
376                    return filteredUserIds;
377            }
378    
379            @Override
380            public long[] filterUnsetOrganizationUserIds(
381                            PermissionChecker permissionChecker, long organizationId,
382                            long[] userIds)
383                    throws PortalException {
384    
385                    long[] filteredUserIds = userIds;
386    
387                    for (long userId : userIds) {
388                            if (OrganizationMembershipPolicyUtil.isMembershipProtected(
389                                            permissionChecker, userId, organizationId)) {
390    
391                                    filteredUserIds = ArrayUtil.remove(filteredUserIds, userId);
392                            }
393                    }
394    
395                    return filteredUserIds;
396            }
397    
398            @Override
399            public List<UserGroupRole> filterUserGroupRoles(
400                            PermissionChecker permissionChecker,
401                            List<UserGroupRole> userGroupRoles)
402                    throws PortalException {
403    
404                    List<UserGroupRole> filteredUserGroupRoles = ListUtil.copy(
405                            userGroupRoles);
406    
407                    Iterator<UserGroupRole> itr = filteredUserGroupRoles.iterator();
408    
409                    while (itr.hasNext()) {
410                            UserGroupRole userGroupRole = itr.next();
411    
412                            Role role = userGroupRole.getRole();
413    
414                            String roleName = role.getName();
415    
416                            if (roleName.equals(RoleConstants.ORGANIZATION_USER) ||
417                                    roleName.equals(RoleConstants.SITE_MEMBER)) {
418    
419                                    itr.remove();
420                            }
421                    }
422    
423                    if (permissionChecker.isCompanyAdmin()) {
424                            return filteredUserGroupRoles;
425                    }
426    
427                    itr = filteredUserGroupRoles.iterator();
428    
429                    while (itr.hasNext()) {
430                            UserGroupRole userGroupRole = itr.next();
431    
432                            if (!UserGroupRolePermissionUtil.contains(
433                                            permissionChecker, userGroupRole.getGroupId(),
434                                            userGroupRole.getRoleId())) {
435    
436                                    itr.remove();
437                            }
438                    }
439    
440                    return filteredUserGroupRoles;
441            }
442    
443            @Override
444            public List<UserGroup> filterUserGroups(
445                    PermissionChecker permissionChecker, List<UserGroup> userGroups) {
446    
447                    if (permissionChecker.isCompanyAdmin()) {
448                            return userGroups;
449                    }
450    
451                    List<UserGroup> filteredUserGroups = ListUtil.copy(userGroups);
452    
453                    Iterator<UserGroup> itr = filteredUserGroups.iterator();
454    
455                    while (itr.hasNext()) {
456                            UserGroup userGroup = itr.next();
457    
458                            if (!UserGroupPermissionUtil.contains(
459                                            permissionChecker, userGroup.getUserGroupId(),
460                                            ActionKeys.ASSIGN_MEMBERS)) {
461    
462                                    itr.remove();
463                            }
464                    }
465    
466                    return filteredUserGroups;
467            }
468    
469            @Override
470            public List<Address> getAddresses(ActionRequest actionRequest) {
471                    return getAddresses(actionRequest, Collections.<Address>emptyList());
472            }
473    
474            @Override
475            public List<Address> getAddresses(
476                    ActionRequest actionRequest, List<Address> defaultAddresses) {
477    
478                    String addressesIndexesString = actionRequest.getParameter(
479                            "addressesIndexes");
480    
481                    if (addressesIndexesString == null) {
482                            return defaultAddresses;
483                    }
484    
485                    List<Address> addresses = new ArrayList<Address>();
486    
487                    int[] addressesIndexes = StringUtil.split(addressesIndexesString, 0);
488    
489                    int addressPrimary = ParamUtil.getInteger(
490                            actionRequest, "addressPrimary");
491    
492                    for (int addressesIndex : addressesIndexes) {
493                            long addressId = ParamUtil.getLong(
494                                    actionRequest, "addressId" + addressesIndex);
495    
496                            String street1 = ParamUtil.getString(
497                                    actionRequest, "addressStreet1_" + addressesIndex);
498                            String street2 = ParamUtil.getString(
499                                    actionRequest, "addressStreet2_" + addressesIndex);
500                            String street3 = ParamUtil.getString(
501                                    actionRequest, "addressStreet3_" + addressesIndex);
502                            String city = ParamUtil.getString(
503                                    actionRequest, "addressCity" + addressesIndex);
504                            String zip = ParamUtil.getString(
505                                    actionRequest, "addressZip" + addressesIndex);
506                            long countryId = ParamUtil.getLong(
507                                    actionRequest, "addressCountryId" + addressesIndex);
508    
509                            if (Validator.isNull(street1) && Validator.isNull(street2) &&
510                                    Validator.isNull(street3) && Validator.isNull(city) &&
511                                    Validator.isNull(zip) && (countryId == 0)) {
512    
513                                    continue;
514                            }
515    
516                            long regionId = ParamUtil.getLong(
517                                    actionRequest, "addressRegionId" + addressesIndex);
518                            int typeId = ParamUtil.getInteger(
519                                    actionRequest, "addressTypeId" + addressesIndex);
520                            boolean mailing = ParamUtil.getBoolean(
521                                    actionRequest, "addressMailing" + addressesIndex);
522    
523                            boolean primary = false;
524    
525                            if (addressesIndex == addressPrimary) {
526                                    primary = true;
527                            }
528    
529                            Address address = AddressLocalServiceUtil.createAddress(addressId);
530    
531                            address.setStreet1(street1);
532                            address.setStreet2(street2);
533                            address.setStreet3(street3);
534                            address.setCity(city);
535                            address.setZip(zip);
536                            address.setRegionId(regionId);
537                            address.setCountryId(countryId);
538                            address.setTypeId(typeId);
539                            address.setMailing(mailing);
540                            address.setPrimary(primary);
541    
542                            addresses.add(address);
543                    }
544    
545                    return addresses;
546            }
547    
548            @Override
549            public List<EmailAddress> getEmailAddresses(ActionRequest actionRequest) {
550                    return getEmailAddresses(
551                            actionRequest, Collections.<EmailAddress>emptyList());
552            }
553    
554            @Override
555            public List<EmailAddress> getEmailAddresses(
556                    ActionRequest actionRequest, List<EmailAddress> defaultEmailAddresses) {
557    
558                    String emailAddressesIndexesString = actionRequest.getParameter(
559                            "emailAddressesIndexes");
560    
561                    if (emailAddressesIndexesString == null) {
562                            return defaultEmailAddresses;
563                    }
564    
565                    List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
566    
567                    int[] emailAddressesIndexes = StringUtil.split(
568                            emailAddressesIndexesString, 0);
569    
570                    int emailAddressPrimary = ParamUtil.getInteger(
571                            actionRequest, "emailAddressPrimary");
572    
573                    for (int emailAddressesIndex : emailAddressesIndexes) {
574                            long emailAddressId = ParamUtil.getLong(
575                                    actionRequest, "emailAddressId" + emailAddressesIndex);
576    
577                            String address = ParamUtil.getString(
578                                    actionRequest, "emailAddressAddress" + emailAddressesIndex);
579    
580                            if (Validator.isNull(address)) {
581                                    continue;
582                            }
583    
584                            int typeId = ParamUtil.getInteger(
585                                    actionRequest, "emailAddressTypeId" + emailAddressesIndex);
586    
587                            boolean primary = false;
588    
589                            if (emailAddressesIndex == emailAddressPrimary) {
590                                    primary = true;
591                            }
592    
593                            EmailAddress emailAddress =
594                                    EmailAddressLocalServiceUtil.createEmailAddress(emailAddressId);
595    
596                            emailAddress.setAddress(address);
597                            emailAddress.setTypeId(typeId);
598                            emailAddress.setPrimary(primary);
599    
600                            emailAddresses.add(emailAddress);
601                    }
602    
603                    return emailAddresses;
604            }
605    
606            @Override
607            public long[] getGroupIds(PortletRequest portletRequest)
608                    throws PortalException {
609    
610                    User user = PortalUtil.getSelectedUser(portletRequest);
611    
612                    if (user == null) {
613                            return null;
614                    }
615    
616                    return getRequestPrimaryKeys(
617                            portletRequest, user.getGroupIds(), "addGroupIds",
618                            "deleteGroupIds");
619            }
620    
621            @Override
622            public OrderByComparator<Group> getGroupOrderByComparator(
623                    String orderByCol, String orderByType) {
624    
625                    boolean orderByAsc = false;
626    
627                    if (orderByType.equals("asc")) {
628                            orderByAsc = true;
629                    }
630    
631                    OrderByComparator<Group> orderByComparator = null;
632    
633                    if (orderByCol.equals("name")) {
634                            orderByComparator = new GroupNameComparator(orderByAsc);
635                    }
636                    else if (orderByCol.equals("type")) {
637                            orderByComparator = new GroupTypeComparator(orderByAsc);
638                    }
639                    else {
640                            orderByComparator = new GroupNameComparator(orderByAsc);
641                    }
642    
643                    return orderByComparator;
644            }
645    
646            @Override
647            public Long[] getOrganizationIds(List<Organization> organizations) {
648                    if ((organizations == null) || organizations.isEmpty()) {
649                            return new Long[0];
650                    }
651    
652                    Long[] organizationIds = new Long[organizations.size()];
653    
654                    for (int i = 0; i < organizations.size(); i++) {
655                            Organization organization = organizations.get(i);
656    
657                            organizationIds[i] = new Long(organization.getOrganizationId());
658                    }
659    
660                    return organizationIds;
661            }
662    
663            @Override
664            public long[] getOrganizationIds(PortletRequest portletRequest)
665                    throws PortalException {
666    
667                    User user = PortalUtil.getSelectedUser(portletRequest);
668    
669                    if (user == null) {
670                            return null;
671                    }
672    
673                    return getRequestPrimaryKeys(
674                            portletRequest, user.getOrganizationIds(), "addOrganizationIds",
675                            "deleteOrganizationIds");
676            }
677    
678            @Override
679            public OrderByComparator<Organization> getOrganizationOrderByComparator(
680                    String orderByCol, String orderByType) {
681    
682                    boolean orderByAsc = false;
683    
684                    if (orderByType.equals("asc")) {
685                            orderByAsc = true;
686                    }
687    
688                    OrderByComparator<Organization> orderByComparator = null;
689    
690                    if (orderByCol.equals("name")) {
691                            orderByComparator = new OrganizationNameComparator(orderByAsc);
692                    }
693                    else if (orderByCol.equals("type")) {
694                            orderByComparator = new OrganizationTypeComparator(orderByAsc);
695                    }
696                    else {
697                            orderByComparator = new OrganizationNameComparator(orderByAsc);
698                    }
699    
700                    return orderByComparator;
701            }
702    
703            @Override
704            public List<Organization> getOrganizations(Hits hits)
705                    throws PortalException {
706    
707                    List<Document> documents = hits.toList();
708    
709                    List<Organization> organizations = new ArrayList<Organization>(
710                            documents.size());
711    
712                    for (Document document : documents) {
713                            long organizationId = GetterUtil.getLong(
714                                    document.get(Field.ORGANIZATION_ID));
715    
716                            Organization organization =
717                                    OrganizationLocalServiceUtil.fetchOrganization(organizationId);
718    
719                            if (organization == null) {
720                                    organizations = null;
721    
722                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
723                                            Organization.class);
724    
725                                    long companyId = GetterUtil.getLong(
726                                            document.get(Field.COMPANY_ID));
727    
728                                    indexer.delete(companyId, document.getUID());
729                            }
730                            else if (organizations != null) {
731                                    organizations.add(organization);
732                            }
733                    }
734    
735                    return organizations;
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 {
870    
871                    User user = PortalUtil.getSelectedUser(portletRequest);
872    
873                    if (user == null) {
874                            return null;
875                    }
876    
877                    return getRequestPrimaryKeys(
878                            portletRequest, user.getRoleIds(), "addRoleIds", "deleteRoleIds");
879            }
880    
881            @Override
882            public OrderByComparator<Role> getRoleOrderByComparator(
883                    String orderByCol, String orderByType) {
884    
885                    boolean orderByAsc = false;
886    
887                    if (orderByType.equals("asc")) {
888                            orderByAsc = true;
889                    }
890    
891                    OrderByComparator<Role> orderByComparator = null;
892    
893                    if (orderByCol.equals("name")) {
894                            orderByComparator = new RoleNameComparator(orderByAsc);
895                    }
896                    else if (orderByCol.equals("description")) {
897                            orderByComparator = new RoleDescriptionComparator(orderByAsc);
898                    }
899                    else if (orderByCol.equals("type")) {
900                            orderByComparator = new RoleTypeComparator(orderByAsc);
901                    }
902                    else {
903                            orderByComparator = new RoleNameComparator(orderByAsc);
904                    }
905    
906                    return orderByComparator;
907            }
908    
909            @Override
910            public <T> String getUserColumnText(
911                    Locale locale, List<? extends T> list, Accessor<T, String> accessor,
912                    int count) {
913    
914                    String result = ListUtil.toString(
915                            list, accessor, StringPool.COMMA_AND_SPACE);
916    
917                    if (list.size() < count) {
918                            result += StringPool.SPACE + LanguageUtil.format(
919                                    locale, "and-x-more", String.valueOf(count - list.size()),
920                                    false);
921                    }
922    
923                    return result;
924            }
925    
926            @Override
927            public long[] getUserGroupIds(PortletRequest portletRequest)
928                    throws PortalException {
929    
930                    User user = PortalUtil.getSelectedUser(portletRequest);
931    
932                    if (user == null) {
933                            return null;
934                    }
935    
936                    return getRequestPrimaryKeys(
937                            portletRequest, user.getUserGroupIds(), "addUserGroupIds",
938                            "deleteUserGroupIds");
939            }
940    
941            @Override
942            public OrderByComparator<UserGroup> getUserGroupOrderByComparator(
943                    String orderByCol, String orderByType) {
944    
945                    boolean orderByAsc = false;
946    
947                    if (orderByType.equals("asc")) {
948                            orderByAsc = true;
949                    }
950    
951                    OrderByComparator<UserGroup> orderByComparator = null;
952    
953                    if (orderByCol.equals("name")) {
954                            orderByComparator = new UserGroupNameComparator(orderByAsc);
955                    }
956                    else if (orderByCol.equals("description")) {
957                            orderByComparator = new UserGroupDescriptionComparator(orderByAsc);
958                    }
959                    else {
960                            orderByComparator = new UserGroupNameComparator(orderByAsc);
961                    }
962    
963                    return orderByComparator;
964            }
965    
966            @Override
967            public List<UserGroupRole> getUserGroupRoles(PortletRequest portletRequest)
968                    throws PortalException {
969    
970                    User user = PortalUtil.getSelectedUser(portletRequest);
971    
972                    if (user == null) {
973                            return Collections.emptyList();
974                    }
975    
976                    Set<UserGroupRole> userGroupRoles =
977                            new HashSet<UserGroupRole>(
978                                    UserGroupRoleLocalServiceUtil.getUserGroupRoles(
979                                            user.getUserId()));
980    
981                    userGroupRoles.addAll(
982                            getUserGroupRoles(
983                                    portletRequest, user, "addGroupRolesGroupIds",
984                                    "addGroupRolesRoleIds"));
985                    userGroupRoles.removeAll(
986                            getUserGroupRoles(
987                                    portletRequest, user, "deleteGroupRolesGroupIds",
988                                    "deleteGroupRolesRoleIds"));
989    
990                    return new ArrayList<UserGroupRole>(userGroupRoles);
991            }
992    
993            @Override
994            public List<UserGroup> getUserGroups(Hits hits) throws PortalException {
995                    List<Document> documents = hits.toList();
996    
997                    List<UserGroup> userGroups = new ArrayList<UserGroup>(documents.size());
998    
999                    for (Document document : documents) {
1000                            long userGroupId = GetterUtil.getLong(
1001                                    document.get(Field.USER_GROUP_ID));
1002    
1003                            UserGroup userGroup = UserGroupLocalServiceUtil.fetchUserGroup(
1004                                    userGroupId);
1005    
1006                            if (userGroup == null) {
1007                                    userGroups = null;
1008    
1009                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
1010                                            UserGroup.class);
1011    
1012                                    long companyId = GetterUtil.getLong(
1013                                            document.get(Field.COMPANY_ID));
1014    
1015                                    indexer.delete(companyId, document.getUID());
1016                            }
1017                            else if (userGroups != null) {
1018                                    userGroups.add(userGroup);
1019                            }
1020                    }
1021    
1022                    return userGroups;
1023            }
1024    
1025            @Override
1026            public OrderByComparator<User> getUserOrderByComparator(
1027                    String orderByCol, String orderByType) {
1028    
1029                    boolean orderByAsc = false;
1030    
1031                    if (orderByType.equals("asc")) {
1032                            orderByAsc = true;
1033                    }
1034    
1035                    OrderByComparator<User> orderByComparator = null;
1036    
1037                    if (orderByCol.equals("email-address")) {
1038                            orderByComparator = new UserEmailAddressComparator(orderByAsc);
1039                    }
1040                    else if (orderByCol.equals("first-name")) {
1041                            orderByComparator = new UserFirstNameComparator(orderByAsc);
1042                    }
1043                    else if (orderByCol.equals("job-title")) {
1044                            orderByComparator = new UserJobTitleComparator(orderByAsc);
1045                    }
1046                    else if (orderByCol.equals("last-name")) {
1047                            orderByComparator = new UserLastNameComparator(orderByAsc);
1048                    }
1049                    else if (orderByCol.equals("screen-name")) {
1050                            orderByComparator = new UserScreenNameComparator(orderByAsc);
1051                    }
1052                    else {
1053                            orderByComparator = new UserLastNameComparator(orderByAsc);
1054                    }
1055    
1056                    return orderByComparator;
1057            }
1058    
1059            @Override
1060            public List<User> getUsers(Hits hits) throws PortalException {
1061                    List<Document> documents = hits.toList();
1062    
1063                    List<User> users = new ArrayList<User>(documents.size());
1064    
1065                    for (Document document : documents) {
1066                            long userId = GetterUtil.getLong(document.get(Field.USER_ID));
1067    
1068                            User user = UserLocalServiceUtil.fetchUser(userId);
1069    
1070                            if (user == null) {
1071                                    users = null;
1072    
1073                                    Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
1074    
1075                                    long companyId = GetterUtil.getLong(
1076                                            document.get(Field.COMPANY_ID));
1077    
1078                                    indexer.delete(companyId, document.getUID());
1079                            }
1080                            else if (users != null) {
1081                                    users.add(user);
1082                            }
1083                    }
1084    
1085                    return users;
1086            }
1087    
1088            @Override
1089            public List<Website> getWebsites(ActionRequest actionRequest) {
1090                    return getWebsites(actionRequest, Collections.<Website>emptyList());
1091            }
1092    
1093            @Override
1094            public List<Website> getWebsites(
1095                    ActionRequest actionRequest, List<Website> defaultWebsites) {
1096    
1097                    String websitesIndexesString = actionRequest.getParameter(
1098                            "websitesIndexes");
1099    
1100                    if (websitesIndexesString == null) {
1101                            return defaultWebsites;
1102                    }
1103    
1104                    List<Website> websites = new ArrayList<Website>();
1105    
1106                    int[] websitesIndexes = StringUtil.split(websitesIndexesString, 0);
1107    
1108                    int websitePrimary = ParamUtil.getInteger(
1109                            actionRequest, "websitePrimary");
1110    
1111                    for (int websitesIndex : websitesIndexes) {
1112                            long websiteId = ParamUtil.getLong(
1113                                    actionRequest, "websiteId" + websitesIndex);
1114    
1115                            String url = ParamUtil.getString(
1116                                    actionRequest, "websiteUrl" + websitesIndex);
1117    
1118                            if (Validator.isNull(url)) {
1119                                    continue;
1120                            }
1121    
1122                            int typeId = ParamUtil.getInteger(
1123                                    actionRequest, "websiteTypeId" + websitesIndex);
1124    
1125                            boolean primary = false;
1126    
1127                            if (websitesIndex == websitePrimary) {
1128                                    primary = true;
1129                            }
1130    
1131                            Website website = WebsiteLocalServiceUtil.createWebsite(websiteId);
1132    
1133                            website.setUrl(url);
1134                            website.setTypeId(typeId);
1135                            website.setPrimary(primary);
1136    
1137                            websites.add(website);
1138                    }
1139    
1140                    return websites;
1141            }
1142    
1143            /**
1144             * @deprecated As of 6.2.0, replaced by {@link
1145             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1146             *             String)}
1147             */
1148            @Deprecated
1149            @Override
1150            public boolean hasUpdateEmailAddress(
1151                            PermissionChecker permissionChecker, User user)
1152                    throws PortalException {
1153    
1154                    return hasUpdateFieldPermission(
1155                            permissionChecker, null, user, "emailAddress");
1156            }
1157    
1158            @Override
1159            public boolean hasUpdateFieldPermission(
1160                            PermissionChecker permissionChecker, User updatingUser,
1161                            User updatedUser, String field)
1162                    throws PortalException {
1163    
1164                    if (updatedUser == null) {
1165                            return true;
1166                    }
1167    
1168                    if (updatingUser == null) {
1169                            long updatingUserId = PrincipalThreadLocal.getUserId();
1170    
1171                            if (updatingUserId > 0) {
1172                                    updatingUser = UserLocalServiceUtil.fetchUserById(
1173                                            updatingUserId);
1174                            }
1175                    }
1176    
1177                    if ((updatingUser != null) && !updatingUser.equals(updatedUser) &&
1178                            UserPermissionUtil.contains(
1179                                    permissionChecker, updatingUser.getUserId(),
1180                                    ActionKeys.UPDATE_USER)) {
1181    
1182                            return true;
1183                    }
1184    
1185                    for (String userType : PropsValues.FIELD_EDITABLE_USER_TYPES) {
1186                            if (userType.equals("user-with-mx") && updatedUser.hasCompanyMx()) {
1187                                    return true;
1188                            }
1189    
1190                            if (userType.equals("user-without-mx") &&
1191                                    !updatedUser.hasCompanyMx()) {
1192    
1193                                    return true;
1194                            }
1195                    }
1196    
1197                    for (String roleName : PropsValues.FIELD_EDITABLE_ROLES) {
1198                            Role role = RoleLocalServiceUtil.fetchRole(
1199                                    updatedUser.getCompanyId(), roleName);
1200    
1201                            if ((role != null) &&
1202                                    RoleLocalServiceUtil.hasUserRole(
1203                                            updatedUser.getUserId(), role.getRoleId())) {
1204    
1205                                    return true;
1206                            }
1207                    }
1208    
1209                    String emailAddress = updatedUser.getEmailAddress();
1210    
1211                    for (String domainName : PropsValues.FIELD_EDITABLE_DOMAINS) {
1212                            if (emailAddress.endsWith(domainName)) {
1213                                    return true;
1214                            }
1215                    }
1216    
1217                    String[] fieldEditableDomainNames = PropsUtil.getArray(
1218                            PropsKeys.FIELD_EDITABLE_DOMAINS, new Filter(field));
1219    
1220                    for (String domainName : fieldEditableDomainNames) {
1221                            if (emailAddress.endsWith(domainName)) {
1222                                    return true;
1223                            }
1224                    }
1225    
1226                    return false;
1227            }
1228    
1229            /**
1230             * @deprecated As of 6.2.0, replaced by {@link
1231             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1232             *             String)}
1233             */
1234            @Deprecated
1235            @Override
1236            public boolean hasUpdateFieldPermission(User user, String field)
1237                    throws PortalException {
1238    
1239                    PermissionChecker permissionChecker =
1240                            PermissionThreadLocal.getPermissionChecker();
1241    
1242                    return hasUpdateFieldPermission(permissionChecker, null, user, field);
1243            }
1244    
1245            /**
1246             * @deprecated As of 6.2.0, replaced by {@link
1247             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1248             *             String)}
1249             */
1250            @Deprecated
1251            @Override
1252            public boolean hasUpdateScreenName(
1253                            PermissionChecker permissionChecker, User user)
1254                    throws PortalException {
1255    
1256                    return hasUpdateFieldPermission(
1257                            permissionChecker, null, user, "screenName");
1258            }
1259    
1260            @Override
1261            public long[] removeRequiredRoles(long userId, long[] roleIds)
1262                    throws PortalException {
1263    
1264                    User user = UserLocalServiceUtil.getUser(userId);
1265    
1266                    return removeRequiredRoles(user, roleIds);
1267            }
1268    
1269            @Override
1270            public long[] removeRequiredRoles(User user, long[] roleIds)
1271                    throws PortalException {
1272    
1273                    Role role = RoleLocalServiceUtil.getRole(
1274                            user.getCompanyId(), RoleConstants.USER);
1275    
1276                    roleIds = ArrayUtil.remove(roleIds, role.getRoleId());
1277    
1278                    return roleIds;
1279            }
1280    
1281            @Override
1282            public void updateAddresses(
1283                            String className, long classPK, List<Address> addresses)
1284                    throws PortalException {
1285    
1286                    Set<Long> addressIds = new HashSet<Long>();
1287    
1288                    for (Address address : addresses) {
1289                            long addressId = address.getAddressId();
1290    
1291                            String street1 = address.getStreet1();
1292                            String street2 = address.getStreet2();
1293                            String street3 = address.getStreet3();
1294                            String city = address.getCity();
1295                            String zip = address.getZip();
1296                            long regionId = address.getRegionId();
1297                            long countryId = address.getCountryId();
1298                            int typeId = address.getTypeId();
1299                            boolean mailing = address.isMailing();
1300                            boolean primary = address.isPrimary();
1301    
1302                            if (addressId <= 0) {
1303                                    address = AddressServiceUtil.addAddress(
1304                                            className, classPK, street1, street2, street3, city, zip,
1305                                            regionId, countryId, typeId, mailing, primary,
1306                                            new ServiceContext());
1307    
1308                                    addressId = address.getAddressId();
1309                            }
1310                            else {
1311                                    AddressServiceUtil.updateAddress(
1312                                            addressId, street1, street2, street3, city, zip, regionId,
1313                                            countryId, typeId, mailing, primary);
1314                            }
1315    
1316                            addressIds.add(addressId);
1317                    }
1318    
1319                    addresses = AddressServiceUtil.getAddresses(className, classPK);
1320    
1321                    for (Address address : addresses) {
1322                            if (!addressIds.contains(address.getAddressId())) {
1323                                    AddressServiceUtil.deleteAddress(address.getAddressId());
1324                            }
1325                    }
1326            }
1327    
1328            @Override
1329            public void updateEmailAddresses(
1330                            String className, long classPK, List<EmailAddress> emailAddresses)
1331                    throws PortalException {
1332    
1333                    Set<Long> emailAddressIds = new HashSet<Long>();
1334    
1335                    for (EmailAddress emailAddress : emailAddresses) {
1336                            long emailAddressId = emailAddress.getEmailAddressId();
1337    
1338                            String address = emailAddress.getAddress();
1339                            int typeId = emailAddress.getTypeId();
1340                            boolean primary = emailAddress.isPrimary();
1341    
1342                            if (emailAddressId <= 0) {
1343                                    emailAddress = EmailAddressServiceUtil.addEmailAddress(
1344                                            className, classPK, address, typeId, primary,
1345                                            new ServiceContext());
1346    
1347                                    emailAddressId = emailAddress.getEmailAddressId();
1348                            }
1349                            else {
1350                                    EmailAddressServiceUtil.updateEmailAddress(
1351                                            emailAddressId, address, typeId, primary);
1352                            }
1353    
1354                            emailAddressIds.add(emailAddressId);
1355                    }
1356    
1357                    emailAddresses = EmailAddressServiceUtil.getEmailAddresses(
1358                            className, classPK);
1359    
1360                    for (EmailAddress emailAddress : emailAddresses) {
1361                            if (!emailAddressIds.contains(emailAddress.getEmailAddressId())) {
1362                                    EmailAddressServiceUtil.deleteEmailAddress(
1363                                            emailAddress.getEmailAddressId());
1364                            }
1365                    }
1366            }
1367    
1368            @Override
1369            public void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
1370                    throws PortalException {
1371    
1372                    Set<Long> orgLaborsIds = new HashSet<Long>();
1373    
1374                    for (OrgLabor orgLabor : orgLabors) {
1375                            long orgLaborId = orgLabor.getOrgLaborId();
1376    
1377                            int typeId = orgLabor.getTypeId();
1378                            int sunOpen = orgLabor.getSunOpen();
1379                            int sunClose = orgLabor.getSunClose();
1380                            int monOpen = orgLabor.getMonOpen();
1381                            int monClose = orgLabor.getMonClose();
1382                            int tueOpen = orgLabor.getTueOpen();
1383                            int tueClose = orgLabor.getTueClose();
1384                            int wedOpen = orgLabor.getWedOpen();
1385                            int wedClose = orgLabor.getWedClose();
1386                            int thuOpen = orgLabor.getThuOpen();
1387                            int thuClose = orgLabor.getThuClose();
1388                            int friOpen = orgLabor.getFriOpen();
1389                            int friClose = orgLabor.getFriClose();
1390                            int satOpen = orgLabor.getSatOpen();
1391                            int satClose = orgLabor.getSatClose();
1392    
1393                            if (orgLaborId <= 0) {
1394                                    orgLabor = OrgLaborServiceUtil.addOrgLabor(
1395                                            classPK, typeId, sunOpen, sunClose, monOpen, monClose,
1396                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1397                                            friOpen, friClose, satOpen, satClose);
1398    
1399                                    orgLaborId = orgLabor.getOrgLaborId();
1400                            }
1401                            else {
1402                                    OrgLaborServiceUtil.updateOrgLabor(
1403                                            orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
1404                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1405                                            friOpen, friClose, satOpen, satClose);
1406                            }
1407    
1408                            orgLaborsIds.add(orgLaborId);
1409                    }
1410    
1411                    orgLabors = OrgLaborServiceUtil.getOrgLabors(classPK);
1412    
1413                    for (OrgLabor orgLabor : orgLabors) {
1414                            if (!orgLaborsIds.contains(orgLabor.getOrgLaborId())) {
1415                                    OrgLaborServiceUtil.deleteOrgLabor(orgLabor.getOrgLaborId());
1416                            }
1417                    }
1418            }
1419    
1420            @Override
1421            public void updatePhones(String className, long classPK, List<Phone> phones)
1422                    throws PortalException {
1423    
1424                    Set<Long> phoneIds = new HashSet<Long>();
1425    
1426                    for (Phone phone : phones) {
1427                            long phoneId = phone.getPhoneId();
1428    
1429                            String number = phone.getNumber();
1430                            String extension = phone.getExtension();
1431                            int typeId = phone.getTypeId();
1432                            boolean primary = phone.isPrimary();
1433    
1434                            if (phoneId <= 0) {
1435                                    phone = PhoneServiceUtil.addPhone(
1436                                            className, classPK, number, extension, typeId, primary,
1437                                            new ServiceContext());
1438    
1439                                    phoneId = phone.getPhoneId();
1440                            }
1441                            else {
1442                                    PhoneServiceUtil.updatePhone(
1443                                            phoneId, number, extension, typeId, primary);
1444                            }
1445    
1446                            phoneIds.add(phoneId);
1447                    }
1448    
1449                    phones = PhoneServiceUtil.getPhones(className, classPK);
1450    
1451                    for (Phone phone : phones) {
1452                            if (!phoneIds.contains(phone.getPhoneId())) {
1453                                    PhoneServiceUtil.deletePhone(phone.getPhoneId());
1454                            }
1455                    }
1456            }
1457    
1458            @Override
1459            public void updateWebsites(
1460                            String className, long classPK, List<Website> websites)
1461                    throws PortalException {
1462    
1463                    Set<Long> websiteIds = new HashSet<Long>();
1464    
1465                    for (Website website : websites) {
1466                            long websiteId = website.getWebsiteId();
1467    
1468                            String url = website.getUrl();
1469                            int typeId = website.getTypeId();
1470                            boolean primary = website.isPrimary();
1471    
1472                            if (websiteId <= 0) {
1473                                    website = WebsiteServiceUtil.addWebsite(
1474                                            className, classPK, url, typeId, primary,
1475                                            new ServiceContext());
1476    
1477                                    websiteId = website.getWebsiteId();
1478                            }
1479                            else {
1480                                    WebsiteServiceUtil.updateWebsite(
1481                                            websiteId, url, typeId, primary);
1482                            }
1483    
1484                            websiteIds.add(websiteId);
1485                    }
1486    
1487                    websites = WebsiteServiceUtil.getWebsites(className, classPK);
1488    
1489                    for (Website website : websites) {
1490                            if (!websiteIds.contains(website.getWebsiteId())) {
1491                                    WebsiteServiceUtil.deleteWebsite(website.getWebsiteId());
1492                            }
1493                    }
1494            }
1495    
1496            protected long[] getRequestPrimaryKeys(
1497                    PortletRequest portletRequest, long[] currentPKs, String addParam,
1498                    String deleteParam) {
1499    
1500                    Set<Long> primaryKeys = SetUtil.fromArray(currentPKs);
1501    
1502                    long[] addPrimaryKeys = StringUtil.split(
1503                            ParamUtil.getString(portletRequest, addParam), 0L);
1504                    long[] deletePrimaryKeys = StringUtil.split(
1505                            ParamUtil.getString(portletRequest, deleteParam), 0L);
1506    
1507                    for (long addPrimaryKey : addPrimaryKeys) {
1508                            primaryKeys.add(addPrimaryKey);
1509                    }
1510    
1511                    for (long deletePrimaryKey : deletePrimaryKeys) {
1512                            primaryKeys.remove(deletePrimaryKey);
1513                    }
1514    
1515                    return ArrayUtil.toLongArray(primaryKeys);
1516            }
1517    
1518            protected Set<UserGroupRole> getUserGroupRoles(
1519                    PortletRequest portletRequest, User user, String groupIdsParam,
1520                    String roleIdsParam) {
1521    
1522                    Set<UserGroupRole> userGroupRoles = new HashSet<UserGroupRole>();
1523    
1524                    long[] groupRolesGroupIds = StringUtil.split(
1525                            ParamUtil.getString(portletRequest, groupIdsParam), 0L);
1526                    long[] groupRolesRoleIds = StringUtil.split(
1527                            ParamUtil.getString(portletRequest, roleIdsParam), 0L);
1528    
1529                    if (groupRolesGroupIds.length != groupRolesRoleIds.length) {
1530                            return userGroupRoles;
1531                    }
1532    
1533                    long userId = 0;
1534    
1535                    if (user != null) {
1536                            userId = user.getUserId();
1537                    }
1538    
1539                    for (int i = 0; i < groupRolesGroupIds.length; i++) {
1540                            if ((groupRolesGroupIds[i] == 0) || (groupRolesRoleIds[i] == 0)) {
1541                                    continue;
1542                            }
1543    
1544                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
1545                                    userId, groupRolesGroupIds[i], groupRolesRoleIds[i]);
1546    
1547                            UserGroupRole userGroupRole =
1548                                    UserGroupRoleLocalServiceUtil.createUserGroupRole(
1549                                            userGroupRolePK);
1550    
1551                            userGroupRoles.add(userGroupRole);
1552                    }
1553    
1554                    return userGroupRoles;
1555            }
1556    
1557    }