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                            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.importReferenceStagedModel(
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);
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(entry, importedEntry);
150            }
151    
152            @Override
153            protected void doRestoreStagedModel(
154                            PortletDataContext portletDataContext, BookmarksEntry entry)
155                    throws Exception {
156    
157                    long userId = portletDataContext.getUserId(entry.getUserUuid());
158    
159                    BookmarksEntry existingEntry =
160                            BookmarksEntryLocalServiceUtil.fetchBookmarksEntryByUuidAndGroupId(
161                                    entry.getUuid(), portletDataContext.getScopeGroupId());
162    
163                    if ((existingEntry == null) || !existingEntry.isInTrash()) {
164                            return;
165                    }
166    
167                    TrashHandler trashHandler = existingEntry.getTrashHandler();
168    
169                    if (trashHandler.isRestorable(existingEntry.getEntryId())) {
170                            trashHandler.restoreTrashEntry(userId, existingEntry.getEntryId());
171                    }
172            }
173    
174    }