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.util.PropsValues;
027    import com.liferay.portlet.polls.NoSuchQuestionException;
028    import com.liferay.portlet.polls.model.PollsChoice;
029    import com.liferay.portlet.polls.model.PollsQuestion;
030    import com.liferay.portlet.polls.model.PollsVote;
031    import com.liferay.portlet.polls.service.permission.PollsPermission;
032    import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
033    
034    import java.util.Map;
035    
036    import javax.portlet.PortletPreferences;
037    
038    /**
039     * @author Marcellus Tavares
040     */
041    public class PollsDisplayPortletDataHandler extends PollsPortletDataHandler {
042    
043            public PollsDisplayPortletDataHandler() {
044                    setDataLevel(DataLevel.PORTLET_INSTANCE);
045                    setDataPortletPreferences("questionId");
046                    setExportControls(new PortletDataHandlerControl[0]);
047                    setPublishToLiveByDefault(PropsValues.POLLS_PUBLISH_TO_LIVE_BY_DEFAULT);
048            }
049    
050            @Override
051            protected PortletPreferences doDeleteData(
052                            PortletDataContext portletDataContext, String portletId,
053                            PortletPreferences portletPreferences)
054                    throws Exception {
055    
056                    if (portletPreferences == null) {
057                            return portletPreferences;
058                    }
059    
060                    portletPreferences.setValue("questionId", StringPool.BLANK);
061    
062                    return portletPreferences;
063            }
064    
065            @Override
066            protected PortletPreferences doProcessExportPortletPreferences(
067                            PortletDataContext portletDataContext, String portletId,
068                            PortletPreferences portletPreferences)
069                    throws Exception {
070    
071                    long questionId = GetterUtil.getLong(
072                            portletPreferences.getValue("questionId", StringPool.BLANK));
073    
074                    if (questionId <= 0) {
075                            if (_log.isWarnEnabled()) {
076                                    _log.warn(
077                                            "No question id found in preferences of portlet " +
078                                                    portletId);
079                            }
080    
081                            return portletPreferences;
082                    }
083    
084                    PollsQuestion question = null;
085    
086                    try {
087                            question = PollsQuestionUtil.findByPrimaryKey(questionId);
088                    }
089                    catch (NoSuchQuestionException nsqe) {
090                            if (_log.isWarnEnabled()) {
091                                    _log.warn(nsqe, nsqe);
092                            }
093    
094                            return portletPreferences;
095                    }
096    
097                    portletDataContext.addPortletPermissions(PollsPermission.RESOURCE_NAME);
098    
099                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
100                            portletDataContext, portletId, question);
101    
102                    for (PollsChoice choice : question.getChoices()) {
103                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
104                                    portletDataContext, portletId, choice);
105                    }
106    
107                    if (portletDataContext.getBooleanParameter(
108                                    PollsPortletDataHandler.NAMESPACE, "votes")) {
109    
110                            for (PollsVote vote : question.getVotes()) {
111                                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
112                                            portletDataContext, portletId, vote);
113                            }
114                    }
115    
116                    return portletPreferences;
117            }
118    
119            @Override
120            protected PortletPreferences doProcessImportPortletPreferences(
121                            PortletDataContext portletDataContext, String portletId,
122                            PortletPreferences portletPreferences)
123                    throws Exception {
124    
125                    portletDataContext.importPortletPermissions(
126                            PollsPermission.RESOURCE_NAME);
127    
128                    StagedModelDataHandlerUtil.importReferenceStagedModels(
129                            portletDataContext, PollsQuestion.class);
130    
131                    StagedModelDataHandlerUtil.importReferenceStagedModels(
132                            portletDataContext, PollsChoice.class);
133    
134                    StagedModelDataHandlerUtil.importReferenceStagedModels(
135                            portletDataContext, PollsVote.class);
136    
137                    long questionId = GetterUtil.getLong(
138                            portletPreferences.getValue("questionId", StringPool.BLANK));
139    
140                    if (questionId > 0) {
141                            Map<Long, Long> questionIds =
142                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
143                                            PollsQuestion.class);
144    
145                            questionId = MapUtil.getLong(questionIds, questionId, questionId);
146    
147                            portletPreferences.setValue(
148                                    "questionId", String.valueOf(questionId));
149                    }
150    
151                    return portletPreferences;
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(
155                    PollsDisplayPortletDataHandler.class);
156    
157    }