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.wiki.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.ExportImportHelperUtil;
021    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
022    import com.liferay.portal.kernel.lar.PortletDataContext;
023    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.repository.model.FileEntry;
027    import com.liferay.portal.kernel.trash.TrashHandler;
028    import com.liferay.portal.kernel.util.MapUtil;
029    import com.liferay.portal.kernel.util.StreamUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portlet.documentlibrary.NoSuchFileException;
034    import com.liferay.portlet.documentlibrary.lar.FileEntryUtil;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
036    import com.liferay.portlet.wiki.NoSuchPageException;
037    import com.liferay.portlet.wiki.model.WikiNode;
038    import com.liferay.portlet.wiki.model.WikiPage;
039    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
040    
041    import java.io.InputStream;
042    
043    import java.util.List;
044    import java.util.Map;
045    
046    /**
047     * @author Zsolt Berentey
048     */
049    public class WikiPageStagedModelDataHandler
050            extends BaseStagedModelDataHandler<WikiPage> {
051    
052            public static final String[] CLASS_NAMES = {WikiPage.class.getName()};
053    
054            @Override
055            public void deleteStagedModel(
056                            String uuid, long groupId, String className, String extraData)
057                    throws PortalException, SystemException {
058    
059                    WikiPage wikiPage =
060                            WikiPageLocalServiceUtil.fetchWikiPageByUuidAndGroupId(
061                                    uuid, groupId);
062    
063                    if (wikiPage != null) {
064                            WikiPageLocalServiceUtil.deletePage(wikiPage);
065                    }
066            }
067    
068            @Override
069            public String[] getClassNames() {
070                    return CLASS_NAMES;
071            }
072    
073            @Override
074            protected void doExportStagedModel(
075                            PortletDataContext portletDataContext, WikiPage page)
076                    throws Exception {
077    
078                    Element pageElement = portletDataContext.getExportDataElement(page);
079    
080                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
081                            portletDataContext, page, page.getNode(),
082                            PortletDataContext.REFERENCE_TYPE_PARENT);
083    
084                    String content = ExportImportHelperUtil.replaceExportContentReferences(
085                            portletDataContext, page, pageElement, page.getContent(),
086                            portletDataContext.getBooleanParameter(
087                                    WikiPortletDataHandler.NAMESPACE, "referenced-content"));
088    
089                    page.setContent(content);
090    
091                    if (page.isHead()) {
092                            for (FileEntry fileEntry : page.getAttachmentsFileEntries()) {
093                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
094                                            portletDataContext, page, fileEntry,
095                                            PortletDataContext.REFERENCE_TYPE_WEAK);
096                            }
097                    }
098    
099                    portletDataContext.addClassedModel(
100                            pageElement, ExportImportPathUtil.getModelPath(page), page);
101            }
102    
103            @Override
104            protected void doImportCompanyStagedModel(
105                            PortletDataContext portletDataContext, String uuid, long pageId)
106                    throws Exception {
107    
108                    WikiPage existingPage =
109                            WikiPageLocalServiceUtil.fetchWikiPageByUuidAndGroupId(
110                                    uuid, portletDataContext.getCompanyGroupId());
111    
112                    Map<Long, Long> pageIds =
113                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
114                                    WikiPage.class);
115    
116                    pageIds.put(pageId, existingPage.getPageId());
117            }
118    
119            @Override
120            protected void doImportStagedModel(
121                            PortletDataContext portletDataContext, WikiPage page)
122                    throws Exception {
123    
124                    long userId = portletDataContext.getUserId(page.getUserUuid());
125    
126                    StagedModelDataHandlerUtil.importReferenceStagedModel(
127                            portletDataContext, page, WikiNode.class, page.getNodeId());
128    
129                    Element pageElement =
130                            portletDataContext.getImportDataStagedModelElement(page);
131    
132                    String content = ExportImportHelperUtil.replaceImportContentReferences(
133                            portletDataContext, pageElement, page.getContent(),
134                            portletDataContext.getBooleanParameter(
135                                    WikiPortletDataHandler.NAMESPACE, "referenced-content"));
136    
137                    page.setContent(content);
138    
139                    ServiceContext serviceContext = portletDataContext.createServiceContext(
140                            page);
141    
142                    serviceContext.setUuid(page.getUuid());
143    
144                    Map<Long, Long> nodeIds =
145                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
146                                    WikiNode.class);
147    
148                    long nodeId = MapUtil.getLong(
149                            nodeIds, page.getNodeId(), page.getNodeId());
150    
151                    WikiPage importedPage = null;
152    
153                    WikiPage existingPage = null;
154    
155                    try {
156                            existingPage = WikiPageLocalServiceUtil.getPage(
157                                    nodeId, page.getTitle());
158                    }
159                    catch (NoSuchPageException nspe) {
160                    }
161    
162                    if (existingPage == null) {
163                            importedPage = WikiPageLocalServiceUtil.addPage(
164                                    userId, nodeId, page.getTitle(), page.getVersion(),
165                                    page.getContent(), page.getSummary(), page.isMinorEdit(),
166                                    page.getFormat(), page.getHead(), page.getParentTitle(),
167                                    page.getRedirectTitle(), serviceContext);
168                    }
169                    else {
170                            existingPage =
171                                    WikiPageLocalServiceUtil.fetchWikiPageByUuidAndGroupId(
172                                            page.getUuid(), portletDataContext.getScopeGroupId());
173    
174                            if (existingPage == null) {
175                                    existingPage = WikiPageLocalServiceUtil.fetchPage(
176                                            nodeId, page.getTitle(), page.getVersion());
177                            }
178    
179                            if (existingPage == null) {
180                                    importedPage = WikiPageLocalServiceUtil.updatePage(
181                                            userId, nodeId, page.getTitle(), 0.0, page.getContent(),
182                                            page.getSummary(), page.isMinorEdit(), page.getFormat(),
183                                            page.getParentTitle(), page.getRedirectTitle(),
184                                            serviceContext);
185                            }
186                            else {
187                                    importedPage = existingPage;
188                            }
189                    }
190    
191                    if (page.isHead()) {
192                            List<Element> attachmentElements =
193                                    portletDataContext.getReferenceDataElements(
194                                            pageElement, DLFileEntry.class,
195                                            PortletDataContext.REFERENCE_TYPE_WEAK);
196    
197                            for (Element attachmentElement : attachmentElements) {
198                                    String path = attachmentElement.attributeValue("path");
199    
200                                    FileEntry fileEntry =
201                                            (FileEntry)portletDataContext.getZipEntryAsObject(path);
202    
203                                    InputStream inputStream = null;
204    
205                                    try {
206                                            String binPath = attachmentElement.attributeValue(
207                                                    "bin-path");
208    
209                                            if (Validator.isNull(binPath) &&
210                                                    portletDataContext.isPerformDirectBinaryImport()) {
211    
212                                                    try {
213                                                            inputStream = FileEntryUtil.getContentStream(
214                                                                    fileEntry);
215                                                    }
216                                                    catch (NoSuchFileException nsfe) {
217                                                    }
218                                            }
219                                            else {
220                                                    inputStream =
221                                                            portletDataContext.getZipEntryAsInputStream(
222                                                                    binPath);
223                                            }
224    
225                                            if (inputStream == null) {
226                                                    if (_log.isWarnEnabled()) {
227                                                            _log.warn(
228                                                                    "Unable to import attachment for file entry " +
229                                                                            fileEntry.getFileEntryId());
230                                                    }
231    
232                                                    continue;
233                                            }
234    
235                                            WikiPageLocalServiceUtil.addPageAttachment(
236                                                    userId, importedPage.getNodeId(),
237                                                    importedPage.getTitle(), fileEntry.getTitle(),
238                                                    inputStream, null);
239                                    }
240                                    finally {
241                                            StreamUtil.cleanUp(inputStream);
242                                    }
243                            }
244                    }
245    
246                    portletDataContext.importClassedModel(page, importedPage);
247    
248                    Map<Long, Long> pageIds =
249                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
250                                    WikiPage.class + ".pageId");
251    
252                    pageIds.put(page.getPageId(), importedPage.getPageId());
253            }
254    
255            @Override
256            protected void doRestoreStagedModel(
257                            PortletDataContext portletDataContext, WikiPage page)
258                    throws Exception {
259    
260                    long userId = portletDataContext.getUserId(page.getUserUuid());
261    
262                    WikiPage existingPage =
263                            WikiPageLocalServiceUtil.fetchWikiPageByUuidAndGroupId(
264                                    page.getUuid(), portletDataContext.getScopeGroupId());
265    
266                    if ((existingPage == null) || !existingPage.isInTrash()) {
267                            return;
268                    }
269    
270                    TrashHandler trashHandler = existingPage.getTrashHandler();
271    
272                    if (trashHandler.isRestorable(existingPage.getResourcePrimKey())) {
273                            trashHandler.restoreTrashEntry(
274                                    userId, existingPage.getResourcePrimKey());
275                    }
276            }
277    
278            private static Log _log = LogFactoryUtil.getLog(
279                    WikiPageStagedModelDataHandler.class);
280    
281    }