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