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