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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.lar.DataLevel;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
021    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
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.portlet.PortletPreferencesFactoryUtil;
028    import com.liferay.portlet.wiki.model.WikiNode;
029    import com.liferay.portlet.wiki.model.WikiPage;
030    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
031    import com.liferay.portlet.wiki.service.permission.WikiPermission;
032    import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
033    
034    import java.util.List;
035    import java.util.Map;
036    
037    import javax.portlet.PortletPreferences;
038    
039    /**
040     * @author Marcellus Tavares
041     * @author Zsolt Berentey
042     */
043    public class WikiDisplayPortletDataHandler extends WikiPortletDataHandler {
044    
045            public WikiDisplayPortletDataHandler() {
046                    setDataLevel(DataLevel.PORTLET_INSTANCE);
047                    setDataPortletPreferences("title", "nodeId");
048                    setExportControls(new PortletDataHandlerControl[0]);
049            }
050    
051            @Override
052            protected PortletPreferences doDeleteData(
053                            PortletDataContext portletDataContext, String portletId,
054                            PortletPreferences portletPreferences)
055                    throws Exception {
056    
057                    if (portletPreferences == null) {
058                            return portletPreferences;
059                    }
060    
061                    portletPreferences.setValue("title", StringPool.BLANK);
062                    portletPreferences.setValue("nodeId", StringPool.BLANK);
063    
064                    return portletPreferences;
065            }
066    
067            @Override
068            protected PortletPreferences doProcessExportPortletPreferences(
069                            PortletDataContext portletDataContext, String portletId,
070                            PortletPreferences portletPreferences)
071                    throws Exception {
072    
073                    long nodeId = GetterUtil.getLong(
074                            portletPreferences.getValue("nodeId", StringPool.BLANK));
075    
076                    if (nodeId <= 0) {
077                            if (_log.isWarnEnabled()) {
078                                    _log.warn(
079                                            "No node id found in preferences of portlet " + portletId);
080                            }
081    
082                            return portletPreferences;
083                    }
084    
085                    String title = portletPreferences.getValue("title", null);
086    
087                    if (title == null) {
088                            if (_log.isWarnEnabled()) {
089                                    _log.warn(
090                                            "No title found in preferences of portlet " + portletId);
091                            }
092    
093                            return portletPreferences;
094                    }
095    
096                    WikiNode node = WikiNodeUtil.fetchByPrimaryKey(nodeId);
097    
098                    if (node == null) {
099                            if (_log.isWarnEnabled()) {
100                                    _log.warn("Unable to find wiki node");
101                            }
102    
103                            return portletPreferences;
104                    }
105    
106                    portletDataContext.addPortletPermissions(WikiPermission.RESOURCE_NAME);
107    
108                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
109                            portletDataContext, portletId, node);
110    
111                    List<WikiPage> pages = WikiPageLocalServiceUtil.getPages(
112                            node.getNodeId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
113    
114                    for (WikiPage page : pages) {
115                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
116                                    portletDataContext, portletId, page);
117                    }
118    
119                    return portletPreferences;
120            }
121    
122            @Override
123            protected PortletPreferences doProcessImportPortletPreferences(
124                            PortletDataContext portletDataContext, String portletId,
125                            PortletPreferences portletPreferences)
126                    throws Exception {
127    
128                    portletDataContext.importPortletPermissions(
129                            WikiPermission.RESOURCE_NAME);
130    
131                    super.importData(
132                            portletDataContext, portletId, portletPreferences,
133                            PortletPreferencesFactoryUtil.toXML(portletPreferences));
134    
135                    long nodeId = GetterUtil.getLong(
136                            portletPreferences.getValue("nodeId", StringPool.BLANK));
137    
138                    if (nodeId > 0) {
139                            Map<Long, Long> nodeIds =
140                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
141                                            WikiNode.class);
142    
143                            nodeId = MapUtil.getLong(nodeIds, nodeId, nodeId);
144    
145                            portletPreferences.setValue("nodeId", String.valueOf(nodeId));
146                    }
147    
148                    return portletPreferences;
149            }
150    
151            private static Log _log = LogFactoryUtil.getLog(
152                    WikiDisplayPortletDataHandler.class);
153    
154    }