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