1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.lar;
24  
25  import com.liferay.portal.kernel.lar.PortletDataContext;
26  import com.liferay.portal.kernel.lar.PortletDataException;
27  import com.liferay.portal.kernel.lar.PortletDataHandler;
28  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
29  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.util.DocumentUtil;
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.persistence.JournalStructureUtil;
47  import com.liferay.portlet.journal.service.persistence.JournalTemplateUtil;
48  import com.liferay.util.MapUtil;
49  import com.liferay.util.xml.XMLFormatter;
50  
51  import java.util.Collections;
52  import java.util.List;
53  import java.util.Map;
54  
55  import javax.portlet.PortletPreferences;
56  
57  import org.apache.commons.logging.Log;
58  import org.apache.commons.logging.LogFactory;
59  
60  import org.dom4j.Document;
61  import org.dom4j.DocumentHelper;
62  import org.dom4j.Element;
63  
64  /**
65   * <a href="JournalContentPortletDataHandlerImpl.java.html"><b><i>View Source
66   * </i></b></a>
67   *
68   * <p>
69   * Provides the Journal Content portlet export and import functionality, which
70   * is to clone the article, structure, and template referenced in the
71   * Journal Content portlet if the article is associated with the layout's group.
72   * Upon import, a new instance of the corresponding article, structure, and
73   * template will be created or updated. The author of the newly created
74   * objects are determined by the JournalCreationStrategy class defined in
75   * <i>portal.properties</i>.
76   * </p>
77   *
78   * <p>
79   * This <code>PortletDataHandler</code> differs from from
80   * <code>JournalPortletDataHandlerImpl</code> in that it only exports articles
81   * referenced in Journal Content portlets. Articles not displayed in Journal
82   * Content portlets will not be exported unless
83   * <code>JournalPortletDataHandlerImpl</code> is activated.
84   * </p>
85   *
86   * @author Joel Kozikowski
87   * @author Raymond Augé
88   * @author Bruno Farache
89   *
90   * @see com.liferay.portal.kernel.lar.PortletDataHandler
91   * @see com.liferay.portlet.journal.lar.JournalCreationStrategy
92   * @see com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl
93   *
94   */
95  public class JournalContentPortletDataHandlerImpl
96      implements PortletDataHandler {
97  
98      public PortletPreferences deleteData(
99              PortletDataContext context, String portletId,
100             PortletPreferences prefs)
101         throws PortletDataException {
102 
103         try {
104             prefs.setValue("group-id", StringPool.BLANK);
105             prefs.setValue("article-id", StringPool.BLANK);
106 
107             return prefs;
108         }
109         catch (Exception e) {
110             throw new PortletDataException(e);
111         }
112     }
113 
114     public String exportData(
115             PortletDataContext context, String portletId,
116             PortletPreferences prefs)
117         throws PortletDataException {
118 
119         try {
120             String articleId = prefs.getValue("article-id", null);
121 
122             if (articleId == null) {
123                 if (_log.isWarnEnabled()) {
124                     _log.warn(
125                         "No article id found in preferences of portlet " +
126                             portletId);
127                 }
128 
129                 return StringPool.BLANK;
130             }
131 
132             long articleGroupId = GetterUtil.getLong(
133                 prefs.getValue("group-id", StringPool.BLANK));
134 
135             if (articleGroupId <= 0) {
136                 if (_log.isWarnEnabled()) {
137                     _log.warn(
138                         "No group id found in preferences of portlet " +
139                             portletId);
140                 }
141 
142                 return StringPool.BLANK;
143             }
144 
145             JournalArticle article = null;
146 
147             try {
148                 article = JournalArticleLocalServiceUtil.getLatestArticle(
149                     articleGroupId, articleId);
150             }
151             catch (NoSuchArticleException nsae) {
152                 if (_log.isWarnEnabled()) {
153                     _log.warn(nsae);
154                 }
155             }
156 
157             if (article == null) {
158                 return StringPool.BLANK;
159             }
160 
161             Document doc = DocumentHelper.createDocument();
162 
163             Element root = doc.addElement("journal-content-data");
164 
165             Element dlFoldersEl = root.addElement("dl-folders");
166             Element dlFilesEl = root.addElement("dl-file-entries");
167             Element dlFileRanksEl = root.addElement("dl-file-ranks");
168             Element igFoldersEl = root.addElement("ig-folders");
169             Element igImagesEl = root.addElement("ig-images");
170 
171             JournalPortletDataHandlerImpl.exportArticle(
172                 context, root, dlFoldersEl, dlFilesEl, dlFileRanksEl,
173                 igFoldersEl, igImagesEl, article);
174 
175             String structureId = article.getStructureId();
176 
177             if (Validator.isNotNull(structureId)) {
178                 JournalStructure structure = JournalStructureUtil.findByG_S(
179                     article.getGroupId(), structureId);
180 
181                 JournalPortletDataHandlerImpl.exportStructure(
182                     context, root, structure);
183             }
184 
185             String templateId = article.getTemplateId();
186 
187             if (Validator.isNotNull(templateId)) {
188                 JournalTemplate template = JournalTemplateUtil.findByG_T(
189                     article.getGroupId(), templateId);
190 
191                 JournalPortletDataHandlerImpl.exportTemplate(
192                     context, root, template);
193             }
194 
195             return XMLFormatter.toString(doc);
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 prefs, String data)
218         throws PortletDataException {
219 
220         try {
221             if (Validator.isNull(data)) {
222                 return null;
223             }
224 
225             Document doc = DocumentUtil.readDocumentFromXML(data);
226 
227             Element root = doc.getRootElement();
228 
229             Element structureEl = root.element("structure");
230 
231             Map<String, String> structureIds = context.getNewPrimaryKeysMap(
232                 JournalStructure.class);
233 
234             if (structureEl != null) {
235                 JournalPortletDataHandlerImpl.importStructure(
236                     context, structureIds, structureEl);
237             }
238 
239             Element templateEl = root.element("template");
240 
241             Map<String, String> templateIds = context.getNewPrimaryKeysMap(
242                 JournalTemplate.class);
243 
244             if (templateEl != null) {
245                 JournalPortletDataHandlerImpl.importTemplate(
246                     context, structureIds, templateIds, templateEl);
247             }
248 
249             Element articleEl = root.element("article");
250 
251             Map<String, String> articleIds = context.getNewPrimaryKeysMap(
252                 JournalArticle.class);
253 
254             if (articleEl != null) {
255                 JournalPortletDataHandlerImpl.importArticle(
256                     context, structureIds, templateIds, articleIds, articleEl);
257             }
258 
259             String articleId = prefs.getValue("article-id", StringPool.BLANK);
260 
261             if (Validator.isNotNull(articleId)) {
262                 articleId = MapUtil.getString(articleIds, articleId, articleId);
263 
264                 prefs.setValue(
265                     "group-id", String.valueOf(context.getGroupId()));
266                 prefs.setValue("article-id", articleId);
267             }
268 
269             Element dlFoldersEl = root.element("dl-folders");
270 
271             List<Element> dlFolderEls = Collections.EMPTY_LIST;
272 
273             if (dlFoldersEl != null) {
274                 dlFolderEls = dlFoldersEl.elements("folder");
275             }
276 
277             Map<Long, Long> dlFolderPKs = context.getNewPrimaryKeysMap(
278                 DLFolder.class);
279 
280             for (Element folderEl : dlFolderEls) {
281                 String path = folderEl.attributeValue("path");
282 
283                 if (context.isPathNotProcessed(path)) {
284                     DLFolder folder = (DLFolder)context.getZipEntryAsObject(
285                         path);
286 
287                     DLPortletDataHandlerImpl.importFolder(
288                         context, dlFolderPKs, folder);
289                 }
290             }
291 
292             Element dlFileEntriesEl = root.element("dl-file-entries");
293 
294             List<Element> dlFileEntryEls = Collections.EMPTY_LIST;
295 
296             if (dlFileEntriesEl != null) {
297                 dlFileEntryEls = dlFoldersEl.elements("file-entry");
298             }
299 
300             Map<String, String> fileEntryNames = context.getNewPrimaryKeysMap(
301                 DLFileEntry.class);
302 
303             for (Element fileEntryEl : dlFileEntryEls) {
304                 String path = fileEntryEl.attributeValue("path");
305                 String binPath = fileEntryEl.attributeValue("bin-path");
306 
307                 if (context.isPathNotProcessed(path)) {
308                     DLFileEntry fileEntry =
309                         (DLFileEntry)context.getZipEntryAsObject(path);
310 
311                     DLPortletDataHandlerImpl.importFileEntry(
312                         context, dlFolderPKs, fileEntryNames, fileEntry,
313                         binPath);
314                 }
315             }
316 
317             Element dlFileRanksEl = root.element("dl-file-ranks");
318 
319             List<Element> dlFileRankEls = Collections.EMPTY_LIST;
320 
321             if (dlFileRanksEl != null) {
322                 dlFileRankEls = dlFileRanksEl.elements("file-rank");
323             }
324 
325             for (Element fileRankEl : dlFileRankEls) {
326                 String path = fileRankEl.attributeValue("path");
327 
328                 if (context.isPathNotProcessed(path)) {
329                     DLFileRank fileRank =
330                         (DLFileRank)context.getZipEntryAsObject(path);
331 
332                     DLPortletDataHandlerImpl.importFileRank(
333                         context, dlFolderPKs, fileEntryNames, fileRank);
334                 }
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 = context.getNewPrimaryKeysMap(
346                 IGFolder.class);
347 
348             for (Element folderEl : igFolderEls) {
349                 String path = folderEl.attributeValue("path");
350 
351                 if (context.isPathNotProcessed(path)) {
352                     IGFolder folder = (IGFolder)context.getZipEntryAsObject(
353                         path);
354 
355                     IGPortletDataHandlerImpl.importFolder(
356                         context, igFolderPKs, folder);
357                 }
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                 String binPath = imageEl.attributeValue("bin-path");
371 
372                 if (context.isPathNotProcessed(path)) {
373                     IGImage image = (IGImage)context.getZipEntryAsObject(path);
374 
375                     IGPortletDataHandlerImpl.importImage(
376                         context, igFolderPKs, image, binPath);
377                 }
378             }
379 
380             return prefs;
381         }
382         catch (Exception e) {
383             throw new PortletDataException(e);
384         }
385     }
386 
387     public boolean isPublishToLiveByDefault() {
388         return true;
389     }
390 
391     private static final String _NAMESPACE = "journal";
392 
393     private static final PortletDataHandlerBoolean _selectedArticles =
394         new PortletDataHandlerBoolean(
395             _NAMESPACE, "selected-articles", true, true);
396 
397     private static final PortletDataHandlerBoolean _embeddedAssets =
398         new PortletDataHandlerBoolean(_NAMESPACE, "embedded-assets");
399 
400     private static final PortletDataHandlerBoolean _images =
401         new PortletDataHandlerBoolean(_NAMESPACE, "images");
402 
403     private static final PortletDataHandlerBoolean _comments =
404         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
405 
406     private static final PortletDataHandlerBoolean _ratings =
407         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
408 
409     private static final PortletDataHandlerBoolean _tags =
410         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
411 
412     private static Log _log =
413         LogFactory.getLog(JournalContentPortletDataHandlerImpl.class);
414 
415 }