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