001    /**
002     * Copyright (c) 2000-2012 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.dynamicdatamapping.lar;
016    
017    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
030    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
031    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
032    import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMStructureUtil;
033    import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMTemplateUtil;
034    
035    import java.util.List;
036    import java.util.Map;
037    
038    import javax.portlet.PortletPreferences;
039    
040    /**
041     * @author Marcellus Tavares
042     */
043    public class DDMPortletDataHandlerImpl extends BasePortletDataHandler {
044    
045            public static void exportStructure(
046                            PortletDataContext portletDataContext, Element structuresElement,
047                            DDMStructure structure)
048                    throws Exception {
049    
050                    String path = getStructurePath(portletDataContext, structure);
051    
052                    if (!portletDataContext.isPathNotProcessed(path)) {
053                            return;
054                    }
055    
056                    Element structureElement = structuresElement.addElement("structure");
057    
058                    portletDataContext.addClassedModel(
059                            structureElement, path, structure, _NAMESPACE);
060            }
061    
062            public static void importStructure(
063                            PortletDataContext portletDataContext, Element structureElement)
064                    throws Exception {
065    
066                    String path = structureElement.attributeValue("path");
067    
068                    if (!portletDataContext.isPathNotProcessed(path)) {
069                            return;
070                    }
071    
072                    DDMStructure structure =
073                            (DDMStructure)portletDataContext.getZipEntryAsObject(path);
074    
075                    long userId = portletDataContext.getUserId(structure.getUserUuid());
076    
077                    ServiceContext serviceContext = portletDataContext.createServiceContext(
078                            structureElement, structure, _NAMESPACE);
079    
080                    DDMStructure importedStructure = null;
081    
082                    if (portletDataContext.isDataStrategyMirror()) {
083                            DDMStructure existingStructure = DDMStructureUtil.fetchByUUID_G(
084                                    structure.getUuid(), portletDataContext.getScopeGroupId());
085    
086                            if (existingStructure == null) {
087                                    serviceContext.setUuid(structure.getUuid());
088    
089                                    importedStructure = DDMStructureLocalServiceUtil.addStructure(
090                                            userId, portletDataContext.getScopeGroupId(),
091                                            structure.getClassNameId(), structure.getStructureKey(),
092                                            structure.getNameMap(), structure.getDescriptionMap(),
093                                            structure.getXsd(), structure.getStorageType(),
094                                            structure.getType(), serviceContext);
095                            }
096                            else {
097                                    importedStructure =
098                                            DDMStructureLocalServiceUtil.updateStructure(
099                                                    existingStructure.getStructureId(),
100                                                    structure.getNameMap(), structure.getDescriptionMap(),
101                                                    structure.getXsd(), serviceContext);
102                            }
103                    }
104                    else {
105                            importedStructure = DDMStructureLocalServiceUtil.addStructure(
106                                    userId, portletDataContext.getScopeGroupId(),
107                                    structure.getClassNameId(), structure.getStructureKey(),
108                                    structure.getNameMap(), structure.getDescriptionMap(),
109                                    structure.getXsd(), structure.getStorageType(),
110                                    structure.getType(), serviceContext);
111                    }
112    
113                    portletDataContext.importClassedModel(
114                            structure, importedStructure, _NAMESPACE);
115            }
116    
117            @Override
118            public PortletDataHandlerControl[] getExportControls() {
119                    return new PortletDataHandlerControl[] {_structures, _templates};
120            }
121    
122            @Override
123            public PortletDataHandlerControl[] getImportControls() {
124                    return new PortletDataHandlerControl[] {_structures, _templates};
125            }
126    
127            @Override
128            public boolean isAlwaysExportable() {
129                    return _ALWAYS_EXPORTABLE;
130            }
131    
132            protected static void exportTemplate(
133                            PortletDataContext portletDataContext, Element templatesElement,
134                            DDMTemplate template)
135                    throws Exception {
136    
137                    String path = getTemplatePath(portletDataContext, template);
138    
139                    if (!portletDataContext.isPathNotProcessed(path)) {
140                            return;
141                    }
142    
143                    Element templateElement = templatesElement.addElement("template");
144    
145                    portletDataContext.addClassedModel(
146                            templateElement, path, template, _NAMESPACE);
147            }
148    
149            protected static String getStructurePath(
150                    PortletDataContext portletDataContext, DDMStructure structure) {
151    
152                    StringBundler sb = new StringBundler(4);
153    
154                    sb.append(
155                            portletDataContext.getPortletPath(
156                                    PortletKeys.DYNAMIC_DATA_MAPPING));
157                    sb.append("/structures/");
158                    sb.append(structure.getStructureId());
159                    sb.append(".xml");
160    
161                    return sb.toString();
162            }
163    
164            protected static String getTemplatePath(
165                    PortletDataContext portletDataContext, DDMTemplate template) {
166    
167                    StringBundler sb = new StringBundler(4);
168    
169                    sb.append(
170                            portletDataContext.getPortletPath(
171                                    PortletKeys.DYNAMIC_DATA_MAPPING));
172                    sb.append("/templates/");
173                    sb.append(template.getTemplateId());
174                    sb.append(".xml");
175    
176                    return sb.toString();
177            }
178    
179            protected static void importTemplate(
180                            PortletDataContext portletDataContext, Element templateElement)
181                    throws Exception {
182    
183                    String path = templateElement.attributeValue("path");
184    
185                    if (!portletDataContext.isPathNotProcessed(path)) {
186                            return;
187                    }
188    
189                    DDMTemplate template =
190                            (DDMTemplate)portletDataContext.getZipEntryAsObject(path);
191    
192                    long userId = portletDataContext.getUserId(template.getUserUuid());
193    
194                    Map<Long, Long> structureIds =
195                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
196                                    DDMStructure.class);
197    
198                    long structureId = MapUtil.getLong(
199                            structureIds, template.getStructureId(), template.getStructureId());
200    
201                    ServiceContext serviceContext = portletDataContext.createServiceContext(
202                            templateElement, template, _NAMESPACE);
203    
204                    DDMTemplate importedTemplate = null;
205    
206                    if (portletDataContext.isDataStrategyMirror()) {
207                            DDMTemplate existingTemplate = DDMTemplateUtil.fetchByUUID_G(
208                                    template.getUuid(), portletDataContext.getScopeGroupId());
209    
210                            if (existingTemplate == null) {
211                                    serviceContext.setUuid(template.getUuid());
212    
213                                    importedTemplate = DDMTemplateLocalServiceUtil.addTemplate(
214                                            userId, portletDataContext.getScopeGroupId(), structureId,
215                                            template.getNameMap(), template.getDescriptionMap(),
216                                            template.getType(), template.getMode(),
217                                            template.getLanguage(), template.getScript(),
218                                            serviceContext);
219                            }
220                            else {
221                                    importedTemplate = DDMTemplateLocalServiceUtil.updateTemplate(
222                                            existingTemplate.getTemplateId(), template.getNameMap(),
223                                            template.getDescriptionMap(), template.getType(),
224                                            template.getMode(), template.getLanguage(),
225                                            template.getScript(), serviceContext);
226                            }
227                    }
228                    else {
229                            importedTemplate = DDMTemplateLocalServiceUtil.addTemplate(
230                                    userId, portletDataContext.getScopeGroupId(), structureId,
231                                    template.getNameMap(), template.getDescriptionMap(),
232                                    template.getType(), template.getMode(), template.getLanguage(),
233                                    template.getScript(), serviceContext);
234                    }
235    
236                    portletDataContext.importClassedModel(
237                            template, importedTemplate, _NAMESPACE);
238            }
239    
240            @Override
241            protected PortletPreferences doDeleteData(
242                            PortletDataContext portletDataContext, String portletId,
243                            PortletPreferences portletPreferences)
244                    throws Exception {
245    
246                    if (!portletDataContext.addPrimaryKey(
247                                    DDMPortletDataHandlerImpl.class, "deleteData")) {
248    
249                            DDMTemplateLocalServiceUtil.deleteTemplates(
250                                    portletDataContext.getScopeGroupId());
251    
252                            DDMStructureLocalServiceUtil.deleteStructures(
253                                    portletDataContext.getScopeGroupId());
254                    }
255    
256                    return portletPreferences;
257            }
258    
259            @Override
260            protected String doExportData(
261                            PortletDataContext portletDataContext, String portletId,
262                            PortletPreferences portletPreferences)
263                    throws Exception {
264    
265                    portletDataContext.addPermissions(
266                            "com.liferay.portlet.dynamicdatamapping",
267                            portletDataContext.getScopeGroupId());
268    
269                    Document document = SAXReaderUtil.createDocument();
270    
271                    Element rootElement = document.addElement("ddm-data");
272    
273                    rootElement.addAttribute(
274                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
275    
276                    Element structuresElement = rootElement.addElement("structures");
277    
278                    List<DDMStructure> ddmStructures = DDMStructureUtil.findByGroupId(
279                            portletDataContext.getScopeGroupId());
280    
281                    for (DDMStructure structure : ddmStructures) {
282                            if (portletDataContext.isWithinDateRange(
283                                            structure.getModifiedDate())) {
284    
285                                    exportStructure(
286                                            portletDataContext, structuresElement, structure);
287                            }
288                    }
289    
290                    if (portletDataContext.getBooleanParameter(_NAMESPACE, "templates")) {
291                            Element templatesElement = rootElement.addElement("templates");
292    
293                            List<DDMTemplate> templates = DDMTemplateUtil.findByGroupId(
294                                    portletDataContext.getScopeGroupId());
295    
296                            for (DDMTemplate template : templates) {
297                                    if (portletDataContext.isWithinDateRange(
298                                                    template.getModifiedDate())) {
299    
300                                            exportTemplate(
301                                                    portletDataContext, templatesElement, template);
302                                    }
303                            }
304                    }
305    
306                    return document.formattedString();
307            }
308    
309            @Override
310            protected PortletPreferences doImportData(
311                            PortletDataContext portletDataContext, String portletId,
312                            PortletPreferences portletPreferences, String data)
313                    throws Exception {
314    
315                    portletDataContext.importPermissions(
316                            "com.liferay.portlet.dynamicdatamapping",
317                            portletDataContext.getSourceGroupId(),
318                            portletDataContext.getScopeGroupId());
319    
320                    Document document = SAXReaderUtil.read(data);
321    
322                    Element rootElement = document.getRootElement();
323    
324                    Element structuresElement = rootElement.element("structures");
325    
326                    List<Element> structureElements = structuresElement.elements(
327                            "structure");
328    
329                    for (Element structureElement : structureElements) {
330                            importStructure(portletDataContext, structureElement);
331                    }
332    
333                    if (portletDataContext.getBooleanParameter(_NAMESPACE, "templates")) {
334                            Element templatesElement = rootElement.element("templates");
335    
336                            List<Element> templateElements = templatesElement.elements(
337                                    "template");
338    
339                            for (Element templateElement : templateElements) {
340                                    importTemplate(portletDataContext, templateElement);
341                            }
342                    }
343    
344                    return portletPreferences;
345            }
346    
347            private static final boolean _ALWAYS_EXPORTABLE = true;
348    
349            private static final String _NAMESPACE = "ddm";
350    
351            private static PortletDataHandlerBoolean _structures =
352                    new PortletDataHandlerBoolean(_NAMESPACE, "structures", true, true);
353    
354            private static PortletDataHandlerBoolean _templates =
355                    new PortletDataHandlerBoolean(_NAMESPACE, "templates");
356    
357    }