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