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.ManifestSummary;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.lar.StagedModelType;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.MapUtil;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.xml.Element;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portlet.polls.NoSuchQuestionException;
032    import com.liferay.portlet.polls.model.PollsChoice;
033    import com.liferay.portlet.polls.model.PollsQuestion;
034    import com.liferay.portlet.polls.model.PollsVote;
035    import com.liferay.portlet.polls.service.permission.PollsPermission;
036    import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
037    
038    import java.util.List;
039    import java.util.Map;
040    
041    import javax.portlet.PortletPreferences;
042    
043    /**
044     * @author Marcellus Tavares
045     */
046    public class PollsDisplayPortletDataHandler extends PollsPortletDataHandler {
047    
048            public PollsDisplayPortletDataHandler() {
049                    setDataLevel(DataLevel.PORTLET_INSTANCE);
050                    setDataPortletPreferences("questionId");
051                    setExportControls(
052                            new PortletDataHandlerBoolean(
053                                    NAMESPACE, "selected-question", true, true,
054                                    new PortletDataHandlerControl[] {
055                                            new PortletDataHandlerBoolean(
056                                                    NAMESPACE, "votes", true, false, null,
057                                                    PollsVote.class.getName())
058                                    },
059                                    PollsQuestion.class.getName()));
060                    setPublishToLiveByDefault(PropsValues.POLLS_PUBLISH_TO_LIVE_BY_DEFAULT);
061            }
062    
063            @Override
064            protected PortletPreferences doDeleteData(
065                            PortletDataContext portletDataContext, String portletId,
066                            PortletPreferences portletPreferences)
067                    throws Exception {
068    
069                    if (portletPreferences == null) {
070                            return portletPreferences;
071                    }
072    
073                    portletPreferences.setValue("questionId", StringPool.BLANK);
074    
075                    return portletPreferences;
076            }
077    
078            @Override
079            protected String doExportData(
080                            PortletDataContext portletDataContext, String portletId,
081                            PortletPreferences portletPreferences)
082                    throws Exception {
083    
084                    long questionId = GetterUtil.getLong(
085                            portletPreferences.getValue("questionId", StringPool.BLANK));
086    
087                    if (questionId <= 0) {
088                            if (_log.isWarnEnabled()) {
089                                    _log.warn(
090                                            "No question id found in preferences of portlet " +
091                                                    portletId);
092                            }
093    
094                            return StringPool.BLANK;
095                    }
096    
097                    PollsQuestion question = null;
098    
099                    try {
100                            question = PollsQuestionUtil.findByPrimaryKey(questionId);
101                    }
102                    catch (NoSuchQuestionException nsqe) {
103                            if (_log.isWarnEnabled()) {
104                                    _log.warn(nsqe, nsqe);
105                            }
106    
107                            return StringPool.BLANK;
108                    }
109    
110                    portletDataContext.addPortletPermissions(PollsPermission.RESOURCE_NAME);
111    
112                    Element rootElement = addExportDataRootElement(portletDataContext);
113    
114                    rootElement.addAttribute(
115                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
116    
117                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
118                            portletDataContext, portletId, question);
119    
120                    for (PollsChoice choice : question.getChoices()) {
121                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
122                                    portletDataContext, portletId, choice);
123                    }
124    
125                    if (portletDataContext.getBooleanParameter(
126                                    PollsPortletDataHandler.NAMESPACE, "votes")) {
127    
128                            for (PollsVote vote : question.getVotes()) {
129                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
130                                            portletDataContext, portletId, vote);
131                            }
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                            PollsPermission.RESOURCE_NAME);
145    
146                    Element questionsElement = portletDataContext.getImportDataGroupElement(
147                            PollsQuestion.class);
148    
149                    List<Element> questionElements = questionsElement.elements();
150    
151                    for (Element questionElement : questionElements) {
152                            StagedModelDataHandlerUtil.importReferenceStagedModel(
153                                    portletDataContext, questionElement);
154                    }
155    
156                    Element choicesElement = portletDataContext.getImportDataGroupElement(
157                            PollsChoice.class);
158    
159                    List<Element> choiceElements = choicesElement.elements();
160    
161                    for (Element choiceElement : choiceElements) {
162                            StagedModelDataHandlerUtil.importReferenceStagedModel(
163                                    portletDataContext, choiceElement);
164                    }
165    
166                    if (portletDataContext.getBooleanParameter(
167                                    PollsPortletDataHandler.NAMESPACE, "votes")) {
168    
169                            Element votesElement = portletDataContext.getImportDataGroupElement(
170                                    PollsVote.class);
171    
172                            List<Element> voteElements = votesElement.elements();
173    
174                            for (Element voteElement : voteElements) {
175                                    StagedModelDataHandlerUtil.importReferenceStagedModel(
176                                            portletDataContext, voteElement);
177                            }
178                    }
179    
180                    long questionId = GetterUtil.getLong(
181                            portletPreferences.getValue("questionId", StringPool.BLANK));
182    
183                    if (questionId > 0) {
184                            Map<Long, Long> questionIds =
185                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
186                                            PollsQuestion.class);
187    
188                            questionId = MapUtil.getLong(questionIds, questionId, questionId);
189    
190                            portletPreferences.setValue(
191                                    "questionId", String.valueOf(questionId));
192                    }
193    
194                    return portletPreferences;
195            }
196    
197            @Override
198            protected void doPrepareManifestSummary(
199                            PortletDataContext portletDataContext,
200                            PortletPreferences portletPreferences) throws Exception {
201    
202                    ManifestSummary manifestSummary =
203                            portletDataContext.getManifestSummary();
204    
205                    if ((portletPreferences == null) ||
206                            (manifestSummary.getModelAdditionCount(PollsQuestion.class) > -1)) {
207    
208                            return;
209                    }
210    
211                    long questionId = GetterUtil.getLong(
212                            portletPreferences.getValue("questionId", StringPool.BLANK));
213    
214                    if (questionId > 0) {
215                            manifestSummary.addModelAdditionCount(
216                                    new StagedModelType(PollsQuestion.class), 1);
217                    }
218            }
219    
220            private static Log _log = LogFactoryUtil.getLog(
221                    PollsDisplayPortletDataHandler.class);
222    
223    }