001    /**
002     * Copyright (c) 2000-present 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.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.lar.DataLevel;
023    import com.liferay.portal.kernel.lar.PortletDataContext;
024    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
025    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.MapUtil;
030    import com.liferay.portal.kernel.util.StringPool;
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.Map;
038    
039    import javax.portlet.PortletPreferences;
040    
041    /**
042     * @author Marcellus Tavares
043     * @author Zsolt Berentey
044     */
045    public class WikiDisplayPortletDataHandler extends WikiPortletDataHandler {
046    
047            public WikiDisplayPortletDataHandler() {
048                    setDataLevel(DataLevel.PORTLET_INSTANCE);
049                    setDataPortletPreferences("title", "nodeId");
050                    setExportControls(new PortletDataHandlerControl[0]);
051            }
052    
053            @Override
054            protected PortletPreferences doDeleteData(
055                            PortletDataContext portletDataContext, String portletId,
056                            PortletPreferences portletPreferences)
057                    throws Exception {
058    
059                    if (portletPreferences == null) {
060                            return portletPreferences;
061                    }
062    
063                    portletPreferences.setValue("title", StringPool.BLANK);
064                    portletPreferences.setValue("nodeId", StringPool.BLANK);
065    
066                    return portletPreferences;
067            }
068    
069            @Override
070            protected PortletPreferences doProcessExportPortletPreferences(
071                            PortletDataContext portletDataContext, String portletId,
072                            PortletPreferences portletPreferences)
073                    throws Exception {
074    
075                    long nodeId = GetterUtil.getLong(
076                            portletPreferences.getValue("nodeId", StringPool.BLANK));
077    
078                    if (nodeId <= 0) {
079                            if (_log.isWarnEnabled()) {
080                                    _log.warn(
081                                            "No node id found in preferences of portlet " + portletId);
082                            }
083    
084                            return portletPreferences;
085                    }
086    
087                    String title = portletPreferences.getValue("title", null);
088    
089                    if (title == null) {
090                            if (_log.isWarnEnabled()) {
091                                    _log.warn(
092                                            "No title found in preferences of portlet " + portletId);
093                            }
094    
095                            return portletPreferences;
096                    }
097    
098                    WikiNode node = WikiNodeUtil.fetchByPrimaryKey(nodeId);
099    
100                    if (node == null) {
101                            if (_log.isWarnEnabled()) {
102                                    _log.warn("Unable to find wiki node");
103                            }
104    
105                            return portletPreferences;
106                    }
107    
108                    portletDataContext.addPortletPermissions(WikiPermission.RESOURCE_NAME);
109    
110                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
111                            portletDataContext, portletId, node);
112    
113                    ActionableDynamicQuery actionableDynamicQuery =
114                            getPageActionableDynamicQuery(
115                                    portletDataContext, node.getNodeId(), portletId);
116    
117                    actionableDynamicQuery.performActions();
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                    StagedModelDataHandlerUtil.importReferenceStagedModels(
132                            portletDataContext, WikiNode.class);
133    
134                    StagedModelDataHandlerUtil.importReferenceStagedModels(
135                            portletDataContext, WikiPage.class);
136    
137                    long nodeId = GetterUtil.getLong(
138                            portletPreferences.getValue("nodeId", StringPool.BLANK));
139    
140                    if (nodeId > 0) {
141                            Map<Long, Long> nodeIds =
142                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
143                                            WikiNode.class);
144    
145                            nodeId = MapUtil.getLong(nodeIds, nodeId, nodeId);
146    
147                            portletPreferences.setValue("nodeId", String.valueOf(nodeId));
148                    }
149    
150                    return portletPreferences;
151            }
152    
153            protected ActionableDynamicQuery getPageActionableDynamicQuery(
154                    final PortletDataContext portletDataContext, final long nodeId,
155                    final String portletId) {
156    
157                    ActionableDynamicQuery actionableDynamicQuery =
158                            WikiPageLocalServiceUtil.getExportActionableDynamicQuery(
159                                    portletDataContext);
160    
161                    final ActionableDynamicQuery.AddCriteriaMethod addCriteriaMethod =
162                            actionableDynamicQuery.getAddCriteriaMethod();
163    
164                    actionableDynamicQuery.setAddCriteriaMethod(
165                            new ActionableDynamicQuery.AddCriteriaMethod() {
166    
167                                    @Override
168                                    public void addCriteria(DynamicQuery dynamicQuery) {
169                                            addCriteriaMethod.addCriteria(dynamicQuery);
170    
171                                            Property property = PropertyFactoryUtil.forName("nodeId");
172    
173                                            dynamicQuery.add(property.eq(nodeId));
174                                    }
175    
176                            });
177    
178                    actionableDynamicQuery.setPerformActionMethod(
179                            new ActionableDynamicQuery.PerformActionMethod() {
180    
181                                    @Override
182                                    public void performAction(Object object)
183                                            throws PortalException {
184    
185                                            WikiPage page = (WikiPage)object;
186    
187                                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
188                                                    portletDataContext, portletId, page);
189                                    }
190    
191                            });
192    
193                    return actionableDynamicQuery;
194            }
195    
196            private static final Log _log = LogFactoryUtil.getLog(
197                    WikiDisplayPortletDataHandler.class);
198    
199    }