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