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.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.StagedModelPathUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.model.Image;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.persistence.ImageUtil;
030    import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
033    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
034    import com.liferay.portlet.dynamicdatamapping.service.persistence.DDMTemplateUtil;
035    import com.liferay.portlet.journal.lar.JournalPortletDataHandler;
036    
037    import java.io.File;
038    
039    import java.util.Map;
040    
041    /**
042     * @author Mate Thurzo
043     * @author Daniel Kocsis
044     */
045    public class DDMTemplateStagedModelDataHandler
046            extends BaseStagedModelDataHandler<DDMTemplate> {
047    
048            @Override
049            public String getClassName() {
050                    return DDMTemplate.class.getName();
051            }
052    
053            protected DDMTemplate addTemplate(
054                            long userId, long groupId, DDMTemplate template, long classPK,
055                            File smallFile, ServiceContext serviceContext)
056                    throws Exception {
057    
058                    DDMTemplate newTemplate = null;
059    
060                    try {
061                            return DDMTemplateLocalServiceUtil.addTemplate(
062                                    userId, groupId, template.getClassNameId(), classPK,
063                                    template.getTemplateKey(), template.getNameMap(),
064                                    template.getDescriptionMap(), template.getType(),
065                                    template.getMode(), template.getLanguage(),
066                                    template.getScript(), template.isCacheable(),
067                                    template.isSmallImage(), template.getSmallImageURL(), smallFile,
068                                    serviceContext);
069                    }
070                    catch (TemplateDuplicateTemplateKeyException tdtke) {
071                            newTemplate = DDMTemplateLocalServiceUtil.addTemplate(
072                                    userId, groupId, template.getClassNameId(), classPK, null,
073                                    template.getNameMap(), template.getDescriptionMap(),
074                                    template.getType(), template.getMode(), template.getLanguage(),
075                                    template.getScript(), template.isCacheable(),
076                                    template.isSmallImage(), template.getSmallImageURL(), smallFile,
077                                    serviceContext);
078    
079                            if (_log.isWarnEnabled()) {
080                                    _log.warn(
081                                            "A template with the key " + template.getTemplateKey() +
082                                                    " already exists. The new generated key is " +
083                                                            newTemplate.getTemplateKey());
084                            }
085                    }
086    
087                    return newTemplate;
088            }
089    
090            @Override
091            protected void doExportStagedModel(
092                            PortletDataContext portletDataContext, Element[] elements,
093                            DDMTemplate template)
094                    throws Exception {
095    
096                    Element templatesElement = elements[0];
097                    Element dlFileEntryTypesElement = null;
098                    Element dlFoldersElement = null;
099                    Element dlFileEntriesElement = null;
100                    Element dlFileRanksElement = null;
101                    Element dlRepositoriesElement = null;
102                    Element dlRepositoryEntriesElement = null;
103    
104                    if (elements.length > 1) {
105                            dlFileEntryTypesElement = elements[1];
106                            dlFoldersElement = elements[2];
107                            dlFileEntriesElement = elements[3];
108                            dlFileRanksElement = elements[4];
109                            dlRepositoriesElement = elements[5];
110                            dlRepositoryEntriesElement = elements[6];
111                    }
112    
113                    Element templateElement = templatesElement.addElement("template");
114    
115                    if (template.isSmallImage()) {
116                            Image smallImage = ImageUtil.fetchByPrimaryKey(
117                                    template.getSmallImageId());
118    
119                            if (Validator.isNotNull(template.getSmallImageURL())) {
120                                    String smallImageURL =
121                                            DDMPortletDataHandler.exportReferencedContent(
122                                                    portletDataContext, dlFileEntryTypesElement,
123                                                    dlFoldersElement, dlFileEntriesElement,
124                                                    dlFileRanksElement, dlRepositoriesElement,
125                                                    dlRepositoryEntriesElement, templateElement,
126                                                    template.getSmallImageURL().concat(StringPool.SPACE));
127    
128                                    template.setSmallImageURL(smallImageURL);
129                            }
130                            else if (smallImage != null) {
131                                    String smallImagePath = StagedModelPathUtil.getPath(
132                                            template,
133                                            smallImage.getImageId() + StringPool.PERIOD +
134                                                    template.getSmallImageType());
135    
136                                    templateElement.addAttribute(
137                                            "small-image-path", smallImagePath);
138    
139                                    template.setSmallImageType(smallImage.getType());
140    
141                                    portletDataContext.addZipEntry(
142                                            smallImagePath, smallImage.getTextObj());
143                            }
144                    }
145    
146                    if (portletDataContext.getBooleanParameter(
147                                    DDMPortletDataHandler.NAMESPACE, "embedded-assets")) {
148    
149                            String content = DDMPortletDataHandler.exportReferencedContent(
150                                    portletDataContext, dlFileEntryTypesElement, dlFoldersElement,
151                                    dlFileEntriesElement, dlFileRanksElement, dlRepositoriesElement,
152                                    dlRepositoryEntriesElement, templateElement,
153                                    template.getScript());
154    
155                            template.setScript(content);
156                    }
157    
158                    portletDataContext.addClassedModel(
159                            templateElement, StagedModelPathUtil.getPath(template), template,
160                            DDMPortletDataHandler.NAMESPACE);
161            }
162    
163            @Override
164            protected void doImportStagedModel(
165                            PortletDataContext portletDataContext, Element element, String path,
166                            DDMTemplate template)
167                    throws Exception {
168    
169                    long userId = portletDataContext.getUserId(template.getUserUuid());
170    
171                    Map<Long, Long> structureIds =
172                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
173                                    DDMStructure.class);
174    
175                    long classPK = MapUtil.getLong(
176                            structureIds, template.getClassPK(), template.getClassPK());
177    
178                    File smallFile = null;
179    
180                    if (template.isSmallImage()) {
181                            String smallImagePath = element.attributeValue("small-image-path");
182    
183                            if (Validator.isNotNull(template.getSmallImageURL())) {
184                                    String smallImageURL =
185                                            JournalPortletDataHandler.importReferencedContent(
186                                                    portletDataContext, element,
187                                                    template.getSmallImageURL());
188    
189                                    template.setSmallImageURL(smallImageURL);
190                            }
191                            else if (Validator.isNotNull(smallImagePath)) {
192                                    byte[] bytes = portletDataContext.getZipEntryAsByteArray(
193                                            smallImagePath);
194    
195                                    if (bytes != null) {
196                                            smallFile = FileUtil.createTempFile(
197                                                    template.getSmallImageType());
198    
199                                            FileUtil.write(smallFile, bytes);
200                                    }
201                            }
202                    }
203    
204                    ServiceContext serviceContext = portletDataContext.createServiceContext(
205                            element, template, DDMPortletDataHandler.NAMESPACE);
206    
207                    DDMTemplate importedTemplate = null;
208    
209                    if (portletDataContext.isDataStrategyMirror()) {
210                            DDMTemplate existingTemplate = DDMTemplateUtil.fetchByUUID_G(
211                                    template.getUuid(), portletDataContext.getScopeGroupId());
212    
213                            if (existingTemplate == null) {
214                                    serviceContext.setUuid(template.getUuid());
215    
216                                    importedTemplate = addTemplate(
217                                            userId, portletDataContext.getScopeGroupId(), template,
218                                            classPK, smallFile, 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(), template.isCacheable(),
226                                            template.isSmallImage(), template.getSmallImageURL(),
227                                            smallFile, serviceContext);
228                            }
229                    }
230                    else {
231                            importedTemplate = addTemplate(
232                                    userId, portletDataContext.getScopeGroupId(), template, classPK,
233                                    smallFile, serviceContext);
234                    }
235    
236                    portletDataContext.importClassedModel(
237                            template, importedTemplate, DDMPortletDataHandler.NAMESPACE);
238            }
239    
240            private static Log _log = LogFactoryUtil.getLog(
241                    DDMTemplateStagedModelDataHandler.class);
242    
243    }