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