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         Long[] organizationIds = new Long[organizations.size()];
426 
427         for (int i = 0; i < organizations.size(); i++) {
428             Organization organization = organizations.get(i);
429 
430             organizationIds[i] = new Long(organization.getOrganizationId());
431         }
432 
433         return organizationIds;
434     }
435 
436     public static OrderByComparator getOrganizationOrderByComparator(
437         String orderByCol, String orderByType) {
438 
439         boolean orderByAsc = false;
440 
441         if (orderByType.equals("asc")) {
442             orderByAsc = true;
443         }
444 
445         OrderByComparator orderByComparator = null;
446 
447         if (orderByCol.equals("name")) {
448             orderByComparator = new OrganizationNameComparator(orderByAsc);
449         }
450         else if (orderByCol.equals("type")) {
451             orderByComparator = new OrganizationTypeComparator(orderByAsc);
452         }
453         else {
454             orderByComparator = new OrganizationNameComparator(orderByAsc);
455         }
456 
457         return orderByComparator;
458     }
459 
460     public static List<OrgLabor> getOrgLabors(ActionRequest actionRequest) {
461         List<OrgLabor> orgLabors = new ArrayList<OrgLabor>();
462 
463         int[] orgLaborsIndexes = StringUtil.split(
464             ParamUtil.getString(actionRequest, "orgLaborsIndexes"), 0);
465 
466         for (int orgLaborsIndex : orgLaborsIndexes) {
467             long orgLaborId = ParamUtil.getLong(
468                 actionRequest, "orgLaborId" + orgLaborsIndex);
469 
470             int typeId = ParamUtil.getInteger(
471                 actionRequest, "orgLaborTypeId" + orgLaborsIndex, -1);
472 
473             if (typeId == -1) {
474                 continue;
475             }
476 
477             int sunOpen = ParamUtil.getInteger(
478                 actionRequest, "sunOpen" + orgLaborsIndex, -1);
479             int sunClose = ParamUtil.getInteger(
480                 actionRequest, "sunClose" + orgLaborsIndex, -1);
481             int monOpen = ParamUtil.getInteger(
482                 actionRequest, "monOpen" + orgLaborsIndex, -1);
483             int monClose = ParamUtil.getInteger(
484                 actionRequest, "monClose" + orgLaborsIndex, -1);
485             int tueOpen = ParamUtil.getInteger(
486                 actionRequest, "tueOpen" + orgLaborsIndex, -1);
487             int tueClose = ParamUtil.getInteger(
488                 actionRequest, "tueClose" + orgLaborsIndex, -1);
489             int wedOpen = ParamUtil.getInteger(
490                 actionRequest, "wedOpen" + orgLaborsIndex, -1);
491             int wedClose = ParamUtil.getInteger(
492                 actionRequest, "wedClose" + orgLaborsIndex, -1);
493             int thuOpen = ParamUtil.getInteger(
494                 actionRequest, "thuOpen" + orgLaborsIndex, -1);
495             int thuClose = ParamUtil.getInteger(
496                 actionRequest, "thuClose" + orgLaborsIndex, -1);
497             int friOpen = ParamUtil.getInteger(
498                 actionRequest, "friOpen" + orgLaborsIndex, -1);
499             int friClose = ParamUtil.getInteger(
500                 actionRequest, "friClose" + orgLaborsIndex, -1);
501             int satOpen = ParamUtil.getInteger(
502                 actionRequest, "satOpen" + orgLaborsIndex, -1);
503             int satClose = ParamUtil.getInteger(
504                 actionRequest, "satClose" + orgLaborsIndex, -1);
505 
506             OrgLabor orgLabor = new OrgLaborImpl();
507 
508             orgLabor.setOrgLaborId(orgLaborId);
509             orgLabor.setTypeId(typeId);
510             orgLabor.setSunOpen(sunOpen);
511             orgLabor.setSunClose(sunClose);
512             orgLabor.setMonOpen(monOpen);
513             orgLabor.setMonClose(monClose);
514             orgLabor.setTueOpen(tueOpen);
515             orgLabor.setTueClose(tueClose);
516             orgLabor.setWedOpen(wedOpen);
517             orgLabor.setWedClose(wedClose);
518             orgLabor.setThuOpen(thuOpen);
519             orgLabor.setThuClose(thuClose);
520             orgLabor.setFriOpen(friOpen);
521             orgLabor.setFriClose(friClose);
522             orgLabor.setSatOpen(satOpen);
523             orgLabor.setSatClose(satClose);
524 
525             orgLabors.add(orgLabor);
526         }
527 
528         return orgLabors;
529     }
530 
531     public static OrderByComparator getPasswordPolicyOrderByComparator(
532         String orderByCol, String orderByType) {
533 
534         boolean orderByAsc = false;
535 
536         if (orderByType.equals("asc")) {
537             orderByAsc = true;
538         }
539 
540         OrderByComparator orderByComparator = null;
541 
542         if (orderByCol.equals("name")) {
543             orderByComparator = new PasswordPolicyNameComparator(orderByAsc);
544         }
545         else if (orderByCol.equals("description")) {
546             orderByComparator = new PasswordPolicyDescriptionComparator(
547                 orderByAsc);
548         }
549         else {
550             orderByComparator = new PasswordPolicyNameComparator(orderByAsc);
551         }
552 
553         return orderByComparator;
554     }
555 
556     public static List<Phone> getPhones(ActionRequest actionRequest) {
557         List<Phone> phones = new ArrayList<Phone>();
558 
559         int[] phonesIndexes = StringUtil.split(
560             ParamUtil.getString(actionRequest, "phonesIndexes"), 0);
561 
562         int phonePrimary = ParamUtil.getInteger(actionRequest, "phonePrimary");
563 
564         for (int phonesIndex : phonesIndexes) {
565             long phoneId = ParamUtil.getLong(
566                 actionRequest, "phoneId" + phonesIndex);
567 
568             String number = ParamUtil.getString(
569                 actionRequest, "phoneNumber" + phonesIndex);
570             String extension = ParamUtil.getString(
571                 actionRequest, "phoneExtension" + phonesIndex);
572 
573             if (Validator.isNull(number) && Validator.isNull(extension)) {
574                 continue;
575             }
576 
577             int typeId = ParamUtil.getInteger(
578                 actionRequest, "phoneTypeId" + phonesIndex);
579 
580             boolean primary = false;
581 
582             if (phonesIndex == phonePrimary) {
583                 primary = true;
584             }
585 
586             Phone phone = new PhoneImpl();
587 
588             phone.setPhoneId(phoneId);
589             phone.setNumber(number);
590             phone.setExtension(extension);
591             phone.setTypeId(typeId);
592             phone.setPrimary(primary);
593 
594             phones.add(phone);
595         }
596 
597         return phones;
598     }
599 
600     public static OrderByComparator getRoleOrderByComparator(
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 RoleNameComparator(orderByAsc);
613         }
614         else if (orderByCol.equals("description")) {
615             orderByComparator = new RoleDescriptionComparator(orderByAsc);
616         }
617         else if (orderByCol.equals("type")) {
618             orderByComparator = new RoleTypeComparator(orderByAsc);
619         }
620         else {
621             orderByComparator = new RoleNameComparator(orderByAsc);
622         }
623 
624         return orderByComparator;
625     }
626 
627     public static OrderByComparator getUserGroupOrderByComparator(
628         String orderByCol, String orderByType) {
629 
630         boolean orderByAsc = false;
631 
632         if (orderByType.equals("asc")) {
633             orderByAsc = true;
634         }
635 
636         OrderByComparator orderByComparator = null;
637 
638         if (orderByCol.equals("name")) {
639             orderByComparator = new UserGroupNameComparator(orderByAsc);
640         }
641         else if (orderByCol.equals("description")) {
642             orderByComparator = new UserGroupDescriptionComparator(orderByAsc);
643         }
644         else {
645             orderByComparator = new UserGroupNameComparator(orderByAsc);
646         }
647 
648         return orderByComparator;
649     }
650 
651     public static List<UserGroupRole> getUserGroupRoles(
652             PortletRequest portletRequest)
653         throws SystemException, PortalException {
654 
655         List<UserGroupRole> userGroupRoles = new UniqueList<UserGroupRole>();
656 
657         long[] groupRolesRoleIds= StringUtil.split(ParamUtil.getString(
658             portletRequest, "groupRolesRoleIds"), 0L);
659         long[] groupRolesGroupIds= StringUtil.split(ParamUtil.getString(
660             portletRequest, "groupRolesGroupIds"), 0L);
661 
662         if (groupRolesGroupIds.length != groupRolesRoleIds.length) {
663             return userGroupRoles;
664         }
665 
666         User user = PortalUtil.getSelectedUser(portletRequest);
667 
668         for (int i = 0; i < groupRolesGroupIds.length; i++) {
669             if ((groupRolesGroupIds[i] == 0) ||
670                 (groupRolesRoleIds[i] == 0)) {
671 
672                 continue;
673             }
674 
675             UserGroupRole userGroupRole = new UserGroupRoleImpl();
676 
677             userGroupRole.setUserId(user.getUserId());
678             userGroupRole.setGroupId(groupRolesGroupIds[i]);
679             userGroupRole.setRoleId(groupRolesRoleIds[i]);
680 
681             userGroupRoles.add(userGroupRole);
682         }
683 
684         return userGroupRoles;
685     }
686 
687     public static OrderByComparator getUserOrderByComparator(
688         String orderByCol, String orderByType) {
689 
690         boolean orderByAsc = false;
691 
692         if (orderByType.equals("asc")) {
693             orderByAsc = true;
694         }
695 
696         OrderByComparator orderByComparator = null;
697 
698         if (orderByCol.equals("email-address")) {
699             orderByComparator = new UserEmailAddressComparator(orderByAsc);
700         }
701         else if (orderByCol.equals("first-name")) {
702             orderByComparator = new ContactFirstNameComparator(orderByAsc);
703         }
704         else if (orderByCol.equals("job-title")) {
705             orderByComparator = new ContactJobTitleComparator(orderByAsc);
706         }
707         else if (orderByCol.equals("last-name")) {
708             orderByComparator = new ContactLastNameComparator(orderByAsc);
709         }
710         else if (orderByCol.equals("screen-name")) {
711             orderByComparator = new UserScreenNameComparator(orderByAsc);
712         }
713         else {
714             orderByComparator = new ContactLastNameComparator(orderByAsc);
715         }
716 
717         return orderByComparator;
718     }
719 
720     public static List<Website> getWebsites(ActionRequest actionRequest) {
721         List<Website> websites = new ArrayList<Website>();
722 
723         int[] websitesIndexes = StringUtil.split(
724             ParamUtil.getString(actionRequest, "websitesIndexes"), 0);
725 
726         int websitePrimary = ParamUtil.getInteger(
727             actionRequest, "websitePrimary");
728 
729         for (int websitesIndex : websitesIndexes) {
730             long websiteId = ParamUtil.getLong(
731                 actionRequest, "websiteId" + websitesIndex);
732 
733             String url = ParamUtil.getString(
734                 actionRequest, "websiteUrl" + websitesIndex);
735 
736             if (Validator.isNull(url)) {
737                 continue;
738             }
739 
740             int typeId = ParamUtil.getInteger(
741                 actionRequest, "websiteTypeId" + websitesIndex);
742 
743             boolean primary = false;
744 
745             if (websitesIndex == websitePrimary) {
746                 primary = true;
747             }
748 
749             Website website = new WebsiteImpl();
750 
751             website.setWebsiteId(websiteId);
752             website.setUrl(url);
753             website.setTypeId(typeId);
754             website.setPrimary(primary);
755 
756             websites.add(website);
757         }
758 
759         return websites;
760     }
761 
762     public static void updateAddresses(
763             String className, long classPK, List<Address> addresses)
764         throws PortalException, SystemException {
765 
766         Set<Long> addressIds = new HashSet<Long>();
767 
768         for (Address address : addresses) {
769             long addressId = address.getAddressId();
770 
771             String street1 = address.getStreet1();
772             String street2 = address.getStreet2();
773             String street3 = address.getStreet3();
774             String city = address.getCity();
775             String zip = address.getZip();
776             long regionId = address.getRegionId();
777             long countryId = address.getCountryId();
778             int typeId = address.getTypeId();
779             boolean mailing = address.isMailing();
780             boolean primary = address.isPrimary();
781 
782             if (addressId <= 0) {
783                 address = AddressServiceUtil.addAddress(
784                     className, classPK, street1, street2, street3, city, zip,
785                     regionId, countryId, typeId, mailing, primary);
786 
787                 addressId = address.getAddressId();
788             }
789             else {
790                 AddressServiceUtil.updateAddress(
791                     addressId, street1, street2, street3, city, zip, regionId,
792                     countryId, typeId, mailing, primary);
793             }
794 
795             addressIds.add(addressId);
796         }
797 
798         addresses = AddressServiceUtil.getAddresses(className, classPK);
799 
800         for (Address address : addresses) {
801             if (!addressIds.contains(address.getAddressId())) {
802                 AddressServiceUtil.deleteAddress(address.getAddressId());
803             }
804         }
805     }
806 
807     public static void updateEmailAddresses(
808             String className, long classPK, List<EmailAddress> emailAddresses)
809         throws PortalException, SystemException {
810 
811         Set<Long> emailAddressIds = new HashSet<Long>();
812 
813         for (EmailAddress emailAddress : emailAddresses) {
814             long emailAddressId = emailAddress.getEmailAddressId();
815 
816             String address = emailAddress.getAddress();
817             int typeId = emailAddress.getTypeId();
818             boolean primary = emailAddress.isPrimary();
819 
820             if (emailAddressId <= 0) {
821                 emailAddress = EmailAddressServiceUtil.addEmailAddress(
822                     className, classPK, address, typeId, primary);
823 
824                 emailAddressId = emailAddress.getEmailAddressId();
825             }
826             else {
827                 EmailAddressServiceUtil.updateEmailAddress(
828                     emailAddressId, address, typeId, primary);
829             }
830 
831             emailAddressIds.add(emailAddressId);
832         }
833 
834         emailAddresses = EmailAddressServiceUtil.getEmailAddresses(
835             className, classPK);
836 
837         for (EmailAddress emailAddress : emailAddresses) {
838             if (!emailAddressIds.contains(emailAddress.getEmailAddressId())) {
839                 EmailAddressServiceUtil.deleteEmailAddress(
840                     emailAddress.getEmailAddressId());
841             }
842         }
843     }
844 
845     public static void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
846         throws PortalException, SystemException {
847 
848         Set<Long> orgLaborsIds = new HashSet<Long>();
849 
850         for (OrgLabor orgLabor : orgLabors) {
851             long orgLaborId = orgLabor.getOrgLaborId();
852 
853             int typeId = orgLabor.getTypeId();
854             int sunOpen = orgLabor.getSunOpen();
855             int sunClose = orgLabor.getSunClose();
856             int monOpen = orgLabor.getMonOpen();
857             int monClose = orgLabor.getMonClose();
858             int tueOpen = orgLabor.getTueOpen();
859             int tueClose = orgLabor.getTueClose();
860             int wedOpen = orgLabor.getWedOpen();
861             int wedClose = orgLabor.getWedClose();
862             int thuOpen = orgLabor.getThuOpen();
863             int thuClose = orgLabor.getThuClose();
864             int friOpen = orgLabor.getFriOpen();
865             int friClose = orgLabor.getFriClose();
866             int satOpen = orgLabor.getSatOpen();
867             int satClose = orgLabor.getSatClose();
868 
869             if (orgLaborId <= 0) {
870                 orgLabor = OrgLaborServiceUtil.addOrgLabor(
871                     classPK, typeId, sunOpen, sunClose, monOpen, monClose,
872                     tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
873                     friOpen, friClose, satOpen, satClose);
874 
875                 orgLaborId = orgLabor.getOrgLaborId();
876             }
877             else {
878                 OrgLaborServiceUtil.updateOrgLabor(
879                     orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
880                     tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
881                     friOpen, friClose, satOpen, satClose);
882             }
883 
884             orgLaborsIds.add(orgLaborId);
885         }
886 
887         orgLabors = OrgLaborServiceUtil.getOrgLabors(classPK);
888 
889         for (OrgLabor orgLabor : orgLabors) {
890             if (!orgLaborsIds.contains(orgLabor.getOrgLaborId())) {
891                 OrgLaborServiceUtil.deleteOrgLabor(orgLabor.getOrgLaborId());
892             }
893         }
894     }
895 
896     public static void updatePhones(
897             String className, long classPK, List<Phone> phones)
898         throws PortalException, SystemException {
899 
900         Set<Long> phoneIds = new HashSet<Long>();
901 
902         for (Phone phone : phones) {
903             long phoneId = phone.getPhoneId();
904 
905             String number = phone.getNumber();
906             String extension = phone.getExtension();
907             int typeId = phone.getTypeId();
908             boolean primary = phone.isPrimary();
909 
910             if (phoneId <= 0) {
911                 phone = PhoneServiceUtil.addPhone(
912                     className, classPK, number, extension, typeId, primary);
913 
914                 phoneId = phone.getPhoneId();
915             }
916             else {
917                 PhoneServiceUtil.updatePhone(
918                     phoneId, number, extension, typeId, primary);
919             }
920 
921             phoneIds.add(phoneId);
922         }
923 
924         phones = PhoneServiceUtil.getPhones(className, classPK);
925 
926         for (Phone phone : phones) {
927             if (!phoneIds.contains(phone.getPhoneId())) {
928                 PhoneServiceUtil.deletePhone(phone.getPhoneId());
929             }
930         }
931     }
932 
933     public static void updateWebsites(
934             String className, long classPK, List<Website> websites)
935         throws PortalException, SystemException {
936 
937         Set<Long> websiteIds = new HashSet<Long>();
938 
939         for (Website website : websites) {
940             long websiteId = website.getWebsiteId();
941 
942             String url = website.getUrl();
943             int typeId = website.getTypeId();
944             boolean primary = website.isPrimary();
945 
946             if (websiteId <= 0) {
947                 website = WebsiteServiceUtil.addWebsite(
948                     className, classPK, url, typeId, primary);
949 
950                 websiteId = website.getWebsiteId();
951             }
952             else {
953                 WebsiteServiceUtil.updateWebsite(
954                     websiteId, url, typeId, primary);
955             }
956 
957             websiteIds.add(websiteId);
958         }
959 
960         websites = WebsiteServiceUtil.getWebsites(className, classPK);
961 
962         for (Website website : websites) {
963             if (!websiteIds.contains(website.getWebsiteId())) {
964                 WebsiteServiceUtil.deleteWebsite(website.getWebsiteId());
965             }
966         }
967     }
968 
969 }