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