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