001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 String[] getDataPortletPreferences() {
047                    return new String[] {"questionId"};
048            }
049    
050            @Override
051            public PortletDataHandlerControl[] getExportControls() {
052                    return new PortletDataHandlerControl[] {_questions, _votes};
053            }
054    
055            @Override
056            public PortletDataHandlerControl[] getImportControls() {
057                    return new PortletDataHandlerControl[] {_questions, _votes};
058            }
059    
060            @Override
061            public boolean isDataLocalized() {
062                    return _DATA_LOCALIZED;
063            }
064    
065            @Override
066            public boolean isPublishToLiveByDefault() {
067                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
068            }
069    
070            @Override
071            protected PortletPreferences doDeleteData(
072                            PortletDataContext portletDataContext, String portletId,
073                            PortletPreferences portletPreferences)
074                    throws Exception {
075    
076                    portletPreferences.setValue("questionId", StringPool.BLANK);
077    
078                    return portletPreferences;
079            }
080    
081            @Override
082            protected String doExportData(
083                            PortletDataContext portletDataContext, String portletId,
084                            PortletPreferences portletPreferences)
085                    throws Exception {
086    
087                    long questionId = GetterUtil.getLong(
088                            portletPreferences.getValue("questionId", StringPool.BLANK));
089    
090                    if (questionId <= 0) {
091                            if (_log.isWarnEnabled()) {
092                                    _log.warn(
093                                            "No question id found in preferences of portlet " +
094                                                    portletId);
095                            }
096    
097                            return StringPool.BLANK;
098                    }
099    
100                    PollsQuestion question = null;
101    
102                    try {
103                            question = PollsQuestionUtil.findByPrimaryKey(questionId);
104                    }
105                    catch (NoSuchQuestionException nsqe) {
106                            if (_log.isWarnEnabled()) {
107                                    _log.warn(nsqe, nsqe);
108                            }
109    
110                            return StringPool.BLANK;
111                    }
112    
113                    portletDataContext.addPermissions(
114                            "com.liferay.portlet.polls", portletDataContext.getScopeGroupId());
115    
116                    Document document = SAXReaderUtil.createDocument();
117    
118                    Element rootElement = document.addElement("polls-display-data");
119    
120                    rootElement.addAttribute(
121                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
122    
123                    Element questionsElement = rootElement.addElement("questions");
124                    Element choicesElement = rootElement.addElement("choices");
125                    Element votesElement = rootElement.addElement("votes");
126    
127                    PollsPortletDataHandlerImpl.exportQuestion(
128                            portletDataContext, questionsElement, choicesElement, votesElement,
129                            question);
130    
131                    return document.formattedString();
132            }
133    
134            @Override
135            protected PortletPreferences doImportData(
136                            PortletDataContext portletDataContext, String portletId,
137                            PortletPreferences portletPreferences, String data)
138                    throws Exception {
139    
140                    portletDataContext.importPermissions(
141                            "com.liferay.portlet.polls", portletDataContext.getSourceGroupId(),
142                            portletDataContext.getScopeGroupId());
143    
144                    if (Validator.isNull(data)) {
145                            return null;
146                    }
147    
148                    Document document = SAXReaderUtil.read(data);
149    
150                    Element rootElement = document.getRootElement();
151    
152                    Element questionsElement = rootElement.element("questions");
153    
154                    for (Element questionElement : questionsElement.elements("question")) {
155                            String path = questionElement.attributeValue("path");
156    
157                            if (!portletDataContext.isPathNotProcessed(path)) {
158                                    continue;
159                            }
160    
161                            PollsQuestion question =
162                                    (PollsQuestion)portletDataContext.getZipEntryAsObject(path);
163    
164                            PollsPortletDataHandlerImpl.importQuestion(
165                                    portletDataContext, questionElement, question);
166                    }
167    
168                    Element choicesElement = rootElement.element("choices");
169    
170                    for (Element choiceElement : choicesElement.elements("choice")) {
171                            String path = choiceElement.attributeValue("path");
172    
173                            if (!portletDataContext.isPathNotProcessed(path)) {
174                                    continue;
175                            }
176    
177                            PollsChoice choice =
178                                    (PollsChoice)portletDataContext.getZipEntryAsObject(path);
179    
180                            PollsPortletDataHandlerImpl.importChoice(
181                                    portletDataContext, choice);
182                    }
183    
184                    if (portletDataContext.getBooleanParameter(_NAMESPACE, "votes")) {
185                            Element votesElement = rootElement.element("votes");
186    
187                            for (Element voteElement : votesElement.elements("vote")) {
188                                    String path = voteElement.attributeValue("path");
189    
190                                    if (!portletDataContext.isPathNotProcessed(path)) {
191                                            continue;
192                                    }
193    
194                                    PollsVote vote =
195                                            (PollsVote)portletDataContext.getZipEntryAsObject(path);
196    
197                                    PollsPortletDataHandlerImpl.importVote(
198                                            portletDataContext, vote);
199                            }
200                    }
201    
202                    long questionId = GetterUtil.getLong(
203                            portletPreferences.getValue("questionId", StringPool.BLANK));
204    
205                    if (questionId > 0) {
206                            Map<Long, Long> questionIds =
207                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
208                                            PollsQuestion.class);
209    
210                            questionId = MapUtil.getLong(questionIds, questionId, questionId);
211    
212                            portletPreferences.setValue(
213                                    "questionId", String.valueOf(questionId));
214                    }
215    
216                    return portletPreferences;
217            }
218    
219            private static final boolean _DATA_LOCALIZED = true;
220    
221            private static final String _NAMESPACE = "polls";
222    
223            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
224    
225            private static Log _log = LogFactoryUtil.getLog(
226                    PollsDisplayPortletDataHandlerImpl.class);
227    
228            private static PortletDataHandlerBoolean _questions =
229                    new PortletDataHandlerBoolean(_NAMESPACE, "questions", true, true);
230    
231            private static PortletDataHandlerBoolean _votes =
232                    new PortletDataHandlerBoolean(_NAMESPACE, "votes");
233    
234    }