001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.bookmarks.lar;
022:
023: import com.liferay.portal.kernel.lar.PortletDataContext;
024: import com.liferay.portal.kernel.lar.PortletDataException;
025: import com.liferay.portal.kernel.lar.PortletDataHandler;
026: import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
027: import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
028: import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
029: import com.liferay.portal.util.PortalUtil;
030: import com.liferay.portlet.bookmarks.NoSuchEntryException;
031: import com.liferay.portlet.bookmarks.NoSuchFolderException;
032: import com.liferay.portlet.bookmarks.model.BookmarksEntry;
033: import com.liferay.portlet.bookmarks.model.BookmarksFolder;
034: import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
035: import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
036: import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
037: import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryFinderUtil;
038: import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
039: import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderUtil;
040: import com.liferay.util.CollectionFactory;
041: import com.liferay.util.MapUtil;
042:
043: import com.thoughtworks.xstream.XStream;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Map;
049:
050: import javax.portlet.PortletPreferences;
051:
052: import org.apache.commons.logging.Log;
053: import org.apache.commons.logging.LogFactory;
054:
055: import org.dom4j.Document;
056: import org.dom4j.DocumentHelper;
057: import org.dom4j.Element;
058:
059: /**
060: * <a href="BookmarksPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
061: * </a>
062: *
063: * @author Jorge Ferrer
064: * @author Bruno Farache
065: *
066: */
067: public class BookmarksPortletDataHandlerImpl implements
068: PortletDataHandler {
069:
070: public PortletPreferences deleteData(PortletDataContext context,
071: String portletId, PortletPreferences prefs)
072: throws PortletDataException {
073:
074: try {
075:
076: // Folders
077:
078: if (!context
079: .addPrimaryKey(
080: BookmarksPortletDataHandlerImpl.class,
081: "deleteData")) {
082:
083: BookmarksFolderLocalServiceUtil.deleteFolders(context
084: .getGroupId());
085: }
086:
087: return null;
088: } catch (Exception e) {
089: throw new PortletDataException(e);
090: }
091: }
092:
093: public String exportData(PortletDataContext context,
094: String portletId, PortletPreferences prefs)
095: throws PortletDataException {
096:
097: try {
098: XStream xStream = new XStream();
099:
100: Document doc = DocumentHelper.createDocument();
101:
102: Element root = doc.addElement("bookmarks-data");
103:
104: root.addAttribute("group-id", String.valueOf(context
105: .getGroupId()));
106:
107: // Folders
108:
109: List folders = BookmarksFolderUtil.findByGroupId(context
110: .getGroupId());
111:
112: List entries = new ArrayList();
113:
114: Iterator itr = folders.iterator();
115:
116: while (itr.hasNext()) {
117: BookmarksFolder folder = (BookmarksFolder) itr.next();
118:
119: if (context.addPrimaryKey(BookmarksFolder.class, folder
120: .getPrimaryKeyObj())) {
121:
122: itr.remove();
123: } else {
124: folder.setUserUuid(folder.getUserUuid());
125:
126: List folderEntries = BookmarksEntryUtil
127: .findByFolderId(folder.getFolderId());
128:
129: entries.addAll(folderEntries);
130: }
131: }
132:
133: String xml = xStream.toXML(folders);
134:
135: Element el = root.addElement("bookmark-folders");
136:
137: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
138:
139: el.content().add(tempDoc.getRootElement().createCopy());
140:
141: // Entries
142:
143: itr = entries.iterator();
144:
145: while (itr.hasNext()) {
146: BookmarksEntry entry = (BookmarksEntry) itr.next();
147:
148: if (context.addPrimaryKey(BookmarksEntry.class, entry
149: .getPrimaryKeyObj())) {
150:
151: itr.remove();
152: } else {
153: entry.setUserUuid(entry.getUserUuid());
154:
155: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
156: context.addTagsEntries(BookmarksEntry.class,
157: entry.getPrimaryKeyObj());
158: }
159: }
160: }
161:
162: xml = xStream.toXML(entries);
163:
164: el = root.addElement("bookmark-entries");
165:
166: tempDoc = PortalUtil.readDocumentFromXML(xml);
167:
168: el.content().add(tempDoc.getRootElement().createCopy());
169:
170: return doc.asXML();
171: } catch (Exception e) {
172: throw new PortletDataException(e);
173: }
174: }
175:
176: public PortletDataHandlerControl[] getExportControls()
177: throws PortletDataException {
178:
179: return new PortletDataHandlerControl[] { _foldersAndEntries,
180: _tags };
181: }
182:
183: public PortletDataHandlerControl[] getImportControls()
184: throws PortletDataException {
185:
186: return new PortletDataHandlerControl[] { _foldersAndEntries,
187: _tags };
188: }
189:
190: public PortletPreferences importData(PortletDataContext context,
191: String portletId, PortletPreferences prefs, String data)
192: throws PortletDataException {
193:
194: try {
195: XStream xStream = new XStream();
196:
197: Document doc = PortalUtil.readDocumentFromXML(data);
198:
199: Element root = doc.getRootElement();
200:
201: // Folders
202:
203: Element el = root.element("bookmark-folders").element(
204: "list");
205:
206: Document tempDoc = DocumentHelper.createDocument();
207:
208: tempDoc.content().add(el.createCopy());
209:
210: Map folderPKs = CollectionFactory.getHashMap();
211:
212: List folders = (List) xStream.fromXML(tempDoc.asXML());
213:
214: Iterator itr = folders.iterator();
215:
216: while (itr.hasNext()) {
217: BookmarksFolder folder = (BookmarksFolder) itr.next();
218:
219: importFolder(context, folderPKs, folder);
220: }
221:
222: // Entries
223:
224: el = root.element("bookmark-entries").element("list");
225:
226: tempDoc = DocumentHelper.createDocument();
227:
228: tempDoc.content().add(el.createCopy());
229:
230: List entries = (List) xStream.fromXML(tempDoc.asXML());
231:
232: itr = entries.iterator();
233:
234: while (itr.hasNext()) {
235: BookmarksEntry entry = (BookmarksEntry) itr.next();
236:
237: importEntry(context, folderPKs, entry);
238: }
239:
240: return null;
241: } catch (Exception e) {
242: throw new PortletDataException(e);
243: }
244: }
245:
246: protected void importEntry(PortletDataContext context,
247: Map folderPKs, BookmarksEntry entry) throws Exception {
248:
249: long userId = context.getUserId(entry.getUserUuid());
250: long folderId = MapUtil.getLong(folderPKs, entry.getFolderId(),
251: entry.getFolderId());
252:
253: String[] tagsEntries = null;
254:
255: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
256: tagsEntries = context.getTagsEntries(BookmarksEntry.class,
257: entry.getPrimaryKeyObj());
258: }
259:
260: boolean addCommunityPermissions = true;
261: boolean addGuestPermissions = true;
262:
263: BookmarksEntry existingEntry = null;
264:
265: try {
266: BookmarksFolderUtil.findByPrimaryKey(folderId);
267:
268: if (context.getDataStrategy().equals(
269: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
270:
271: try {
272: existingEntry = BookmarksEntryFinderUtil
273: .findByUuid_G(entry.getUuid(), context
274: .getGroupId());
275:
276: BookmarksEntryLocalServiceUtil.updateEntry(userId,
277: existingEntry.getEntryId(), folderId, entry
278: .getName(), entry.getUrl(), entry
279: .getComments(), tagsEntries);
280: } catch (NoSuchEntryException nsee) {
281: BookmarksEntryLocalServiceUtil.addEntry(entry
282: .getUuid(), userId, folderId, entry
283: .getName(), entry.getUrl(), entry
284: .getComments(), tagsEntries,
285: addCommunityPermissions,
286: addGuestPermissions);
287: }
288: } else {
289: BookmarksEntryLocalServiceUtil.addEntry(userId,
290: folderId, entry.getName(), entry.getUrl(),
291: entry.getComments(), tagsEntries,
292: addCommunityPermissions, addGuestPermissions);
293: }
294: } catch (NoSuchFolderException nsfe) {
295: _log.error("Could not find the parent folder for entry "
296: + entry.getEntryId());
297: }
298: }
299:
300: protected void importFolder(PortletDataContext context,
301: Map folderPKs, BookmarksFolder folder) throws Exception {
302:
303: long userId = context.getUserId(folder.getUserUuid());
304: long plid = context.getPlid();
305: long parentFolderId = MapUtil.getLong(folderPKs, folder
306: .getParentFolderId(), folder.getParentFolderId());
307:
308: boolean addCommunityPermissions = true;
309: boolean addGuestPermissions = true;
310:
311: BookmarksFolder existingFolder = null;
312:
313: try {
314: if (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
315:
316: BookmarksFolderUtil.findByPrimaryKey(parentFolderId);
317: }
318:
319: if (context.getDataStrategy().equals(
320: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
321: existingFolder = BookmarksFolderUtil.fetchByUUID_G(
322: folder.getUuid(), context.getGroupId());
323:
324: if (existingFolder == null) {
325: existingFolder = BookmarksFolderLocalServiceUtil
326: .addFolder(folder.getUuid(), userId, plid,
327: parentFolderId, folder.getName(),
328: folder.getDescription(),
329: addCommunityPermissions,
330: addGuestPermissions);
331: } else {
332: existingFolder = BookmarksFolderLocalServiceUtil
333: .updateFolder(existingFolder.getFolderId(),
334: parentFolderId, folder.getName(),
335: folder.getDescription(), false);
336: }
337: } else {
338: existingFolder = BookmarksFolderLocalServiceUtil
339: .addFolder(userId, plid, parentFolderId, folder
340: .getName(), folder.getDescription(),
341: addCommunityPermissions,
342: addGuestPermissions);
343: }
344:
345: folderPKs.put(folder.getPrimaryKeyObj(), existingFolder
346: .getPrimaryKeyObj());
347: } catch (NoSuchFolderException nsfe) {
348: _log.error("Could not find the parent folder for folder "
349: + folder.getFolderId());
350: }
351: }
352:
353: private static final String _NAMESPACE = "bookmarks";
354:
355: private static final PortletDataHandlerBoolean _foldersAndEntries = new PortletDataHandlerBoolean(
356: _NAMESPACE, "folders-and-entries", true, true);
357:
358: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
359: _NAMESPACE, "tags");
360:
361: private static Log _log = LogFactory
362: .getLog(BookmarksPortletDataHandlerImpl.class);
363:
364: }
|