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.util.GetterUtil;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.LayoutSetPrototype;
024    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    
027    /**
028     * @author Daniela Zapata Riesco
029     */
030    public class LayoutSetPrototypeStagedModelDataHandler
031            extends BaseStagedModelDataHandler<LayoutSetPrototype> {
032    
033            public static final String[] CLASS_NAMES =
034                    {LayoutSetPrototype.class.getName()};
035    
036            @Override
037            public String[] getClassNames() {
038                    return CLASS_NAMES;
039            }
040    
041            @Override
042            protected void doExportStagedModel(
043                            PortletDataContext portletDataContext,
044                            LayoutSetPrototype layoutSetPrototype)
045                    throws Exception {
046    
047                    Element layoutSetPrototypeElement =
048                            portletDataContext.getExportDataStagedModelElement(
049                                    layoutSetPrototype);
050    
051                    portletDataContext.addClassedModel(
052                            layoutSetPrototypeElement,
053                            ExportImportPathUtil.getModelPath(layoutSetPrototype),
054                            layoutSetPrototype, LayoutSetPrototypePortletDataHandler.NAMESPACE);
055            }
056    
057            @Override
058            protected void doImportStagedModel(
059                            PortletDataContext portletDataContext,
060                            LayoutSetPrototype layoutSetPrototype)
061                    throws Exception {
062    
063                    long userId = portletDataContext.getUserId(
064                            layoutSetPrototype.getUserUuid());
065    
066                    UnicodeProperties settingsProperties =
067                            layoutSetPrototype.getSettingsProperties();
068    
069                    boolean layoutsUpdateable = GetterUtil.getBoolean(
070                            settingsProperties.getProperty("layoutsUpdateable"), true);
071    
072                    ServiceContext serviceContext = portletDataContext.createServiceContext(
073                            layoutSetPrototype, LayoutSetPrototypePortletDataHandler.NAMESPACE);
074    
075                    LayoutSetPrototype importedLayoutSetPrototype = null;
076    
077                    if (portletDataContext.isDataStrategyMirror()) {
078                            LayoutSetPrototype existingLayoutSetPrototype =
079                                    LayoutSetPrototypeLocalServiceUtil.
080                                            fetchLayoutSetPrototypeByUuidAndCompanyId(
081                                                    layoutSetPrototype.getUuid(),
082                                                    portletDataContext.getCompanyId());
083    
084                            if (existingLayoutSetPrototype == null) {
085                                    serviceContext.setUuid(layoutSetPrototype.getUuid());
086    
087                                    importedLayoutSetPrototype =
088                                            LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
089                                                    userId, portletDataContext.getCompanyId(),
090                                                    layoutSetPrototype.getNameMap(),
091                                                    layoutSetPrototype.getDescription(),
092                                                    layoutSetPrototype.isActive(), layoutsUpdateable,
093                                                    serviceContext);
094                            }
095                            else {
096                                    importedLayoutSetPrototype =
097                                            LayoutSetPrototypeLocalServiceUtil.updateLayoutSetPrototype(
098                                                    existingLayoutSetPrototype.getLayoutSetPrototypeId(),
099                                                    layoutSetPrototype.getNameMap(),
100                                                    layoutSetPrototype.getDescription(),
101                                                    layoutSetPrototype.isActive(), layoutsUpdateable,
102                                                    serviceContext);
103                            }
104                    }
105                    else {
106                            importedLayoutSetPrototype =
107                                    LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
108                                            userId, portletDataContext.getCompanyId(),
109                                            layoutSetPrototype.getNameMap(),
110                                            layoutSetPrototype.getDescription(),
111                                            layoutSetPrototype.isActive(), layoutsUpdateable,
112                                            serviceContext);
113                    }
114    
115                    portletDataContext.importClassedModel(
116                            layoutSetPrototype, importedLayoutSetPrototype,
117                            LayoutSetPrototypePortletDataHandler.NAMESPACE);
118            }
119    
120    }