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.polls.lar;
016    
017    import com.liferay.portal.kernel.lar.DataLevel;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portlet.polls.NoSuchQuestionException;
029    import com.liferay.portlet.polls.model.PollsChoice;
030    import com.liferay.portlet.polls.model.PollsQuestion;
031    import com.liferay.portlet.polls.model.PollsVote;
032    import com.liferay.portlet.polls.service.permission.PollsPermission;
033    import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
034    
035    import java.util.List;
036    import java.util.Map;
037    
038    import javax.portlet.PortletPreferences;
039    
040    /**
041     * @author Marcellus Tavares
042     */
043    public class PollsDisplayPortletDataHandler extends PollsPortletDataHandler {
044    
045            public PollsDisplayPortletDataHandler() {
046                    setDataLevel(DataLevel.PORTLET_INSTANCE);
047                    setDataPortletPreferences("questionId");
048                    setExportControls(new PortletDataHandlerControl[0]);
049                    setPublishToLiveByDefault(PropsValues.POLLS_PUBLISH_TO_LIVE_BY_DEFAULT);
050            }
051    
052            @Override
053            protected PortletPreferences doDeleteData(
054                            PortletDataContext portletDataContext, String portletId,
055                            PortletPreferences portletPreferences)
056                    throws Exception {
057    
058                    if (portletPreferences == null) {
059                            return portletPreferences;
060                    }
061    
062                    portletPreferences.setValue("questionId", 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 questionId = GetterUtil.getLong(
074                            portletPreferences.getValue("questionId", StringPool.BLANK));
075    
076                    if (questionId <= 0) {
077                            if (_log.isWarnEnabled()) {
078                                    _log.warn(
079                                            "No question id found in preferences of portlet " +
080                                                    portletId);
081                            }
082    
083                            return portletPreferences;
084                    }
085    
086                    PollsQuestion question = null;
087    
088                    try {
089                            question = PollsQuestionUtil.findByPrimaryKey(questionId);
090                    }
091                    catch (NoSuchQuestionException nsqe) {
092                            if (_log.isWarnEnabled()) {
093                                    _log.warn(nsqe, nsqe);
094                            }
095    
096                            return portletPreferences;
097                    }
098    
099                    portletDataContext.addPortletPermissions(PollsPermission.RESOURCE_NAME);
100    
101                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
102                            portletDataContext, portletId, question);
103    
104                    for (PollsChoice choice : question.getChoices()) {
105                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
106                                    portletDataContext, portletId, choice);
107                    }
108    
109                    if (portletDataContext.getBooleanParameter(
110                                    PollsPortletDataHandler.NAMESPACE, "votes")) {
111    
112                            for (PollsVote vote : question.getVotes()) {
113                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
114                                            portletDataContext, portletId, vote);
115                            }
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                            PollsPermission.RESOURCE_NAME);
129    
130                    Element questionsElement = portletDataContext.getImportDataGroupElement(
131                            PollsQuestion.class);
132    
133                    List<Element> questionElements = questionsElement.elements();
134    
135                    for (Element questionElement : questionElements) {
136                            StagedModelDataHandlerUtil.importReferenceStagedModel(
137                                    portletDataContext, questionElement);
138                    }
139    
140                    Element choicesElement = portletDataContext.getImportDataGroupElement(
141                            PollsChoice.class);
142    
143                    List<Element> choiceElements = choicesElement.elements();
144    
145                    for (Element choiceElement : choiceElements) {
146                            StagedModelDataHandlerUtil.importReferenceStagedModel(
147                                    portletDataContext, choiceElement);
148                    }
149    
150                    Element votesElement = portletDataContext.getImportDataGroupElement(
151                            PollsVote.class);
152    
153                    List<Element> voteElements = votesElement.elements();
154    
155                    for (Element voteElement : voteElements) {
156                            StagedModelDataHandlerUtil.importReferenceStagedModel(
157                                    portletDataContext, voteElement);
158                    }
159    
160                    long questionId = GetterUtil.getLong(
161                            portletPreferences.getValue("questionId", StringPool.BLANK));
162    
163                    if (questionId > 0) {
164                            Map<Long, Long> questionIds =
165                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
166                                            PollsQuestion.class);
167    
168                            questionId = MapUtil.getLong(questionIds, questionId, questionId);
169    
170                            portletPreferences.setValue(
171                                    "questionId", String.valueOf(questionId));
172                    }
173    
174                    return portletPreferences;
175            }
176    
177            private static Log _log = LogFactoryUtil.getLog(
178                    PollsDisplayPortletDataHandler.class);
179    
180    }