001    /**
002     * Copyright (c) 2000-present 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.usergroupsadmin.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.xml.Element;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.UserGroup;
021    import com.liferay.portal.service.GroupLocalServiceUtil;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.UserGroupLocalServiceUtil;
024    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
025    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
026    import com.liferay.portlet.exportimport.lar.PortletDataContext;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @author David Mendez Gonzalez
033     */
034    public class UserGroupStagedModelDataHandler
035            extends BaseStagedModelDataHandler<UserGroup> {
036    
037            public static final String[] CLASS_NAMES = {UserGroup.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(
041                            String uuid, long groupId, String className, String extraData)
042                    throws PortalException {
043    
044                    Group group = GroupLocalServiceUtil.getGroup(groupId);
045    
046                    UserGroup userGroup = fetchStagedModelByUuidAndGroupId(
047                            uuid, group.getCompanyId());
048    
049                    if (userGroup != null) {
050                            deleteStagedModel(userGroup);
051                    }
052            }
053    
054            @Override
055            public void deleteStagedModel(UserGroup userGroup) throws PortalException {
056                    UserGroupLocalServiceUtil.deleteUserGroup(userGroup);
057            }
058    
059            @Override
060            public List<UserGroup> fetchStagedModelsByUuidAndCompanyId(
061                    String uuid, long companyId) {
062    
063                    List<UserGroup> userGroups = new ArrayList<>();
064    
065                    userGroups.add(
066                            UserGroupLocalServiceUtil.fetchUserGroupByUuidAndCompanyId(
067                                    uuid, companyId));
068    
069                    return userGroups;
070            }
071    
072            @Override
073            public String[] getClassNames() {
074                    return CLASS_NAMES;
075            }
076    
077            @Override
078            public String getDisplayName(UserGroup userGroup) {
079                    return userGroup.getName();
080            }
081    
082            @Override
083            protected void doExportStagedModel(
084                            PortletDataContext portletDataContext, UserGroup userGroup)
085                    throws Exception {
086    
087                    Element userGroupElement = portletDataContext.getExportDataElement(
088                            userGroup);
089    
090                    portletDataContext.addClassedModel(
091                            userGroupElement, ExportImportPathUtil.getModelPath(userGroup),
092                            userGroup);
093            }
094    
095            @Override
096            protected void doImportStagedModel(
097                            PortletDataContext portletDataContext, UserGroup userGroup)
098                    throws Exception {
099    
100                    long userId = portletDataContext.getUserId(userGroup.getUserUuid());
101    
102                    ServiceContext serviceContext = portletDataContext.createServiceContext(
103                            userGroup);
104    
105                    UserGroup existingUserGroup = fetchStagedModelByUuidAndGroupId(
106                            userGroup.getUuid(), portletDataContext.getGroupId());
107    
108                    if (existingUserGroup == null) {
109                            existingUserGroup = UserGroupLocalServiceUtil.fetchUserGroup(
110                                    portletDataContext.getCompanyId(), userGroup.getName());
111                    }
112    
113                    UserGroup importedUserGroup = null;
114    
115                    if (existingUserGroup == null) {
116                            serviceContext.setUuid(userGroup.getUuid());
117    
118                            importedUserGroup = UserGroupLocalServiceUtil.addUserGroup(
119                                    userId, portletDataContext.getCompanyId(), userGroup.getName(),
120                                    userGroup.getDescription(), serviceContext);
121                    }
122                    else {
123                            importedUserGroup = UserGroupLocalServiceUtil.updateUserGroup(
124                                    portletDataContext.getCompanyId(),
125                                    existingUserGroup.getUserGroupId(), userGroup.getName(),
126                                    userGroup.getDescription(), serviceContext);
127                    }
128    
129                    portletDataContext.importClassedModel(userGroup, importedUserGroup);
130            }
131    
132    }