001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.bookmarks.lar;
016    
017    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
029    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
030    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
031    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
032    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
033    import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
034    import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderUtil;
035    
036    import java.util.List;
037    import java.util.Map;
038    
039    import javax.portlet.PortletPreferences;
040    
041    /**
042     * @author Jorge Ferrer
043     * @author Bruno Farache
044     * @author Raymond Augé
045     * @author Juan Fernández
046     */
047    public class BookmarksPortletDataHandlerImpl extends BasePortletDataHandler {
048    
049            public PortletDataHandlerControl[] getExportControls() {
050                    return new PortletDataHandlerControl[] {
051                            _foldersAndEntries, _categories, _ratings, _tags
052                    };
053            }
054    
055            public PortletDataHandlerControl[] getImportControls() {
056                    return new PortletDataHandlerControl[] {
057                            _foldersAndEntries, _categories, _ratings, _tags
058                    };
059            }
060    
061            public boolean isPublishToLiveByDefault() {
062                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
063            }
064    
065            protected PortletPreferences doDeleteData(
066                            PortletDataContext context, String portletId,
067                            PortletPreferences preferences)
068                    throws Exception {
069    
070                    if (!context.addPrimaryKey(
071                                    BookmarksPortletDataHandlerImpl.class, "deleteData")) {
072    
073                            BookmarksFolderLocalServiceUtil.deleteFolders(
074                                    context.getScopeGroupId());
075    
076                            BookmarksEntryLocalServiceUtil.deleteEntries(
077                                    context.getScopeGroupId(),
078                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
079                    }
080    
081                    return null;
082            }
083    
084            protected String doExportData(
085                            PortletDataContext context, String portletId,
086                            PortletPreferences preferences)
087                    throws Exception {
088    
089                    context.addPermissions(
090                            "com.liferay.portlet.bookmarks", context.getScopeGroupId());
091    
092                    Document document = SAXReaderUtil.createDocument();
093    
094                    Element rootElement = document.addElement("bookmarks-data");
095    
096                    rootElement.addAttribute(
097                            "group-id", String.valueOf(context.getScopeGroupId()));
098    
099                    Element foldersElement = rootElement.addElement("folders");
100                    Element entriesElement = rootElement.addElement("entries");
101    
102                    List<BookmarksFolder> folders = BookmarksFolderUtil.findByGroupId(
103                            context.getScopeGroupId());
104    
105                    for (BookmarksFolder folder : folders) {
106                            exportFolder(context, foldersElement, entriesElement, folder);
107                    }
108    
109                    List<BookmarksEntry> entries = BookmarksEntryUtil.findByG_F(
110                            context.getScopeGroupId(),
111                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
112    
113                    for (BookmarksEntry entry : entries) {
114                            exportEntry(context, null, entriesElement, entry);
115                    }
116    
117                    return document.formattedString();
118            }
119    
120            protected PortletPreferences doImportData(
121                            PortletDataContext context, String portletId,
122                            PortletPreferences preferences, String data)
123                    throws Exception {
124    
125                    context.importPermissions(
126                            "com.liferay.portlet.bookmarks", context.getSourceGroupId(),
127                            context.getScopeGroupId());
128    
129                    Document document = SAXReaderUtil.read(data);
130    
131                    Element rootElement = document.getRootElement();
132    
133                    Element foldersElement = rootElement.element("folders");
134    
135                    for (Element folderElement : foldersElement.elements("folder")) {
136                            String path = folderElement.attributeValue("path");
137    
138                            if (!context.isPathNotProcessed(path)) {
139                                    continue;
140                            }
141    
142                            BookmarksFolder folder =
143                                    (BookmarksFolder)context.getZipEntryAsObject(path);
144    
145                            importFolder(context, folder);
146                    }
147    
148                    Element entriesElement = rootElement.element("entries");
149    
150                    for (Element entryElement : entriesElement.elements("entry")) {
151                            String path = entryElement.attributeValue("path");
152    
153                            if (!context.isPathNotProcessed(path)) {
154                                    continue;
155                            }
156    
157                            BookmarksEntry entry = (BookmarksEntry)context.getZipEntryAsObject(
158                                    path);
159    
160                            importEntry(context, entry);
161                    }
162    
163                    return null;
164            }
165    
166            protected void exportFolder(
167                            PortletDataContext context, Element foldersElement,
168                            Element entriesElement, BookmarksFolder folder)
169                    throws Exception {
170    
171                    if (context.isWithinDateRange(folder.getModifiedDate())) {
172                            exportParentFolder(
173                                    context, foldersElement, folder.getParentFolderId());
174    
175                            String path = getFolderPath(context, folder);
176    
177                            if (context.isPathNotProcessed(path)) {
178                                    Element folderElement = foldersElement.addElement("folder");
179    
180                                    folderElement.addAttribute("path", path);
181    
182                                    folder.setUserUuid(folder.getUserUuid());
183    
184                                    context.addPermissions(
185                                            BookmarksFolder.class, folder.getFolderId());
186    
187                                    context.addZipEntry(path, folder);
188                            }
189                    }
190    
191                    List<BookmarksEntry> entries = BookmarksEntryUtil.findByG_F(
192                            folder.getGroupId(), folder.getFolderId());
193    
194                    for (BookmarksEntry entry : entries) {
195                            exportEntry(context, foldersElement, entriesElement, entry);
196                    }
197            }
198    
199            protected void exportEntry(
200                            PortletDataContext context, Element foldersElement,
201                            Element entriesElement, BookmarksEntry entry)
202                    throws Exception {
203    
204                    if (!context.isWithinDateRange(entry.getModifiedDate())) {
205                            return;
206                    }
207    
208                    if (foldersElement != null) {
209                            exportParentFolder(context, foldersElement, entry.getFolderId());
210                    }
211    
212                    String path = getEntryPath(context, entry);
213    
214                    if (context.isPathNotProcessed(path)) {
215                            Element entryElement = entriesElement.addElement("entry");
216    
217                            entryElement.addAttribute("path", path);
218    
219                            context.addPermissions(BookmarksEntry.class, entry.getEntryId());
220    
221                            if (context.getBooleanParameter(_NAMESPACE, "categories")) {
222                                    context.addAssetCategories(
223                                            BookmarksEntry.class, entry.getEntryId());
224                            }
225    
226                            if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
227                                    context.addRatingsEntries(
228                                            BookmarksEntry.class, entry.getEntryId());
229                            }
230    
231                            if (context.getBooleanParameter(_NAMESPACE, "tags")) {
232                                    context.addAssetTags(BookmarksEntry.class, entry.getEntryId());
233                            }
234    
235                            entry.setUserUuid(entry.getUserUuid());
236    
237                            context.addZipEntry(path, entry);
238                    }
239            }
240    
241            protected void exportParentFolder(
242                            PortletDataContext context, Element foldersElement, long folderId)
243                    throws Exception {
244    
245                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
246                            return;
247                    }
248    
249                    BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
250    
251                    exportParentFolder(context, foldersElement, folder.getParentFolderId());
252    
253                    String path = getFolderPath(context, folder);
254    
255                    if (context.isPathNotProcessed(path)) {
256                            Element folderElement = foldersElement.addElement("folder");
257    
258                            folderElement.addAttribute("path", path);
259    
260                            folder.setUserUuid(folder.getUserUuid());
261    
262                            context.addPermissions(BookmarksFolder.class, folder.getFolderId());
263    
264                            context.addZipEntry(path, folder);
265                    }
266            }
267    
268            protected String getEntryPath(
269                    PortletDataContext context, BookmarksEntry entry) {
270    
271                    StringBundler sb = new StringBundler(4);
272    
273                    sb.append(context.getPortletPath(PortletKeys.BOOKMARKS));
274                    sb.append("/entries/");
275                    sb.append(entry.getEntryId());
276                    sb.append(".xml");
277    
278                    return sb.toString();
279            }
280    
281            protected String getFolderPath(
282                    PortletDataContext context, BookmarksFolder folder) {
283    
284                    StringBundler sb = new StringBundler(4);
285    
286                    sb.append(context.getPortletPath(PortletKeys.BOOKMARKS));
287                    sb.append("/folders/");
288                    sb.append(folder.getFolderId());
289                    sb.append(".xml");
290    
291                    return sb.toString();
292            }
293    
294            protected String getImportFolderPath(
295                    PortletDataContext context, long folderId) {
296    
297                    StringBundler sb = new StringBundler(4);
298    
299                    sb.append(context.getSourcePortletPath(PortletKeys.BOOKMARKS));
300                    sb.append("/folders/");
301                    sb.append(folderId);
302                    sb.append(".xml");
303    
304                    return sb.toString();
305            }
306    
307            protected void importEntry(PortletDataContext context, BookmarksEntry entry)
308                    throws Exception {
309    
310                    long userId = context.getUserId(entry.getUserUuid());
311    
312                    Map<Long, Long> folderPKs =
313                            (Map<Long, Long>)context.getNewPrimaryKeysMap(
314                                    BookmarksFolder.class);
315    
316                    long folderId = MapUtil.getLong(
317                            folderPKs, entry.getFolderId(), entry.getFolderId());
318    
319                    long[] assetCategoryIds = null;
320                    String[] assetTagNames = null;
321    
322                    if (context.getBooleanParameter(_NAMESPACE, "categories")) {
323                            assetCategoryIds = context.getAssetCategoryIds(
324                                    BookmarksEntry.class, entry.getEntryId());
325                    }
326    
327                    if (context.getBooleanParameter(_NAMESPACE, "tags")) {
328                            assetTagNames = context.getAssetTagNames(
329                                    BookmarksEntry.class, entry.getEntryId());
330                    }
331    
332                    ServiceContext serviceContext = new ServiceContext();
333    
334                    serviceContext.setAddCommunityPermissions(true);
335                    serviceContext.setAddGuestPermissions(true);
336                    serviceContext.setAssetCategoryIds(assetCategoryIds);
337                    serviceContext.setAssetTagNames(assetTagNames);
338                    serviceContext.setCreateDate(entry.getCreateDate());
339                    serviceContext.setModifiedDate(entry.getModifiedDate());
340    
341                    if ((folderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
342                            (folderId == entry.getFolderId())) {
343    
344                            String path = getImportFolderPath(context, folderId);
345    
346                            BookmarksFolder folder =
347                                    (BookmarksFolder)context.getZipEntryAsObject(path);
348    
349                            importFolder(context, folder);
350    
351                            folderId = MapUtil.getLong(
352                                    folderPKs, entry.getFolderId(), entry.getFolderId());
353                    }
354    
355                    BookmarksEntry importedEntry = null;
356    
357                    if (context.isDataStrategyMirror()) {
358                            BookmarksEntry existingEntry = BookmarksEntryUtil.fetchByUUID_G(
359                                    entry.getUuid(), context.getScopeGroupId());
360    
361                            if (existingEntry == null) {
362                                    serviceContext.setUuid(entry.getUuid());
363    
364                                    importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
365                                            userId, context.getScopeGroupId(), folderId,
366                                            entry.getName(), entry.getUrl(), entry.getComments(),
367                                            serviceContext);
368                            }
369                            else {
370                                    importedEntry = BookmarksEntryLocalServiceUtil.updateEntry(
371                                            userId, existingEntry.getEntryId(),
372                                            context.getScopeGroupId(), folderId, entry.getName(),
373                                            entry.getUrl(), entry.getComments(), serviceContext);
374                            }
375                    }
376                    else {
377                            importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
378                                    userId, context.getScopeGroupId(), folderId, entry.getName(),
379                                    entry.getUrl(), entry.getComments(), serviceContext);
380                    }
381    
382                    context.importPermissions(
383                            BookmarksEntry.class, entry.getEntryId(),
384                            importedEntry.getEntryId());
385    
386                    if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
387                            context.importRatingsEntries(
388                                    BookmarksEntry.class, entry.getEntryId(),
389                                    importedEntry.getEntryId());
390                    }
391            }
392    
393            protected void importFolder(
394                            PortletDataContext context, BookmarksFolder folder)
395                    throws Exception {
396    
397                    long userId = context.getUserId(folder.getUserUuid());
398    
399                    Map<Long, Long> folderPKs =
400                            (Map<Long, Long>)context.getNewPrimaryKeysMap(
401                                    BookmarksFolder.class);
402    
403                    long parentFolderId = MapUtil.getLong(
404                            folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
405    
406                    ServiceContext serviceContext = new ServiceContext();
407    
408                    serviceContext.setAddCommunityPermissions(true);
409                    serviceContext.setAddGuestPermissions(true);
410                    serviceContext.setCreateDate(folder.getCreateDate());
411                    serviceContext.setModifiedDate(folder.getModifiedDate());
412                    serviceContext.setScopeGroupId(context.getScopeGroupId());
413    
414                    if ((parentFolderId !=
415                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
416                            (parentFolderId == folder.getParentFolderId())) {
417    
418                            String path = getImportFolderPath(context, parentFolderId);
419    
420                            BookmarksFolder parentFolder =
421                                    (BookmarksFolder)context.getZipEntryAsObject(path);
422    
423                            importFolder(context, parentFolder);
424    
425                            parentFolderId = MapUtil.getLong(
426                                    folderPKs, folder.getParentFolderId(),
427                                    folder.getParentFolderId());
428                    }
429    
430                    BookmarksFolder importedFolder = null;
431    
432                    if (context.isDataStrategyMirror()) {
433                            BookmarksFolder existingFolder = BookmarksFolderUtil.fetchByUUID_G(
434                                    folder.getUuid(), context.getScopeGroupId());
435    
436                            if (existingFolder == null) {
437                                    serviceContext.setUuid(folder.getUuid());
438    
439                                    importedFolder = BookmarksFolderLocalServiceUtil.addFolder(
440                                            userId, parentFolderId, folder.getName(),
441                                            folder.getDescription(), serviceContext);
442                            }
443                            else {
444                                    importedFolder = BookmarksFolderLocalServiceUtil.updateFolder(
445                                            existingFolder.getFolderId(), parentFolderId,
446                                            folder.getName(), folder.getDescription(), false,
447                                            serviceContext);
448                            }
449                    }
450                    else {
451                            importedFolder = BookmarksFolderLocalServiceUtil.addFolder(
452                                    userId, parentFolderId, folder.getName(),
453                                    folder.getDescription(), serviceContext);
454                    }
455    
456                    folderPKs.put(folder.getFolderId(), importedFolder.getFolderId());
457    
458                    context.importPermissions(
459                            BookmarksFolder.class, folder.getFolderId(),
460                            importedFolder.getFolderId());
461            }
462    
463            private static final String _NAMESPACE = "bookmarks";
464    
465            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
466    
467            private static PortletDataHandlerBoolean _categories =
468                    new PortletDataHandlerBoolean(_NAMESPACE, "categories");
469    
470            private static PortletDataHandlerBoolean _foldersAndEntries =
471                    new PortletDataHandlerBoolean(
472                            _NAMESPACE, "folders-and-entries", true, true);
473    
474            private static PortletDataHandlerBoolean _ratings =
475                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
476    
477            private static PortletDataHandlerBoolean _tags =
478                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
479    
480    }