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.lar.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.model.UserGroup;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.UserGroupLocalServiceUtil;
024    
025    /**
026     * @author David Mendez Gonzalez
027     */
028    public class UserGroupStagedModelDataHandler
029            extends BaseStagedModelDataHandler<UserGroup> {
030    
031            public static final String[] CLASS_NAMES = {UserGroup.class.getName()};
032    
033            @Override
034            public String[] getClassNames() {
035                    return CLASS_NAMES;
036            }
037    
038            @Override
039            protected void doExportStagedModel(
040                            PortletDataContext portletDataContext, UserGroup userGroup)
041                    throws Exception {
042    
043                    Element userGroupElement =
044                            portletDataContext.getExportDataStagedModelElement(userGroup);
045    
046                    portletDataContext.addClassedModel(
047                            userGroupElement, ExportImportPathUtil.getModelPath(userGroup),
048                            userGroup, UserGroupsAdminPortletDataHandler.NAMESPACE);
049            }
050    
051            @Override
052            protected void doImportStagedModel(
053                            PortletDataContext portletDataContext, UserGroup userGroup)
054                    throws Exception {
055    
056                    long userId = portletDataContext.getUserId(userGroup.getUserUuid());
057    
058                    ServiceContext serviceContext = portletDataContext.createServiceContext(
059                            userGroup, UserGroupsAdminPortletDataHandler.NAMESPACE);
060    
061                    UserGroup existingUserGroup =
062                            UserGroupLocalServiceUtil.fetchUserGroupByUuidAndCompanyId(
063                                    userGroup.getUuid(), portletDataContext.getCompanyId());
064    
065                    if (existingUserGroup == null) {
066                            existingUserGroup = UserGroupLocalServiceUtil.fetchUserGroup(
067                                    portletDataContext.getCompanyId(), userGroup.getName());
068                    }
069    
070                    UserGroup importedUserGroup = null;
071    
072                    if (existingUserGroup == null) {
073                            serviceContext.setUuid(userGroup.getUuid());
074    
075                            importedUserGroup = UserGroupLocalServiceUtil.addUserGroup(
076                                    userId, portletDataContext.getCompanyId(), userGroup.getName(),
077                                    userGroup.getDescription(), serviceContext);
078                    }
079                    else {
080                            importedUserGroup = UserGroupLocalServiceUtil.updateUserGroup(
081                                    portletDataContext.getCompanyId(),
082                                    existingUserGroup.getUserGroupId(), userGroup.getName(),
083                                    userGroup.getDescription(), serviceContext);
084                    }
085    
086                    portletDataContext.importClassedModel(
087                            userGroup, importedUserGroup,
088                            UserGroupsAdminPortletDataHandler.NAMESPACE);
089            }
090    
091    }