001    /**
002     * Copyright (c) 2000-2010 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.wiki.NoSuchNodeException;
032    import com.liferay.portlet.wiki.model.WikiNode;
033    import com.liferay.portlet.wiki.model.WikiPage;
034    import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
035    import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
036    import com.liferay.portlet.wiki.util.WikiCacheUtil;
037    
038    import java.util.Map;
039    
040    import javax.portlet.PortletPreferences;
041    
042    /**
043     * @author Marcellus Tavares
044     */
045    public class WikiDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
046    
047            public PortletDataHandlerControl[] getExportControls() {
048                    return new PortletDataHandlerControl[] {
049                            _nodesAndPages, _attachments, _categories, _comments, _tags
050                    };
051            }
052    
053            public PortletDataHandlerControl[] getImportControls() {
054                    return new PortletDataHandlerControl[] {
055                            _nodesAndPages, _attachments, _categories, _comments, _tags
056                    };
057            }
058    
059            public PortletPreferences importData(
060                            PortletDataContext context, String portletId,
061                            PortletPreferences preferences, String data)
062                    throws PortletDataException {
063    
064                    WikiCacheThreadLocal.setClearCache(false);
065    
066                    try {
067                            return super.importData(context, portletId, preferences, data);
068                    }
069                    finally {
070                            WikiCacheThreadLocal.setClearCache(true);
071                    }
072            }
073    
074            protected PortletPreferences doDeleteData(
075                            PortletDataContext context, String portletId,
076                            PortletPreferences preferences)
077                    throws Exception {
078    
079                    preferences.setValue("title", StringPool.BLANK);
080                    preferences.setValue("node-id", StringPool.BLANK);
081    
082                    return preferences;
083            }
084    
085            protected String doExportData(
086                            PortletDataContext context, String portletId,
087                            PortletPreferences preferences)
088                    throws Exception {
089    
090                    long nodeId = GetterUtil.getLong(
091                            preferences.getValue("node-id", StringPool.BLANK));
092    
093                    if (nodeId <= 0) {
094                            if (_log.isWarnEnabled()) {
095                                    _log.warn(
096                                            "No node id found in preferences of portlet " + portletId);
097                            }
098    
099                            return StringPool.BLANK;
100                    }
101    
102                    String title = preferences.getValue("title", null);
103    
104                    if (title == null) {
105                            if (_log.isWarnEnabled()) {
106                                    _log.warn(
107                                            "No title found in preferences of portlet " + portletId);
108                            }
109    
110                            return StringPool.BLANK;
111                    }
112    
113                    WikiNode node = null;
114    
115                    try {
116                            node = WikiNodeUtil.findByPrimaryKey(nodeId);
117    
118                            return StringPool.BLANK;
119                    }
120                    catch (NoSuchNodeException nsne) {
121                            if (_log.isWarnEnabled()) {
122                                    _log.warn(nsne, nsne);
123                            }
124                    }
125    
126                    context.addPermissions(
127                            "com.liferay.portlet.wiki", context.getScopeGroupId());
128    
129                    Document document = SAXReaderUtil.createDocument();
130    
131                    Element rootElement = document.addElement("wiki-display-data");
132    
133                    rootElement.addAttribute(
134                            "group-id", String.valueOf(context.getScopeGroupId()));
135    
136                    Element nodesElement = rootElement.addElement("nodes");
137                    Element pagesElement = rootElement.addElement("pages");
138    
139                    WikiPortletDataHandlerImpl.exportNode(
140                            context, nodesElement, pagesElement, node);
141    
142                    return document.formattedString();
143            }
144    
145            protected PortletPreferences doImportData(
146                            PortletDataContext context, String portletId,
147                            PortletPreferences preferences, String data)
148                    throws Exception {
149    
150                    context.importPermissions(
151                            "com.liferay.portlet.wiki", context.getSourceGroupId(),
152                            context.getScopeGroupId());
153    
154                    if (Validator.isNull(data)) {
155                            return null;
156                    }
157    
158                    Document document = SAXReaderUtil.read(data);
159    
160                    Element rootElement = document.getRootElement();
161    
162                    Element nodesElement = rootElement.element("nodes");
163    
164                    for (Element nodeElement : nodesElement.elements("node")) {
165                            String path = nodeElement.attributeValue("path");
166    
167                            if (!context.isPathNotProcessed(path)) {
168                                    continue;
169                            }
170    
171                            WikiNode node = (WikiNode)context.getZipEntryAsObject(path);
172    
173                            WikiPortletDataHandlerImpl.importNode(context, node);
174                    }
175    
176                    Element pagesElement = rootElement.element("pages");
177    
178                    for (Element pageElement : pagesElement.elements("page")) {
179                            String path = pageElement.attributeValue("path");
180    
181                            if (!context.isPathNotProcessed(path)) {
182                                    continue;
183                            }
184    
185                            WikiPage page = (WikiPage)context.getZipEntryAsObject(path);
186    
187                            WikiPortletDataHandlerImpl.importPage(context, pageElement, page);
188                    }
189    
190                    Map<Long, Long> nodePKs =
191                            (Map<Long, Long>)context.getNewPrimaryKeysMap(WikiNode.class);
192    
193                    for (long nodeId : nodePKs.values()) {
194                            WikiCacheUtil.clearCache(nodeId);
195                    }
196    
197                    long nodeId = GetterUtil.getLong(
198                            preferences.getValue("node-id", StringPool.BLANK));
199    
200                    if (nodeId > 0) {
201                            nodeId = MapUtil.getLong(nodePKs, nodeId, nodeId);
202    
203                            preferences.setValue("node-id", String.valueOf(nodeId));
204                    }
205    
206                    return preferences;
207            }
208    
209            private static final String _NAMESPACE = "wiki";
210    
211            private static Log _log = LogFactoryUtil.getLog(
212                    WikiDisplayPortletDataHandlerImpl.class);
213    
214            private static PortletDataHandlerBoolean _attachments =
215                    new PortletDataHandlerBoolean(_NAMESPACE, "attachments");
216    
217            private static PortletDataHandlerBoolean _categories =
218                    new PortletDataHandlerBoolean(_NAMESPACE, "categories");
219    
220            private static PortletDataHandlerBoolean _comments =
221                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
222    
223            private static PortletDataHandlerBoolean _nodesAndPages =
224                    new PortletDataHandlerBoolean(
225                            _NAMESPACE, "wikis-and-pages", true, true);
226    
227            private static PortletDataHandlerBoolean _tags =
228                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
229    
230    }