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