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