001    /**
002     * Copyright (c) 2000-present 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
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.lar.StagedModelModifiedDateComparator;
024    import com.liferay.portal.kernel.trash.TrashHandler;
025    import com.liferay.portal.kernel.util.ListUtil;
026    import com.liferay.portal.kernel.util.MapUtil;
027    import com.liferay.portal.kernel.xml.Element;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
030    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
031    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
032    
033    import java.util.List;
034    import java.util.Map;
035    
036    /**
037     * @author Mate Thurzo
038     * @author Daniel Kocsis
039     */
040    public class BookmarksFolderStagedModelDataHandler
041            extends BaseStagedModelDataHandler<BookmarksFolder> {
042    
043            public static final String[] CLASS_NAMES =
044                    {BookmarksFolder.class.getName()};
045    
046            @Override
047            public void deleteStagedModel(
048                            String uuid, long groupId, String className, String extraData)
049                    throws PortalException {
050    
051                    BookmarksFolder folder = fetchStagedModelByUuidAndGroupId(
052                            uuid, groupId);
053    
054                    if (folder != null) {
055                            BookmarksFolderLocalServiceUtil.deleteFolder(folder);
056                    }
057            }
058    
059            @Override
060            public BookmarksFolder fetchStagedModelByUuidAndCompanyId(
061                    String uuid, long companyId) {
062    
063                    List<BookmarksFolder> folders =
064                            BookmarksFolderLocalServiceUtil.
065                                    getBookmarksFoldersByUuidAndCompanyId(
066                                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
067                                            new StagedModelModifiedDateComparator<BookmarksFolder>());
068    
069                    if (ListUtil.isEmpty(folders)) {
070                            return null;
071                    }
072    
073                    return folders.get(0);
074            }
075    
076            @Override
077            public BookmarksFolder fetchStagedModelByUuidAndGroupId(
078                    String uuid, long groupId) {
079    
080                    return BookmarksFolderLocalServiceUtil.
081                            fetchBookmarksFolderByUuidAndGroupId(uuid, groupId);
082            }
083    
084            @Override
085            public String[] getClassNames() {
086                    return CLASS_NAMES;
087            }
088    
089            @Override
090            public String getDisplayName(BookmarksFolder folder) {
091                    return folder.getName();
092            }
093    
094            @Override
095            protected void doExportStagedModel(
096                            PortletDataContext portletDataContext, BookmarksFolder folder)
097                    throws Exception {
098    
099                    if (folder.getParentFolderId() !=
100                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
101    
102                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
103                                    portletDataContext, folder, folder.getParentFolder(),
104                                    PortletDataContext.REFERENCE_TYPE_PARENT);
105                    }
106    
107                    Element folderElement = portletDataContext.getExportDataElement(folder);
108    
109                    portletDataContext.addClassedModel(
110                            folderElement, ExportImportPathUtil.getModelPath(folder), folder);
111            }
112    
113            @Override
114            protected void doImportStagedModel(
115                            PortletDataContext portletDataContext, BookmarksFolder folder)
116                    throws Exception {
117    
118                    long userId = portletDataContext.getUserId(folder.getUserUuid());
119    
120                    Map<Long, Long> folderIds =
121                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
122                                    BookmarksFolder.class);
123    
124                    long parentFolderId = MapUtil.getLong(
125                            folderIds, folder.getParentFolderId(), folder.getParentFolderId());
126    
127                    ServiceContext serviceContext = portletDataContext.createServiceContext(
128                            folder);
129    
130                    BookmarksFolder importedFolder = null;
131    
132                    if (portletDataContext.isDataStrategyMirror()) {
133                            BookmarksFolder existingFolder = fetchStagedModelByUuidAndGroupId(
134                                    folder.getUuid(), portletDataContext.getScopeGroupId());
135    
136                            if (existingFolder == null) {
137                                    serviceContext.setUuid(folder.getUuid());
138    
139                                    importedFolder = BookmarksFolderLocalServiceUtil.addFolder(
140                                            userId, parentFolderId, folder.getName(),
141                                            folder.getDescription(), serviceContext);
142                            }
143                            else {
144                                    importedFolder = BookmarksFolderLocalServiceUtil.updateFolder(
145                                            userId, existingFolder.getFolderId(), parentFolderId,
146                                            folder.getName(), folder.getDescription(), false,
147                                            serviceContext);
148                            }
149                    }
150                    else {
151                            importedFolder = BookmarksFolderLocalServiceUtil.addFolder(
152                                    userId, parentFolderId, folder.getName(),
153                                    folder.getDescription(), serviceContext);
154                    }
155    
156                    portletDataContext.importClassedModel(folder, importedFolder);
157            }
158    
159            @Override
160            protected void doRestoreStagedModel(
161                            PortletDataContext portletDataContext, BookmarksFolder folder)
162                    throws Exception {
163    
164                    long userId = portletDataContext.getUserId(folder.getUserUuid());
165    
166                    BookmarksFolder existingFolder = fetchStagedModelByUuidAndGroupId(
167                            folder.getUuid(), portletDataContext.getScopeGroupId());
168    
169                    if ((existingFolder == null) || !existingFolder.isInTrash()) {
170                            return;
171                    }
172    
173                    TrashHandler trashHandler = existingFolder.getTrashHandler();
174    
175                    if (trashHandler.isRestorable(existingFolder.getFolderId())) {
176                            trashHandler.restoreTrashEntry(
177                                    userId, existingFolder.getFolderId());
178                    }
179            }
180    
181    }