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