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