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