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.exportReferenceStagedModel(
073                                    portletDataContext, folder, folder.getParentFolder(),
074                                    PortletDataContext.REFERENCE_TYPE_PARENT);
075                    }
076    
077                    Element folderElement = portletDataContext.getExportDataElement(folder);
078    
079                    portletDataContext.addClassedModel(
080                            folderElement, ExportImportPathUtil.getModelPath(folder), folder,
081                            JournalPortletDataHandler.NAMESPACE);
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                            String path = ExportImportPathUtil.getModelPath(
095                                    portletDataContext, JournalFolder.class.getName(),
096                                    folder.getParentFolderId());
097    
098                            JournalFolder parentFolder =
099                                    (JournalFolder)portletDataContext.getZipEntryAsObject(path);
100    
101                            StagedModelDataHandlerUtil.importReferenceStagedModel(
102                                    portletDataContext, parentFolder);
103                    }
104    
105                    Map<Long, Long> folderIds =
106                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
107                                    JournalFolder.class);
108    
109                    long parentFolderId = MapUtil.getLong(
110                            folderIds, folder.getParentFolderId(), folder.getParentFolderId());
111    
112                    ServiceContext serviceContext = portletDataContext.createServiceContext(
113                            folder, JournalPortletDataHandler.NAMESPACE);
114    
115                    JournalFolder importedFolder = null;
116    
117                    long groupId = portletDataContext.getScopeGroupId();
118    
119                    if (portletDataContext.isDataStrategyMirror()) {
120                            JournalFolder existingFolder =
121                                    JournalFolderLocalServiceUtil.
122                                            fetchJournalFolderByUuidAndGroupId(
123                                                    folder.getUuid(), groupId);
124    
125                            if (existingFolder == null) {
126                                    serviceContext.setUuid(folder.getUuid());
127    
128                                    importedFolder = JournalFolderLocalServiceUtil.addFolder(
129                                            userId, groupId, parentFolderId, folder.getName(),
130                                            folder.getDescription(), serviceContext);
131                            }
132                            else {
133                                    importedFolder = JournalFolderLocalServiceUtil.updateFolder(
134                                            userId, existingFolder.getFolderId(), parentFolderId,
135                                            folder.getName(), folder.getDescription(), false,
136                                            serviceContext);
137                            }
138                    }
139                    else {
140                            importedFolder = JournalFolderLocalServiceUtil.addFolder(
141                                    userId, groupId, parentFolderId, folder.getName(),
142                                    folder.getDescription(), serviceContext);
143                    }
144    
145                    portletDataContext.importClassedModel(
146                            folder, importedFolder, JournalPortletDataHandler.NAMESPACE);
147            }
148    
149    }