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