001    /**
002     * Copyright (c) 2000-2012 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.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
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.util.Validator;
027    import com.liferay.portal.kernel.xml.Document;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.kernel.xml.SAXReaderUtil;
030    import com.liferay.portlet.polls.NoSuchQuestionException;
031    import com.liferay.portlet.polls.model.PollsChoice;
032    import com.liferay.portlet.polls.model.PollsQuestion;
033    import com.liferay.portlet.polls.model.PollsVote;
034    import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
035    
036    import java.util.Map;
037    
038    import javax.portlet.PortletPreferences;
039    
040    /**
041     * @author Marcellus Tavares
042     */
043    public class PollsDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
044    
045            @Override
046            public PortletDataHandlerControl[] getExportControls() {
047                    return new PortletDataHandlerControl[] {_questions, _votes};
048            }
049    
050            @Override
051            public PortletDataHandlerControl[] getImportControls() {
052                    return new PortletDataHandlerControl[] {_questions, _votes};
053            }
054    
055            @Override
056            public boolean isPublishToLiveByDefault() {
057                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
058            }
059    
060            @Override
061            protected PortletPreferences doDeleteData(
062                            PortletDataContext portletDataContext, String portletId,
063                            PortletPreferences portletPreferences)
064                    throws Exception {
065    
066                    portletPreferences.setValue("questionId", StringPool.BLANK);
067    
068                    return portletPreferences;
069            }
070    
071            @Override
072            protected String doExportData(
073                            PortletDataContext portletDataContext, String portletId,
074                            PortletPreferences portletPreferences)
075                    throws Exception {
076    
077                    long questionId = GetterUtil.getLong(
078                            portletPreferences.getValue("questionId", StringPool.BLANK));
079    
080                    if (questionId <= 0) {
081                            if (_log.isWarnEnabled()) {
082                                    _log.warn(
083                                            "No question id found in preferences of portlet " +
084                                                    portletId);
085                            }
086    
087                            return StringPool.BLANK;
088                    }
089    
090                    PollsQuestion question = null;
091    
092                    try {
093                            question = PollsQuestionUtil.findByPrimaryKey(questionId);
094                    }
095                    catch (NoSuchQuestionException nsqe) {
096                            if (_log.isWarnEnabled()) {
097                                    _log.warn(nsqe, nsqe);
098                            }
099    
100                            return StringPool.BLANK;
101                    }
102    
103                    portletDataContext.addPermissions(
104                            "com.liferay.portlet.polls", portletDataContext.getScopeGroupId());
105    
106                    Document document = SAXReaderUtil.createDocument();
107    
108                    Element rootElement = document.addElement("polls-display-data");
109    
110                    rootElement.addAttribute(
111                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
112    
113                    Element questionsElement = rootElement.addElement("questions");
114                    Element choicesElement = rootElement.addElement("choices");
115                    Element votesElement = rootElement.addElement("votes");
116    
117                    PollsPortletDataHandlerImpl.exportQuestion(
118                            portletDataContext, questionsElement, choicesElement, votesElement,
119                            question);
120    
121                    return document.formattedString();
122            }
123    
124            @Override
125            protected PortletPreferences doImportData(
126                            PortletDataContext portletDataContext, String portletId,
127                            PortletPreferences portletPreferences, String data)
128                    throws Exception {
129    
130                    portletDataContext.importPermissions(
131                            "com.liferay.portlet.polls", portletDataContext.getSourceGroupId(),
132                            portletDataContext.getScopeGroupId());
133    
134                    if (Validator.isNull(data)) {
135                            return null;
136                    }
137    
138                    Document document = SAXReaderUtil.read(data);
139    
140                    Element rootElement = document.getRootElement();
141    
142                    Element questionsElement = rootElement.element("questions");
143    
144                    for (Element questionElement : questionsElement.elements("question")) {
145                            String path = questionElement.attributeValue("path");
146    
147                            if (!portletDataContext.isPathNotProcessed(path)) {
148                                    continue;
149                            }
150    
151                            PollsQuestion question =
152                                    (PollsQuestion)portletDataContext.getZipEntryAsObject(path);
153    
154                            PollsPortletDataHandlerImpl.importQuestion(
155                                    portletDataContext, questionElement, question);
156                    }
157    
158                    Element choicesElement = rootElement.element("choices");
159    
160                    for (Element choiceElement : choicesElement.elements("choice")) {
161                            String path = choiceElement.attributeValue("path");
162    
163                            if (!portletDataContext.isPathNotProcessed(path)) {
164                                    continue;
165                            }
166    
167                            PollsChoice choice =
168                                    (PollsChoice)portletDataContext.getZipEntryAsObject(path);
169    
170                            PollsPortletDataHandlerImpl.importChoice(
171                                    portletDataContext, choice);
172                    }
173    
174                    if (portletDataContext.getBooleanParameter(_NAMESPACE, "votes")) {
175                            Element votesElement = rootElement.element("votes");
176    
177                            for (Element voteElement : votesElement.elements("vote")) {
178                                    String path = voteElement.attributeValue("path");
179    
180                                    if (!portletDataContext.isPathNotProcessed(path)) {
181                                            continue;
182                                    }
183    
184                                    PollsVote vote =
185                                            (PollsVote)portletDataContext.getZipEntryAsObject(path);
186    
187                                    PollsPortletDataHandlerImpl.importVote(
188                                            portletDataContext, vote);
189                            }
190                    }
191    
192                    long questionId = GetterUtil.getLong(
193                            portletPreferences.getValue("questionId", StringPool.BLANK));
194    
195                    if (questionId > 0) {
196                            Map<Long, Long> questionPKs =
197                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
198                                            PollsQuestion.class);
199    
200                            questionId = MapUtil.getLong(questionPKs, questionId, questionId);
201    
202                            portletPreferences.setValue(
203                                    "questionId", String.valueOf(questionId));
204                    }
205    
206                    return portletPreferences;
207            }
208    
209            private static final String _NAMESPACE = "polls";
210    
211            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
212    
213            private static Log _log = LogFactoryUtil.getLog(
214                    PollsDisplayPortletDataHandlerImpl.class);
215    
216            private static PortletDataHandlerBoolean _questions =
217                    new PortletDataHandlerBoolean(_NAMESPACE, "questions", true, true);
218    
219            private static PortletDataHandlerBoolean _votes =
220                    new PortletDataHandlerBoolean(_NAMESPACE, "votes");
221    
222    }