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