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