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