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