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.exportReferenceStagedModel(
075                                    portletDataContext, entry, entry.getFolder(),
076                                    PortletDataContext.REFERENCE_TYPE_PARENT);
077                    }
078    
079                    Element entryElement = portletDataContext.getExportDataElement(entry);
080    
081                    portletDataContext.addClassedModel(
082                            entryElement, ExportImportPathUtil.getModelPath(entry), entry,
083                            BookmarksPortletDataHandler.NAMESPACE);
084            }
085    
086            @Override
087            protected void doImportStagedModel(
088                            PortletDataContext portletDataContext, BookmarksEntry entry)
089                    throws Exception {
090    
091                    long userId = portletDataContext.getUserId(entry.getUserUuid());
092    
093                    if (entry.getFolderId() !=
094                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
095    
096                            String parentFolderPath = ExportImportPathUtil.getModelPath(
097                                    portletDataContext, BookmarksFolder.class.getName(),
098                                    entry.getFolderId());
099    
100                            BookmarksFolder parentFolder =
101                                    (BookmarksFolder)portletDataContext.getZipEntryAsObject(
102                                            parentFolderPath);
103    
104                            StagedModelDataHandlerUtil.importStagedModel(
105                                    portletDataContext, parentFolder);
106                    }
107    
108                    Map<Long, Long> folderIds =
109                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
110                                    BookmarksFolder.class);
111    
112                    long folderId = MapUtil.getLong(
113                            folderIds, entry.getFolderId(), entry.getFolderId());
114    
115                    ServiceContext serviceContext = portletDataContext.createServiceContext(
116                            entry, BookmarksPortletDataHandler.NAMESPACE);
117    
118                    BookmarksEntry importedEntry = null;
119    
120                    if (portletDataContext.isDataStrategyMirror()) {
121                            BookmarksEntry existingEntry =
122                                    BookmarksEntryLocalServiceUtil.
123                                            fetchBookmarksEntryByUuidAndGroupId(
124                                                    entry.getUuid(), portletDataContext.getScopeGroupId());
125    
126                            if (existingEntry == null) {
127                                    serviceContext.setUuid(entry.getUuid());
128    
129                                    importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
130                                            userId, portletDataContext.getScopeGroupId(), folderId,
131                                            entry.getName(), entry.getUrl(), entry.getDescription(),
132                                            serviceContext);
133                            }
134                            else {
135                                    importedEntry = BookmarksEntryLocalServiceUtil.updateEntry(
136                                            userId, existingEntry.getEntryId(),
137                                            portletDataContext.getScopeGroupId(), folderId,
138                                            entry.getName(), entry.getUrl(), entry.getDescription(),
139                                            serviceContext);
140                            }
141                    }
142                    else {
143                            importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
144                                    userId, portletDataContext.getScopeGroupId(), folderId,
145                                    entry.getName(), entry.getUrl(), entry.getDescription(),
146                                    serviceContext);
147                    }
148    
149                    portletDataContext.importClassedModel(
150                            entry, importedEntry, BookmarksPortletDataHandler.NAMESPACE);
151            }
152    
153    }