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