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