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