001    /**
002     * Copyright (c) 2000-present 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.blogs.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.servlet.taglib.ui.ImageSelector;
023    import com.liferay.portal.kernel.trash.TrashHandler;
024    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
025    import com.liferay.portal.kernel.util.MimeTypesUtil;
026    import com.liferay.portal.kernel.util.StreamUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.TempFileEntryUtil;
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.portletfilerepository.PortletFileRepositoryUtil;
034    import com.liferay.portal.service.ImageLocalServiceUtil;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portlet.blogs.model.BlogsEntry;
037    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
038    import com.liferay.portlet.documentlibrary.lar.FileEntryUtil;
039    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
040    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
041    import com.liferay.portlet.exportimport.lar.ExportImportHelperUtil;
042    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
043    import com.liferay.portlet.exportimport.lar.PortletDataContext;
044    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
045    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
046    
047    import java.io.InputStream;
048    
049    import java.util.Calendar;
050    import java.util.List;
051    
052    /**
053     * @author Zsolt Berentey
054     */
055    public class BlogsEntryStagedModelDataHandler
056            extends BaseStagedModelDataHandler<BlogsEntry> {
057    
058            public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
059    
060            @Override
061            public void deleteStagedModel(BlogsEntry entry) throws PortalException {
062                    BlogsEntryLocalServiceUtil.deleteEntry(entry);
063            }
064    
065            @Override
066            public void deleteStagedModel(
067                            String uuid, long groupId, String className, String extraData)
068                    throws PortalException {
069    
070                    BlogsEntry entry = fetchStagedModelByUuidAndGroupId(uuid, groupId);
071    
072                    if (entry != null) {
073                            deleteStagedModel(entry);
074                    }
075            }
076    
077            @Override
078            public BlogsEntry fetchStagedModelByUuidAndGroupId(
079                    String uuid, long groupId) {
080    
081                    return BlogsEntryLocalServiceUtil.fetchBlogsEntryByUuidAndGroupId(
082                            uuid, groupId);
083            }
084    
085            @Override
086            public List<BlogsEntry> fetchStagedModelsByUuidAndCompanyId(
087                    String uuid, long companyId) {
088    
089                    return BlogsEntryLocalServiceUtil.getBlogsEntriesByUuidAndCompanyId(
090                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
091                            new StagedModelModifiedDateComparator<BlogsEntry>());
092            }
093    
094            @Override
095            public String[] getClassNames() {
096                    return CLASS_NAMES;
097            }
098    
099            @Override
100            public String getDisplayName(BlogsEntry entry) {
101                    return entry.getTitle();
102            }
103    
104            @Override
105            protected void doExportStagedModel(
106                            PortletDataContext portletDataContext, BlogsEntry entry)
107                    throws Exception {
108    
109                    Element entryElement = portletDataContext.getExportDataElement(entry);
110    
111                    if (entry.isSmallImage()) {
112                            Image smallImage = ImageLocalServiceUtil.fetchImage(
113                                    entry.getSmallImageId());
114    
115                            if (Validator.isNotNull(entry.getSmallImageURL())) {
116                                    String smallImageURL =
117                                            ExportImportHelperUtil.replaceExportContentReferences(
118                                                    portletDataContext, entry,
119                                                    entry.getSmallImageURL() + StringPool.SPACE, true);
120    
121                                    entry.setSmallImageURL(smallImageURL);
122                            }
123                            else if (smallImage != null) {
124                                    String smallImagePath = ExportImportPathUtil.getModelPath(
125                                            entry,
126                                            smallImage.getImageId() + StringPool.PERIOD +
127                                                    smallImage.getType());
128    
129                                    entryElement.addAttribute("small-image-path", smallImagePath);
130    
131                                    entry.setSmallImageType(smallImage.getType());
132    
133                                    portletDataContext.addZipEntry(
134                                            smallImagePath, smallImage.getTextObj());
135                            }
136                    }
137    
138                    if (entry.getSmallImageFileEntryId() != 0) {
139                            FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
140                                    entry.getSmallImageFileEntryId());
141    
142                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
143                                    portletDataContext, entry, fileEntry,
144                                    PortletDataContext.REFERENCE_TYPE_WEAK);
145                    }
146    
147                    String content = ExportImportHelperUtil.replaceExportContentReferences(
148                            portletDataContext, entry, entry.getContent(),
149                            portletDataContext.getBooleanParameter(
150                                    BlogsPortletDataHandler.NAMESPACE, "referenced-content"));
151    
152                    entry.setContent(content);
153    
154                    portletDataContext.addClassedModel(
155                            entryElement, ExportImportPathUtil.getModelPath(entry), entry);
156            }
157    
158            @Override
159            protected void doImportStagedModel(
160                            PortletDataContext portletDataContext, BlogsEntry entry)
161                    throws Exception {
162    
163                    long userId = portletDataContext.getUserId(entry.getUserUuid());
164    
165                    Element entryElement =
166                            portletDataContext.getImportDataStagedModelElement(entry);
167    
168                    String content = ExportImportHelperUtil.replaceImportContentReferences(
169                            portletDataContext, entry, entry.getContent());
170    
171                    entry.setContent(content);
172    
173                    Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
174    
175                    displayDateCal.setTime(entry.getDisplayDate());
176    
177                    int displayDateMonth = displayDateCal.get(Calendar.MONTH);
178                    int displayDateDay = displayDateCal.get(Calendar.DATE);
179                    int displayDateYear = displayDateCal.get(Calendar.YEAR);
180                    int displayDateHour = displayDateCal.get(Calendar.HOUR);
181                    int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
182    
183                    if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
184                            displayDateHour += 12;
185                    }
186    
187                    boolean allowPingbacks = entry.isAllowPingbacks();
188                    boolean allowTrackbacks = entry.isAllowTrackbacks();
189                    String[] trackbacks = StringUtil.split(entry.getTrackbacks());
190    
191                    long smallImageFileEntryId = 0;
192    
193                    ServiceContext serviceContext = portletDataContext.createServiceContext(
194                            entry);
195    
196                    if (entry.isSmallImage()) {
197                            String smallImagePath = entryElement.attributeValue(
198                                    "small-image-path");
199    
200                            if (Validator.isNotNull(entry.getSmallImageURL())) {
201                                    String smallImageURL =
202                                            ExportImportHelperUtil.replaceImportContentReferences(
203                                                    portletDataContext, entry, entry.getSmallImageURL());
204    
205                                    entry.setSmallImageURL(smallImageURL);
206                            }
207                            else if (Validator.isNotNull(smallImagePath)) {
208                                    String smallImageFileName =
209                                            entry.getSmallImageId() + StringPool.PERIOD +
210                                                    entry.getSmallImageType();
211    
212                                    InputStream inputStream = null;
213    
214                                    try {
215                                            inputStream = portletDataContext.getZipEntryAsInputStream(
216                                                    smallImagePath);
217    
218                                            FileEntry fileEntry = TempFileEntryUtil.addTempFileEntry(
219                                                    serviceContext.getScopeGroupId(), userId,
220                                                    BlogsEntry.class.getName(), smallImageFileName,
221                                                    inputStream,
222                                                    MimeTypesUtil.getContentType(smallImageFileName));
223    
224                                            smallImageFileEntryId = fileEntry.getFileEntryId();
225                                    }
226                                    finally {
227                                            StreamUtil.cleanUp(inputStream);
228                                    }
229                            }
230                    }
231    
232                    if (smallImageFileEntryId == 0) {
233                            List<Element> attachmentElements =
234                                    portletDataContext.getReferenceDataElements(
235                                            entry, DLFileEntry.class,
236                                            PortletDataContext.REFERENCE_TYPE_WEAK);
237    
238                            for (Element attachmentElement : attachmentElements) {
239                                    InputStream inputStream = getSmallImageInputStream(
240                                            portletDataContext, attachmentElement);
241    
242                                    if (inputStream != null) {
243                                            String path = attachmentElement.attributeValue("path");
244    
245                                            FileEntry fileEntry =
246                                                    (FileEntry)portletDataContext.getZipEntryAsObject(path);
247    
248                                            FileEntry smallImageFileEntry =
249                                                    TempFileEntryUtil.addTempFileEntry(
250                                                            serviceContext.getScopeGroupId(), userId,
251                                                            BlogsEntry.class.getName(), fileEntry.getTitle(),
252                                                            inputStream, fileEntry.getMimeType());
253    
254                                            if (fileEntry != null) {
255                                                    smallImageFileEntryId =
256                                                            smallImageFileEntry.getFileEntryId();
257                                            }
258                                    }
259                            }
260                    }
261    
262                    ImageSelector coverImageImageSelector = new ImageSelector(
263                            smallImageFileEntryId, entry.getCoverImageURL(), null);
264    
265                    ImageSelector smallImageImageSelector = null;
266    
267                    if (!entry.isSmallImage()) {
268                            smallImageImageSelector = new ImageSelector(0);
269                    }
270                    else {
271                            smallImageImageSelector = new ImageSelector(
272                                    smallImageFileEntryId, entry.getSmallImageURL(), null);
273                    }
274    
275                    BlogsEntry importedEntry = null;
276    
277                    if (portletDataContext.isDataStrategyMirror()) {
278                            serviceContext.setAttribute("urlTitle", entry.getUrlTitle());
279    
280                            BlogsEntry existingEntry = fetchStagedModelByUuidAndGroupId(
281                                    entry.getUuid(), portletDataContext.getScopeGroupId());
282    
283                            if (existingEntry == null) {
284                                    serviceContext.setUuid(entry.getUuid());
285    
286                                    importedEntry = BlogsEntryLocalServiceUtil.addEntry(
287                                            userId, entry.getTitle(), entry.getSubtitle(),
288                                            entry.getDescription(), entry.getContent(),
289                                            displayDateMonth, displayDateDay, displayDateYear,
290                                            displayDateHour, displayDateMinute, allowPingbacks,
291                                            allowTrackbacks, trackbacks, entry.getCoverImageCaption(),
292                                            coverImageImageSelector, smallImageImageSelector,
293                                            serviceContext);
294                            }
295                            else {
296                                    importedEntry = BlogsEntryLocalServiceUtil.updateEntry(
297                                            userId, existingEntry.getEntryId(), entry.getTitle(),
298                                            entry.getSubtitle(), entry.getDescription(),
299                                            entry.getContent(), displayDateMonth, displayDateDay,
300                                            displayDateYear, displayDateHour, displayDateMinute,
301                                            allowPingbacks, allowTrackbacks, trackbacks,
302                                            entry.getCoverImageCaption(), coverImageImageSelector,
303                                            smallImageImageSelector, serviceContext);
304                            }
305                    }
306                    else {
307                            importedEntry = BlogsEntryLocalServiceUtil.addEntry(
308                                    userId, entry.getTitle(), entry.getSubtitle(),
309                                    entry.getDescription(), entry.getContent(), displayDateMonth,
310                                    displayDateDay, displayDateYear, displayDateHour,
311                                    displayDateMinute, allowPingbacks, allowTrackbacks, trackbacks,
312                                    entry.getCoverImageCaption(), coverImageImageSelector,
313                                    smallImageImageSelector, serviceContext);
314                    }
315    
316                    portletDataContext.importClassedModel(entry, importedEntry);
317            }
318    
319            @Override
320            protected void doRestoreStagedModel(
321                            PortletDataContext portletDataContext, BlogsEntry entry)
322                    throws Exception {
323    
324                    long userId = portletDataContext.getUserId(entry.getUserUuid());
325    
326                    BlogsEntry existingEntry = fetchStagedModelByUuidAndGroupId(
327                            entry.getUuid(), portletDataContext.getScopeGroupId());
328    
329                    if ((existingEntry == null) || !existingEntry.isInTrash()) {
330                            return;
331                    }
332    
333                    TrashHandler trashHandler = existingEntry.getTrashHandler();
334    
335                    if (trashHandler.isRestorable(existingEntry.getEntryId())) {
336                            trashHandler.restoreTrashEntry(userId, existingEntry.getEntryId());
337                    }
338            }
339    
340            protected InputStream getSmallImageInputStream(
341                    PortletDataContext portletDataContext, Element attachmentElement) {
342    
343                    InputStream inputStream = null;
344    
345                    String path = attachmentElement.attributeValue("path");
346    
347                    FileEntry fileEntry = (FileEntry)portletDataContext.getZipEntryAsObject(
348                            path);
349    
350                    String binPath = attachmentElement.attributeValue("bin-path");
351    
352                    if (Validator.isNull(binPath) &&
353                            portletDataContext.isPerformDirectBinaryImport()) {
354    
355                            try {
356                                    inputStream = FileEntryUtil.getContentStream(fileEntry);
357                            }
358                            catch (Exception e) {
359                            }
360                    }
361                    else {
362                            inputStream = portletDataContext.getZipEntryAsInputStream(binPath);
363                    }
364    
365                    if (inputStream == null) {
366                            if (_log.isWarnEnabled()) {
367                                    _log.warn(
368                                            "Unable to import small image file entry " +
369                                                    fileEntry.getFileEntryId());
370                            }
371                    }
372    
373                    return inputStream;
374            }
375    
376            private static final Log _log = LogFactoryUtil.getLog(
377                    BlogsEntryStagedModelDataHandler.class);
378    
379    }