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.BasePortletDataHandler;
27  import com.liferay.portal.lar.PortletDataContext;
28  import com.liferay.portal.lar.PortletDataException;
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      extends BasePortletDataHandler {
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             context.addPermissions(
151                 "com.liferay.portlet.journal", context.getGroupId());
152 
153             Document doc = SAXReaderUtil.createDocument();
154 
155             Element root = doc.addElement("journal-content-data");
156 
157             Element dlFoldersEl = root.addElement("dl-folders");
158             Element dlFilesEl = root.addElement("dl-file-entries");
159             Element dlFileRanksEl = root.addElement("dl-file-ranks");
160             Element igFoldersEl = root.addElement("ig-folders");
161             Element igImagesEl = root.addElement("ig-images");
162 
163             JournalPortletDataHandlerImpl.exportArticle(
164                 context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
165                 igFoldersEl, igImagesEl, article);
166 
167             String structureId = article.getStructureId();
168 
169             if (Validator.isNotNull(structureId)) {
170                 JournalStructure structure = JournalStructureUtil.findByG_S(
171                     article.getGroupId(), structureId);
172 
173                 JournalPortletDataHandlerImpl.exportStructure(
174                     context, root, structure);
175             }
176 
177             String templateId = article.getTemplateId();
178 
179             if (Validator.isNotNull(templateId)) {
180                 JournalTemplate template = JournalTemplateUtil.findByG_T(
181                     article.getGroupId(), templateId);
182 
183                 JournalPortletDataHandlerImpl.exportTemplate(
184                     context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
185                     igFoldersEl, igImagesEl, template);
186             }
187 
188             return doc.formattedString();
189         }
190         catch (Exception e) {
191             throw new PortletDataException(e);
192         }
193     }
194 
195     public PortletDataHandlerControl[] getExportControls() {
196         return new PortletDataHandlerControl[] {
197             _selectedArticles, _embeddedAssets, _images, _comments, _ratings,
198             _tags
199         };
200     }
201 
202     public PortletDataHandlerControl[] getImportControls() {
203         return new PortletDataHandlerControl[] {
204             _selectedArticles, _images, _comments, _ratings, _tags
205         };
206     }
207 
208     public PortletPreferences importData(
209             PortletDataContext context, String portletId,
210             PortletPreferences preferences, String data)
211         throws PortletDataException {
212 
213         try {
214             context.importPermissions(
215                 "com.liferay.portlet.journal", context.getSourceGroupId(),
216                 context.getGroupId());
217 
218             if (Validator.isNull(data)) {
219                 return null;
220             }
221 
222             Document doc = SAXReaderUtil.read(data);
223 
224             Element root = doc.getRootElement();
225 
226             Element structureEl = root.element("structure");
227 
228             Map<String, String> structureIds =
229                 (Map<String, String>)context.getNewPrimaryKeysMap(
230                     JournalStructure.class);
231 
232             if (structureEl != null) {
233                 JournalPortletDataHandlerImpl.importStructure(
234                     context, structureIds, structureEl);
235             }
236 
237             Element templateEl = root.element("template");
238 
239             Map<String, String> templateIds =
240                 (Map<String, String>)context.getNewPrimaryKeysMap(
241                     JournalTemplate.class);
242 
243             if (templateEl != null) {
244                 JournalPortletDataHandlerImpl.importTemplate(
245                     context, structureIds, templateIds, templateEl);
246             }
247 
248             Element articleEl = root.element("article");
249 
250             Map<String, String> articleIds =
251                 (Map<String, String>)context.getNewPrimaryKeysMap(
252                     JournalArticle.class);
253 
254             if (articleEl != null) {
255                 JournalPortletDataHandlerImpl.importArticle(
256                     context, structureIds, templateIds, articleIds, articleEl);
257             }
258 
259             Element dlFoldersEl = root.element("dl-folders");
260 
261             List<Element> dlFolderEls = Collections.EMPTY_LIST;
262 
263             if (dlFoldersEl != null) {
264                 dlFolderEls = dlFoldersEl.elements("folder");
265             }
266 
267             Map<Long, Long> dlFolderPKs =
268                 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
269 
270             for (Element folderEl : dlFolderEls) {
271                 String path = folderEl.attributeValue("path");
272 
273                 if (!context.isPathNotProcessed(path)) {
274                     continue;
275                 }
276 
277                 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
278 
279                 DLPortletDataHandlerImpl.importFolder(
280                     context, dlFolderPKs, folder);
281             }
282 
283             Element dlFileEntriesEl = root.element("dl-file-entries");
284 
285             List<Element> dlFileEntryEls = Collections.EMPTY_LIST;
286 
287             if (dlFileEntriesEl != null) {
288                 dlFileEntryEls = dlFileEntriesEl.elements("file-entry");
289             }
290 
291             Map<String, String> fileEntryNames =
292                 (Map<String, String>)context.getNewPrimaryKeysMap(
293                     DLFileEntry.class);
294 
295             for (Element fileEntryEl : dlFileEntryEls) {
296                 String path = fileEntryEl.attributeValue("path");
297 
298                 if (!context.isPathNotProcessed(path)) {
299                     continue;
300                 }
301 
302                 DLFileEntry fileEntry =
303                     (DLFileEntry)context.getZipEntryAsObject(path);
304 
305                 String binPath = fileEntryEl.attributeValue("bin-path");
306 
307                 DLPortletDataHandlerImpl.importFileEntry(
308                     context, dlFolderPKs, fileEntryNames, fileEntry, binPath);
309             }
310 
311             Element dlFileRanksEl = root.element("dl-file-ranks");
312 
313             List<Element> dlFileRankEls = Collections.EMPTY_LIST;
314 
315             if (dlFileRanksEl != null) {
316                 dlFileRankEls = dlFileRanksEl.elements("file-rank");
317             }
318 
319             for (Element fileRankEl : dlFileRankEls) {
320                 String path = fileRankEl.attributeValue("path");
321 
322                 if (!context.isPathNotProcessed(path)) {
323                     continue;
324                 }
325 
326                 DLFileRank fileRank =
327                     (DLFileRank)context.getZipEntryAsObject(path);
328 
329                 DLPortletDataHandlerImpl.importFileRank(
330                     context, dlFolderPKs, fileEntryNames, fileRank);
331             }
332 
333             Element igFoldersEl = root.element("ig-folders");
334 
335             List<Element> igFolderEls = Collections.EMPTY_LIST;
336 
337             if (igFoldersEl != null) {
338                 igFolderEls = igFoldersEl.elements("folder");
339             }
340 
341             Map<Long, Long> igFolderPKs =
342                 (Map<Long, Long>)context.getNewPrimaryKeysMap(IGFolder.class);
343 
344             for (Element folderEl : igFolderEls) {
345                 String path = folderEl.attributeValue("path");
346 
347                 if (!context.isPathNotProcessed(path)) {
348                     continue;
349                 }
350 
351                 IGFolder folder = (IGFolder)context.getZipEntryAsObject(path);
352 
353                 IGPortletDataHandlerImpl.importFolder(
354                     context, igFolderPKs, folder);
355             }
356 
357             Element igImagesEl = root.element("ig-images");
358 
359             List<Element> igImageEls = Collections.EMPTY_LIST;
360 
361             if (igImagesEl != null) {
362                 igImageEls = igImagesEl.elements("image");
363             }
364 
365             for (Element imageEl : igImageEls) {
366                 String path = imageEl.attributeValue("path");
367 
368                 if (!context.isPathNotProcessed(path)) {
369                     continue;
370                 }
371 
372                 IGImage image = (IGImage)context.getZipEntryAsObject(path);
373 
374                 String binPath = imageEl.attributeValue("bin-path");
375 
376                 IGPortletDataHandlerImpl.importImage(
377                     context, igFolderPKs, image, binPath);
378             }
379 
380             String articleId = preferences.getValue(
381                 "article-id", StringPool.BLANK);
382 
383             if (Validator.isNotNull(articleId)) {
384                 articleId = MapUtil.getString(articleIds, articleId, articleId);
385 
386                 preferences.setValue(
387                     "group-id", String.valueOf(context.getGroupId()));
388                 preferences.setValue("article-id", articleId);
389 
390                 Layout layout = LayoutLocalServiceUtil.getLayout(
391                     context.getPlid());
392 
393                 JournalContentSearchLocalServiceUtil.updateContentSearch(
394                     context.getGroupId(), layout.isPrivateLayout(),
395                     layout.getLayoutId(), portletId, articleId, true);
396             }
397 
398             return preferences;
399         }
400         catch (Exception e) {
401             throw new PortletDataException(e);
402         }
403     }
404 
405     public boolean isPublishToLiveByDefault() {
406         return  _PUBLISH_TO_LIVE_BY_DEFAULT;
407     }
408 
409     private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
410 
411     private static final String _NAMESPACE = "journal";
412 
413     private static final PortletDataHandlerBoolean _selectedArticles =
414         new PortletDataHandlerBoolean(
415             _NAMESPACE, "selected-web-content", true, true);
416 
417     private static final PortletDataHandlerBoolean _embeddedAssets =
418         new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
419 
420     private static final PortletDataHandlerBoolean _images =
421         new PortletDataHandlerBoolean(_NAMESPACE, "images");
422 
423     private static final PortletDataHandlerBoolean _comments =
424         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
425 
426     private static final PortletDataHandlerBoolean _ratings =
427         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
428 
429     private static final PortletDataHandlerBoolean _tags =
430         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
431 
432     private static Log _log = LogFactoryUtil.getLog(
433         JournalContentPortletDataHandlerImpl.class);
434 
435 }