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