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.usersadmin.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.Website;
025    import com.liferay.portal.service.GroupLocalServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.WebsiteLocalServiceUtil;
028    
029    /**
030     * @author David Mendez Gonzalez
031     */
032    public class WebsiteStagedModelDataHandler
033            extends BaseStagedModelDataHandler<Website> {
034    
035            public static final String[] CLASS_NAMES = {Website.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                    Website website =
045                            WebsiteLocalServiceUtil.fetchWebsiteByUuidAndCompanyId(
046                                    uuid, group.getCompanyId());
047    
048                    if (website != null) {
049                            WebsiteLocalServiceUtil.deleteWebsite(website);
050                    }
051            }
052    
053            @Override
054            public String[] getClassNames() {
055                    return CLASS_NAMES;
056            }
057    
058            @Override
059            protected void doExportStagedModel(
060                            PortletDataContext portletDataContext, Website website)
061                    throws Exception {
062    
063                    Element websiteElement = portletDataContext.getExportDataElement(
064                            website);
065    
066                    portletDataContext.addClassedModel(
067                            websiteElement, ExportImportPathUtil.getModelPath(website),
068                            website);
069            }
070    
071            @Override
072            protected void doImportStagedModel(
073                            PortletDataContext portletDataContext, Website website)
074                    throws Exception {
075    
076                    long userId = portletDataContext.getUserId(website.getUserUuid());
077    
078                    ServiceContext serviceContext = portletDataContext.createServiceContext(
079                            website);
080    
081                    Website existingWebsite =
082                            WebsiteLocalServiceUtil.fetchWebsiteByUuidAndCompanyId(
083                                    website.getUuid(), portletDataContext.getCompanyId());
084    
085                    Website importedWebsite = null;
086    
087                    if (existingWebsite == null) {
088                            serviceContext.setUuid(website.getUuid());
089    
090                            importedWebsite = WebsiteLocalServiceUtil.addWebsite(
091                                    userId, website.getClassName(), website.getClassPK(),
092                                    website.getUrl(), website.getTypeId(), website.isPrimary(),
093                                    serviceContext);
094                    }
095                    else {
096                            importedWebsite = WebsiteLocalServiceUtil.updateWebsite(
097                                    existingWebsite.getWebsiteId(), website.getUrl(),
098                                    website.getTypeId(), website.isPrimary());
099                    }
100    
101                    portletDataContext.importClassedModel(website, importedWebsite);
102            }
103    
104    }