001    /**
002     * Copyright (c) 2000-2012 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.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                    portletPreferences.setValue("title", StringPool.BLANK);
111                    portletPreferences.setValue("nodeId", StringPool.BLANK);
112    
113                    return portletPreferences;
114            }
115    
116            @Override
117            protected String doExportData(
118                            PortletDataContext portletDataContext, String portletId,
119                            PortletPreferences portletPreferences)
120                    throws Exception {
121    
122                    long nodeId = GetterUtil.getLong(
123                            portletPreferences.getValue("nodeId", StringPool.BLANK));
124    
125                    if (nodeId <= 0) {
126                            if (_log.isWarnEnabled()) {
127                                    _log.warn(
128                                            "No node id found in preferences of portlet " + portletId);
129                            }
130    
131                            return StringPool.BLANK;
132                    }
133    
134                    String title = portletPreferences.getValue("title", null);
135    
136                    if (title == null) {
137                            if (_log.isWarnEnabled()) {
138                                    _log.warn(
139                                            "No title found in preferences of portlet " + portletId);
140                            }
141    
142                            return StringPool.BLANK;
143                    }
144    
145                    WikiNode node = null;
146    
147                    try {
148                            node = WikiNodeUtil.findByPrimaryKey(nodeId);
149                    }
150                    catch (NoSuchNodeException nsne) {
151                            if (_log.isWarnEnabled()) {
152                                    _log.warn(nsne, nsne);
153                            }
154    
155                            return StringPool.BLANK;
156                    }
157    
158                    portletDataContext.addPermissions(
159                            "com.liferay.portlet.wiki", portletDataContext.getScopeGroupId());
160    
161                    Document document = SAXReaderUtil.createDocument();
162    
163                    Element rootElement = document.addElement("wiki-display-data");
164    
165                    rootElement.addAttribute(
166                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
167    
168                    Element nodesElement = rootElement.addElement("nodes");
169                    Element pagesElement = rootElement.addElement("pages");
170    
171                    WikiPortletDataHandlerImpl.exportNode(
172                            portletDataContext, nodesElement, pagesElement, node);
173    
174                    return document.formattedString();
175            }
176    
177            @Override
178            protected PortletPreferences doImportData(
179                            PortletDataContext portletDataContext, String portletId,
180                            PortletPreferences portletPreferences, String data)
181                    throws Exception {
182    
183                    portletDataContext.importPermissions(
184                            "com.liferay.portlet.wiki", portletDataContext.getSourceGroupId(),
185                            portletDataContext.getScopeGroupId());
186    
187                    if (Validator.isNull(data)) {
188                            return null;
189                    }
190    
191                    Document document = SAXReaderUtil.read(data);
192    
193                    Element rootElement = document.getRootElement();
194    
195                    Element nodesElement = rootElement.element("nodes");
196    
197                    for (Element nodeElement : nodesElement.elements("node")) {
198                            String path = nodeElement.attributeValue("path");
199    
200                            if (!portletDataContext.isPathNotProcessed(path)) {
201                                    continue;
202                            }
203    
204                            WikiNode node = (WikiNode)portletDataContext.getZipEntryAsObject(
205                                    path);
206    
207                            WikiPortletDataHandlerImpl.importNode(portletDataContext, node);
208                    }
209    
210                    Element pagesElement = rootElement.element("pages");
211    
212                    JournalPortletDataHandlerImpl.importReferencedData(
213                            portletDataContext, pagesElement);
214    
215                    for (Element pageElement : pagesElement.elements("page")) {
216                            String path = pageElement.attributeValue("path");
217    
218                            if (!portletDataContext.isPathNotProcessed(path)) {
219                                    continue;
220                            }
221    
222                            WikiPage page = (WikiPage)portletDataContext.getZipEntryAsObject(
223                                    path);
224    
225                            WikiPortletDataHandlerImpl.importPage(
226                                    portletDataContext, pageElement, page);
227                    }
228    
229                    Map<Long, Long> nodeIds =
230                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
231                                    WikiNode.class);
232    
233                    for (long nodeId : nodeIds.values()) {
234                            WikiCacheUtil.clearCache(nodeId);
235                    }
236    
237                    long nodeId = GetterUtil.getLong(
238                            portletPreferences.getValue("nodeId", StringPool.BLANK));
239    
240                    if (nodeId > 0) {
241                            nodeId = MapUtil.getLong(nodeIds, nodeId, nodeId);
242    
243                            portletPreferences.setValue("nodeId", String.valueOf(nodeId));
244                    }
245    
246                    return portletPreferences;
247            }
248    
249            private static final String _NAMESPACE = "wiki";
250    
251            private static Log _log = LogFactoryUtil.getLog(
252                    WikiDisplayPortletDataHandlerImpl.class);
253    
254            private static PortletDataHandlerBoolean _nodesAndPages =
255                    new PortletDataHandlerBoolean(
256                            _NAMESPACE, "wikis-and-pages", true, true);
257    
258    }