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.layoutsadmin.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.LayoutPrototype;
022    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
023    import com.liferay.portal.service.ServiceContext;
024    
025    /**
026     * @author Daniela Zapata Riesco
027     */
028    public class LayoutPrototypeStagedModelDataHandler
029            extends BaseStagedModelDataHandler <LayoutPrototype> {
030    
031            public static final String[] CLASS_NAMES =
032                    {LayoutPrototype.class.getName()};
033    
034            @Override
035            public String[] getClassNames() {
036                    return CLASS_NAMES;
037            }
038    
039            @Override
040            protected void doExportStagedModel(
041                            PortletDataContext portletDataContext,
042                            LayoutPrototype layoutPrototype)
043                    throws Exception {
044    
045                    Element layoutPrototypeElement =
046                            portletDataContext.getExportDataStagedModelElement(layoutPrototype);
047    
048                    portletDataContext.addClassedModel(
049                            layoutPrototypeElement,
050                            ExportImportPathUtil.getModelPath(layoutPrototype), layoutPrototype,
051                            LayoutPrototypePortletDataHandler.NAMESPACE);
052            }
053    
054            @Override
055            protected void doImportStagedModel(
056                            PortletDataContext portletDataContext,
057                            LayoutPrototype layoutPrototype)
058                    throws Exception {
059    
060                    long userId = portletDataContext.getUserId(
061                            layoutPrototype.getUserUuid());
062    
063                    ServiceContext serviceContext = portletDataContext.createServiceContext(
064                            layoutPrototype, LayoutPrototypePortletDataHandler.NAMESPACE);
065    
066                    LayoutPrototype importedLayoutPrototype = null;
067    
068                    if (portletDataContext.isDataStrategyMirror()) {
069                            LayoutPrototype existingLayoutPrototype =
070                                    LayoutPrototypeLocalServiceUtil.
071                                            fetchLayoutPrototypeByUuidAndCompanyId(
072                                                    layoutPrototype.getUuid(),
073                                                    portletDataContext.getCompanyId());
074    
075                            if (existingLayoutPrototype == null) {
076                                    serviceContext.setUuid(layoutPrototype.getUuid());
077    
078                                    importedLayoutPrototype =
079                                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
080                                                    userId, portletDataContext.getCompanyId(),
081                                                    layoutPrototype.getNameMap(),
082                                                    layoutPrototype.getDescription(),
083                                                    layoutPrototype.isActive(), serviceContext);
084                            }
085                            else {
086                                    importedLayoutPrototype =
087                                            LayoutPrototypeLocalServiceUtil.updateLayoutPrototype(
088                                                    existingLayoutPrototype.getLayoutPrototypeId(),
089                                                    layoutPrototype.getNameMap(),
090                                                    layoutPrototype.getDescription(),
091                                                    layoutPrototype.isActive(), serviceContext);
092                            }
093                    }
094                    else {
095                            importedLayoutPrototype =
096                                    LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
097                                            userId, portletDataContext.getCompanyId(),
098                                            layoutPrototype.getNameMap(),
099                                            layoutPrototype.getDescription(),
100                                            layoutPrototype.isActive(), serviceContext);
101                    }
102    
103                    portletDataContext.importClassedModel(
104                            layoutPrototype, importedLayoutPrototype,
105                            LayoutPrototypePortletDataHandler.NAMESPACE);
106            }
107    
108    }