1
22
23 package com.liferay.portlet.journal.lar;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.kernel.xml.Document;
29 import com.liferay.portal.kernel.xml.Element;
30 import com.liferay.portal.kernel.xml.SAXReaderUtil;
31 import com.liferay.portal.lar.PortletDataContext;
32 import com.liferay.portal.lar.PortletDataException;
33 import com.liferay.portal.lar.PortletDataHandler;
34 import com.liferay.portal.lar.PortletDataHandlerBoolean;
35 import com.liferay.portal.lar.PortletDataHandlerControl;
36 import com.liferay.portal.model.Layout;
37 import com.liferay.portal.service.LayoutLocalServiceUtil;
38 import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
39 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
40 import com.liferay.portlet.documentlibrary.model.DLFileRank;
41 import com.liferay.portlet.documentlibrary.model.DLFolder;
42 import com.liferay.portlet.imagegallery.lar.IGPortletDataHandlerImpl;
43 import com.liferay.portlet.imagegallery.model.IGFolder;
44 import com.liferay.portlet.imagegallery.model.IGImage;
45 import com.liferay.portlet.journal.NoSuchArticleException;
46 import com.liferay.portlet.journal.model.JournalArticle;
47 import com.liferay.portlet.journal.model.JournalStructure;
48 import com.liferay.portlet.journal.model.JournalTemplate;
49 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
50 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
51 import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
52 import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
53 import com.liferay.util.MapUtil;
54
55 import java.util.Collections;
56 import java.util.List;
57 import java.util.Map;
58
59 import javax.portlet.PortletPreferences;
60
61 import org.apache.commons.logging.Log;
62 import org.apache.commons.logging.LogFactory;
63
64
95 public class JournalContentPortletDataHandlerImpl
96 implements PortletDataHandler {
97
98 public PortletPreferences deleteData(
99 PortletDataContext context, String portletId,
100 PortletPreferences preferences)
101 throws PortletDataException {
102
103 try {
104 preferences.setValue("group-id", StringPool.BLANK);
105 preferences.setValue("article-id", StringPool.BLANK);
106
107 return preferences;
108 }
109 catch (Exception e) {
110 throw new PortletDataException(e);
111 }
112 }
113
114 public String exportData(
115 PortletDataContext context, String portletId,
116 PortletPreferences preferences)
117 throws PortletDataException {
118
119 try {
120 String articleId = preferences.getValue("article-id", null);
121
122 if (articleId == null) {
123 if (_log.isWarnEnabled()) {
124 _log.warn(
125 "No article id found in preferences of portlet " +
126 portletId);
127 }
128
129 return StringPool.BLANK;
130 }
131
132 long articleGroupId = GetterUtil.getLong(
133 preferences.getValue("group-id", StringPool.BLANK));
134
135 if (articleGroupId <= 0) {
136 if (_log.isWarnEnabled()) {
137 _log.warn(
138 "No group id found in preferences of portlet " +
139 portletId);
140 }
141
142 return StringPool.BLANK;
143 }
144
145 JournalArticle article = null;
146
147 try {
148 article = JournalArticleLocalServiceUtil.getLatestArticle(
149 articleGroupId, articleId);
150 }
151 catch (NoSuchArticleException nsae) {
152 if (_log.isWarnEnabled()) {
153 _log.warn(nsae);
154 }
155 }
156
157 if (article == null) {
158 return StringPool.BLANK;
159 }
160
161 Document doc = SAXReaderUtil.createDocument();
162
163 Element root = doc.addElement("journal-content-data");
164
165 Element dlFoldersEl = root.addElement("dl-folders");
166 Element dlFilesEl = root.addElement("dl-file-entries");
167 Element dlFileRanksEl = root.addElement("dl-file-ranks");
168 Element igFoldersEl = root.addElement("ig-folders");
169 Element igImagesEl = root.addElement("ig-images");
170
171 JournalPortletDataHandlerImpl.exportArticle(
172 context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
173 igFoldersEl, igImagesEl, article);
174
175 String structureId = article.getStructureId();
176
177 if (Validator.isNotNull(structureId)) {
178 JournalStructure structure = JournalStructureUtil.findByG_S(
179 article.getGroupId(), structureId);
180
181 JournalPortletDataHandlerImpl.exportStructure(
182 context, root, structure);
183 }
184
185 String templateId = article.getTemplateId();
186
187 if (Validator.isNotNull(templateId)) {
188 JournalTemplate template = JournalTemplateUtil.findByG_T(
189 article.getGroupId(), templateId);
190
191 JournalPortletDataHandlerImpl.exportTemplate(
192 context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
193 igFoldersEl, igImagesEl, template);
194 }
195
196 return doc.formattedString();
197 }
198 catch (Exception e) {
199 throw new PortletDataException(e);
200 }
201 }
202
203 public PortletDataHandlerControl[] getExportControls() {
204 return new PortletDataHandlerControl[] {
205 _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
206 _tags
207 };
208 }
209
210 public PortletDataHandlerControl[] getImportControls() {
211 return new PortletDataHandlerControl[] {
212 _selectedArticles, _images, _comments, _ratings, _tags
213 };
214 }
215
216 public PortletPreferences importData(
217 PortletDataContext context, String portletId,
218 PortletPreferences preferences, String data)
219 throws PortletDataException {
220
221 try {
222 if (Validator.isNull(data)) {
223 return null;
224 }
225
226 Document doc = SAXReaderUtil.read(data);
227
228 Element root = doc.getRootElement();
229
230 Element structureEl = root.element("structure");
231
232 Map<String, String> structureIds =
233 (Map<String, String>)context.getNewPrimaryKeysMap(
234 JournalStructure.class);
235
236 if (structureEl != null) {
237 JournalPortletDataHandlerImpl.importStructure(
238 context, structureIds, structureEl);
239 }
240
241 Element templateEl = root.element("template");
242
243 Map<String, String> templateIds =
244 (Map<String, String>)context.getNewPrimaryKeysMap(
245 JournalTemplate.class);
246
247 if (templateEl != null) {
248 JournalPortletDataHandlerImpl.importTemplate(
249 context, structureIds, templateIds, templateEl);
250 }
251
252 Element articleEl = root.element("article");
253
254 Map<String, String> articleIds =
255 (Map<String, String>)context.getNewPrimaryKeysMap(
256 JournalArticle.class);
257
258 if (articleEl != null) {
259 JournalPortletDataHandlerImpl.importArticle(
260 context, structureIds, templateIds, articleIds, articleEl);
261 }
262
263 Element dlFoldersEl = root.element("dl-folders");
264
265 List<Element> dlFolderEls = Collections.EMPTY_LIST;
266
267 if (dlFoldersEl != null) {
268 dlFolderEls = dlFoldersEl.elements("folder");
269 }
270
271 Map<Long, Long> dlFolderPKs =
272 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
273
274 for (Element folderEl : dlFolderEls) {
275 String path = folderEl.attributeValue("path");
276
277 if (!context.isPathNotProcessed(path)) {
278 continue;
279 }
280
281 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
282
283 DLPortletDataHandlerImpl.importFolder(
284 context, dlFolderPKs, folder);
285 }
286
287 Element dlFileEntriesEl = root.element("dl-file-entries");
288
289 List<Element> dlFileEntryEls = Collections.EMPTY_LIST;
290
291 if (dlFileEntriesEl != null) {
292 dlFileEntryEls = dlFileEntriesEl.elements("file-entry");
293 }
294
295 Map<String, String> fileEntryNames =
296 (Map<String, String>)context.getNewPrimaryKeysMap(
297 DLFileEntry.class);
298
299 for (Element fileEntryEl : dlFileEntryEls) {
300 String path = fileEntryEl.attributeValue("path");
301
302 if (!context.isPathNotProcessed(path)) {
303 continue;
304 }
305
306 DLFileEntry fileEntry =
307 (DLFileEntry)context.getZipEntryAsObject(path);
308
309 String binPath = fileEntryEl.attributeValue("bin-path");
310
311 DLPortletDataHandlerImpl.importFileEntry(
312 context, dlFolderPKs, fileEntryNames, fileEntry, binPath);
313 }
314
315 Element dlFileRanksEl = root.element("dl-file-ranks");
316
317 List<Element> dlFileRankEls = Collections.EMPTY_LIST;
318
319 if (dlFileRanksEl != null) {
320 dlFileRankEls = dlFileRanksEl.elements("file-rank");
321 }
322
323 for (Element fileRankEl : dlFileRankEls) {
324 String path = fileRankEl.attributeValue("path");
325
326 if (!context.isPathNotProcessed(path)) {
327 continue;
328 }
329
330 DLFileRank fileRank =
331 (DLFileRank)context.getZipEntryAsObject(path);
332
333 DLPortletDataHandlerImpl.importFileRank(
334 context, dlFolderPKs, fileEntryNames, fileRank);
335 }
336
337 Element igFoldersEl = root.element("ig-folders");
338
339 List<Element> igFolderEls = Collections.EMPTY_LIST;
340
341 if (igFoldersEl != null) {
342 igFolderEls = igFoldersEl.elements("folder");
343 }
344
345 Map<Long, Long> igFolderPKs =
346 (Map<Long, Long>)context.getNewPrimaryKeysMap(IGFolder.class);
347
348 for (Element folderEl : igFolderEls) {
349 String path = folderEl.attributeValue("path");
350
351 if (!context.isPathNotProcessed(path)) {
352 continue;
353 }
354
355 IGFolder folder = (IGFolder)context.getZipEntryAsObject(path);
356
357 IGPortletDataHandlerImpl.importFolder(
358 context, igFolderPKs, folder);
359 }
360
361 Element igImagesEl = root.element("ig-images");
362
363 List<Element> igImageEls = Collections.EMPTY_LIST;
364
365 if (igImagesEl != null) {
366 igImageEls = igImagesEl.elements("image");
367 }
368
369 for (Element imageEl : igImageEls) {
370 String path = imageEl.attributeValue("path");
371
372 if (!context.isPathNotProcessed(path)) {
373 continue;
374 }
375
376 IGImage image = (IGImage)context.getZipEntryAsObject(path);
377
378 String binPath = imageEl.attributeValue("bin-path");
379
380 IGPortletDataHandlerImpl.importImage(
381 context, igFolderPKs, image, binPath);
382 }
383
384 String articleId = preferences.getValue(
385 "article-id", StringPool.BLANK);
386
387 if (Validator.isNotNull(articleId)) {
388 articleId = MapUtil.getString(articleIds, articleId, articleId);
389
390 preferences.setValue(
391 "group-id", String.valueOf(context.getGroupId()));
392 preferences.setValue("article-id", articleId);
393
394 Layout layout = LayoutLocalServiceUtil.getLayout(
395 context.getPlid());
396
397 JournalContentSearchLocalServiceUtil.updateContentSearch(
398 context.getGroupId(), layout.isPrivateLayout(),
399 layout.getLayoutId(), StringPool.BLANK, articleId, true);
400 }
401
402 return preferences;
403 }
404 catch (Exception e) {
405 throw new PortletDataException(e);
406 }
407 }
408
409 public boolean isPublishToLiveByDefault() {
410 return true;
411 }
412
413 private static final String _NAMESPACE = "journal";
414
415 private static final PortletDataHandlerBoolean _selectedArticles =
416 new PortletDataHandlerBoolean(
417 _NAMESPACE, "selected-articles", true, true);
418
419 private static final PortletDataHandlerBoolean _embeddedAssets =
420 new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
421
422 private static final PortletDataHandlerBoolean _images =
423 new PortletDataHandlerBoolean(_NAMESPACE, "images");
424
425 private static final PortletDataHandlerBoolean _comments =
426 new PortletDataHandlerBoolean(_NAMESPACE, "comments");
427
428 private static final PortletDataHandlerBoolean _ratings =
429 new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
430
431 private static final PortletDataHandlerBoolean _tags =
432 new PortletDataHandlerBoolean(_NAMESPACE, "tags");
433
434 private static Log _log =
435 LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
436
437 }