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.PortletDataContext;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portlet.journal.lar.JournalPortletDataHandler;
025    import com.liferay.portlet.wiki.NoSuchNodeException;
026    import com.liferay.portlet.wiki.model.WikiNode;
027    import com.liferay.portlet.wiki.model.WikiPage;
028    import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
029    import com.liferay.portlet.wiki.util.WikiCacheUtil;
030    
031    import java.util.Map;
032    
033    import javax.portlet.PortletPreferences;
034    
035    /**
036     * @author Marcellus Tavares
037     */
038    public class WikiDisplayPortletDataHandler extends WikiPortletDataHandler {
039    
040            public WikiDisplayPortletDataHandler() {
041                    setDataPortletPreferences("title", "nodeId");
042            }
043    
044            @Override
045            protected PortletPreferences doDeleteData(
046                            PortletDataContext portletDataContext, String portletId,
047                            PortletPreferences portletPreferences)
048                    throws Exception {
049    
050                    if (portletPreferences == null) {
051                            return portletPreferences;
052                    }
053    
054                    portletPreferences.setValue("title", StringPool.BLANK);
055                    portletPreferences.setValue("nodeId", StringPool.BLANK);
056    
057                    return portletPreferences;
058            }
059    
060            @Override
061            protected String doExportData(
062                            PortletDataContext portletDataContext, String portletId,
063                            PortletPreferences portletPreferences)
064                    throws Exception {
065    
066                    long nodeId = GetterUtil.getLong(
067                            portletPreferences.getValue("nodeId", StringPool.BLANK));
068    
069                    if (nodeId <= 0) {
070                            if (_log.isWarnEnabled()) {
071                                    _log.warn(
072                                            "No node id found in preferences of portlet " + portletId);
073                            }
074    
075                            return StringPool.BLANK;
076                    }
077    
078                    String title = portletPreferences.getValue("title", null);
079    
080                    if (title == null) {
081                            if (_log.isWarnEnabled()) {
082                                    _log.warn(
083                                            "No title found in preferences of portlet " + portletId);
084                            }
085    
086                            return StringPool.BLANK;
087                    }
088    
089                    WikiNode node = null;
090    
091                    try {
092                            node = WikiNodeUtil.findByPrimaryKey(nodeId);
093                    }
094                    catch (NoSuchNodeException nsne) {
095                            if (_log.isWarnEnabled()) {
096                                    _log.warn(nsne, nsne);
097                            }
098    
099                            return StringPool.BLANK;
100                    }
101    
102                    portletDataContext.addPermissions(
103                            "com.liferay.portlet.wiki", portletDataContext.getScopeGroupId());
104    
105                    Element rootElement = addExportDataRootElement(portletDataContext);
106    
107                    rootElement.addAttribute(
108                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
109    
110                    Element nodesElement = rootElement.addElement("nodes");
111                    Element pagesElement = rootElement.addElement("pages");
112    
113                    Element dlFileEntryTypesElement = pagesElement.addElement(
114                            "dl-file-entry-types");
115                    Element dlFoldersElement = pagesElement.addElement("dl-folders");
116                    Element dlFileEntriesElement = pagesElement.addElement(
117                            "dl-file-entries");
118                    Element dlFileRanksElement = pagesElement.addElement("dl-file-ranks");
119                    Element dlRepositoriesElement = pagesElement.addElement(
120                            "dl-repositories");
121                    Element dlRepositoryEntriesElement = pagesElement.addElement(
122                            "dl-repository-entries");
123    
124                    WikiPortletDataHandler.exportNode(
125                            portletDataContext, nodesElement, pagesElement,
126                            dlFileEntryTypesElement, dlFoldersElement, dlFileEntriesElement,
127                            dlFileRanksElement, dlRepositoriesElement,
128                            dlRepositoryEntriesElement, node);
129    
130                    return getExportDataRootElementString(rootElement);
131            }
132    
133            @Override
134            protected PortletPreferences doImportData(
135                            PortletDataContext portletDataContext, String portletId,
136                            PortletPreferences portletPreferences, String data)
137                    throws Exception {
138    
139                    portletDataContext.importPermissions(
140                            "com.liferay.portlet.wiki", portletDataContext.getSourceGroupId(),
141                            portletDataContext.getScopeGroupId());
142    
143                    Element rootElement = portletDataContext.getImportDataRootElement();
144    
145                    Element nodesElement = rootElement.element("nodes");
146    
147                    for (Element nodeElement : nodesElement.elements("node")) {
148                            String path = nodeElement.attributeValue("path");
149    
150                            if (!portletDataContext.isPathNotProcessed(path)) {
151                                    continue;
152                            }
153    
154                            WikiNode node = (WikiNode)portletDataContext.getZipEntryAsObject(
155                                    path);
156    
157                            WikiPortletDataHandler.importNode(portletDataContext, node);
158                    }
159    
160                    Element pagesElement = rootElement.element("pages");
161    
162                    JournalPortletDataHandler.importReferencedData(
163                            portletDataContext, pagesElement);
164    
165                    for (Element pageElement : pagesElement.elements("page")) {
166                            String path = pageElement.attributeValue("path");
167    
168                            if (!portletDataContext.isPathNotProcessed(path)) {
169                                    continue;
170                            }
171    
172                            WikiPage page = (WikiPage)portletDataContext.getZipEntryAsObject(
173                                    path);
174    
175                            WikiPortletDataHandler.importPage(
176                                    portletDataContext, pageElement, page);
177                    }
178    
179                    Map<Long, Long> nodeIds =
180                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
181                                    WikiNode.class);
182    
183                    for (long nodeId : nodeIds.values()) {
184                            WikiCacheUtil.clearCache(nodeId);
185                    }
186    
187                    long nodeId = GetterUtil.getLong(
188                            portletPreferences.getValue("nodeId", StringPool.BLANK));
189    
190                    if (nodeId > 0) {
191                            nodeId = MapUtil.getLong(nodeIds, nodeId, nodeId);
192    
193                            portletPreferences.setValue("nodeId", String.valueOf(nodeId));
194                    }
195    
196                    return portletPreferences;
197            }
198    
199            private static Log _log = LogFactoryUtil.getLog(
200                    WikiDisplayPortletDataHandler.class);
201    
202    }