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