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.usersadmin.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.xml.Element;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Website;
021    import com.liferay.portal.service.GroupLocalServiceUtil;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.WebsiteLocalServiceUtil;
024    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
025    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
026    import com.liferay.portlet.exportimport.lar.PortletDataContext;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @author David Mendez Gonzalez
033     */
034    public class WebsiteStagedModelDataHandler
035            extends BaseStagedModelDataHandler<Website> {
036    
037            public static final String[] CLASS_NAMES = {Website.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(
041                            String uuid, long groupId, String className, String extraData)
042                    throws PortalException {
043    
044                    Group group = GroupLocalServiceUtil.getGroup(groupId);
045    
046                    Website website =
047                            WebsiteLocalServiceUtil.fetchWebsiteByUuidAndCompanyId(
048                                    uuid, group.getCompanyId());
049    
050                    if (website != null) {
051                            deleteStagedModel(website);
052                    }
053            }
054    
055            @Override
056            public void deleteStagedModel(Website website) {
057                    WebsiteLocalServiceUtil.deleteWebsite(website);
058            }
059    
060            @Override
061            public List<Website> fetchStagedModelsByUuidAndCompanyId(
062                    String uuid, long companyId) {
063    
064                    List<Website> websites = new ArrayList<>();
065    
066                    websites.add(
067                            WebsiteLocalServiceUtil.fetchWebsiteByUuidAndCompanyId(
068                                    uuid, companyId));
069    
070                    return websites;
071            }
072    
073            @Override
074            public String[] getClassNames() {
075                    return CLASS_NAMES;
076            }
077    
078            @Override
079            protected void doExportStagedModel(
080                            PortletDataContext portletDataContext, Website website)
081                    throws Exception {
082    
083                    Element websiteElement = portletDataContext.getExportDataElement(
084                            website);
085    
086                    portletDataContext.addClassedModel(
087                            websiteElement, ExportImportPathUtil.getModelPath(website),
088                            website);
089            }
090    
091            @Override
092            protected void doImportStagedModel(
093                            PortletDataContext portletDataContext, Website website)
094                    throws Exception {
095    
096                    long userId = portletDataContext.getUserId(website.getUserUuid());
097    
098                    ServiceContext serviceContext = portletDataContext.createServiceContext(
099                            website);
100    
101                    Website existingWebsite =
102                            WebsiteLocalServiceUtil.fetchWebsiteByUuidAndCompanyId(
103                                    website.getUuid(), portletDataContext.getCompanyGroupId());
104    
105                    Website importedWebsite = null;
106    
107                    if (existingWebsite == null) {
108                            serviceContext.setUuid(website.getUuid());
109    
110                            importedWebsite = WebsiteLocalServiceUtil.addWebsite(
111                                    userId, website.getClassName(), website.getClassPK(),
112                                    website.getUrl(), website.getTypeId(), website.isPrimary(),
113                                    serviceContext);
114                    }
115                    else {
116                            importedWebsite = WebsiteLocalServiceUtil.updateWebsite(
117                                    existingWebsite.getWebsiteId(), website.getUrl(),
118                                    website.getTypeId(), website.isPrimary());
119                    }
120    
121                    portletDataContext.importClassedModel(website, importedWebsite);
122            }
123    
124    }