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