001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.layoutprototypes.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.lar.PortletDataHandlerKeys;
023    import com.liferay.portal.kernel.lar.UserIdStrategy;
024    import com.liferay.portal.kernel.util.StreamUtil;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.model.LayoutPrototype;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.LayoutLocalServiceUtil;
031    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
032    import com.liferay.portal.service.LayoutServiceUtil;
033    import com.liferay.portal.service.ServiceContext;
034    
035    import java.io.File;
036    import java.io.FileInputStream;
037    import java.io.InputStream;
038    
039    import java.util.LinkedHashMap;
040    import java.util.List;
041    import java.util.Map;
042    
043    /**
044     * @author Daniela Zapata Riesco
045     */
046    public class LayoutPrototypeStagedModelDataHandler
047            extends BaseStagedModelDataHandler <LayoutPrototype> {
048    
049            public static final String[] CLASS_NAMES =
050                    {LayoutPrototype.class.getName()};
051    
052            @Override
053            public void deleteStagedModel(
054                            String uuid, long groupId, String className, String extraData)
055                    throws PortalException, SystemException {
056    
057                    Group group = GroupLocalServiceUtil.getGroup(groupId);
058    
059                    LayoutPrototype layoutPrototype =
060                            LayoutPrototypeLocalServiceUtil.
061                                    fetchLayoutPrototypeByUuidAndCompanyId(
062                                            uuid, group.getCompanyId());
063    
064                    if (layoutPrototype != null) {
065                            LayoutPrototypeLocalServiceUtil.deleteLayoutPrototype(
066                                    layoutPrototype);
067                    }
068            }
069    
070            @Override
071            public String[] getClassNames() {
072                    return CLASS_NAMES;
073            }
074    
075            @Override
076            public String getDisplayName(LayoutPrototype layoutPrototype) {
077                    return layoutPrototype.getNameCurrentValue();
078            }
079    
080            @Override
081            protected void doExportStagedModel(
082                            PortletDataContext portletDataContext,
083                            LayoutPrototype layoutPrototype)
084                    throws Exception {
085    
086                    Element layoutPrototypeElement =
087                            portletDataContext.getExportDataElement(layoutPrototype);
088    
089                    portletDataContext.addClassedModel(
090                            layoutPrototypeElement,
091                            ExportImportPathUtil.getModelPath(layoutPrototype),
092                            layoutPrototype);
093    
094                    exportLayouts(
095                            portletDataContext, layoutPrototype, layoutPrototypeElement);
096            }
097    
098            @Override
099            protected void doImportStagedModel(
100                            PortletDataContext portletDataContext,
101                            LayoutPrototype layoutPrototype)
102                    throws Exception {
103    
104                    long userId = portletDataContext.getUserId(
105                            layoutPrototype.getUserUuid());
106    
107                    ServiceContext serviceContext = portletDataContext.createServiceContext(
108                            layoutPrototype);
109    
110                    serviceContext.setAttribute("addDefaultLayout", false);
111    
112                    LayoutPrototype importedLayoutPrototype = null;
113    
114                    if (portletDataContext.isDataStrategyMirror()) {
115                            LayoutPrototype existingLayoutPrototype =
116                                    LayoutPrototypeLocalServiceUtil.
117                                            fetchLayoutPrototypeByUuidAndCompanyId(
118                                                    layoutPrototype.getUuid(),
119                                                    portletDataContext.getCompanyId());
120    
121                            if (existingLayoutPrototype == null) {
122                                    serviceContext.setUuid(layoutPrototype.getUuid());
123    
124                                    importedLayoutPrototype =
125                                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
126                                                    userId, portletDataContext.getCompanyId(),
127                                                    layoutPrototype.getNameMap(),
128                                                    layoutPrototype.getDescription(),
129                                                    layoutPrototype.isActive(), serviceContext);
130                            }
131                            else {
132                                    importedLayoutPrototype =
133                                            LayoutPrototypeLocalServiceUtil.updateLayoutPrototype(
134                                                    existingLayoutPrototype.getLayoutPrototypeId(),
135                                                    layoutPrototype.getNameMap(),
136                                                    layoutPrototype.getDescription(),
137                                                    layoutPrototype.isActive(), serviceContext);
138                            }
139                    }
140                    else {
141                            importedLayoutPrototype =
142                                    LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
143                                            userId, portletDataContext.getCompanyId(),
144                                            layoutPrototype.getNameMap(),
145                                            layoutPrototype.getDescription(),
146                                            layoutPrototype.isActive(), serviceContext);
147                    }
148    
149                    importLayouts(
150                            portletDataContext, layoutPrototype,
151                            importedLayoutPrototype.getGroupId());
152    
153                    portletDataContext.importClassedModel(
154                            layoutPrototype, importedLayoutPrototype);
155            }
156    
157            protected void exportLayouts(
158                            PortletDataContext portletDataContext,
159                            LayoutPrototype layoutPrototype, Element layoutPrototypeElement)
160                    throws Exception {
161    
162                    File file = null;
163                    InputStream inputStream = null;
164    
165                    try {
166                            Group group = layoutPrototype.getGroup();
167    
168                            Map<String, String[]> parameterMap = getLayoutPrototypeParameters();
169    
170                            file = LayoutLocalServiceUtil.exportLayoutsAsFile(
171                                    group.getGroupId(), true, null, parameterMap, null, null);
172    
173                            inputStream = new FileInputStream(file);
174    
175                            String layoutPrototypeLARPath =
176                                    ExportImportPathUtil.getModelPath(
177                                            layoutPrototype,
178                                            getLayoutPrototypeLARFileName(layoutPrototype));
179    
180                            if (portletDataContext.isPathNotProcessed(layoutPrototypeLARPath)) {
181                                    portletDataContext.addZipEntry(
182                                            layoutPrototypeLARPath, inputStream);
183                            }
184    
185                            List<Layout> layoutPrototypeLayouts =
186                                    LayoutLocalServiceUtil.getLayouts(
187                                            layoutPrototype.getGroupId(), true);
188    
189                            for (Layout layoutPrototypeLayout : layoutPrototypeLayouts) {
190                                    portletDataContext.addReferenceElement(
191                                            layoutPrototype, layoutPrototypeElement,
192                                            layoutPrototypeLayout,
193                                            PortletDataContext.REFERENCE_TYPE_EMBEDDED, false);
194                            }
195                    }
196                    finally {
197                            StreamUtil.cleanUp(inputStream);
198    
199                            if (file != null) {
200                                    file.delete();
201                            }
202                    }
203            }
204    
205            private String getLayoutPrototypeLARFileName(
206                    LayoutPrototype layoutPrototype) {
207    
208                    return layoutPrototype.getLayoutPrototypeId() + ".lar";
209            }
210    
211            protected void importLayouts(
212                            PortletDataContext portletDataContext,
213                            LayoutPrototype layoutPrototype, long importedGroupId)
214                    throws PortalException {
215    
216                    InputStream inputStream = null;
217    
218                    try {
219                            String layoutPrototypeLARPath =
220                                    ExportImportPathUtil.getModelPath(
221                                            layoutPrototype,
222                                            getLayoutPrototypeLARFileName(layoutPrototype));
223    
224                            inputStream = portletDataContext.getZipEntryAsInputStream(
225                                    layoutPrototypeLARPath);
226    
227                            Map<String, String[]> parameterMap = getLayoutPrototypeParameters();
228    
229                            LayoutServiceUtil.importLayouts(
230                                    importedGroupId, true, parameterMap, inputStream);
231                    }
232                    catch (SystemException se) {
233                            throw new RuntimeException(se);
234                    }
235                    finally {
236                            StreamUtil.cleanUp(inputStream);
237                    }
238            }
239    
240            @Override
241            protected boolean validateMissingReference(
242                            String uuid, long companyId, long groupId)
243                    throws Exception {
244    
245                    LayoutPrototype layoutPrototype =
246                            LayoutPrototypeLocalServiceUtil.
247                                    fetchLayoutPrototypeByUuidAndCompanyId(uuid, companyId);
248    
249                    if (layoutPrototype == null) {
250                            return false;
251                    }
252    
253                    return true;
254            }
255    
256            private Map<String, String[]> getLayoutPrototypeParameters() {
257    
258                    Map<String, String[]> parameterMap =
259                            new LinkedHashMap<String, String[]>();
260    
261                    parameterMap.put(
262                            PortletDataHandlerKeys.CATEGORIES,
263                            new String[] {Boolean.TRUE.toString()});
264                    parameterMap.put(
265                            PortletDataHandlerKeys.DATA_STRATEGY,
266                            new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
267                    parameterMap.put(
268                            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
269                            new String[] {Boolean.TRUE.toString()});
270                    parameterMap.put(
271                            PortletDataHandlerKeys.DELETE_PORTLET_DATA,
272                            new String[] {Boolean.FALSE.toString()});
273                    parameterMap.put(
274                            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
275                            new String[] {
276                                    PortletDataHandlerKeys.
277                                            LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE
278                            });
279                    parameterMap.put(
280                            PortletDataHandlerKeys.PERFORM_DIRECT_BINARY_IMPORT,
281                            new String[] {Boolean.TRUE.toString()});
282                    parameterMap.put(
283                            PortletDataHandlerKeys.PERMISSIONS,
284                            new String[] {Boolean.TRUE.toString()});
285                    parameterMap.put(
286                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
287                            new String[] {Boolean.TRUE.toString()});
288                    parameterMap.put(
289                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
290                            new String[] {Boolean.TRUE.toString()});
291                    parameterMap.put(
292                            PortletDataHandlerKeys.PORTLET_DATA,
293                            new String[] {Boolean.TRUE.toString()});
294                    parameterMap.put(
295                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
296                            new String[] {Boolean.TRUE.toString()});
297                    parameterMap.put(
298                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
299                            new String[] {Boolean.TRUE.toString()});
300                    parameterMap.put(
301                            PortletDataHandlerKeys.USER_ID_STRATEGY,
302                            new String[] {UserIdStrategy.CURRENT_USER_ID});
303    
304                    return parameterMap;
305            }
306    
307    }