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