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.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.exception.SystemException;
023    import com.liferay.portal.kernel.lar.DataLevel;
024    import com.liferay.portal.kernel.lar.PortletDataContext;
025    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
026    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.MapUtil;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portlet.wiki.model.WikiNode;
033    import com.liferay.portlet.wiki.model.WikiPage;
034    import com.liferay.portlet.wiki.service.permission.WikiPermission;
035    import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
036    import com.liferay.portlet.wiki.service.persistence.WikiPageExportActionableDynamicQuery;
037    
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(new PortletDataHandlerControl[0]);
052            }
053    
054            @Override
055            protected PortletPreferences doDeleteData(
056                            PortletDataContext portletDataContext, String portletId,
057                            PortletPreferences portletPreferences)
058                    throws Exception {
059    
060                    if (portletPreferences == null) {
061                            return portletPreferences;
062                    }
063    
064                    portletPreferences.setValue("title", StringPool.BLANK);
065                    portletPreferences.setValue("nodeId", StringPool.BLANK);
066    
067                    return portletPreferences;
068            }
069    
070            @Override
071            protected PortletPreferences doProcessExportPortletPreferences(
072                            PortletDataContext portletDataContext, String portletId,
073                            PortletPreferences portletPreferences)
074                    throws Exception {
075    
076                    long nodeId = GetterUtil.getLong(
077                            portletPreferences.getValue("nodeId", StringPool.BLANK));
078    
079                    if (nodeId <= 0) {
080                            if (_log.isWarnEnabled()) {
081                                    _log.warn(
082                                            "No node id found in preferences of portlet " + portletId);
083                            }
084    
085                            return portletPreferences;
086                    }
087    
088                    String title = portletPreferences.getValue("title", null);
089    
090                    if (title == null) {
091                            if (_log.isWarnEnabled()) {
092                                    _log.warn(
093                                            "No title found in preferences of portlet " + portletId);
094                            }
095    
096                            return portletPreferences;
097                    }
098    
099                    WikiNode node = WikiNodeUtil.fetchByPrimaryKey(nodeId);
100    
101                    if (node == null) {
102                            if (_log.isWarnEnabled()) {
103                                    _log.warn("Unable to find wiki node");
104                            }
105    
106                            return portletPreferences;
107                    }
108    
109                    portletDataContext.addPortletPermissions(WikiPermission.RESOURCE_NAME);
110    
111                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
112                            portletDataContext, portletId, node);
113    
114                    ActionableDynamicQuery actionableDynamicQuery =
115                            getPageActionableDynamicQuery(
116                                    portletDataContext, node.getNodeId(), portletId);
117    
118                    actionableDynamicQuery.performActions();
119    
120                    return portletPreferences;
121            }
122    
123            @Override
124            protected PortletPreferences doProcessImportPortletPreferences(
125                            PortletDataContext portletDataContext, String portletId,
126                            PortletPreferences portletPreferences)
127                    throws Exception {
128    
129                    portletDataContext.importPortletPermissions(
130                            WikiPermission.RESOURCE_NAME);
131    
132                    StagedModelDataHandlerUtil.importReferenceStagedModels(
133                            portletDataContext, WikiNode.class);
134    
135                    StagedModelDataHandlerUtil.importReferenceStagedModels(
136                            portletDataContext, WikiPage.class);
137    
138                    long nodeId = GetterUtil.getLong(
139                            portletPreferences.getValue("nodeId", StringPool.BLANK));
140    
141                    if (nodeId > 0) {
142                            Map<Long, Long> nodeIds =
143                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
144                                            WikiNode.class);
145    
146                            nodeId = MapUtil.getLong(nodeIds, nodeId, nodeId);
147    
148                            portletPreferences.setValue("nodeId", String.valueOf(nodeId));
149                    }
150    
151                    return portletPreferences;
152            }
153    
154            protected ActionableDynamicQuery getPageActionableDynamicQuery(
155                            final PortletDataContext portletDataContext, final long nodeId,
156                            final String portletId)
157                    throws SystemException {
158    
159                    return new WikiPageExportActionableDynamicQuery(portletDataContext) {
160    
161                            @Override
162                            protected void addCriteria(DynamicQuery dynamicQuery) {
163                                    super.addCriteria(dynamicQuery);
164    
165                                    Property property = PropertyFactoryUtil.forName("nodeId");
166    
167                                    dynamicQuery.add(property.eq(nodeId));
168                            }
169    
170                            @Override
171                            protected void performAction(Object object) throws PortalException {
172                                    WikiPage page = (WikiPage)object;
173    
174                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
175                                            portletDataContext, portletId, page);
176                            }
177    
178                    };
179            }
180    
181            private static Log _log = LogFactoryUtil.getLog(
182                    WikiDisplayPortletDataHandler.class);
183    
184    }