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