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