001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.usersadmin.util;
016    
017    import com.liferay.portal.NoSuchOrganizationException;
018    import com.liferay.portal.NoSuchUserGroupException;
019    import com.liferay.portal.kernel.configuration.Filter;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.Hits;
025    import com.liferay.portal.kernel.search.Indexer;
026    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.ListUtil;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.ParamUtil;
033    import com.liferay.portal.kernel.util.PropsKeys;
034    import com.liferay.portal.kernel.util.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                    User user = PortalUtil.getSelectedUser(portletRequest);
604    
605                    if (user == null) {
606                            return null;
607                    }
608    
609                    return getRequestPrimaryKeys(
610                            portletRequest, user.getGroupIds(), "addGroupIds",
611                            "deleteGroupIds");
612            }
613    
614            @Override
615            public OrderByComparator getGroupOrderByComparator(
616                    String orderByCol, String orderByType) {
617    
618                    boolean orderByAsc = false;
619    
620                    if (orderByType.equals("asc")) {
621                            orderByAsc = true;
622                    }
623    
624                    OrderByComparator orderByComparator = null;
625    
626                    if (orderByCol.equals("name")) {
627                            orderByComparator = new GroupNameComparator(orderByAsc);
628                    }
629                    else if (orderByCol.equals("type")) {
630                            orderByComparator = new GroupTypeComparator(orderByAsc);
631                    }
632                    else {
633                            orderByComparator = new GroupNameComparator(orderByAsc);
634                    }
635    
636                    return orderByComparator;
637            }
638    
639            @Override
640            public Long[] getOrganizationIds(List<Organization> organizations) {
641                    if ((organizations == null) || organizations.isEmpty()) {
642                            return new Long[0];
643                    }
644    
645                    Long[] organizationIds = new Long[organizations.size()];
646    
647                    for (int i = 0; i < organizations.size(); i++) {
648                            Organization organization = organizations.get(i);
649    
650                            organizationIds[i] = new Long(organization.getOrganizationId());
651                    }
652    
653                    return organizationIds;
654            }
655    
656            @Override
657            public long[] getOrganizationIds(PortletRequest portletRequest)
658                    throws PortalException, SystemException {
659    
660                    User user = PortalUtil.getSelectedUser(portletRequest);
661    
662                    if (user == null) {
663                            return null;
664                    }
665    
666                    return getRequestPrimaryKeys(
667                            portletRequest, user.getOrganizationIds(), "addOrganizationIds",
668                            "deleteOrganizationIds");
669            }
670    
671            @Override
672            public OrderByComparator getOrganizationOrderByComparator(
673                    String orderByCol, String orderByType) {
674    
675                    boolean orderByAsc = false;
676    
677                    if (orderByType.equals("asc")) {
678                            orderByAsc = true;
679                    }
680    
681                    OrderByComparator orderByComparator = null;
682    
683                    if (orderByCol.equals("name")) {
684                            orderByComparator = new OrganizationNameComparator(orderByAsc);
685                    }
686                    else if (orderByCol.equals("type")) {
687                            orderByComparator = new OrganizationTypeComparator(orderByAsc);
688                    }
689                    else {
690                            orderByComparator = new OrganizationNameComparator(orderByAsc);
691                    }
692    
693                    return orderByComparator;
694            }
695    
696            @Override
697            public Tuple getOrganizations(Hits hits)
698                    throws PortalException, SystemException {
699    
700                    List<Organization> organizations = new ArrayList<Organization>();
701                    boolean corruptIndex = false;
702    
703                    List<Document> documents = hits.toList();
704    
705                    for (Document document : documents) {
706                            long organizationId = GetterUtil.getLong(
707                                    document.get(Field.ORGANIZATION_ID));
708    
709                            try {
710                                    Organization organization =
711                                            OrganizationLocalServiceUtil.getOrganization(
712                                                    organizationId);
713    
714                                    organizations.add(organization);
715                            }
716                            catch (NoSuchOrganizationException nsoe) {
717                                    corruptIndex = true;
718    
719                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
720                                            Organization.class);
721    
722                                    long companyId = GetterUtil.getLong(
723                                            document.get(Field.COMPANY_ID));
724    
725                                    indexer.delete(companyId, document.getUID());
726                            }
727                    }
728    
729                    return new Tuple(organizations, corruptIndex);
730            }
731    
732            @Override
733            public List<OrgLabor> getOrgLabors(ActionRequest actionRequest) {
734                    List<OrgLabor> orgLabors = new ArrayList<OrgLabor>();
735    
736                    int[] orgLaborsIndexes = StringUtil.split(
737                            ParamUtil.getString(actionRequest, "orgLaborsIndexes"), 0);
738    
739                    for (int orgLaborsIndex : orgLaborsIndexes) {
740                            long orgLaborId = ParamUtil.getLong(
741                                    actionRequest, "orgLaborId" + orgLaborsIndex);
742    
743                            int typeId = ParamUtil.getInteger(
744                                    actionRequest, "orgLaborTypeId" + orgLaborsIndex, -1);
745    
746                            if (typeId == -1) {
747                                    continue;
748                            }
749    
750                            int sunOpen = ParamUtil.getInteger(
751                                    actionRequest, "sunOpen" + orgLaborsIndex, -1);
752                            int sunClose = ParamUtil.getInteger(
753                                    actionRequest, "sunClose" + orgLaborsIndex, -1);
754                            int monOpen = ParamUtil.getInteger(
755                                    actionRequest, "monOpen" + orgLaborsIndex, -1);
756                            int monClose = ParamUtil.getInteger(
757                                    actionRequest, "monClose" + orgLaborsIndex, -1);
758                            int tueOpen = ParamUtil.getInteger(
759                                    actionRequest, "tueOpen" + orgLaborsIndex, -1);
760                            int tueClose = ParamUtil.getInteger(
761                                    actionRequest, "tueClose" + orgLaborsIndex, -1);
762                            int wedOpen = ParamUtil.getInteger(
763                                    actionRequest, "wedOpen" + orgLaborsIndex, -1);
764                            int wedClose = ParamUtil.getInteger(
765                                    actionRequest, "wedClose" + orgLaborsIndex, -1);
766                            int thuOpen = ParamUtil.getInteger(
767                                    actionRequest, "thuOpen" + orgLaborsIndex, -1);
768                            int thuClose = ParamUtil.getInteger(
769                                    actionRequest, "thuClose" + orgLaborsIndex, -1);
770                            int friOpen = ParamUtil.getInteger(
771                                    actionRequest, "friOpen" + orgLaborsIndex, -1);
772                            int friClose = ParamUtil.getInteger(
773                                    actionRequest, "friClose" + orgLaborsIndex, -1);
774                            int satOpen = ParamUtil.getInteger(
775                                    actionRequest, "satOpen" + orgLaborsIndex, -1);
776                            int satClose = ParamUtil.getInteger(
777                                    actionRequest, "satClose" + orgLaborsIndex, -1);
778    
779                            OrgLabor orgLabor = OrgLaborLocalServiceUtil.createOrgLabor(
780                                    orgLaborId);
781    
782                            orgLabor.setTypeId(typeId);
783                            orgLabor.setSunOpen(sunOpen);
784                            orgLabor.setSunClose(sunClose);
785                            orgLabor.setMonOpen(monOpen);
786                            orgLabor.setMonClose(monClose);
787                            orgLabor.setTueOpen(tueOpen);
788                            orgLabor.setTueClose(tueClose);
789                            orgLabor.setWedOpen(wedOpen);
790                            orgLabor.setWedClose(wedClose);
791                            orgLabor.setThuOpen(thuOpen);
792                            orgLabor.setThuClose(thuClose);
793                            orgLabor.setFriOpen(friOpen);
794                            orgLabor.setFriClose(friClose);
795                            orgLabor.setSatOpen(satOpen);
796                            orgLabor.setSatClose(satClose);
797    
798                            orgLabors.add(orgLabor);
799                    }
800    
801                    return orgLabors;
802            }
803    
804            @Override
805            public List<Phone> getPhones(ActionRequest actionRequest) {
806                    return getPhones(actionRequest, Collections.<Phone>emptyList());
807            }
808    
809            @Override
810            public List<Phone> getPhones(
811                    ActionRequest actionRequest, List<Phone> defaultPhones) {
812    
813                    String phonesIndexesString = actionRequest.getParameter(
814                            "phonesIndexes");
815    
816                    if (phonesIndexesString == null) {
817                            return defaultPhones;
818                    }
819    
820                    List<Phone> phones = new ArrayList<Phone>();
821    
822                    int[] phonesIndexes = StringUtil.split(phonesIndexesString, 0);
823    
824                    int phonePrimary = ParamUtil.getInteger(actionRequest, "phonePrimary");
825    
826                    for (int phonesIndex : phonesIndexes) {
827                            long phoneId = ParamUtil.getLong(
828                                    actionRequest, "phoneId" + phonesIndex);
829    
830                            String number = ParamUtil.getString(
831                                    actionRequest, "phoneNumber" + phonesIndex);
832                            String extension = ParamUtil.getString(
833                                    actionRequest, "phoneExtension" + phonesIndex);
834    
835                            if (Validator.isNull(number) && Validator.isNull(extension)) {
836                                    continue;
837                            }
838    
839                            int typeId = ParamUtil.getInteger(
840                                    actionRequest, "phoneTypeId" + phonesIndex);
841    
842                            boolean primary = false;
843    
844                            if (phonesIndex == phonePrimary) {
845                                    primary = true;
846                            }
847    
848                            Phone phone = PhoneLocalServiceUtil.createPhone(phoneId);
849    
850                            phone.setNumber(number);
851                            phone.setExtension(extension);
852                            phone.setTypeId(typeId);
853                            phone.setPrimary(primary);
854    
855                            phones.add(phone);
856                    }
857    
858                    return phones;
859            }
860    
861            @Override
862            public long[] getRoleIds(PortletRequest portletRequest)
863                    throws PortalException, SystemException {
864    
865                    User user = PortalUtil.getSelectedUser(portletRequest);
866    
867                    if (user == null) {
868                            return null;
869                    }
870    
871                    return getRequestPrimaryKeys(
872                            portletRequest, user.getRoleIds(), "addRoleIds", "deleteRoleIds");
873            }
874    
875            @Override
876            public OrderByComparator getRoleOrderByComparator(
877                    String orderByCol, String orderByType) {
878    
879                    boolean orderByAsc = false;
880    
881                    if (orderByType.equals("asc")) {
882                            orderByAsc = true;
883                    }
884    
885                    OrderByComparator orderByComparator = null;
886    
887                    if (orderByCol.equals("name")) {
888                            orderByComparator = new RoleNameComparator(orderByAsc);
889                    }
890                    else if (orderByCol.equals("description")) {
891                            orderByComparator = new RoleDescriptionComparator(orderByAsc);
892                    }
893                    else if (orderByCol.equals("type")) {
894                            orderByComparator = new RoleTypeComparator(orderByAsc);
895                    }
896                    else {
897                            orderByComparator = new RoleNameComparator(orderByAsc);
898                    }
899    
900                    return orderByComparator;
901            }
902    
903            @Override
904            public long[] getUserGroupIds(PortletRequest portletRequest)
905                    throws PortalException, SystemException {
906    
907                    User user = PortalUtil.getSelectedUser(portletRequest);
908    
909                    if (user == null) {
910                            return null;
911                    }
912    
913                    return getRequestPrimaryKeys(
914                            portletRequest, user.getUserGroupIds(), "addUserGroupIds",
915                            "deleteUserGroupIds");
916            }
917    
918            @Override
919            public OrderByComparator getUserGroupOrderByComparator(
920                    String orderByCol, String orderByType) {
921    
922                    boolean orderByAsc = false;
923    
924                    if (orderByType.equals("asc")) {
925                            orderByAsc = true;
926                    }
927    
928                    OrderByComparator orderByComparator = null;
929    
930                    if (orderByCol.equals("name")) {
931                            orderByComparator = new UserGroupNameComparator(orderByAsc);
932                    }
933                    else if (orderByCol.equals("description")) {
934                            orderByComparator = new UserGroupDescriptionComparator(orderByAsc);
935                    }
936                    else {
937                            orderByComparator = new UserGroupNameComparator(orderByAsc);
938                    }
939    
940                    return orderByComparator;
941            }
942    
943            @Override
944            public List<UserGroupRole> getUserGroupRoles(PortletRequest portletRequest)
945                    throws PortalException, SystemException {
946    
947                    User user = PortalUtil.getSelectedUser(portletRequest);
948    
949                    if (user == null) {
950                            return Collections.emptyList();
951                    }
952    
953                    Set<UserGroupRole> userGroupRoles =
954                            new HashSet<UserGroupRole>(
955                                    UserGroupRoleLocalServiceUtil.getUserGroupRoles(
956                                            user.getUserId()));
957    
958                    userGroupRoles.addAll(
959                            getUserGroupRoles(
960                                    portletRequest, user, "addGroupRolesGroupIds",
961                                    "addGroupRolesRoleIds"));
962                    userGroupRoles.removeAll(
963                            getUserGroupRoles(
964                                    portletRequest, user, "deleteGroupRolesGroupIds",
965                                    "deleteGroupRolesRoleIds"));
966    
967                    return new ArrayList<UserGroupRole>(userGroupRoles);
968            }
969    
970            @Override
971            public Tuple getUserGroups(Hits hits)
972                    throws PortalException, SystemException {
973    
974                    List<UserGroup> userGroups = new ArrayList<UserGroup>();
975                    boolean corruptIndex = false;
976    
977                    List<Document> documents = hits.toList();
978    
979                    for (Document document : documents) {
980                            long userGroupId = GetterUtil.getLong(
981                                    document.get(Field.USER_GROUP_ID));
982    
983                            try {
984                                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
985                                            userGroupId);
986    
987                                    userGroups.add(userGroup);
988                            }
989                            catch (NoSuchUserGroupException nsuge) {
990                                    corruptIndex = true;
991    
992                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
993                                            UserGroup.class);
994    
995                                    long companyId = GetterUtil.getLong(
996                                            document.get(Field.COMPANY_ID));
997    
998                                    indexer.delete(companyId, document.getUID());
999                            }
1000                    }
1001    
1002                    return new Tuple(userGroups, corruptIndex);
1003            }
1004    
1005            @Override
1006            public OrderByComparator getUserOrderByComparator(
1007                    String orderByCol, String orderByType) {
1008    
1009                    boolean orderByAsc = false;
1010    
1011                    if (orderByType.equals("asc")) {
1012                            orderByAsc = true;
1013                    }
1014    
1015                    OrderByComparator orderByComparator = null;
1016    
1017                    if (orderByCol.equals("email-address")) {
1018                            orderByComparator = new UserEmailAddressComparator(orderByAsc);
1019                    }
1020                    else if (orderByCol.equals("first-name")) {
1021                            orderByComparator = new UserFirstNameComparator(orderByAsc);
1022                    }
1023                    else if (orderByCol.equals("job-title")) {
1024                            orderByComparator = new UserJobTitleComparator(orderByAsc);
1025                    }
1026                    else if (orderByCol.equals("last-name")) {
1027                            orderByComparator = new UserLastNameComparator(orderByAsc);
1028                    }
1029                    else if (orderByCol.equals("screen-name")) {
1030                            orderByComparator = new UserScreenNameComparator(orderByAsc);
1031                    }
1032                    else {
1033                            orderByComparator = new UserLastNameComparator(orderByAsc);
1034                    }
1035    
1036                    return orderByComparator;
1037            }
1038    
1039            @Override
1040            public Tuple getUsers(Hits hits) throws PortalException, SystemException {
1041                    List<User> users = new ArrayList<User>();
1042                    boolean corruptIndex = false;
1043    
1044                    List<Document> documents = hits.toList();
1045    
1046                    for (Document document : documents) {
1047                            long userId = GetterUtil.getLong(document.get(Field.USER_ID));
1048    
1049                            User user = UserLocalServiceUtil.fetchUser(userId);
1050    
1051                            if (user != null) {
1052                                    users.add(user);
1053                            }
1054                            else {
1055                                    corruptIndex = true;
1056    
1057                                    Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
1058    
1059                                    long companyId = GetterUtil.getLong(
1060                                            document.get(Field.COMPANY_ID));
1061    
1062                                    indexer.delete(companyId, document.getUID());
1063                            }
1064                    }
1065    
1066                    return new Tuple(users, corruptIndex);
1067            }
1068    
1069            @Override
1070            public List<Website> getWebsites(ActionRequest actionRequest) {
1071                    return getWebsites(actionRequest, Collections.<Website>emptyList());
1072            }
1073    
1074            @Override
1075            public List<Website> getWebsites(
1076                    ActionRequest actionRequest, List<Website> defaultWebsites) {
1077    
1078                    String websitesIndexesString = actionRequest.getParameter(
1079                            "websitesIndexes");
1080    
1081                    if (websitesIndexesString == null) {
1082                            return defaultWebsites;
1083                    }
1084    
1085                    List<Website> websites = new ArrayList<Website>();
1086    
1087                    int[] websitesIndexes = StringUtil.split(websitesIndexesString, 0);
1088    
1089                    int websitePrimary = ParamUtil.getInteger(
1090                            actionRequest, "websitePrimary");
1091    
1092                    for (int websitesIndex : websitesIndexes) {
1093                            long websiteId = ParamUtil.getLong(
1094                                    actionRequest, "websiteId" + websitesIndex);
1095    
1096                            String url = ParamUtil.getString(
1097                                    actionRequest, "websiteUrl" + websitesIndex);
1098    
1099                            if (Validator.isNull(url)) {
1100                                    continue;
1101                            }
1102    
1103                            int typeId = ParamUtil.getInteger(
1104                                    actionRequest, "websiteTypeId" + websitesIndex);
1105    
1106                            boolean primary = false;
1107    
1108                            if (websitesIndex == websitePrimary) {
1109                                    primary = true;
1110                            }
1111    
1112                            Website website = WebsiteLocalServiceUtil.createWebsite(websiteId);
1113    
1114                            website.setUrl(url);
1115                            website.setTypeId(typeId);
1116                            website.setPrimary(primary);
1117    
1118                            websites.add(website);
1119                    }
1120    
1121                    return websites;
1122            }
1123    
1124            /**
1125             * @deprecated As of 6.2.0, replaced by {@link
1126             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1127             *             String)}
1128             */
1129            @Override
1130            public boolean hasUpdateEmailAddress(
1131                            PermissionChecker permissionChecker, User user)
1132                    throws PortalException, SystemException {
1133    
1134                    return hasUpdateFieldPermission(
1135                            permissionChecker, null, user, "emailAddress");
1136            }
1137    
1138            @Override
1139            public boolean hasUpdateFieldPermission(
1140                            PermissionChecker permissionChecker, User updatingUser,
1141                            User updatedUser, String field)
1142                    throws PortalException, SystemException {
1143    
1144                    if (updatedUser == null) {
1145                            return true;
1146                    }
1147    
1148                    if (updatingUser == null) {
1149                            long updatingUserId = PrincipalThreadLocal.getUserId();
1150    
1151                            if (updatingUserId > 0) {
1152                                    updatingUser = UserLocalServiceUtil.fetchUserById(
1153                                            updatingUserId);
1154                            }
1155                    }
1156    
1157                    if ((updatingUser != null) && !updatingUser.equals(updatedUser) &&
1158                            UserPermissionUtil.contains(
1159                                    permissionChecker, updatingUser.getUserId(),
1160                                    ActionKeys.UPDATE_USER)) {
1161    
1162                            return true;
1163                    }
1164    
1165                    for (String userType : PropsValues.FIELD_EDITABLE_USER_TYPES) {
1166                            if (userType.equals("user-with-mx") && updatedUser.hasCompanyMx()) {
1167                                    return true;
1168                            }
1169    
1170                            if (userType.equals("user-without-mx") &&
1171                                    !updatedUser.hasCompanyMx()) {
1172    
1173                                    return true;
1174                            }
1175                    }
1176    
1177                    for (String roleName : PropsValues.FIELD_EDITABLE_ROLES) {
1178                            Role role = RoleLocalServiceUtil.fetchRole(
1179                                    updatedUser.getCompanyId(), roleName);
1180    
1181                            if ((role != null) &&
1182                                    RoleLocalServiceUtil.hasUserRole(
1183                                            updatedUser.getUserId(), role.getRoleId())) {
1184    
1185                                    return true;
1186                            }
1187                    }
1188    
1189                    String emailAddress = updatedUser.getEmailAddress();
1190    
1191                    for (String domainName : PropsValues.FIELD_EDITABLE_DOMAINS) {
1192                            if (emailAddress.endsWith(domainName)) {
1193                                    return true;
1194                            }
1195                    }
1196    
1197                    String[] fieldEditableDomainNames = PropsUtil.getArray(
1198                            PropsKeys.FIELD_EDITABLE_DOMAINS, new Filter(field));
1199    
1200                    for (String domainName : fieldEditableDomainNames) {
1201                            if (domainName.equals(StringPool.STAR) ||
1202                                    emailAddress.endsWith(domainName)) {
1203    
1204                                    return true;
1205                            }
1206                    }
1207    
1208                    return false;
1209            }
1210    
1211            /**
1212             * @deprecated As of 6.2.0, replaced by {@link
1213             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1214             *             String)}
1215             */
1216            @Override
1217            public boolean hasUpdateFieldPermission(User user, String field)
1218                    throws PortalException, SystemException {
1219    
1220                    PermissionChecker permissionChecker =
1221                            PermissionThreadLocal.getPermissionChecker();
1222    
1223                    return hasUpdateFieldPermission(permissionChecker, null, user, field);
1224            }
1225    
1226            /**
1227             * @deprecated As of 6.2.0, replaced by {@link
1228             *             #hasUpdateFieldPermission(PermissionChecker, User, User,
1229             *             String)}
1230             */
1231            @Deprecated
1232            @Override
1233            public boolean hasUpdateScreenName(
1234                            PermissionChecker permissionChecker, User user)
1235                    throws PortalException, SystemException {
1236    
1237                    return hasUpdateFieldPermission(
1238                            permissionChecker, null, user, "screenName");
1239            }
1240    
1241            @Override
1242            public long[] removeRequiredRoles(long userId, long[] roleIds)
1243                    throws PortalException, SystemException {
1244    
1245                    User user = UserLocalServiceUtil.getUser(userId);
1246    
1247                    return removeRequiredRoles(user, roleIds);
1248            }
1249    
1250            @Override
1251            public long[] removeRequiredRoles(User user, long[] roleIds)
1252                    throws PortalException, SystemException {
1253    
1254                    Role role = RoleLocalServiceUtil.getRole(
1255                            user.getCompanyId(), RoleConstants.USER);
1256    
1257                    roleIds = ArrayUtil.remove(roleIds, role.getRoleId());
1258    
1259                    return roleIds;
1260            }
1261    
1262            @Override
1263            public void updateAddresses(
1264                            String className, long classPK, List<Address> addresses)
1265                    throws PortalException, SystemException {
1266    
1267                    Set<Long> addressIds = new HashSet<Long>();
1268    
1269                    for (Address address : addresses) {
1270                            long addressId = address.getAddressId();
1271    
1272                            String street1 = address.getStreet1();
1273                            String street2 = address.getStreet2();
1274                            String street3 = address.getStreet3();
1275                            String city = address.getCity();
1276                            String zip = address.getZip();
1277                            long regionId = address.getRegionId();
1278                            long countryId = address.getCountryId();
1279                            int typeId = address.getTypeId();
1280                            boolean mailing = address.isMailing();
1281                            boolean primary = address.isPrimary();
1282    
1283                            if (addressId <= 0) {
1284                                    address = AddressServiceUtil.addAddress(
1285                                            className, classPK, street1, street2, street3, city, zip,
1286                                            regionId, countryId, typeId, mailing, primary,
1287                                            new ServiceContext());
1288    
1289                                    addressId = address.getAddressId();
1290                            }
1291                            else {
1292                                    AddressServiceUtil.updateAddress(
1293                                            addressId, street1, street2, street3, city, zip, regionId,
1294                                            countryId, typeId, mailing, primary);
1295                            }
1296    
1297                            addressIds.add(addressId);
1298                    }
1299    
1300                    addresses = AddressServiceUtil.getAddresses(className, classPK);
1301    
1302                    for (Address address : addresses) {
1303                            if (!addressIds.contains(address.getAddressId())) {
1304                                    AddressServiceUtil.deleteAddress(address.getAddressId());
1305                            }
1306                    }
1307            }
1308    
1309            @Override
1310            public void updateEmailAddresses(
1311                            String className, long classPK, List<EmailAddress> emailAddresses)
1312                    throws PortalException, SystemException {
1313    
1314                    Set<Long> emailAddressIds = new HashSet<Long>();
1315    
1316                    for (EmailAddress emailAddress : emailAddresses) {
1317                            long emailAddressId = emailAddress.getEmailAddressId();
1318    
1319                            String address = emailAddress.getAddress();
1320                            int typeId = emailAddress.getTypeId();
1321                            boolean primary = emailAddress.isPrimary();
1322    
1323                            if (emailAddressId <= 0) {
1324                                    emailAddress = EmailAddressServiceUtil.addEmailAddress(
1325                                            className, classPK, address, typeId, primary,
1326                                            new ServiceContext());
1327    
1328                                    emailAddressId = emailAddress.getEmailAddressId();
1329                            }
1330                            else {
1331                                    EmailAddressServiceUtil.updateEmailAddress(
1332                                            emailAddressId, address, typeId, primary);
1333                            }
1334    
1335                            emailAddressIds.add(emailAddressId);
1336                    }
1337    
1338                    emailAddresses = EmailAddressServiceUtil.getEmailAddresses(
1339                            className, classPK);
1340    
1341                    for (EmailAddress emailAddress : emailAddresses) {
1342                            if (!emailAddressIds.contains(emailAddress.getEmailAddressId())) {
1343                                    EmailAddressServiceUtil.deleteEmailAddress(
1344                                            emailAddress.getEmailAddressId());
1345                            }
1346                    }
1347            }
1348    
1349            @Override
1350            public void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
1351                    throws PortalException, SystemException {
1352    
1353                    Set<Long> orgLaborsIds = new HashSet<Long>();
1354    
1355                    for (OrgLabor orgLabor : orgLabors) {
1356                            long orgLaborId = orgLabor.getOrgLaborId();
1357    
1358                            int typeId = orgLabor.getTypeId();
1359                            int sunOpen = orgLabor.getSunOpen();
1360                            int sunClose = orgLabor.getSunClose();
1361                            int monOpen = orgLabor.getMonOpen();
1362                            int monClose = orgLabor.getMonClose();
1363                            int tueOpen = orgLabor.getTueOpen();
1364                            int tueClose = orgLabor.getTueClose();
1365                            int wedOpen = orgLabor.getWedOpen();
1366                            int wedClose = orgLabor.getWedClose();
1367                            int thuOpen = orgLabor.getThuOpen();
1368                            int thuClose = orgLabor.getThuClose();
1369                            int friOpen = orgLabor.getFriOpen();
1370                            int friClose = orgLabor.getFriClose();
1371                            int satOpen = orgLabor.getSatOpen();
1372                            int satClose = orgLabor.getSatClose();
1373    
1374                            if (orgLaborId <= 0) {
1375                                    orgLabor = OrgLaborServiceUtil.addOrgLabor(
1376                                            classPK, typeId, sunOpen, sunClose, monOpen, monClose,
1377                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1378                                            friOpen, friClose, satOpen, satClose);
1379    
1380                                    orgLaborId = orgLabor.getOrgLaborId();
1381                            }
1382                            else {
1383                                    OrgLaborServiceUtil.updateOrgLabor(
1384                                            orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
1385                                            tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
1386                                            friOpen, friClose, satOpen, satClose);
1387                            }
1388    
1389                            orgLaborsIds.add(orgLaborId);
1390                    }
1391    
1392                    orgLabors = OrgLaborServiceUtil.getOrgLabors(classPK);
1393    
1394                    for (OrgLabor orgLabor : orgLabors) {
1395                            if (!orgLaborsIds.contains(orgLabor.getOrgLaborId())) {
1396                                    OrgLaborServiceUtil.deleteOrgLabor(orgLabor.getOrgLaborId());
1397                            }
1398                    }
1399            }
1400    
1401            @Override
1402            public void updatePhones(String className, long classPK, List<Phone> phones)
1403                    throws PortalException, SystemException {
1404    
1405                    Set<Long> phoneIds = new HashSet<Long>();
1406    
1407                    for (Phone phone : phones) {
1408                            long phoneId = phone.getPhoneId();
1409    
1410                            String number = phone.getNumber();
1411                            String extension = phone.getExtension();
1412                            int typeId = phone.getTypeId();
1413                            boolean primary = phone.isPrimary();
1414    
1415                            if (phoneId <= 0) {
1416                                    phone = PhoneServiceUtil.addPhone(
1417                                            className, classPK, number, extension, typeId, primary,
1418                                            new ServiceContext());
1419    
1420                                    phoneId = phone.getPhoneId();
1421                            }
1422                            else {
1423                                    PhoneServiceUtil.updatePhone(
1424                                            phoneId, number, extension, typeId, primary);
1425                            }
1426    
1427                            phoneIds.add(phoneId);
1428                    }
1429    
1430                    phones = PhoneServiceUtil.getPhones(className, classPK);
1431    
1432                    for (Phone phone : phones) {
1433                            if (!phoneIds.contains(phone.getPhoneId())) {
1434                                    PhoneServiceUtil.deletePhone(phone.getPhoneId());
1435                            }
1436                    }
1437            }
1438    
1439            @Override
1440            public void updateWebsites(
1441                            String className, long classPK, List<Website> websites)
1442                    throws PortalException, SystemException {
1443    
1444                    Set<Long> websiteIds = new HashSet<Long>();
1445    
1446                    for (Website website : websites) {
1447                            long websiteId = website.getWebsiteId();
1448    
1449                            String url = website.getUrl();
1450                            int typeId = website.getTypeId();
1451                            boolean primary = website.isPrimary();
1452    
1453                            if (websiteId <= 0) {
1454                                    website = WebsiteServiceUtil.addWebsite(
1455                                            className, classPK, url, typeId, primary,
1456                                            new ServiceContext());
1457    
1458                                    websiteId = website.getWebsiteId();
1459                            }
1460                            else {
1461                                    WebsiteServiceUtil.updateWebsite(
1462                                            websiteId, url, typeId, primary);
1463                            }
1464    
1465                            websiteIds.add(websiteId);
1466                    }
1467    
1468                    websites = WebsiteServiceUtil.getWebsites(className, classPK);
1469    
1470                    for (Website website : websites) {
1471                            if (!websiteIds.contains(website.getWebsiteId())) {
1472                                    WebsiteServiceUtil.deleteWebsite(website.getWebsiteId());
1473                            }
1474                    }
1475            }
1476    
1477            protected long[] getRequestPrimaryKeys(
1478                    PortletRequest portletRequest, long[] currentPKs, String addParam,
1479                    String deleteParam) {
1480    
1481                    Set<Long> primaryKeys = SetUtil.fromArray(currentPKs);
1482    
1483                    long[] addPrimaryKeys = StringUtil.split(
1484                            ParamUtil.getString(portletRequest, addParam), 0L);
1485                    long[] deletePrimaryKeys = StringUtil.split(
1486                            ParamUtil.getString(portletRequest, deleteParam), 0L);
1487    
1488                    for (long addPrimaryKey : addPrimaryKeys) {
1489                            primaryKeys.add(addPrimaryKey);
1490                    }
1491    
1492                    for (long deletePrimaryKey : deletePrimaryKeys) {
1493                            primaryKeys.remove(deletePrimaryKey);
1494                    }
1495    
1496                    return ArrayUtil.toLongArray(primaryKeys);
1497            }
1498    
1499            protected List<UserGroupRole> getUserGroupRoles(
1500                    PortletRequest portletRequest, User user, String groupIdsParam,
1501                    String roleIdsParam) {
1502    
1503                    List<UserGroupRole> userGroupRoles = new UniqueList<UserGroupRole>();
1504    
1505                    long[] groupRolesGroupIds = StringUtil.split(
1506                            ParamUtil.getString(portletRequest, groupIdsParam), 0L);
1507                    long[] groupRolesRoleIds = StringUtil.split(
1508                            ParamUtil.getString(portletRequest, roleIdsParam), 0L);
1509    
1510                    if (groupRolesGroupIds.length != groupRolesRoleIds.length) {
1511                            return userGroupRoles;
1512                    }
1513    
1514                    long userId = 0;
1515    
1516                    if (user != null) {
1517                            userId = user.getUserId();
1518                    }
1519    
1520                    for (int i = 0; i < groupRolesGroupIds.length; i++) {
1521                            if ((groupRolesGroupIds[i] == 0) || (groupRolesRoleIds[i] == 0)) {
1522                                    continue;
1523                            }
1524    
1525                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
1526                                    userId, groupRolesGroupIds[i], groupRolesRoleIds[i]);
1527    
1528                            UserGroupRole userGroupRole =
1529                                    UserGroupRoleLocalServiceUtil.createUserGroupRole(
1530                                            userGroupRolePK);
1531    
1532                            userGroupRoles.add(userGroupRole);
1533                    }
1534    
1535                    return userGroupRoles;
1536            }
1537    
1538    }