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