1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.bookmarks.lar;
24  
25  import com.liferay.portal.kernel.lar.PortletDataContext;
26  import com.liferay.portal.kernel.lar.PortletDataException;
27  import com.liferay.portal.kernel.lar.PortletDataHandler;
28  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
29  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
30  import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.bookmarks.NoSuchEntryException;
33  import com.liferay.portlet.bookmarks.NoSuchFolderException;
34  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
35  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
36  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
37  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
38  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
39  import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryFinderUtil;
40  import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
41  import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderUtil;
42  import com.liferay.util.CollectionFactory;
43  import com.liferay.util.MapUtil;
44  
45  import com.thoughtworks.xstream.XStream;
46  
47  import java.util.ArrayList;
48  import java.util.Iterator;
49  import java.util.List;
50  import java.util.Map;
51  
52  import javax.portlet.PortletPreferences;
53  
54  import org.apache.commons.logging.Log;
55  import org.apache.commons.logging.LogFactory;
56  
57  import org.dom4j.Document;
58  import org.dom4j.DocumentHelper;
59  import org.dom4j.Element;
60  
61  /**
62   * <a href="BookmarksPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
63   * </a>
64   *
65   * @author Jorge Ferrer
66   * @author Bruno Farache
67   *
68   */
69  public class BookmarksPortletDataHandlerImpl implements PortletDataHandler {
70  
71      public PortletPreferences deleteData(
72              PortletDataContext context, String portletId,
73              PortletPreferences prefs)
74          throws PortletDataException {
75  
76          try {
77  
78              // Folders
79  
80              if (!context.addPrimaryKey(
81                      BookmarksPortletDataHandlerImpl.class, "deleteData")) {
82  
83                  BookmarksFolderLocalServiceUtil.deleteFolders(
84                      context.getGroupId());
85              }
86  
87              return null;
88          }
89          catch (Exception e) {
90              throw new PortletDataException(e);
91          }
92      }
93  
94      public String exportData(
95              PortletDataContext context, String portletId,
96              PortletPreferences prefs)
97          throws PortletDataException {
98  
99          try {
100             XStream xStream = new XStream();
101 
102             Document doc = DocumentHelper.createDocument();
103 
104             Element root = doc.addElement("bookmarks-data");
105 
106             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
107 
108             // Folders
109 
110             List folders = BookmarksFolderUtil.findByGroupId(
111                 context.getGroupId());
112 
113             List entries = new ArrayList();
114 
115             Iterator itr = folders.iterator();
116 
117             while (itr.hasNext()) {
118                 BookmarksFolder folder = (BookmarksFolder)itr.next();
119 
120                 if (context.addPrimaryKey(
121                         BookmarksFolder.class, folder.getPrimaryKeyObj())) {
122 
123                     itr.remove();
124                 }
125                 else {
126                     folder.setUserUuid(folder.getUserUuid());
127 
128                     List folderEntries = BookmarksEntryUtil.findByFolderId(
129                         folder.getFolderId());
130 
131                     entries.addAll(folderEntries);
132                 }
133             }
134 
135             String xml = xStream.toXML(folders);
136 
137             Element el = root.addElement("bookmark-folders");
138 
139             Document tempDoc = PortalUtil.readDocumentFromXML(xml);
140 
141             el.content().add(tempDoc.getRootElement().createCopy());
142 
143             // Entries
144 
145             itr = entries.iterator();
146 
147             while (itr.hasNext()) {
148                 BookmarksEntry entry = (BookmarksEntry)itr.next();
149 
150                 if (context.addPrimaryKey(
151                         BookmarksEntry.class, entry.getPrimaryKeyObj())) {
152 
153                     itr.remove();
154                 }
155                 else {
156                     entry.setUserUuid(entry.getUserUuid());
157 
158                     if (context.getBooleanParameter(_NAMESPACE, "tags")) {
159                         context.addTagsEntries(
160                             BookmarksEntry.class, entry.getPrimaryKeyObj());
161                     }
162                 }
163             }
164 
165             xml = xStream.toXML(entries);
166 
167             el = root.addElement("bookmark-entries");
168 
169             tempDoc = PortalUtil.readDocumentFromXML(xml);
170 
171             el.content().add(tempDoc.getRootElement().createCopy());
172 
173             return doc.asXML();
174         }
175         catch (Exception e) {
176             throw new PortletDataException(e);
177         }
178     }
179 
180     public PortletDataHandlerControl[] getExportControls()
181         throws PortletDataException {
182 
183         return new PortletDataHandlerControl[] {_foldersAndEntries, _tags};
184     }
185 
186     public PortletDataHandlerControl[] getImportControls()
187         throws PortletDataException {
188 
189         return new PortletDataHandlerControl[] {_foldersAndEntries, _tags};
190     }
191 
192     public PortletPreferences importData(
193             PortletDataContext context, String portletId,
194             PortletPreferences prefs, String data)
195         throws PortletDataException {
196 
197         try {
198             XStream xStream = new XStream();
199 
200             Document doc = PortalUtil.readDocumentFromXML(data);
201 
202             Element root = doc.getRootElement();
203 
204             // Folders
205 
206             Element el = root.element("bookmark-folders").element("list");
207 
208             Document tempDoc = DocumentHelper.createDocument();
209 
210             tempDoc.content().add(el.createCopy());
211 
212             Map folderPKs = CollectionFactory.getHashMap();
213 
214             List folders = (List)xStream.fromXML(tempDoc.asXML());
215 
216             Iterator itr = folders.iterator();
217 
218             while (itr.hasNext()) {
219                 BookmarksFolder folder = (BookmarksFolder)itr.next();
220 
221                 importFolder(context, folderPKs, folder);
222             }
223 
224             // Entries
225 
226             el = root.element("bookmark-entries").element("list");
227 
228             tempDoc = DocumentHelper.createDocument();
229 
230             tempDoc.content().add(el.createCopy());
231 
232             List entries = (List)xStream.fromXML(tempDoc.asXML());
233 
234             itr = entries.iterator();
235 
236             while (itr.hasNext()) {
237                 BookmarksEntry entry = (BookmarksEntry)itr.next();
238 
239                 importEntry(context, folderPKs, entry);
240             }
241 
242             return null;
243         }
244         catch (Exception e) {
245             throw new PortletDataException(e);
246         }
247     }
248 
249     protected void importEntry(
250             PortletDataContext context, Map folderPKs, BookmarksEntry entry)
251         throws Exception {
252 
253         long userId = context.getUserId(entry.getUserUuid());
254         long folderId = MapUtil.getLong(
255             folderPKs, entry.getFolderId(), entry.getFolderId());
256 
257         String[] tagsEntries = null;
258 
259         if (context.getBooleanParameter(_NAMESPACE, "tags")) {
260             tagsEntries = context.getTagsEntries(
261                 BookmarksEntry.class, entry.getPrimaryKeyObj());
262         }
263 
264         boolean addCommunityPermissions = true;
265         boolean addGuestPermissions = true;
266 
267         BookmarksEntry existingEntry = null;
268 
269         try {
270             BookmarksFolderUtil.findByPrimaryKey(folderId);
271 
272             if (context.getDataStrategy().equals(
273                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
274 
275                 try {
276                     existingEntry = BookmarksEntryFinderUtil.findByUuid_G(
277                         entry.getUuid(), context.getGroupId());
278 
279                     BookmarksEntryLocalServiceUtil.updateEntry(
280                         userId, existingEntry.getEntryId(), folderId,
281                         entry.getName(), entry.getUrl(), entry.getComments(),
282                         tagsEntries);
283                 }
284                 catch (NoSuchEntryException nsee) {
285                     BookmarksEntryLocalServiceUtil.addEntry(
286                         entry.getUuid(), userId, folderId, entry.getName(),
287                         entry.getUrl(), entry.getComments(), tagsEntries,
288                         addCommunityPermissions, addGuestPermissions);
289                 }
290             }
291             else {
292                 BookmarksEntryLocalServiceUtil.addEntry(
293                     userId, folderId, entry.getName(), entry.getUrl(),
294                     entry.getComments(), tagsEntries, addCommunityPermissions,
295                     addGuestPermissions);
296             }
297         }
298         catch (NoSuchFolderException nsfe) {
299             _log.error(
300                 "Could not find the parent folder for entry " +
301                     entry.getEntryId());
302         }
303     }
304 
305     protected void importFolder(
306             PortletDataContext context, Map folderPKs, BookmarksFolder folder)
307         throws Exception {
308 
309         long userId = context.getUserId(folder.getUserUuid());
310         long plid = context.getPlid();
311         long parentFolderId = MapUtil.getLong(
312             folderPKs, folder.getParentFolderId(), folder.getParentFolderId());
313 
314         boolean addCommunityPermissions = true;
315         boolean addGuestPermissions = true;
316 
317         BookmarksFolder existingFolder = null;
318 
319         try {
320             if (parentFolderId !=
321                     BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
322 
323                 BookmarksFolderUtil.findByPrimaryKey(parentFolderId);
324             }
325 
326             if (context.getDataStrategy().equals(
327                     PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
328                 existingFolder = BookmarksFolderUtil.fetchByUUID_G(
329                     folder.getUuid(), context.getGroupId());
330 
331                 if (existingFolder == null) {
332                     existingFolder = BookmarksFolderLocalServiceUtil.addFolder(
333                         folder.getUuid(), userId, plid, parentFolderId,
334                         folder.getName(), folder.getDescription(),
335                         addCommunityPermissions, addGuestPermissions);
336                 }
337                 else {
338                     existingFolder =
339                         BookmarksFolderLocalServiceUtil.updateFolder(
340                             existingFolder.getFolderId(), parentFolderId,
341                             folder.getName(), folder.getDescription(), false);
342                 }
343             }
344             else {
345                 existingFolder = BookmarksFolderLocalServiceUtil.addFolder(
346                     userId, plid, parentFolderId, folder.getName(),
347                     folder.getDescription(), addCommunityPermissions,
348                     addGuestPermissions);
349             }
350 
351             folderPKs.put(
352                 folder.getPrimaryKeyObj(), existingFolder.getPrimaryKeyObj());
353         }
354         catch (NoSuchFolderException nsfe) {
355             _log.error(
356                 "Could not find the parent folder for folder " +
357                     folder.getFolderId());
358         }
359     }
360 
361     private static final String _NAMESPACE = "bookmarks";
362 
363     private static final PortletDataHandlerBoolean _foldersAndEntries =
364         new PortletDataHandlerBoolean(
365             _NAMESPACE, "folders-and-entries", true, true);
366 
367     private static final PortletDataHandlerBoolean _tags =
368         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
369 
370     private static Log _log =
371         LogFactory.getLog(BookmarksPortletDataHandlerImpl.class);
372 
373 }