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.dynamicdatamapping.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.ExportImportHelperUtil;
021    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
022    import com.liferay.portal.kernel.lar.PortletDataContext;
023    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.FileUtil;
027    import com.liferay.portal.kernel.util.MapUtil;
028    import com.liferay.portal.kernel.util.StringBundler;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.model.Image;
033    import com.liferay.portal.service.ImageLocalServiceUtil;
034    import com.liferay.portal.service.ServiceContext;
035    import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
038    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
040    
041    import java.io.File;
042    
043    import java.util.Map;
044    
045    /**
046     * @author Mate Thurzo
047     * @author Daniel Kocsis
048     */
049    public class DDMTemplateStagedModelDataHandler
050            extends BaseStagedModelDataHandler<DDMTemplate> {
051    
052            public static final String[] CLASS_NAMES = {DDMTemplate.class.getName()};
053    
054            @Override
055            public void deleteStagedModel(
056                            String uuid, long groupId, String className, String extraData)
057                    throws PortalException, SystemException {
058    
059                    DDMTemplate ddmTemplate =
060                            DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
061                                    uuid, groupId);
062    
063                    if (ddmTemplate != null) {
064                            DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
065                    }
066            }
067    
068            @Override
069            public String[] getClassNames() {
070                    return CLASS_NAMES;
071            }
072    
073            @Override
074            public String getDisplayName(DDMTemplate template) {
075                    return template.getNameCurrentValue();
076            }
077    
078            protected DDMTemplate addTemplate(
079                            long userId, long groupId, DDMTemplate template, long classPK,
080                            File smallFile, ServiceContext serviceContext)
081                    throws Exception {
082    
083                    DDMTemplate newTemplate = null;
084    
085                    try {
086                            return DDMTemplateLocalServiceUtil.addTemplate(
087                                    userId, groupId, template.getClassNameId(), classPK,
088                                    template.getTemplateKey(), template.getNameMap(),
089                                    template.getDescriptionMap(), template.getType(),
090                                    template.getMode(), template.getLanguage(),
091                                    template.getScript(), template.isCacheable(),
092                                    template.isSmallImage(), template.getSmallImageURL(), smallFile,
093                                    serviceContext);
094                    }
095                    catch (TemplateDuplicateTemplateKeyException tdtke) {
096                            newTemplate = DDMTemplateLocalServiceUtil.addTemplate(
097                                    userId, groupId, template.getClassNameId(), classPK, null,
098                                    template.getNameMap(), template.getDescriptionMap(),
099                                    template.getType(), template.getMode(), template.getLanguage(),
100                                    template.getScript(), template.isCacheable(),
101                                    template.isSmallImage(), template.getSmallImageURL(), smallFile,
102                                    serviceContext);
103    
104                            if (_log.isWarnEnabled()) {
105                                    StringBundler sb = new StringBundler(4);
106    
107                                    sb.append("A template with the key ");
108                                    sb.append(template.getTemplateKey());
109                                    sb.append(" already exists. The new generated key is ");
110                                    sb.append(newTemplate.getTemplateKey());
111    
112                                    _log.warn(sb.toString());
113                            }
114                    }
115    
116                    return newTemplate;
117            }
118    
119            @Override
120            protected void doExportStagedModel(
121                            PortletDataContext portletDataContext, DDMTemplate template)
122                    throws Exception {
123    
124                    Element templateElement = portletDataContext.getExportDataElement(
125                            template);
126    
127                    DDMStructure structure = DDMStructureLocalServiceUtil.fetchStructure(
128                            template.getClassPK());
129    
130                    if (structure != null) {
131                            StagedModelDataHandlerUtil.exportStagedModel(
132                                    portletDataContext, structure);
133    
134                            portletDataContext.addReferenceElement(
135                                    template, templateElement, structure,
136                                    PortletDataContext.REFERENCE_TYPE_STRONG, false);
137                    }
138    
139                    if (template.isSmallImage()) {
140                            Image smallImage = ImageLocalServiceUtil.fetchImage(
141                                    template.getSmallImageId());
142    
143                            if (Validator.isNotNull(template.getSmallImageURL())) {
144                                    String smallImageURL =
145                                            ExportImportHelperUtil.replaceExportContentReferences(
146                                                    portletDataContext, template, templateElement,
147                                                    template.getSmallImageURL().concat(StringPool.SPACE),
148                                                    true);
149    
150                                    template.setSmallImageURL(smallImageURL);
151                            }
152                            else if (smallImage != null) {
153                                    String smallImagePath = ExportImportPathUtil.getModelPath(
154                                            template,
155                                            smallImage.getImageId() + StringPool.PERIOD +
156                                                    template.getSmallImageType());
157    
158                                    templateElement.addAttribute(
159                                            "small-image-path", smallImagePath);
160    
161                                    template.setSmallImageType(smallImage.getType());
162    
163                                    portletDataContext.addZipEntry(
164                                            smallImagePath, smallImage.getTextObj());
165                            }
166                    }
167    
168                    if (portletDataContext.getBooleanParameter(
169                                    DDMPortletDataHandler.NAMESPACE, "referenced-content")) {
170    
171                            String content =
172                                    ExportImportHelperUtil.replaceExportContentReferences(
173                                            portletDataContext, template, templateElement,
174                                            template.getScript(), true);
175    
176                            template.setScript(content);
177                    }
178    
179                    portletDataContext.addClassedModel(
180                            templateElement, ExportImportPathUtil.getModelPath(template),
181                            template, DDMPortletDataHandler.NAMESPACE);
182            }
183    
184            @Override
185            protected void doImportStagedModel(
186                            PortletDataContext portletDataContext, DDMTemplate template)
187                    throws Exception {
188    
189                    long userId = portletDataContext.getUserId(template.getUserUuid());
190    
191                    long classPK = template.getClassPK();
192    
193                    Element structureElement = portletDataContext.getReferenceDataElement(
194                            template, DDMStructure.class, classPK);
195    
196                    if (structureElement != null) {
197                            StagedModelDataHandlerUtil.importStagedModel(
198                                    portletDataContext, structureElement);
199    
200                            Map<Long, Long> structureIds =
201                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
202                                            DDMStructure.class);
203    
204                            classPK = MapUtil.getLong(structureIds, classPK, classPK);
205                    }
206    
207                    File smallFile = null;
208    
209                    try {
210                            if (template.isSmallImage()) {
211                                    Element element =
212                                            portletDataContext.getImportDataStagedModelElement(
213                                                    template);
214    
215                                    String smallImagePath = element.attributeValue(
216                                            "small-image-path");
217    
218                                    if (Validator.isNotNull(template.getSmallImageURL())) {
219                                            String smallImageURL =
220                                                    ExportImportHelperUtil.replaceImportContentReferences(
221                                                            portletDataContext, element,
222                                                            template.getSmallImageURL(), true);
223    
224                                            template.setSmallImageURL(smallImageURL);
225                                    }
226                                    else if (Validator.isNotNull(smallImagePath)) {
227                                            byte[] bytes = portletDataContext.getZipEntryAsByteArray(
228                                                    smallImagePath);
229    
230                                            if (bytes != null) {
231                                                    smallFile = FileUtil.createTempFile(
232                                                            template.getSmallImageType());
233    
234                                                    FileUtil.write(smallFile, bytes);
235                                            }
236                                    }
237                            }
238    
239                            ServiceContext serviceContext =
240                                    portletDataContext.createServiceContext(
241                                            template, DDMPortletDataHandler.NAMESPACE);
242    
243                            DDMTemplate importedTemplate = null;
244    
245                            if (portletDataContext.isDataStrategyMirror()) {
246                                    DDMTemplate existingTemplate =
247                                            DDMTemplateLocalServiceUtil.
248                                                    fetchDDMTemplateByUuidAndGroupId(
249                                                            template.getUuid(),
250                                                            portletDataContext.getScopeGroupId());
251    
252                                    if (existingTemplate == null) {
253                                            existingTemplate =
254                                                    DDMTemplateLocalServiceUtil.
255                                                            fetchDDMTemplateByUuidAndGroupId(
256                                                                    template.getUuid(),
257                                                                    portletDataContext.getCompanyGroupId());
258                                    }
259    
260                                    if (existingTemplate == null) {
261                                            serviceContext.setUuid(template.getUuid());
262    
263                                            importedTemplate = addTemplate(
264                                                    userId, portletDataContext.getScopeGroupId(), template,
265                                                    classPK, smallFile, serviceContext);
266                                    }
267                                    else if (portletDataContext.isCompanyStagedGroupedModel(
268                                                            existingTemplate)) {
269    
270                                            return;
271                                    }
272                                    else {
273                                            importedTemplate =
274                                                    DDMTemplateLocalServiceUtil.updateTemplate(
275                                                            existingTemplate.getTemplateId(),
276                                                            template.getClassPK(), template.getNameMap(),
277                                                            template.getDescriptionMap(), template.getType(),
278                                                            template.getMode(), template.getLanguage(),
279                                                            template.getScript(), template.isCacheable(),
280                                                            template.isSmallImage(),
281                                                            template.getSmallImageURL(), smallFile,
282                                                            serviceContext);
283                                    }
284                            }
285                            else {
286                                    importedTemplate = addTemplate(
287                                            userId, portletDataContext.getScopeGroupId(), template,
288                                            classPK, smallFile, serviceContext);
289                            }
290    
291                            portletDataContext.importClassedModel(
292                                    template, importedTemplate, DDMPortletDataHandler.NAMESPACE);
293    
294                            Map<String, String> ddmTemplateKeys =
295                                    (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
296                                            DDMTemplate.class + ".ddmTemplateKey");
297    
298                            ddmTemplateKeys.put(
299                                    template.getTemplateKey(), importedTemplate.getTemplateKey());
300                    }
301                    finally {
302                            if (smallFile != null) {
303                                    smallFile.delete();
304                            }
305                    }
306            }
307    
308            private static Log _log = LogFactoryUtil.getLog(
309                    DDMTemplateStagedModelDataHandler.class);
310    
311    }