1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.lar;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.MapUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.workflow.StatusConstants;
24  import com.liferay.portal.kernel.xml.Document;
25  import com.liferay.portal.kernel.xml.Element;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  import com.liferay.portal.lar.BasePortletDataHandler;
28  import com.liferay.portal.lar.PortletDataContext;
29  import com.liferay.portal.lar.PortletDataException;
30  import com.liferay.portal.lar.PortletDataHandlerBoolean;
31  import com.liferay.portal.lar.PortletDataHandlerControl;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portlet.documentlibrary.lar.DLPortletDataHandlerImpl;
35  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
36  import com.liferay.portlet.documentlibrary.model.DLFileRank;
37  import com.liferay.portlet.documentlibrary.model.DLFolder;
38  import com.liferay.portlet.imagegallery.lar.IGPortletDataHandlerImpl;
39  import com.liferay.portlet.imagegallery.model.IGFolder;
40  import com.liferay.portlet.imagegallery.model.IGImage;
41  import com.liferay.portlet.journal.NoSuchArticleException;
42  import com.liferay.portlet.journal.model.JournalArticle;
43  import com.liferay.portlet.journal.model.JournalStructure;
44  import com.liferay.portlet.journal.model.JournalTemplate;
45  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
46  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
47  import com.liferay.portlet.journal.service.persistence.JournalStructureUtil;
48  import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
49  
50  import java.util.Collections;
51  import java.util.List;
52  import java.util.Map;
53  
54  import javax.portlet.PortletPreferences;
55  
56  /**
57   * <a href="JournalContentPortletDataHandlerImpl.java.html"><b><i>View Source
58   * </i></b></a>
59   *
60   * <p>
61   * Provides the Journal Content portlet export and import functionality, which
62   * is to clone the article, structure, and template referenced in the Journal
63   * Content portlet if the article is associated with the layout's group. Upon
64   * import, a new instance of the corresponding article, structure, and template
65   * will be created or updated. The author of the newly created objects are
66   * determined by the JournalCreationStrategy class defined in
67   * <i>portal.properties</i>.
68   * </p>
69   *
70   * <p>
71   * This <code>PortletDataHandler</code> differs from from
72   * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
73   * referenced in Journal Content portlets. Articles not displayed in Journal
74   * Content portlets will not be exported unless
75   * <code>JournalPortletDataHandlerImpl</code> is activated.
76   * </p>
77   *
78   * @author Joel Kozikowski
79   * @author Raymond Augé
80   * @author Bruno Farache
81   * @see    com.liferay.portal.lar.PortletDataHandler
82   * @see    com.liferay.portlet.journal.lar.JournalCreationStrategy
83   * @see    com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
84   */
85  public class JournalContentPortletDataHandlerImpl
86      extends BasePortletDataHandler {
87  
88      public PortletPreferences deleteData(
89              PortletDataContext context, String portletId,
90              PortletPreferences preferences)
91          throws PortletDataException {
92  
93          try {
94              preferences.setValue("group-id", StringPool.BLANK);
95              preferences.setValue("article-id", StringPool.BLANK);
96  
97              return preferences;
98          }
99          catch (Exception e) {
100             throw new PortletDataException(e);
101         }
102     }
103 
104     public String exportData(
105             PortletDataContext context, String portletId,
106             PortletPreferences preferences)
107         throws PortletDataException {
108 
109         try {
110             String articleId = preferences.getValue("article-id", null);
111 
112             if (articleId == null) {
113                 if (_log.isWarnEnabled()) {
114                     _log.warn(
115                         "No article id found in preferences of portlet " +
116                             portletId);
117                 }
118 
119                 return StringPool.BLANK;
120             }
121 
122             long articleGroupId = GetterUtil.getLong(
123                 preferences.getValue("group-id", StringPool.BLANK));
124 
125             if (articleGroupId <= 0) {
126                 if (_log.isWarnEnabled()) {
127                     _log.warn(
128                         "No group id found in preferences of portlet " +
129                             portletId);
130                 }
131 
132                 return StringPool.BLANK;
133             }
134 
135             JournalArticle article = null;
136 
137             try {
138                 article = JournalArticleLocalServiceUtil.getLatestArticle(
139                     articleGroupId, articleId, StatusConstants.APPROVED);
140             }
141             catch (NoSuchArticleException nsae) {
142                 if (_log.isWarnEnabled()) {
143                     _log.warn(
144                         "No approved article found with group id " +
145                             articleGroupId + " and article id " + articleId);
146                 }
147             }
148 
149             if (article == null) {
150                 return StringPool.BLANK;
151             }
152 
153             context.addPermissions(
154                 "com.liferay.portlet.journal", context.getGroupId());
155 
156             Document doc = SAXReaderUtil.createDocument();
157 
158             Element root = doc.addElement("journal-content-data");
159 
160             Element dlFoldersEl = root.addElement("dl-folders");
161             Element dlFilesEl = root.addElement("dl-file-entries");
162             Element dlFileRanksEl = root.addElement("dl-file-ranks");
163             Element igFoldersEl = root.addElement("ig-folders");
164             Element igImagesEl = root.addElement("ig-images");
165 
166             JournalPortletDataHandlerImpl.exportArticle(
167                 context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
168                 igFoldersEl, igImagesEl, article);
169 
170             String structureId = article.getStructureId();
171 
172             if (Validator.isNotNull(structureId)) {
173                 JournalStructure structure = JournalStructureUtil.findByG_S(
174                     article.getGroupId(), structureId);
175 
176                 JournalPortletDataHandlerImpl.exportStructure(
177                     context, root, structure);
178             }
179 
180             String templateId = article.getTemplateId();
181 
182             if (Validator.isNotNull(templateId)) {
183                 JournalTemplate template = JournalTemplateUtil.findByG_T(
184                     article.getGroupId(), templateId);
185 
186                 JournalPortletDataHandlerImpl.exportTemplate(
187                     context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
188                     igFoldersEl, igImagesEl, template);
189             }
190 
191             return doc.formattedString();
192         }
193         catch (Exception e) {
194             throw new PortletDataException(e);
195         }
196     }
197 
198     public PortletDataHandlerControl[] getExportControls() {
199         return new PortletDataHandlerControl[] {
200             _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
201             _tags
202         };
203     }
204 
205     public PortletDataHandlerControl[] getImportControls() {
206         return new PortletDataHandlerControl[] {
207             _selectedArticles, _images, _comments, _ratings, _tags
208         };
209     }
210 
211     public PortletPreferences importData(
212             PortletDataContext context, String portletId,
213             PortletPreferences preferences, String data)
214         throws PortletDataException {
215 
216         try {
217             context.importPermissions(
218                 "com.liferay.portlet.journal", context.getSourceGroupId(),
219                 context.getGroupId());
220 
221             if (Validator.isNull(data)) {
222                 return null;
223             }
224 
225             Document doc = SAXReaderUtil.read(data);
226 
227             Element root = doc.getRootElement();
228 
229             Element structureEl = root.element("structure");
230 
231             Map<String, String> structureIds =
232                 (Map<String, String>)context.getNewPrimaryKeysMap(
233                     JournalStructure.class);
234 
235             if (structureEl != null) {
236                 JournalPortletDataHandlerImpl.importStructure(
237                     context, structureIds, structureEl);
238             }
239 
240             Element templateEl = root.element("template");
241 
242             Map<String, String> templateIds =
243                 (Map<String, String>)context.getNewPrimaryKeysMap(
244                     JournalTemplate.class);
245 
246             if (templateEl != null) {
247                 JournalPortletDataHandlerImpl.importTemplate(
248                     context, structureIds, templateIds, templateEl);
249             }
250 
251             Element articleEl = root.element("article");
252 
253             Map<String, String> articleIds =
254                 (Map<String, String>)context.getNewPrimaryKeysMap(
255                     JournalArticle.class);
256 
257             if (articleEl != null) {
258                 JournalPortletDataHandlerImpl.importArticle(
259                     context, structureIds, templateIds, articleIds, articleEl);
260             }
261 
262             Element dlFoldersEl = root.element("dl-folders");
263 
264             List<Element> dlFolderEls = Collections.EMPTY_LIST;
265 
266             if (dlFoldersEl != null) {
267                 dlFolderEls = dlFoldersEl.elements("folder");
268             }
269 
270             Map<Long, Long> dlFolderPKs =
271                 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
272 
273             for (Element folderEl : dlFolderEls) {
274                 String path = folderEl.attributeValue("path");
275 
276                 if (!context.isPathNotProcessed(path)) {
277                     continue;
278                 }
279 
280                 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
281 
282                 DLPortletDataHandlerImpl.importFolder(
283                     context, dlFolderPKs, folder);
284             }
285 
286             Element dlFileEntriesEl = root.element("dl-file-entries");
287 
288             List<Element> dlFileEntryEls = Collections.EMPTY_LIST;
289 
290             if (dlFileEntriesEl != null) {
291                 dlFileEntryEls = dlFileEntriesEl.elements("file-entry");
292             }
293 
294             Map<String, String> fileEntryNames =
295                 (Map<String, String>)context.getNewPrimaryKeysMap(
296                     DLFileEntry.class);
297 
298             for (Element fileEntryEl : dlFileEntryEls) {
299                 String path = fileEntryEl.attributeValue("path");
300 
301                 if (!context.isPathNotProcessed(path)) {
302                     continue;
303                 }
304 
305                 DLFileEntry fileEntry =
306                     (DLFileEntry)context.getZipEntryAsObject(path);
307 
308                 String binPath = fileEntryEl.attributeValue("bin-path");
309 
310                 DLPortletDataHandlerImpl.importFileEntry(
311                     context, dlFolderPKs, fileEntryNames, fileEntry, binPath);
312             }
313 
314             Element dlFileRanksEl = root.element("dl-file-ranks");
315 
316             List<Element> dlFileRankEls = Collections.EMPTY_LIST;
317 
318             if (dlFileRanksEl != null) {
319                 dlFileRankEls = dlFileRanksEl.elements("file-rank");
320             }
321 
322             for (Element fileRankEl : dlFileRankEls) {
323                 String path = fileRankEl.attributeValue("path");
324 
325                 if (!context.isPathNotProcessed(path)) {
326                     continue;
327                 }
328 
329                 DLFileRank fileRank =
330                     (DLFileRank)context.getZipEntryAsObject(path);
331 
332                 DLPortletDataHandlerImpl.importFileRank(
333                     context, dlFolderPKs, fileEntryNames, fileRank);
334             }
335 
336             Element igFoldersEl = root.element("ig-folders");
337 
338             List<Element> igFolderEls = Collections.EMPTY_LIST;
339 
340             if (igFoldersEl != null) {
341                 igFolderEls = igFoldersEl.elements("folder");
342             }
343 
344             Map<Long, Long> igFolderPKs =
345                 (Map<Long, Long>)context.getNewPrimaryKeysMap(IGFolder.class);
346 
347             for (Element folderEl : igFolderEls) {
348                 String path = folderEl.attributeValue("path");
349 
350                 if (!context.isPathNotProcessed(path)) {
351                     continue;
352                 }
353 
354                 IGFolder folder = (IGFolder)context.getZipEntryAsObject(path);
355 
356                 IGPortletDataHandlerImpl.importFolder(
357                     context, igFolderPKs, folder);
358             }
359 
360             Element igImagesEl = root.element("ig-images");
361 
362             List<Element> igImageEls = Collections.EMPTY_LIST;
363 
364             if (igImagesEl != null) {
365                 igImageEls = igImagesEl.elements("image");
366             }
367 
368             for (Element imageEl : igImageEls) {
369                 String path = imageEl.attributeValue("path");
370 
371                 if (!context.isPathNotProcessed(path)) {
372                     continue;
373                 }
374 
375                 IGImage image = (IGImage)context.getZipEntryAsObject(path);
376 
377                 String binPath = imageEl.attributeValue("bin-path");
378 
379                 IGPortletDataHandlerImpl.importImage(
380                     context, igFolderPKs, image, binPath);
381             }
382 
383             String articleId = preferences.getValue(
384                 "article-id", StringPool.BLANK);
385 
386             if (Validator.isNotNull(articleId)) {
387                 articleId = MapUtil.getString(articleIds, articleId, articleId);
388 
389                 preferences.setValue(
390                     "group-id", String.valueOf(context.getGroupId()));
391                 preferences.setValue("article-id", articleId);
392 
393                 Layout layout = LayoutLocalServiceUtil.getLayout(
394                     context.getPlid());
395 
396                 JournalContentSearchLocalServiceUtil.updateContentSearch(
397                     context.getGroupId(), layout.isPrivateLayout(),
398                     layout.getLayoutId(), portletId, articleId, true);
399             }
400 
401             return preferences;
402         }
403         catch (Exception e) {
404             throw new PortletDataException(e);
405         }
406     }
407 
408     public boolean isPublishToLiveByDefault() {
409         return  _PUBLISH_TO_LIVE_BY_DEFAULT;
410     }
411 
412     private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
413 
414     private static final String _NAMESPACE = "journal";
415 
416     private static final PortletDataHandlerBoolean _selectedArticles =
417         new PortletDataHandlerBoolean(
418             _NAMESPACE, "selected-web-content", true, true);
419 
420     private static final PortletDataHandlerBoolean _embeddedAssets =
421         new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
422 
423     private static final PortletDataHandlerBoolean _images =
424         new PortletDataHandlerBoolean(_NAMESPACE, "images");
425 
426     private static final PortletDataHandlerBoolean _comments =
427         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
428 
429     private static final PortletDataHandlerBoolean _ratings =
430         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
431 
432     private static final PortletDataHandlerBoolean _tags =
433         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
434 
435     private static Log _log = LogFactoryUtil.getLog(
436         JournalContentPortletDataHandlerImpl.class);
437 
438 }