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.lar.BasePortletDataHandler;
18  import com.liferay.portal.kernel.lar.PortletDataContext;
19  import com.liferay.portal.kernel.lar.PortletDataException;
20  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
21  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.MapUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.workflow.WorkflowConstants;
29  import com.liferay.portal.kernel.xml.Document;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
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.kernel.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,
140                     WorkflowConstants.STATUS_APPROVED);
141             }
142             catch (NoSuchArticleException nsae) {
143                 if (_log.isWarnEnabled()) {
144                     _log.warn(
145                         "No approved article found with group id " +
146                             articleGroupId + " and article id " + articleId);
147                 }
148             }
149 
150             if (article == null) {
151                 return StringPool.BLANK;
152             }
153 
154             context.addPermissions(
155                 "com.liferay.portlet.journal", context.getGroupId());
156 
157             Document doc = SAXReaderUtil.createDocument();
158 
159             Element root = doc.addElement("journal-content-data");
160 
161             Element dlFoldersEl = root.addElement("dl-folders");
162             Element dlFilesEl = root.addElement("dl-file-entries");
163             Element dlFileRanksEl = root.addElement("dl-file-ranks");
164             Element igFoldersEl = root.addElement("ig-folders");
165             Element igImagesEl = root.addElement("ig-images");
166 
167             JournalPortletDataHandlerImpl.exportArticle(
168                 context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
169                 igFoldersEl, igImagesEl, article);
170 
171             String structureId = article.getStructureId();
172 
173             if (Validator.isNotNull(structureId)) {
174                 JournalStructure structure = JournalStructureUtil.findByG_S(
175                     article.getGroupId(), structureId);
176 
177                 JournalPortletDataHandlerImpl.exportStructure(
178                     context, root, structure);
179             }
180 
181             String templateId = article.getTemplateId();
182 
183             if (Validator.isNotNull(templateId)) {
184                 JournalTemplate template = JournalTemplateUtil.findByG_T(
185                     article.getGroupId(), templateId);
186 
187                 JournalPortletDataHandlerImpl.exportTemplate(
188                     context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
189                     igFoldersEl, igImagesEl, template);
190             }
191 
192             return doc.formattedString();
193         }
194         catch (Exception e) {
195             throw new PortletDataException(e);
196         }
197     }
198 
199     public PortletDataHandlerControl[] getExportControls() {
200         return new PortletDataHandlerControl[] {
201             _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
202             _tags
203         };
204     }
205 
206     public PortletDataHandlerControl[] getImportControls() {
207         return new PortletDataHandlerControl[] {
208             _selectedArticles, _images, _comments, _ratings, _tags
209         };
210     }
211 
212     public PortletPreferences importData(
213             PortletDataContext context, String portletId,
214             PortletPreferences preferences, String data)
215         throws PortletDataException {
216 
217         try {
218             context.importPermissions(
219                 "com.liferay.portlet.journal", context.getSourceGroupId(),
220                 context.getGroupId());
221 
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(), portletId, 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  _PUBLISH_TO_LIVE_BY_DEFAULT;
411     }
412 
413     private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
414 
415     private static final String _NAMESPACE = "journal";
416 
417     private static Log _log = LogFactoryUtil.getLog(
418         JournalContentPortletDataHandlerImpl.class);
419 
420     private static PortletDataHandlerBoolean _comments =
421         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
422 
423     private static PortletDataHandlerBoolean _embeddedAssets =
424         new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
425 
426     private static PortletDataHandlerBoolean _images =
427         new PortletDataHandlerBoolean(_NAMESPACE, "images");
428 
429     private static PortletDataHandlerBoolean _ratings =
430         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
431 
432     private static PortletDataHandlerBoolean _selectedArticles =
433         new PortletDataHandlerBoolean(
434             _NAMESPACE, "selected-web-content", true, true);
435 
436     private static PortletDataHandlerBoolean _tags =
437         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
438 
439 }