Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portlet.wiki.lar; import com.liferay.portal.kernel.lar.BasePortletDataHandler; import com.liferay.portal.kernel.lar.PortletDataContext; import com.liferay.portal.kernel.lar.PortletDataException; import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean; import com.liferay.portal.kernel.lar.PortletDataHandlerControl; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.MapUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.kernel.xml.Document; import com.liferay.portal.kernel.xml.Element; import com.liferay.portal.kernel.xml.SAXReaderUtil; import com.liferay.portlet.journal.lar.JournalPortletDataHandlerImpl; import com.liferay.portlet.wiki.NoSuchNodeException; import com.liferay.portlet.wiki.model.WikiNode; import com.liferay.portlet.wiki.model.WikiPage; import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil; import com.liferay.portlet.wiki.util.WikiCacheThreadLocal; import com.liferay.portlet.wiki.util.WikiCacheUtil; import java.util.Map; import javax.portlet.PortletPreferences; /** * @author Marcellus Tavares */ public class WikiDisplayPortletDataHandlerImpl extends BasePortletDataHandler { @Override public PortletDataHandlerControl[] getExportControls() { return new PortletDataHandlerControl[] { _nodesAndPages, _attachments, _categories, _comments, _tags }; } @Override public PortletDataHandlerControl[] getImportControls() { return new PortletDataHandlerControl[] { _nodesAndPages, _attachments, _categories, _comments, _tags }; } @Override public PortletPreferences importData(PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences, String data) throws PortletDataException { WikiCacheThreadLocal.setClearCache(false); try { return super.importData(portletDataContext, portletId, portletPreferences, data); } finally { WikiCacheThreadLocal.setClearCache(true); } } @Override protected PortletPreferences doDeleteData(PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences) throws Exception { portletPreferences.setValue("title", StringPool.BLANK); portletPreferences.setValue("nodeId", StringPool.BLANK); return portletPreferences; } @Override protected String doExportData(PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences) throws Exception { long nodeId = GetterUtil.getLong(portletPreferences.getValue("nodeId", StringPool.BLANK)); if (nodeId <= 0) { if (_log.isWarnEnabled()) { _log.warn("No node id found in preferences of portlet " + portletId); } return StringPool.BLANK; } String title = portletPreferences.getValue("title", null); if (title == null) { if (_log.isWarnEnabled()) { _log.warn("No title found in preferences of portlet " + portletId); } return StringPool.BLANK; } WikiNode node = null; try { node = WikiNodeUtil.findByPrimaryKey(nodeId); } catch (NoSuchNodeException nsne) { if (_log.isWarnEnabled()) { _log.warn(nsne, nsne); } return StringPool.BLANK; } portletDataContext.addPermissions("com.liferay.portlet.wiki", portletDataContext.getScopeGroupId()); Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("wiki-display-data"); rootElement.addAttribute("group-id", String.valueOf(portletDataContext.getScopeGroupId())); Element nodesElement = rootElement.addElement("nodes"); Element pagesElement = rootElement.addElement("pages"); WikiPortletDataHandlerImpl.exportNode(portletDataContext, nodesElement, pagesElement, node); return document.formattedString(); } @Override protected PortletPreferences doImportData(PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences, String data) throws Exception { portletDataContext.importPermissions("com.liferay.portlet.wiki", portletDataContext.getSourceGroupId(), portletDataContext.getScopeGroupId()); if (Validator.isNull(data)) { return null; } Document document = SAXReaderUtil.read(data); Element rootElement = document.getRootElement(); Element nodesElement = rootElement.element("nodes"); for (Element nodeElement : nodesElement.elements("node")) { String path = nodeElement.attributeValue("path"); if (!portletDataContext.isPathNotProcessed(path)) { continue; } WikiNode node = (WikiNode) portletDataContext.getZipEntryAsObject(path); WikiPortletDataHandlerImpl.importNode(portletDataContext, node); } Element pagesElement = rootElement.element("pages"); JournalPortletDataHandlerImpl.importReferencedData(portletDataContext, pagesElement); for (Element pageElement : pagesElement.elements("page")) { String path = pageElement.attributeValue("path"); if (!portletDataContext.isPathNotProcessed(path)) { continue; } WikiPage page = (WikiPage) portletDataContext.getZipEntryAsObject(path); WikiPortletDataHandlerImpl.importPage(portletDataContext, pageElement, page); } Map<Long, Long> nodePKs = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(WikiNode.class); for (long nodeId : nodePKs.values()) { WikiCacheUtil.clearCache(nodeId); } long nodeId = GetterUtil.getLong(portletPreferences.getValue("nodeId", StringPool.BLANK)); if (nodeId > 0) { nodeId = MapUtil.getLong(nodePKs, nodeId, nodeId); portletPreferences.setValue("nodeId", String.valueOf(nodeId)); } return portletPreferences; } private static final String _NAMESPACE = "wiki"; private static Log _log = LogFactoryUtil.getLog(WikiDisplayPortletDataHandlerImpl.class); private static PortletDataHandlerBoolean _attachments = new PortletDataHandlerBoolean(_NAMESPACE, "attachments"); private static PortletDataHandlerBoolean _categories = new PortletDataHandlerBoolean(_NAMESPACE, "categories"); private static PortletDataHandlerBoolean _comments = new PortletDataHandlerBoolean(_NAMESPACE, "comments"); private static PortletDataHandlerBoolean _nodesAndPages = new PortletDataHandlerBoolean(_NAMESPACE, "wikis-and-pages", true, true); private static PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(_NAMESPACE, "tags"); }