001    /**
002     * Copyright (c) 2000-2013 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.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
025    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
026    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
027    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
028    import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryUtil;
029    
030    import java.util.Map;
031    
032    /**
033     * @author Mate Thurzo
034     * @author Daniel Kocsis
035     */
036    public class BookmarksEntryStagedModelDataHandler
037            extends BaseStagedModelDataHandler<BookmarksEntry> {
038    
039            public static final String[] CLASS_NAMES = {BookmarksEntry.class.getName()};
040    
041            @Override
042            public String[] getClassNames() {
043                    return CLASS_NAMES;
044            }
045    
046            @Override
047            protected void doExportStagedModel(
048                            PortletDataContext portletDataContext, BookmarksEntry entry)
049                    throws Exception {
050    
051                    if (entry.getFolderId() !=
052                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
053    
054                            StagedModelDataHandlerUtil.exportStagedModel(
055                                    portletDataContext, entry.getFolder());
056                    }
057    
058                    Element entryElement =
059                            portletDataContext.getExportDataStagedModelElement(entry);
060    
061                    portletDataContext.addClassedModel(
062                            entryElement, ExportImportPathUtil.getModelPath(entry), entry,
063                            BookmarksPortletDataHandler.NAMESPACE);
064            }
065    
066            @Override
067            protected void doImportStagedModel(
068                            PortletDataContext portletDataContext, BookmarksEntry entry)
069                    throws Exception {
070    
071                    long userId = portletDataContext.getUserId(entry.getUserUuid());
072    
073                    Map<Long, Long> folderIds =
074                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
075                                    BookmarksFolder.class);
076    
077                    long folderId = MapUtil.getLong(
078                            folderIds, entry.getFolderId(), entry.getFolderId());
079    
080                    if ((folderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) &&
081                            (folderId == entry.getFolderId())) {
082    
083                            String parentFolderPath = ExportImportPathUtil.getModelPath(
084                                    portletDataContext, BookmarksFolder.class.getName(), folderId);
085    
086                            BookmarksFolder parentFolder =
087                                    (BookmarksFolder)portletDataContext.getZipEntryAsObject(
088                                            parentFolderPath);
089    
090                            StagedModelDataHandlerUtil.importStagedModel(
091                                    portletDataContext, parentFolder);
092    
093                            folderId = MapUtil.getLong(
094                                    folderIds, entry.getFolderId(), entry.getFolderId());
095                    }
096    
097                    ServiceContext serviceContext = portletDataContext.createServiceContext(
098                            entry, BookmarksPortletDataHandler.NAMESPACE);
099    
100                    BookmarksEntry importedEntry = null;
101    
102                    if (portletDataContext.isDataStrategyMirror()) {
103                            BookmarksEntry existingEntry = BookmarksEntryUtil.fetchByUUID_G(
104                                    entry.getUuid(), portletDataContext.getScopeGroupId());
105    
106                            if (existingEntry == null) {
107                                    serviceContext.setUuid(entry.getUuid());
108    
109                                    importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
110                                            userId, portletDataContext.getScopeGroupId(), folderId,
111                                            entry.getName(), entry.getUrl(), entry.getDescription(),
112                                            serviceContext);
113                            }
114                            else {
115                                    importedEntry = BookmarksEntryLocalServiceUtil.updateEntry(
116                                            userId, existingEntry.getEntryId(),
117                                            portletDataContext.getScopeGroupId(), folderId,
118                                            entry.getName(), entry.getUrl(), entry.getDescription(),
119                                            serviceContext);
120                            }
121                    }
122                    else {
123                            importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
124                                    userId, portletDataContext.getScopeGroupId(), folderId,
125                                    entry.getName(), entry.getUrl(), entry.getDescription(),
126                                    serviceContext);
127                    }
128    
129                    portletDataContext.importClassedModel(
130                            entry, importedEntry, BookmarksPortletDataHandler.NAMESPACE);
131            }
132    
133    }