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