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.wiki.lar;
016    
017    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataException;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.MapUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.xml.Document;
029    import com.liferay.portal.kernel.xml.Element;
030    import com.liferay.portal.kernel.xml.SAXReaderUtil;
031    import com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl;
032    import com.liferay.portlet.wiki.NoSuchNodeException;
033    import com.liferay.portlet.wiki.model.WikiNode;
034    import com.liferay.portlet.wiki.model.WikiPage;
035    import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
036    import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
037    import com.liferay.portlet.wiki.util.WikiCacheUtil;
038    
039    import java.util.Map;
040    
041    import javax.portlet.PortletPreferences;
042    
043    /**
044     * @author Marcellus Tavares
045     */
046    public class WikiDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
047    
048            @Override
049            public String[] getDataPortletPreferences() {
050                    return new String[] {"title", "nodeId"};
051            }
052    
053            @Override
054            public PortletDataHandlerControl[] getExportControls() {
055                    return new PortletDataHandlerControl[] {
056                            _nodesAndPages
057                    };
058            }
059    
060            @Override
061            public PortletDataHandlerControl[] getExportMetadataControls() {
062                    return new PortletDataHandlerControl[] {
063                            new PortletDataHandlerBoolean(
064                                    _NAMESPACE, "wiki-pages", true,
065                                    WikiPortletDataHandlerImpl.getMetadataControls()
066                            )
067                    };
068            }
069    
070            @Override
071            public PortletDataHandlerControl[] getImportControls() {
072                    return new PortletDataHandlerControl[] {
073                            _nodesAndPages
074                    };
075            }
076    
077            @Override
078            public PortletDataHandlerControl[] getImportMetadataControls() {
079                    return new PortletDataHandlerControl[] {
080                            new PortletDataHandlerBoolean(
081                                    _NAMESPACE, "wiki-pages", true,
082                                    WikiPortletDataHandlerImpl.getMetadataControls()
083                            )
084                    };
085            }
086    
087            @Override
088            public PortletPreferences importData(
089                            PortletDataContext portletDataContext, String portletId,
090                            PortletPreferences portletPreferences, String data)
091                    throws PortletDataException {
092    
093                    WikiCacheThreadLocal.setClearCache(false);
094    
095                    try {
096                            return super.importData(
097                                    portletDataContext, portletId, portletPreferences, data);
098                    }
099                    finally {
100                            WikiCacheThreadLocal.setClearCache(true);
101                    }
102            }
103    
104            @Override
105            protected PortletPreferences doDeleteData(
106                            PortletDataContext portletDataContext, String portletId,
107                            PortletPreferences portletPreferences)
108                    throws Exception {
109    
110                    if (portletPreferences == null) {
111                            return portletPreferences;
112                    }
113    
114                    portletPreferences.setValue("title", StringPool.BLANK);
115                    portletPreferences.setValue("nodeId", StringPool.BLANK);
116    
117                    return portletPreferences;
118            }
119    
120            @Override
121            protected String doExportData(
122                            PortletDataContext portletDataContext, String portletId,
123                            PortletPreferences portletPreferences)
124                    throws Exception {
125    
126                    long nodeId = GetterUtil.getLong(
127                            portletPreferences.getValue("nodeId", StringPool.BLANK));
128    
129                    if (nodeId <= 0) {
130                            if (_log.isWarnEnabled()) {
131                                    _log.warn(
132                                            "No node id found in preferences of portlet " + portletId);
133                            }
134    
135                            return StringPool.BLANK;
136                    }
137    
138                    String title = portletPreferences.getValue("title", null);
139    
140                    if (title == null) {
141                            if (_log.isWarnEnabled()) {
142                                    _log.warn(
143                                            "No title found in preferences of portlet " + portletId);
144                            }
145    
146                            return StringPool.BLANK;
147                    }
148    
149                    WikiNode node = null;
150    
151                    try {
152                            node = WikiNodeUtil.findByPrimaryKey(nodeId);
153                    }
154                    catch (NoSuchNodeException nsne) {
155                            if (_log.isWarnEnabled()) {
156                                    _log.warn(nsne, nsne);
157                            }
158    
159                            return StringPool.BLANK;
160                    }
161    
162                    portletDataContext.addPermissions(
163                            "com.liferay.portlet.wiki", portletDataContext.getScopeGroupId());
164    
165                    Document document = SAXReaderUtil.createDocument();
166    
167                    Element rootElement = document.addElement("wiki-display-data");
168    
169                    rootElement.addAttribute(
170                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
171    
172                    Element nodesElement = rootElement.addElement("nodes");
173                    Element pagesElement = rootElement.addElement("pages");
174    
175                    Element dlFileEntryTypesElement = pagesElement.addElement(
176                            "dl-file-entry-types");
177                    Element dlFoldersElement = pagesElement.addElement("dl-folders");
178                    Element dlFileEntriesElement = pagesElement.addElement(
179                            "dl-file-entries");
180                    Element dlFileRanksElement = pagesElement.addElement("dl-file-ranks");
181                    Element dlRepositoriesElement = pagesElement.addElement(
182                            "dl-repositories");
183                    Element dlRepositoryEntriesElement = pagesElement.addElement(
184                            "dl-repository-entries");
185    
186                    WikiPortletDataHandlerImpl.exportNode(
187                            portletDataContext, nodesElement, pagesElement,
188                            dlFileEntryTypesElement, dlFoldersElement, dlFileEntriesElement,
189                            dlFileRanksElement, dlRepositoriesElement,
190                            dlRepositoryEntriesElement, node);
191    
192                    return document.formattedString();
193            }
194    
195            @Override
196            protected PortletPreferences doImportData(
197                            PortletDataContext portletDataContext, String portletId,
198                            PortletPreferences portletPreferences, String data)
199                    throws Exception {
200    
201                    portletDataContext.importPermissions(
202                            "com.liferay.portlet.wiki", portletDataContext.getSourceGroupId(),
203                            portletDataContext.getScopeGroupId());
204    
205                    if (Validator.isNull(data)) {
206                            return null;
207                    }
208    
209                    Document document = SAXReaderUtil.read(data);
210    
211                    Element rootElement = document.getRootElement();
212    
213                    Element nodesElement = rootElement.element("nodes");
214    
215                    for (Element nodeElement : nodesElement.elements("node")) {
216                            String path = nodeElement.attributeValue("path");
217    
218                            if (!portletDataContext.isPathNotProcessed(path)) {
219                                    continue;
220                            }
221    
222                            WikiNode node = (WikiNode)portletDataContext.getZipEntryAsObject(
223                                    path);
224    
225                            WikiPortletDataHandlerImpl.importNode(portletDataContext, node);
226                    }
227    
228                    Element pagesElement = rootElement.element("pages");
229    
230                    JournalPortletDataHandlerImpl.importReferencedData(
231                            portletDataContext, pagesElement);
232    
233                    for (Element pageElement : pagesElement.elements("page")) {
234                            String path = pageElement.attributeValue("path");
235    
236                            if (!portletDataContext.isPathNotProcessed(path)) {
237                                    continue;
238                            }
239    
240                            WikiPage page = (WikiPage)portletDataContext.getZipEntryAsObject(
241                                    path);
242    
243                            WikiPortletDataHandlerImpl.importPage(
244                                    portletDataContext, pageElement, page);
245                    }
246    
247                    Map<Long, Long> nodeIds =
248                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
249                                    WikiNode.class);
250    
251                    for (long nodeId : nodeIds.values()) {
252                            WikiCacheUtil.clearCache(nodeId);
253                    }
254    
255                    long nodeId = GetterUtil.getLong(
256                            portletPreferences.getValue("nodeId", StringPool.BLANK));
257    
258                    if (nodeId > 0) {
259                            nodeId = MapUtil.getLong(nodeIds, nodeId, nodeId);
260    
261                            portletPreferences.setValue("nodeId", String.valueOf(nodeId));
262                    }
263    
264                    return portletPreferences;
265            }
266    
267            private static final String _NAMESPACE = "wiki";
268    
269            private static Log _log = LogFactoryUtil.getLog(
270                    WikiDisplayPortletDataHandlerImpl.class);
271    
272            private static PortletDataHandlerBoolean _nodesAndPages =
273                    new PortletDataHandlerBoolean(
274                            _NAMESPACE, "wikis-and-pages", true, true);
275    
276    }