1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.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.NoSuchEntryException;
35  import com.liferay.portlet.bookmarks.NoSuchFolderException;
36  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
37  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
38  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
39  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
40  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
41  import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
42  import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderUtil;
43  
44  import java.util.List;
45  import java.util.Map;
46  
47  import javax.portlet.PortletPreferences;
48  
49  /**
50   * <a href="BookmarksPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
51   * </a>
52   *
53   * @author Jorge Ferrer
54   * @author Bruno Farache
55   * @author Raymond Augé
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.getScopeGroupId());
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.getScopeGroupId());
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             return doc.formattedString();
105         }
106         catch (Exception e) {
107             throw new PortletDataException(e);
108         }
109     }
110 
111     public PortletDataHandlerControl[] getExportControls() {
112         return new PortletDataHandlerControl[] {_foldersAndEntries, _tags};
113     }
114 
115     public PortletDataHandlerControl[] getImportControls() {
116         return new PortletDataHandlerControl[] {_foldersAndEntries, _tags};
117     }
118 
119     public PortletPreferences importData(
120             PortletDataContext context, String portletId,
121             PortletPreferences preferences, String data)
122         throws PortletDataException {
123 
124         try {
125             context.importPermissions(
126                 "com.liferay.portlet.bookmarks", context.getSourceGroupId(),
127                 context.getGroupId());
128 
129             Document doc = SAXReaderUtil.read(data);
130 
131             Element root = doc.getRootElement();
132 
133             List<Element> folderEls = root.element("folders").elements(
134                 "folder");
135 
136             Map<Long, Long> folderPKs =
137                 (Map<Long, Long>)context.getNewPrimaryKeysMap(
138                     BookmarksFolder.class);
139 
140             for (Element folderEl : folderEls) {
141                 String path = folderEl.attributeValue("path");
142 
143                 if (!context.isPathNotProcessed(path)) {
144                     continue;
145                 }
146 
147                 BookmarksFolder folder =
148                     (BookmarksFolder)context.getZipEntryAsObject(path);
149 
150                 importFolder(context, folderPKs, folder);
151             }
152 
153             List<Element> entryEls = root.element("entries").elements("entry");
154 
155             for (Element entryEl : entryEls) {
156                 String path = entryEl.attributeValue("path");
157 
158                 if (!context.isPathNotProcessed(path)) {
159                     continue;
160                 }
161 
162                 BookmarksEntry entry =
163                     (BookmarksEntry)context.getZipEntryAsObject(path);
164 
165                 importEntry(context, folderPKs, entry);
166             }
167 
168             return null;
169         }
170         catch (Exception e) {
171             throw new PortletDataException(e);
172         }
173     }
174 
175     protected void exportFolder(
176             PortletDataContext context, Element foldersEl, Element entriesEl,
177             BookmarksFolder folder)
178         throws PortalException, SystemException {
179 
180         if (context.isWithinDateRange(folder.getModifiedDate())) {
181             exportParentFolder(context, foldersEl, folder.getParentFolderId());
182 
183             String path = getFolderPath(context, folder);
184 
185             if (context.isPathNotProcessed(path)) {
186                 Element folderEl = foldersEl.addElement("folder");
187 
188                 folderEl.addAttribute("path", path);
189 
190                 folder.setUserUuid(folder.getUserUuid());
191 
192                 context.addPermissions(
193                     BookmarksFolder.class, folder.getFolderId());
194 
195                 context.addZipEntry(path, folder);
196             }
197         }
198 
199         List<BookmarksEntry> entries = BookmarksEntryUtil.findByFolderId(
200             folder.getFolderId());
201 
202         for (BookmarksEntry entry : entries) {
203             exportEntry(context, foldersEl, entriesEl, entry);
204         }
205     }
206 
207     protected void exportEntry(
208             PortletDataContext context, Element foldersEl, Element entriesEl,
209             BookmarksEntry entry)
210         throws PortalException, SystemException {
211 
212         if (!context.isWithinDateRange(entry.getModifiedDate())) {
213             return;
214         }
215 
216         exportParentFolder(context, foldersEl, entry.getFolderId());
217 
218         String path = getEntryPath(context, entry);
219 
220         if (context.isPathNotProcessed(path)) {
221             Element entryEl = entriesEl.addElement("entry");
222 
223             entryEl.addAttribute("path", path);
224 
225             context.addPermissions(BookmarksEntry.class, entry.getEntryId());
226 
227             if (context.getBooleanParameter(_NAMESPACE, "tags")) {
228                 context.addTagsEntries(
229                     BookmarksEntry.class, entry.getEntryId());
230             }
231 
232             entry.setUserUuid(entry.getUserUuid());
233 
234             context.addZipEntry(path, entry);
235         }
236     }
237 
238     protected void exportParentFolder(
239             PortletDataContext context, Element foldersEl, long folderId)
240         throws PortalException, SystemException {
241 
242         if (folderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
243             return;
244         }
245 
246         BookmarksFolder folder = BookmarksFolderUtil.findByPrimaryKey(folderId);
247 
248         exportParentFolder(context, foldersEl, folder.getParentFolderId());
249 
250         String path = getFolderPath(context, folder);
251 
252         if (context.isPathNotProcessed(path)) {
253             Element folderEl = foldersEl.addElement("folder");
254 
255             folderEl.addAttribute("path", path);
256 
257             folder.setUserUuid(folder.getUserUuid());
258 
259             context.addPermissions(BookmarksFolder.class, folder.getFolderId());
260 
261             context.addZipEntry(path, folder);
262         }
263     }
264 
265     protected String getEntryPath(
266         PortletDataContext context, BookmarksEntry entry) {
267 
268         StringBundler sb = new StringBundler(4);
269 
270         sb.append(context.getPortletPath(PortletKeys.BOOKMARKS));
271         sb.append("/entries/");
272         sb.append(entry.getEntryId());
273         sb.append(".xml");
274 
275         return sb.toString();
276     }
277 
278     protected String getFolderPath(
279         PortletDataContext context, BookmarksFolder folder) {
280 
281         StringBundler sb = new StringBundler(4);
282 
283         sb.append(context.getPortletPath(PortletKeys.BOOKMARKS));
284         sb.append("/folders/");
285         sb.append(folder.getFolderId());
286         sb.append(".xml");
287 
288         return sb.toString();
289     }
290 
291     protected String getImportFolderPath(
292         PortletDataContext context, long folderId) {
293 
294         StringBundler sb = new StringBundler(4);
295 
296         sb.append(context.getSourcePortletPath(PortletKeys.BOOKMARKS));
297         sb.append("/folders/");
298         sb.append(folderId);
299         sb.append(".xml");
300 
301         return sb.toString();
302     }
303 
304     protected void importEntry(
305             PortletDataContext context, Map<Long, Long> folderPKs,
306             BookmarksEntry entry)
307         throws Exception {
308 
309         long userId = context.getUserId(entry.getUserUuid());
310         long folderId = MapUtil.getLong(
311             folderPKs, entry.getFolderId(), entry.getFolderId());
312 
313         String[] tagsEntries = null;
314 
315         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
316             tagsEntries = context.getTagsEntries(
317                 BookmarksEntry.class, entry.getEntryId());
318         }
319 
320         ServiceContext serviceContext = new ServiceContext();
321 
322         serviceContext.setAddCommunityPermissions(true);
323         serviceContext.setAddGuestPermissions(true);
324         serviceContext.setTagsEntries(tagsEntries);
325         serviceContext.setCreateDate(entry.getCreateDate());
326         serviceContext.setModifiedDate(entry.getModifiedDate());
327 
328         if ((folderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) &&
329             (folderId == entry.getFolderId())) {
330 
331             String path = getImportFolderPath(context, folderId);
332 
333             BookmarksFolder folder =
334                 (BookmarksFolder)context.getZipEntryAsObject(path);
335 
336             importFolder(context, folderPKs, folder);
337 
338             folderId = MapUtil.getLong(
339                 folderPKs, entry.getFolderId(), entry.getFolderId());
340         }
341 
342         BookmarksEntry existingEntry = null;
343 
344         try {
345             BookmarksFolderUtil.findByPrimaryKey(folderId);
346 
347             if (context.getDataStrategy().equals(
348                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
349 
350                 try {
351                     existingEntry = BookmarksEntryUtil.findByUUID_G(
352                         entry.getUuid(), context.getGroupId());
353 
354                     existingEntry = BookmarksEntryLocalServiceUtil.updateEntry(
355                         userId, existingEntry.getEntryId(), folderId,
356                         entry.getName(), entry.getUrl(), entry.getComments(),
357                         serviceContext);
358                 }
359                 catch (NoSuchEntryException nsee) {
360                     existingEntry = BookmarksEntryLocalServiceUtil.addEntry(
361                         entry.getUuid(), userId, folderId, entry.getName(),
362                         entry.getUrl(), entry.getComments(), serviceContext);
363                 }
364             }
365             else {
366                 existingEntry = BookmarksEntryLocalServiceUtil.addEntry(
367                     userId, folderId, entry.getName(), entry.getUrl(),
368                     entry.getComments(), serviceContext);
369             }
370 
371             context.importPermissions(
372                 BookmarksEntry.class, entry.getEntryId(),
373                 existingEntry.getEntryId());
374         }
375         catch (NoSuchFolderException nsfe) {
376             _log.error(
377                 "Could not find the parent folder for entry " +
378                     entry.getEntryId());
379         }
380     }
381 
382     protected void importFolder(
383             PortletDataContext context, Map<Long, Long> folderPKs,
384             BookmarksFolder folder)
385         throws Exception {
386 
387         long userId = context.getUserId(folder.getUserUuid());
388         long parentFolderId = MapUtil.getLong(
389             folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
390 
391         ServiceContext serviceContext = new ServiceContext();
392 
393         serviceContext.setAddCommunityPermissions(true);
394         serviceContext.setAddGuestPermissions(true);
395         serviceContext.setCreateDate(folder.getCreateDate());
396         serviceContext.setModifiedDate(folder.getModifiedDate());
397         serviceContext.setScopeGroupId(context.getScopeGroupId());
398 
399         if ((parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) &&
400             (parentFolderId == folder.getParentFolderId())) {
401 
402             String path = getImportFolderPath(context, parentFolderId);
403 
404             BookmarksFolder parentFolder =
405                 (BookmarksFolder)context.getZipEntryAsObject(path);
406 
407             importFolder(context, folderPKs, parentFolder);
408 
409             parentFolderId = MapUtil.getLong(
410                 folderPKs, folder.getParentFolderId(),
411                 folder.getParentFolderId());
412         }
413 
414         BookmarksFolder existingFolder = null;
415 
416         try {
417             if (parentFolderId !=
418                     BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
419 
420                 BookmarksFolderUtil.findByPrimaryKey(parentFolderId);
421             }
422 
423             if (context.getDataStrategy().equals(
424                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
425                 existingFolder = BookmarksFolderUtil.fetchByUUID_G(
426                     folder.getUuid(), context.getGroupId());
427 
428                 if (existingFolder == null) {
429                     existingFolder = BookmarksFolderLocalServiceUtil.addFolder(
430                         folder.getUuid(), userId, parentFolderId,
431                         folder.getName(), folder.getDescription(),
432                         serviceContext);
433                 }
434                 else {
435                     existingFolder =
436                         BookmarksFolderLocalServiceUtil.updateFolder(
437                             existingFolder.getFolderId(), parentFolderId,
438                             folder.getName(), folder.getDescription(), false,
439                             serviceContext);
440                 }
441             }
442             else {
443                 existingFolder = BookmarksFolderLocalServiceUtil.addFolder(
444                     userId, parentFolderId, folder.getName(),
445                     folder.getDescription(), serviceContext);
446             }
447 
448             folderPKs.put(folder.getFolderId(), existingFolder.getFolderId());
449 
450             context.importPermissions(
451                 BookmarksFolder.class, folder.getFolderId(),
452                 existingFolder.getFolderId());
453         }
454         catch (NoSuchFolderException nsfe) {
455             _log.error(
456                 "Could not find the parent folder for folder " +
457                     folder.getFolderId());
458         }
459     }
460 
461     private static final String _NAMESPACE = "bookmarks";
462 
463     private static final PortletDataHandlerBoolean _foldersAndEntries =
464         new PortletDataHandlerBoolean(
465             _NAMESPACE, "folders-and-entries", true, true);
466 
467     private static final PortletDataHandlerBoolean _tags =
468         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
469 
470     private static Log _log = LogFactoryUtil.getLog(
471         BookmarksPortletDataHandlerImpl.class);
472 
473 }