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.rolesadmin.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.Role;
022    import com.liferay.portal.service.RoleLocalServiceUtil;
023    import com.liferay.portal.service.ServiceContext;
024    
025    /**
026     * @author David Mendez Gonzalez
027     */
028    public class RoleStagedModelDataHandler
029            extends BaseStagedModelDataHandler<Role> {
030    
031            public static final String[] CLASS_NAMES = {Role.class.getName()};
032    
033            @Override
034            public String[] getClassNames() {
035                    return CLASS_NAMES;
036            }
037    
038            @Override
039            protected void doExportStagedModel(
040                            PortletDataContext portletDataContext, Role role)
041                    throws Exception {
042    
043                    Element roleElement =
044                            portletDataContext.getExportDataStagedModelElement(role);
045    
046                    portletDataContext.addClassedModel(
047                            roleElement, ExportImportPathUtil.getModelPath(role), role,
048                            RolesAdminPortletDataHandler.NAMESPACE);
049            }
050    
051            @Override
052            protected void doImportStagedModel(
053                            PortletDataContext portletDataContext, Role role)
054                    throws Exception {
055    
056                    long userId = portletDataContext.getUserId(role.getUserUuid());
057    
058                    ServiceContext serviceContext = portletDataContext.createServiceContext(
059                            role, RolesAdminPortletDataHandler.NAMESPACE);
060    
061                    Role existingRole = RoleLocalServiceUtil.fetchRoleByUuidAndCompanyId(
062                            role.getUuid(), portletDataContext.getCompanyId());
063    
064                    if (existingRole == null) {
065                            existingRole = RoleLocalServiceUtil.fetchRole(
066                                    portletDataContext.getCompanyId(), role.getName());
067                    }
068    
069                    Role importedRole = null;
070    
071                    if (existingRole == null) {
072                            serviceContext.setUuid(role.getUuid());
073    
074                            importedRole = RoleLocalServiceUtil.addRole(
075                                    userId, null, 0, role.getName(), role.getTitleMap(),
076                                    role.getDescriptionMap(), role.getType(), role.getSubtype(),
077                                    serviceContext);
078                    }
079                    else {
080                            importedRole = RoleLocalServiceUtil.updateRole(
081                                    existingRole.getRoleId(), role.getName(), role.getTitleMap(),
082                                    role.getDescriptionMap(), role.getSubtype(), serviceContext);
083                    }
084    
085                    portletDataContext.importClassedModel(
086                            role, importedRole, RolesAdminPortletDataHandler.NAMESPACE);
087    
088            }
089    
090    }