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