001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.GetterUtil;
025    import com.liferay.portal.kernel.util.MapUtil;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portlet.journal.model.JournalFolder;
031    import com.liferay.portlet.journal.model.JournalFolderConstants;
032    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
033    import com.liferay.portlet.journal.service.persistence.JournalFolderUtil;
034    
035    import java.util.Map;
036    
037    /**
038     * @author Daniel Kocsis
039     */
040    public class JournalFolderStagedModelDataHandler
041            extends BaseStagedModelDataHandler<JournalFolder> {
042    
043            public static final String[] CLASS_NAMES = {JournalFolder.class.getName()};
044    
045            @Override
046            public void deleteStagedModel(
047                            String uuid, long groupId, String className, String extraData)
048                    throws PortalException, SystemException {
049    
050                    JournalFolder folder =
051                            JournalFolderLocalServiceUtil.fetchJournalFolderByUuidAndGroupId(
052                                    uuid, groupId);
053    
054                    if (folder != null) {
055                            JournalFolderLocalServiceUtil.deleteFolder(folder);
056                    }
057            }
058    
059            @Override
060            public String[] getClassNames() {
061                    return CLASS_NAMES;
062            }
063    
064            @Override
065            public String getDisplayName(JournalFolder folder) {
066                    return folder.getName();
067            }
068    
069            @Override
070            protected void doExportStagedModel(
071                            PortletDataContext portletDataContext, JournalFolder folder)
072                    throws Exception {
073    
074                    if (folder.getParentFolderId() !=
075                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
076    
077                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
078                                    portletDataContext, folder, folder.getParentFolder(),
079                                    PortletDataContext.REFERENCE_TYPE_PARENT);
080                    }
081    
082                    Element folderElement = portletDataContext.getExportDataElement(folder);
083    
084                    portletDataContext.addClassedModel(
085                            folderElement, ExportImportPathUtil.getModelPath(folder), folder);
086            }
087    
088            @Override
089            protected void doImportGroupStagedModel(
090                            PortletDataContext portletDataContext, Element referenceElement,
091                            long groupId)
092                    throws Exception {
093    
094                    String folderUuid = referenceElement.attributeValue("uuid");
095    
096                    Map<Long, Long> folderIds =
097                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
098                                    JournalFolder.class);
099    
100                    JournalFolder existingFolder =
101                            JournalFolderLocalServiceUtil.fetchJournalFolderByUuidAndGroupId(
102                                    folderUuid, groupId);
103    
104                    if (existingFolder == null) {
105                            return;
106                    }
107    
108                    long folderId = GetterUtil.getLong(
109                            referenceElement.attributeValue("class-pk"));
110    
111                    folderIds.put(folderId, existingFolder.getFolderId());
112            }
113    
114            @Override
115            protected void doImportStagedModel(
116                            PortletDataContext portletDataContext, JournalFolder folder)
117                    throws Exception {
118    
119                    long userId = portletDataContext.getUserId(folder.getUserUuid());
120    
121                    if (folder.getParentFolderId() !=
122                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
123    
124                            StagedModelDataHandlerUtil.importReferenceStagedModel(
125                                    portletDataContext, folder, JournalFolder.class,
126                                    folder.getParentFolderId());
127                    }
128    
129                    Map<Long, Long> folderIds =
130                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
131                                    JournalFolder.class);
132    
133                    long parentFolderId = MapUtil.getLong(
134                            folderIds, folder.getParentFolderId(), folder.getParentFolderId());
135    
136                    ServiceContext serviceContext = portletDataContext.createServiceContext(
137                            folder);
138    
139                    JournalFolder importedFolder = null;
140    
141                    long groupId = portletDataContext.getScopeGroupId();
142    
143                    if (portletDataContext.isDataStrategyMirror()) {
144                            JournalFolder existingFolder =
145                                    JournalFolderLocalServiceUtil.
146                                            fetchJournalFolderByUuidAndGroupId(
147                                                    folder.getUuid(), groupId);
148    
149                            if (existingFolder == null) {
150                                    String name = getFolderName(
151                                            null, groupId, parentFolderId, folder.getName(), 2);
152    
153                                    serviceContext.setUuid(folder.getUuid());
154    
155                                    importedFolder = JournalFolderLocalServiceUtil.addFolder(
156                                            userId, groupId, parentFolderId, name,
157                                            folder.getDescription(), serviceContext);
158                            }
159                            else {
160                                    String name = getFolderName(
161                                            folder.getUuid(), groupId, parentFolderId, folder.getName(),
162                                            2);
163    
164                                    importedFolder = JournalFolderLocalServiceUtil.updateFolder(
165                                            userId, existingFolder.getFolderId(), parentFolderId, name,
166                                            folder.getDescription(), false, serviceContext);
167                            }
168                    }
169                    else {
170                            String name = getFolderName(
171                                    null, groupId, parentFolderId, folder.getName(), 2);
172    
173                            importedFolder = JournalFolderLocalServiceUtil.addFolder(
174                                    userId, groupId, parentFolderId, name, folder.getDescription(),
175                                    serviceContext);
176                    }
177    
178                    portletDataContext.importClassedModel(folder, importedFolder);
179            }
180    
181            @Override
182            protected void doRestoreStagedModel(
183                            PortletDataContext portletDataContext, JournalFolder folder)
184                    throws Exception {
185    
186                    long userId = portletDataContext.getUserId(folder.getUserUuid());
187    
188                    JournalFolder existingFolder =
189                            JournalFolderLocalServiceUtil.fetchJournalFolderByUuidAndGroupId(
190                                    folder.getUuid(), portletDataContext.getScopeGroupId());
191    
192                    if ((existingFolder == null) || !existingFolder.isInTrash()) {
193                            return;
194                    }
195    
196                    TrashHandler trashHandler = existingFolder.getTrashHandler();
197    
198                    if (trashHandler.isRestorable(existingFolder.getFolderId())) {
199                            trashHandler.restoreTrashEntry(
200                                    userId, existingFolder.getFolderId());
201                    }
202            }
203    
204            protected String getFolderName(
205                            String uuid, long groupId, long parentFolderId, String name,
206                            int count)
207                    throws Exception {
208    
209                    JournalFolder folder = JournalFolderUtil.fetchByG_P_N(
210                            groupId, parentFolderId, name);
211    
212                    if (folder == null) {
213                            return name;
214                    }
215    
216                    if (Validator.isNotNull(uuid) && uuid.equals(folder.getUuid())) {
217                            return name;
218                    }
219    
220                    name = StringUtil.appendParentheticalSuffix(name, count);
221    
222                    return getFolderName(uuid, groupId, parentFolderId, name, ++count);
223            }
224    
225    }