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.documentlibrary.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.documentlibrary.NoSuchFileEntryException;
031: import com.liferay.portlet.documentlibrary.NoSuchFileShortcutException;
032: import com.liferay.portlet.documentlibrary.NoSuchFolderException;
033: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034: import com.liferay.portlet.documentlibrary.model.DLFileRank;
035: import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
036: import com.liferay.portlet.documentlibrary.model.DLFolder;
037: import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
038: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
039: import com.liferay.portlet.documentlibrary.service.DLFileRankLocalServiceUtil;
040: import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
041: import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
042: import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryFinderUtil;
043: import com.liferay.portlet.documentlibrary.service.persistence.DLFileEntryUtil;
044: import com.liferay.portlet.documentlibrary.service.persistence.DLFileRankUtil;
045: import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutFinderUtil;
046: import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutUtil;
047: import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
048: import com.liferay.util.CollectionFactory;
049: import com.liferay.util.FileUtil;
050: import com.liferay.util.MapUtil;
051:
052: import com.thoughtworks.xstream.XStream;
053:
054: import java.io.InputStream;
055:
056: import java.util.ArrayList;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.Map;
060:
061: import javax.portlet.PortletPreferences;
062:
063: import org.apache.commons.logging.Log;
064: import org.apache.commons.logging.LogFactory;
065:
066: import org.dom4j.Document;
067: import org.dom4j.DocumentHelper;
068: import org.dom4j.Element;
069:
070: /**
071: * <a href="DLPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
072: *
073: * @author Bruno Farache
074: *
075: */
076: public class DLPortletDataHandlerImpl implements PortletDataHandler {
077:
078: public PortletPreferences deleteData(PortletDataContext context,
079: String portletId, PortletPreferences prefs)
080: throws PortletDataException {
081:
082: try {
083:
084: // Folders
085:
086: if (!context.addPrimaryKey(DLPortletDataHandlerImpl.class,
087: "deleteData")) {
088:
089: DLFolderLocalServiceUtil.deleteFolders(context
090: .getGroupId());
091: }
092:
093: return null;
094: } catch (Exception e) {
095: throw new PortletDataException(e);
096: }
097: }
098:
099: public String exportData(PortletDataContext context,
100: String portletId, PortletPreferences prefs)
101: throws PortletDataException {
102:
103: try {
104: XStream xStream = new XStream();
105:
106: Document doc = DocumentHelper.createDocument();
107:
108: Element root = doc.addElement("documentlibrary-data");
109:
110: root.addAttribute("group-id", String.valueOf(context
111: .getGroupId()));
112:
113: // Folders
114:
115: List folders = DLFolderUtil.findByGroupId(context
116: .getGroupId());
117:
118: List entries = new ArrayList();
119:
120: List shortcuts = new ArrayList();
121:
122: Iterator itr = folders.iterator();
123:
124: while (itr.hasNext()) {
125: DLFolder folder = (DLFolder) itr.next();
126:
127: if (context.addPrimaryKey(DLFolder.class, folder
128: .getPrimaryKeyObj())) {
129:
130: itr.remove();
131: } else {
132: folder.setUserUuid(folder.getUserUuid());
133:
134: List folderEntries = DLFileEntryUtil
135: .findByFolderId(folder.getFolderId());
136:
137: entries.addAll(folderEntries);
138:
139: if (context.getBooleanParameter(_NAMESPACE,
140: "shortcuts")) {
141: List folderShortcuts = DLFileShortcutUtil
142: .findByFolderId(folder.getFolderId());
143:
144: shortcuts.addAll(folderShortcuts);
145: }
146: }
147: }
148:
149: String xml = xStream.toXML(folders);
150:
151: Element el = root.addElement("documentlibrary-folders");
152:
153: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
154:
155: el.content().add(tempDoc.getRootElement().createCopy());
156:
157: // Entries
158:
159: List ranks = new ArrayList();
160:
161: itr = entries.iterator();
162:
163: while (itr.hasNext()) {
164: DLFileEntry entry = (DLFileEntry) itr.next();
165:
166: if (context.addPrimaryKey(DLFileEntry.class, entry
167: .getPrimaryKeyObj())) {
168:
169: itr.remove();
170: } else {
171: entry.setUserUuid(entry.getUserUuid());
172:
173: if (context.getBooleanParameter(_NAMESPACE,
174: "comments")) {
175: context.addComments(DLFileEntry.class, entry
176: .getPrimaryKeyObj());
177: }
178:
179: if (context.getBooleanParameter(_NAMESPACE,
180: "ratings")) {
181: context.addRatingsEntries(DLFileEntry.class,
182: entry.getPrimaryKeyObj());
183: }
184:
185: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
186: context.addTagsEntries(DLFileEntry.class, entry
187: .getPrimaryKeyObj());
188: }
189:
190: InputStream in = DLFileEntryLocalServiceUtil
191: .getFileAsStream(entry.getCompanyId(),
192: entry.getUserId(), entry
193: .getFolderId(), entry
194: .getName());
195:
196: context.getZipWriter().addEntry(
197: _ZIP_FOLDER + entry.getName(),
198: FileUtil.getBytes(in));
199:
200: if (context
201: .getBooleanParameter(_NAMESPACE, "ranks")) {
202: List entryRanks = DLFileRankUtil.findByF_N(
203: entry.getFolderId(), entry.getName());
204:
205: ranks.addAll(entryRanks);
206: }
207: }
208: }
209:
210: xml = xStream.toXML(entries);
211:
212: el = root.addElement("documentlibrary-entries");
213:
214: tempDoc = PortalUtil.readDocumentFromXML(xml);
215:
216: el.content().add(tempDoc.getRootElement().createCopy());
217:
218: // Shortcuts
219:
220: itr = shortcuts.iterator();
221:
222: while (itr.hasNext()) {
223: DLFileShortcut shortcut = (DLFileShortcut) itr.next();
224:
225: if (context.addPrimaryKey(DLFileShortcut.class,
226: shortcut.getPrimaryKeyObj())) {
227:
228: itr.remove();
229: } else {
230: shortcut.setUserUuid(shortcut.getUserUuid());
231: }
232: }
233:
234: xml = xStream.toXML(shortcuts);
235:
236: el = root.addElement("documentlibrary-shortcuts");
237:
238: tempDoc = PortalUtil.readDocumentFromXML(xml);
239:
240: el.content().add(tempDoc.getRootElement().createCopy());
241:
242: // Ranks
243:
244: itr = ranks.iterator();
245:
246: while (itr.hasNext()) {
247: DLFileRank rank = (DLFileRank) itr.next();
248:
249: if (context.addPrimaryKey(DLFileRank.class, rank
250: .getPrimaryKeyObj())) {
251:
252: itr.remove();
253: } else {
254: rank.setUserUuid(rank.getUserUuid());
255: }
256: }
257:
258: xml = xStream.toXML(ranks);
259:
260: el = root.addElement("documentlibrary-ranks");
261:
262: tempDoc = PortalUtil.readDocumentFromXML(xml);
263:
264: el.content().add(tempDoc.getRootElement().createCopy());
265:
266: return doc.asXML();
267: } catch (Exception e) {
268: throw new PortletDataException(e);
269: }
270: }
271:
272: public PortletDataHandlerControl[] getExportControls()
273: throws PortletDataException {
274:
275: return new PortletDataHandlerControl[] { _foldersAndDocuments,
276: _shortcuts, _ranks, _comments, _ratings, _tags };
277: }
278:
279: public PortletDataHandlerControl[] getImportControls()
280: throws PortletDataException {
281:
282: return new PortletDataHandlerControl[] { _foldersAndDocuments,
283: _shortcuts, _ranks, _comments, _ratings, _tags };
284: }
285:
286: public PortletPreferences importData(PortletDataContext context,
287: String portletId, PortletPreferences prefs, String data)
288: throws PortletDataException {
289:
290: try {
291: XStream xStream = new XStream();
292:
293: Document doc = PortalUtil.readDocumentFromXML(data);
294:
295: Element root = doc.getRootElement();
296:
297: // Folders
298:
299: Element el = root.element("documentlibrary-folders")
300: .element("list");
301:
302: Document tempDoc = DocumentHelper.createDocument();
303:
304: tempDoc.content().add(el.createCopy());
305:
306: Map folderPKs = CollectionFactory.getHashMap();
307:
308: List folders = (List) xStream.fromXML(tempDoc.asXML());
309:
310: Iterator itr = folders.iterator();
311:
312: while (itr.hasNext()) {
313: DLFolder folder = (DLFolder) itr.next();
314:
315: importFolder(context, folderPKs, folder);
316: }
317:
318: // Entries
319:
320: el = root.element("documentlibrary-entries")
321: .element("list");
322:
323: tempDoc = DocumentHelper.createDocument();
324:
325: tempDoc.content().add(el.createCopy());
326:
327: Map entryNames = CollectionFactory.getHashMap();
328:
329: List entries = (List) xStream.fromXML(tempDoc.asXML());
330:
331: itr = entries.iterator();
332:
333: while (itr.hasNext()) {
334: DLFileEntry entry = (DLFileEntry) itr.next();
335:
336: importEntry(context, folderPKs, entryNames, entry);
337: }
338:
339: // Shortcuts
340:
341: if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
342: el = root.element("documentlibrary-shortcuts").element(
343: "list");
344:
345: tempDoc = DocumentHelper.createDocument();
346:
347: tempDoc.content().add(el.createCopy());
348:
349: List shortcuts = (List) xStream
350: .fromXML(tempDoc.asXML());
351:
352: itr = shortcuts.iterator();
353:
354: while (itr.hasNext()) {
355: DLFileShortcut shortcut = (DLFileShortcut) itr
356: .next();
357:
358: importShortcut(context, folderPKs, entryNames,
359: shortcut);
360: }
361: }
362:
363: // Ranks
364:
365: if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
366: el = root.element("documentlibrary-ranks").element(
367: "list");
368:
369: tempDoc = DocumentHelper.createDocument();
370:
371: tempDoc.content().add(el.createCopy());
372:
373: List ranks = (List) xStream.fromXML(tempDoc.asXML());
374:
375: itr = ranks.iterator();
376:
377: while (itr.hasNext()) {
378: DLFileRank rank = (DLFileRank) itr.next();
379:
380: importRank(context, folderPKs, entryNames, rank);
381: }
382: }
383:
384: return null;
385: } catch (Exception e) {
386: throw new PortletDataException(e);
387: }
388: }
389:
390: protected void importEntry(PortletDataContext context,
391: Map folderPKs, Map entryNames, DLFileEntry entry)
392: throws Exception {
393:
394: long userId = context.getUserId(entry.getUserUuid());
395: long folderId = MapUtil.getLong(folderPKs, entry.getFolderId(),
396: entry.getFolderId());
397:
398: String[] tagsEntries = null;
399:
400: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
401: tagsEntries = context.getTagsEntries(DLFileEntry.class,
402: entry.getPrimaryKeyObj());
403: }
404:
405: boolean addCommunityPermissions = true;
406: boolean addGuestPermissions = true;
407:
408: byte[] byteArray = context.getZipReader().getEntryAsByteArray(
409: _ZIP_FOLDER + entry.getName());
410:
411: DLFileEntry existingEntry = null;
412:
413: try {
414: DLFolderUtil.findByPrimaryKey(folderId);
415:
416: if (context.getDataStrategy().equals(
417: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
418:
419: try {
420: existingEntry = DLFileEntryFinderUtil.findByUuid_G(
421: entry.getUuid(), context.getGroupId());
422:
423: existingEntry = DLFileEntryLocalServiceUtil
424: .updateFileEntry(userId, existingEntry
425: .getFolderId(), folderId,
426: existingEntry.getName(), entry
427: .getName(), entry
428: .getTitle(), entry
429: .getDescription(),
430: tagsEntries, entry
431: .getExtraSettings(),
432: byteArray);
433: } catch (NoSuchFileEntryException nsfee) {
434: existingEntry = DLFileEntryLocalServiceUtil
435: .addFileEntry(entry.getUuid(), userId,
436: folderId, entry.getName(), entry
437: .getTitle(), entry
438: .getDescription(),
439: tagsEntries, entry
440: .getExtraSettings(),
441: byteArray, addCommunityPermissions,
442: addGuestPermissions);
443: }
444: } else {
445: existingEntry = DLFileEntryLocalServiceUtil
446: .addFileEntry(userId, folderId,
447: entry.getName(), entry.getTitle(),
448: entry.getDescription(), tagsEntries,
449: entry.getExtraSettings(), byteArray,
450: addCommunityPermissions,
451: addGuestPermissions);
452: }
453:
454: entryNames.put(entry.getName(), existingEntry.getName());
455:
456: if (context.getBooleanParameter(_NAMESPACE, "comments")) {
457: context.importComments(DLFileEntry.class, entry
458: .getPrimaryKeyObj(), existingEntry
459: .getPrimaryKeyObj(), context.getGroupId());
460: }
461:
462: if (context.getBooleanParameter(_NAMESPACE, "ratings")) {
463: context.importRatingsEntries(DLFileEntry.class, entry
464: .getPrimaryKeyObj(), existingEntry
465: .getPrimaryKeyObj());
466: }
467: } catch (NoSuchFolderException nsfe) {
468: _log.error("Could not find the parent folder for entry "
469: + entry.getFileEntryId());
470: }
471: }
472:
473: protected void importFolder(PortletDataContext context,
474: Map folderPKs, DLFolder folder) throws Exception {
475:
476: long userId = context.getUserId(folder.getUserUuid());
477: long plid = context.getPlid();
478: long parentFolderId = MapUtil.getLong(folderPKs, folder
479: .getParentFolderId(), folder.getParentFolderId());
480:
481: boolean addCommunityPermissions = true;
482: boolean addGuestPermissions = true;
483:
484: DLFolder existingFolder = null;
485:
486: try {
487: if (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
488: DLFolderUtil.findByPrimaryKey(parentFolderId);
489: }
490:
491: if (context.getDataStrategy().equals(
492: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
493:
494: existingFolder = DLFolderUtil.fetchByUUID_G(folder
495: .getUuid(), context.getGroupId());
496:
497: if (existingFolder == null) {
498: existingFolder = DLFolderLocalServiceUtil
499: .addFolder(folder.getUuid(), userId, plid,
500: parentFolderId, folder.getName(),
501: folder.getDescription(),
502: addCommunityPermissions,
503: addGuestPermissions);
504: } else {
505: existingFolder = DLFolderLocalServiceUtil
506: .updateFolder(existingFolder.getFolderId(),
507: parentFolderId, folder.getName(),
508: folder.getDescription());
509: }
510: } else {
511: existingFolder = DLFolderLocalServiceUtil.addFolder(
512: userId, plid, parentFolderId, folder.getName(),
513: folder.getDescription(),
514: addCommunityPermissions, addGuestPermissions);
515: }
516:
517: folderPKs.put(folder.getPrimaryKeyObj(), existingFolder
518: .getPrimaryKeyObj());
519: } catch (NoSuchFolderException nsfe) {
520: _log.error("Could not find the parent folder for folder "
521: + folder.getFolderId());
522: }
523: }
524:
525: protected void importRank(PortletDataContext context,
526: Map folderPKs, Map entryNames, DLFileRank rank)
527: throws Exception {
528:
529: long userId = context.getUserId(rank.getUserUuid());
530: long folderId = MapUtil.getLong(folderPKs, rank.getFolderId(),
531: rank.getFolderId());
532:
533: String name = (String) folderPKs.get(rank.getName());
534:
535: if (name == null) {
536: name = rank.getName();
537: }
538:
539: try {
540: DLFolderUtil.findByPrimaryKey(folderId);
541:
542: DLFileRankLocalServiceUtil.updateFileRank(context
543: .getGroupId(), context.getCompanyId(), userId,
544: folderId, name);
545: } catch (NoSuchFolderException nsfe) {
546: _log.error("Could not find the folder for rank "
547: + rank.getFileRankId());
548: }
549: }
550:
551: protected void importShortcut(PortletDataContext context,
552: Map folderPKs, Map entryNames, DLFileShortcut shortcut)
553: throws Exception {
554:
555: long userId = context.getUserId(shortcut.getUserUuid());
556: long folderId = MapUtil.getLong(folderPKs, shortcut
557: .getFolderId(), shortcut.getFolderId());
558: long toFolderId = MapUtil.getLong(folderPKs, shortcut
559: .getToFolderId(), shortcut.getToFolderId());
560: String toName = MapUtil.getString(entryNames, shortcut
561: .getToName(), shortcut.getToName());
562:
563: boolean addCommunityPermissions = true;
564: boolean addGuestPermissions = true;
565:
566: try {
567: DLFolderUtil.findByPrimaryKey(folderId);
568: DLFolderUtil.findByPrimaryKey(toFolderId);
569:
570: if (context.getDataStrategy().equals(
571: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
572:
573: try {
574: DLFileShortcut existingShortcut = DLFileShortcutFinderUtil
575: .findByUuid_G(shortcut.getUuid(), context
576: .getGroupId());
577:
578: DLFileShortcutLocalServiceUtil.updateFileShortcut(
579: userId, existingShortcut
580: .getFileShortcutId(), folderId,
581: toFolderId, toName);
582: } catch (NoSuchFileShortcutException nsfse) {
583: DLFileShortcutLocalServiceUtil.addFileShortcut(
584: shortcut.getUuid(), userId, folderId,
585: toFolderId, toName,
586: addCommunityPermissions,
587: addGuestPermissions);
588: }
589: } else {
590: DLFileShortcutLocalServiceUtil.addFileShortcut(userId,
591: folderId, toFolderId, toName,
592: addCommunityPermissions, addGuestPermissions);
593: }
594: } catch (NoSuchFolderException nsfe) {
595: _log.error("Could not find the folder for shortcut "
596: + shortcut.getFileShortcutId());
597: }
598: }
599:
600: private static final String _NAMESPACE = "document_library";
601:
602: private static final String _ZIP_FOLDER = "document-library/";
603:
604: private static final PortletDataHandlerBoolean _foldersAndDocuments = new PortletDataHandlerBoolean(
605: _NAMESPACE, "folders-and-documents", true, true);
606:
607: private static final PortletDataHandlerBoolean _ranks = new PortletDataHandlerBoolean(
608: _NAMESPACE, "ranks");
609:
610: private static final PortletDataHandlerBoolean _shortcuts = new PortletDataHandlerBoolean(
611: _NAMESPACE, "shortcuts");
612:
613: private static final PortletDataHandlerBoolean _comments = new PortletDataHandlerBoolean(
614: _NAMESPACE, "comments");
615:
616: private static final PortletDataHandlerBoolean _ratings = new PortletDataHandlerBoolean(
617: _NAMESPACE, "ratings");
618:
619: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
620: _NAMESPACE, "tags");
621:
622: private static Log _log = LogFactory
623: .getLog(DLPortletDataHandlerImpl.class);
624:
625: }
|