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.ManifestSummary;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
023    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024    import com.liferay.portal.kernel.lar.StagedModelType;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.MapUtil;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portlet.wiki.model.WikiNode;
032    import com.liferay.portlet.wiki.model.WikiPage;
033    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
034    import com.liferay.portlet.wiki.service.permission.WikiPermission;
035    import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
036    
037    import java.util.List;
038    import java.util.Map;
039    
040    import javax.portlet.PortletPreferences;
041    
042    /**
043     * @author Marcellus Tavares
044     * @author Zsolt Berentey
045     */
046    public class WikiDisplayPortletDataHandler extends WikiPortletDataHandler {
047    
048            public WikiDisplayPortletDataHandler() {
049                    setDataLevel(DataLevel.PORTLET_INSTANCE);
050                    setDataPortletPreferences("title", "nodeId");
051                    setExportControls(
052                            new PortletDataHandlerBoolean(
053                                    NAMESPACE, "selected-node", true, true,
054                                    new PortletDataHandlerControl[] {
055                                            new PortletDataHandlerBoolean(
056                                                    NAMESPACE, "referenced-content")
057                                    },
058                                    WikiPage.class.getName()));
059            }
060    
061            @Override
062            protected PortletPreferences doDeleteData(
063                            PortletDataContext portletDataContext, String portletId,
064                            PortletPreferences portletPreferences)
065                    throws Exception {
066    
067                    if (portletPreferences == null) {
068                            return portletPreferences;
069                    }
070    
071                    portletPreferences.setValue("title", StringPool.BLANK);
072                    portletPreferences.setValue("nodeId", StringPool.BLANK);
073    
074                    return portletPreferences;
075            }
076    
077            @Override
078            protected String doExportData(
079                            PortletDataContext portletDataContext, String portletId,
080                            PortletPreferences portletPreferences)
081                    throws Exception {
082    
083                    long nodeId = GetterUtil.getLong(
084                            portletPreferences.getValue("nodeId", StringPool.BLANK));
085    
086                    if (nodeId <= 0) {
087                            if (_log.isWarnEnabled()) {
088                                    _log.warn(
089                                            "No node id found in preferences of portlet " + portletId);
090                            }
091    
092                            return StringPool.BLANK;
093                    }
094    
095                    String title = portletPreferences.getValue("title", null);
096    
097                    if (title == null) {
098                            if (_log.isWarnEnabled()) {
099                                    _log.warn(
100                                            "No title found in preferences of portlet " + portletId);
101                            }
102    
103                            return StringPool.BLANK;
104                    }
105    
106                    WikiNode node = WikiNodeUtil.fetchByPrimaryKey(nodeId);
107    
108                    if (node == null) {
109                            if (_log.isWarnEnabled()) {
110                                    _log.warn("Unable to find wiki node");
111                            }
112    
113                            return StringPool.BLANK;
114                    }
115    
116                    portletDataContext.addPortletPermissions(WikiPermission.RESOURCE_NAME);
117    
118                    Element rootElement = addExportDataRootElement(portletDataContext);
119    
120                    rootElement.addAttribute(
121                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
122    
123                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
124                            portletDataContext, portletId, node);
125    
126                    List<WikiPage> pages = WikiPageLocalServiceUtil.getPages(
127                            node.getNodeId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
128    
129                    for (WikiPage page : pages) {
130                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
131                                    portletDataContext, portletId, page);
132                    }
133    
134                    return getExportDataRootElementString(rootElement);
135            }
136    
137            @Override
138            protected PortletPreferences doImportData(
139                            PortletDataContext portletDataContext, String portletId,
140                            PortletPreferences portletPreferences, String data)
141                    throws Exception {
142    
143                    portletDataContext.importPortletPermissions(
144                            WikiPermission.RESOURCE_NAME);
145    
146                    super.importData(
147                            portletDataContext, portletId, portletPreferences, data);
148    
149                    long nodeId = GetterUtil.getLong(
150                            portletPreferences.getValue("nodeId", StringPool.BLANK));
151    
152                    if (nodeId > 0) {
153                            Map<Long, Long> nodeIds =
154                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
155                                            WikiNode.class);
156    
157                            nodeId = MapUtil.getLong(nodeIds, nodeId, nodeId);
158    
159                            portletPreferences.setValue("nodeId", String.valueOf(nodeId));
160                    }
161    
162                    return portletPreferences;
163            }
164    
165            @Override
166            protected void doPrepareManifestSummary(
167                            PortletDataContext portletDataContext,
168                            PortletPreferences portletPreferences)
169                    throws Exception {
170    
171                    ManifestSummary manifestSummary =
172                            portletDataContext.getManifestSummary();
173    
174                    if ((portletPreferences == null) ||
175                            (manifestSummary.getModelAdditionCount(WikiPage.class) > -1)) {
176    
177                            return;
178                    }
179    
180                    long nodeId = GetterUtil.getLong(
181                            portletPreferences.getValue("nodeId", StringPool.BLANK));
182    
183                    if (nodeId > 0) {
184                            manifestSummary.addModelAdditionCount(
185                                    new StagedModelType(WikiPage.class),
186                                    WikiPageLocalServiceUtil.getPagesCount(nodeId));
187                    }
188            }
189    
190            private static Log _log = LogFactoryUtil.getLog(
191                    WikiDisplayPortletDataHandler.class);
192    
193    }