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.bookmarks.lar;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.lar.BasePortletDataHandler;
20  import com.liferay.portal.kernel.lar.PortletDataContext;
21  import com.liferay.portal.kernel.lar.PortletDataException;
22  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
23  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
24  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.MapUtil;
28  import com.liferay.portal.kernel.util.StringBundler;
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.service.ServiceContext;
33  import com.liferay.portal.util.PortletKeys;
34  import com.liferay.portlet.bookmarks.NoSuchFolderException;
35  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
36  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
37  import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
38  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
39  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
40  import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
41  import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderUtil;
42  
43  import java.util.List;
44  import java.util.Map;
45  
46  import javax.portlet.PortletPreferences;
47  
48  /**
49   * <a href="BookmarksPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
50   * </a>
51   *
52   * @author Jorge Ferrer
53   * @author Bruno Farache
54   * @author Raymond Augé
55   * @author Juan Fernández
56   */
57  public class BookmarksPortletDataHandlerImpl extends BasePortletDataHandler {
58  
59      public PortletPreferences deleteData(
60              PortletDataContext context, String portletId,
61              PortletPreferences preferences)
62          throws PortletDataException {
63  
64          try {
65              if (!context.addPrimaryKey(
66                      BookmarksPortletDataHandlerImpl.class, "deleteData")) {
67  
68                  BookmarksFolderLocalServiceUtil.deleteFolders(
69                      context.getGroupId());
70              }
71  
72              return null;
73          }
74          catch (Exception e) {
75              throw new PortletDataException(e);
76          }
77      }
78  
79      public String exportData(
80              PortletDataContext context, String portletId,
81              PortletPreferences preferences)
82          throws PortletDataException {
83  
84          try {
85              context.addPermissions(
86                  "com.liferay.portlet.bookmarks", context.getGroupId());
87  
88              Document doc = SAXReaderUtil.createDocument();
89  
90              Element root = doc.addElement("bookmarks-data");
91  
92              root.addAttribute("group-id", String.valueOf(context.getGroupId()));
93  
94              Element foldersEl = root.addElement("folders");
95              Element entriesEl = root.addElement("entries");
96  
97              List<BookmarksFolder> folders = BookmarksFolderUtil.findByGroupId(
98                  context.getGroupId());
99  
100             for (BookmarksFolder folder : folders) {
101                 exportFolder(context, foldersEl, entriesEl, folder);
102             }
103 
104             List<BookmarksEntry> entries = BookmarksEntryUtil.findByG_F(
105                 context.getGroupId(),
106                 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
107 
108             for (BookmarksEntry entry : entries) {
109                 exportEntry(context, null, entriesEl, entry);
110             }
111 
112             return doc.formattedString();
113         }
114         catch (Exception e) {
115             throw new PortletDataException(e);
116         }
117     }
118 
119     public PortletDataHandlerControl[] getExportControls() {
120         return new PortletDataHandlerControl[] {
121             _foldersAndEntries, _categories, _ratings, _tags
122         };
123     }
124 
125     public PortletDataHandlerControl[] getImportControls() {
126         return new PortletDataHandlerControl[] {
127             _foldersAndEntries, _categories, _ratings, _tags
128         };
129     }
130 
131     public PortletPreferences importData(
132             PortletDataContext context, String portletId,
133             PortletPreferences preferences, String data)
134         throws PortletDataException {
135 
136         try {
137             context.importPermissions(
138                 "com.liferay.portlet.bookmarks", context.getSourceGroupId(),
139                 context.getGroupId());
140 
141             Document doc = SAXReaderUtil.read(data);
142 
143             Element root = doc.getRootElement();
144 
145             List<Element> folderEls = root.element("folders").elements(
146                 "folder");
147 
148             Map<Long, Long> folderPKs =
149                 (Map<Long, Long>)context.getNewPrimaryKeysMap(
150                     BookmarksFolder.class);
151 
152             for (Element folderEl : folderEls) {
153                 String path = folderEl.attributeValue("path");
154 
155                 if (!context.isPathNotProcessed(path)) {
156                     continue;
157                 }
158 
159                 BookmarksFolder folder =
160                     (BookmarksFolder)context.getZipEntryAsObject(path);
161 
162                 importFolder(context, folderPKs, folder);
163             }
164 
165             List<Element> entryEls = root.element("entries").elements("entry");
166 
167             for (Element entryEl : entryEls) {
168                 String path = entryEl.attributeValue("path");
169 
170                 if (!context.isPathNotProcessed(path)) {
171                     continue;
172                 }
173 
174                 BookmarksEntry entry =
175                     (BookmarksEntry)context.getZipEntryAsObject(path);
176 
177                 importEntry(context, folderPKs, entry);
178             }
179 
180             return null;
181         }
182         catch (Exception e) {
183             throw new PortletDataException(e);
184         }
185     }
186 
187     protected void exportFolder(
188             PortletDataContext context, Element foldersEl, Element entriesEl,
189             BookmarksFolder folder)
190         throws PortalException, SystemException {
191 
192         if (context.isWithinDateRange(folder.getModifiedDate())) {
193             exportParentFolder(context, foldersEl, folder.getParentFolderId());
194 
195             String path = getFolderPath(context, folder);
196 
197             if (context.isPathNotProcessed(path)) {
198                 Element folderEl = foldersEl.addElement("folder");
199 
200                 folderEl.addAttribute("path", path);
201 
202                 folder.setUserUuid(folder.getUserUuid());
203 
204                 context.addPermissions(
205                     BookmarksFolder.class, folder.getFolderId());
206 
207                 context.addZipEntry(path, folder);
208             }
209         }
210 
211         List<BookmarksEntry> entries = BookmarksEntryUtil.findByG_F(
212             folder.getGroupId(), folder.getFolderId());
213 
214         for (BookmarksEntry entry : entries) {
215             exportEntry(context, foldersEl, entriesEl, entry);
216         }
217     }
218 
219     protected void exportEntry(
220             PortletDataContext context, Element foldersEl, Element entriesEl,
221             BookmarksEntry entry)
222         throws PortalException, SystemException {
223 
224         if (!context.isWithinDateRange(entry.getModifiedDate())) {
225             return;
226         }
227 
228         if (foldersEl != null) {
229             exportParentFolder(context, foldersEl, entry.getFolderId());
230         }
231 
232         String path = getEntryPath(context, entry);
233 
234         if (context.isPathNotProcessed(path)) {
235             Element entryEl = entriesEl.addElement("entry");
236 
237             entryEl.addAttribute("path", path);
238 
239             context.addPermissions(BookmarksEntry.class, entry.getEntryId());
240 
241             if (context.getBooleanParameter(_NAMESPACE, "categories")) {
242                 context.addAssetCategories(
243                     BookmarksEntry.class, entry.getEntryId());
244             }
245 
246             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
247                 context.addRatingsEntries(
248                     BookmarksEntry.class, entry.getEntryId());
249             }
250 
251             if (context.getBooleanParameter(_NAMESPACE, "tags")) {
252                 context.addAssetTags(BookmarksEntry.class, entry.getEntryId());
253             }
254 
255             entry.setUserUuid(entry.getUserUuid());
256 
257             context.addZipEntry(path, entry);
258         }
259     }
260 
261     protected void exportParentFolder(
262             PortletDataContext context, Element foldersEl, long folderId)
263         throws PortalException, SystemException {
264 
265         if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
266             return;
267         }
268 
269         BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
270 
271         exportParentFolder(context, foldersEl, folder.getParentFolderId());
272 
273         String path = getFolderPath(context, folder);
274 
275         if (context.isPathNotProcessed(path)) {
276             Element folderEl = foldersEl.addElement("folder");
277 
278             folderEl.addAttribute("path", path);
279 
280             folder.setUserUuid(folder.getUserUuid());
281 
282             context.addPermissions(BookmarksFolder.class, folder.getFolderId());
283 
284             context.addZipEntry(path, folder);
285         }
286     }
287 
288     protected String getEntryPath(
289         PortletDataContext context, BookmarksEntry entry) {
290 
291         StringBundler sb = new StringBundler(4);
292 
293         sb.append(context.getPortletPath(PortletKeys.BOOKMARKS));
294         sb.append("/entries/");
295         sb.append(entry.getEntryId());
296         sb.append(".xml");
297 
298         return sb.toString();
299     }
300 
301     protected String getFolderPath(
302         PortletDataContext context, BookmarksFolder folder) {
303 
304         StringBundler sb = new StringBundler(4);
305 
306         sb.append(context.getPortletPath(PortletKeys.BOOKMARKS));
307         sb.append("/folders/");
308         sb.append(folder.getFolderId());
309         sb.append(".xml");
310 
311         return sb.toString();
312     }
313 
314     protected String getImportFolderPath(
315         PortletDataContext context, long folderId) {
316 
317         StringBundler sb = new StringBundler(4);
318 
319         sb.append(context.getSourcePortletPath(PortletKeys.BOOKMARKS));
320         sb.append("/folders/");
321         sb.append(folderId);
322         sb.append(".xml");
323 
324         return sb.toString();
325     }
326 
327     protected void importEntry(
328             PortletDataContext context, Map<Long, Long> folderPKs,
329             BookmarksEntry entry)
330         throws Exception {
331 
332         long userId = context.getUserId(entry.getUserUuid());
333         long groupId = context.getGroupId();
334         long folderId = MapUtil.getLong(
335             folderPKs, entry.getFolderId(), entry.getFolderId());
336 
337         long[] assetCategoryIds = null;
338         String[] assetTagNames = null;
339 
340         if (context.getBooleanParameter(_NAMESPACE, "categories")) {
341             assetCategoryIds = context.getAssetCategoryIds(
342                 BookmarksEntry.class, entry.getEntryId());
343         }
344 
345         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
346             assetTagNames = context.getAssetTagNames(
347                 BookmarksEntry.class, entry.getEntryId());
348         }
349 
350         ServiceContext serviceContext = new ServiceContext();
351 
352         serviceContext.setAddCommunityPermissions(true);
353         serviceContext.setAddGuestPermissions(true);
354         serviceContext.setAssetCategoryIds(assetCategoryIds);
355         serviceContext.setAssetTagNames(assetTagNames);
356         serviceContext.setCreateDate(entry.getCreateDate());
357         serviceContext.setModifiedDate(entry.getModifiedDate());
358 
359         if ((folderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
360             (folderId == entry.getFolderId())) {
361 
362             String path = getImportFolderPath(context, folderId);
363 
364             BookmarksFolder folder =
365                 (BookmarksFolder)context.getZipEntryAsObject(path);
366 
367             importFolder(context, folderPKs, folder);
368 
369             folderId = MapUtil.getLong(
370                 folderPKs, entry.getFolderId(), entry.getFolderId());
371         }
372 
373         BookmarksEntry importedEntry = null;
374 
375         try {
376             if (context.getDataStrategy().equals(
377                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
378 
379                 BookmarksEntry existingEntry = BookmarksEntryUtil.fetchByUUID_G(
380                     entry.getUuid(), groupId);
381 
382                 if (existingEntry == null) {
383                     importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
384                         entry.getUuid(), userId, groupId, folderId,
385                         entry.getName(), entry.getUrl(), entry.getComments(),
386                         serviceContext);
387                 }
388                 else {
389                     importedEntry = BookmarksEntryLocalServiceUtil.updateEntry(
390                         userId, existingEntry.getEntryId(), groupId, folderId,
391                         entry.getName(), entry.getUrl(), entry.getComments(),
392                         serviceContext);
393                 }
394             }
395             else {
396                 importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
397                     null, userId, groupId, folderId, entry.getName(),
398                     entry.getUrl(), entry.getComments(), serviceContext);
399             }
400 
401             context.importPermissions(
402                 BookmarksEntry.class, entry.getEntryId(),
403                 importedEntry.getEntryId());
404 
405             if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
406                 context.importRatingsEntries(
407                     BookmarksEntry.class, entry.getEntryId(),
408                     importedEntry.getEntryId());
409             }
410         }
411         catch (NoSuchFolderException nsfe) {
412             _log.error(
413                 "Could not find the parent folder for entry " +
414                     entry.getEntryId());
415         }
416     }
417 
418     protected void importFolder(
419             PortletDataContext context, Map<Long, Long> folderPKs,
420             BookmarksFolder folder)
421         throws Exception {
422 
423         long userId = context.getUserId(folder.getUserUuid());
424         long parentFolderId = MapUtil.getLong(
425             folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
426 
427         ServiceContext serviceContext = new ServiceContext();
428 
429         serviceContext.setAddCommunityPermissions(true);
430         serviceContext.setAddGuestPermissions(true);
431         serviceContext.setCreateDate(folder.getCreateDate());
432         serviceContext.setModifiedDate(folder.getModifiedDate());
433         serviceContext.setScopeGroupId(context.getScopeGroupId());
434 
435         if ((parentFolderId !=
436                 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
437             (parentFolderId == folder.getParentFolderId())) {
438 
439             String path = getImportFolderPath(context, parentFolderId);
440 
441             BookmarksFolder parentFolder =
442                 (BookmarksFolder)context.getZipEntryAsObject(path);
443 
444             importFolder(context, folderPKs, parentFolder);
445 
446             parentFolderId = MapUtil.getLong(
447                 folderPKs, folder.getParentFolderId(),
448                 folder.getParentFolderId());
449         }
450 
451         BookmarksFolder importedFolder = null;
452 
453         try {
454             if (parentFolderId !=
455                 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
456 
457                 BookmarksFolderUtil.findByPrimaryKey(parentFolderId);
458             }
459 
460             if (context.getDataStrategy().equals(
461                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
462 
463                 BookmarksFolder existingFolder =
464                     BookmarksFolderUtil.fetchByUUID_G(
465                         folder.getUuid(), context.getGroupId());
466 
467                 if (existingFolder == null) {
468                     importedFolder = BookmarksFolderLocalServiceUtil.addFolder(
469                         folder.getUuid(), userId, parentFolderId,
470                         folder.getName(), folder.getDescription(),
471                         serviceContext);
472                 }
473                 else {
474                     importedFolder =
475                         BookmarksFolderLocalServiceUtil.updateFolder(
476                             existingFolder.getFolderId(), parentFolderId,
477                             folder.getName(), folder.getDescription(), false,
478                             serviceContext);
479                 }
480             }
481             else {
482                 importedFolder = BookmarksFolderLocalServiceUtil.addFolder(
483                     null, userId, parentFolderId, folder.getName(),
484                     folder.getDescription(), serviceContext);
485             }
486 
487             folderPKs.put(folder.getFolderId(), importedFolder.getFolderId());
488 
489             context.importPermissions(
490                 BookmarksFolder.class, folder.getFolderId(),
491                 importedFolder.getFolderId());
492         }
493         catch (NoSuchFolderException nsfe) {
494             _log.error(
495                 "Could not find the parent folder for folder " +
496                     folder.getFolderId());
497         }
498     }
499 
500     private static final String _NAMESPACE = "bookmarks";
501 
502     private static Log _log = LogFactoryUtil.getLog(
503         BookmarksPortletDataHandlerImpl.class);
504 
505     private static PortletDataHandlerBoolean _categories =
506         new PortletDataHandlerBoolean(_NAMESPACE, "categories");
507 
508     private static PortletDataHandlerBoolean _foldersAndEntries =
509         new PortletDataHandlerBoolean(
510             _NAMESPACE, "folders-and-entries", true, true);
511 
512     private static PortletDataHandlerBoolean _ratings =
513         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
514 
515     private static PortletDataHandlerBoolean _tags =
516         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
517 
518 }