001
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
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 if (portletPreferences == null) {
077 return portletPreferences;
078 }
079
080 portletPreferences.setValue("questionId", StringPool.BLANK);
081
082 return portletPreferences;
083 }
084
085 @Override
086 protected String doExportData(
087 PortletDataContext portletDataContext, String portletId,
088 PortletPreferences portletPreferences)
089 throws Exception {
090
091 long questionId = GetterUtil.getLong(
092 portletPreferences.getValue("questionId", StringPool.BLANK));
093
094 if (questionId <= 0) {
095 if (_log.isWarnEnabled()) {
096 _log.warn(
097 "No question id found in preferences of portlet " +
098 portletId);
099 }
100
101 return StringPool.BLANK;
102 }
103
104 PollsQuestion question = null;
105
106 try {
107 question = PollsQuestionUtil.findByPrimaryKey(questionId);
108 }
109 catch (NoSuchQuestionException nsqe) {
110 if (_log.isWarnEnabled()) {
111 _log.warn(nsqe, nsqe);
112 }
113
114 return StringPool.BLANK;
115 }
116
117 portletDataContext.addPermissions(
118 "com.liferay.portlet.polls", portletDataContext.getScopeGroupId());
119
120 Document document = SAXReaderUtil.createDocument();
121
122 Element rootElement = document.addElement("polls-display-data");
123
124 rootElement.addAttribute(
125 "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
126
127 Element questionsElement = rootElement.addElement("questions");
128 Element choicesElement = rootElement.addElement("choices");
129 Element votesElement = rootElement.addElement("votes");
130
131 PollsPortletDataHandlerImpl.exportQuestion(
132 portletDataContext, questionsElement, choicesElement, votesElement,
133 question);
134
135 return document.formattedString();
136 }
137
138 @Override
139 protected PortletPreferences doImportData(
140 PortletDataContext portletDataContext, String portletId,
141 PortletPreferences portletPreferences, String data)
142 throws Exception {
143
144 portletDataContext.importPermissions(
145 "com.liferay.portlet.polls", portletDataContext.getSourceGroupId(),
146 portletDataContext.getScopeGroupId());
147
148 if (Validator.isNull(data)) {
149 return null;
150 }
151
152 Document document = SAXReaderUtil.read(data);
153
154 Element rootElement = document.getRootElement();
155
156 Element questionsElement = rootElement.element("questions");
157
158 for (Element questionElement : questionsElement.elements("question")) {
159 String path = questionElement.attributeValue("path");
160
161 if (!portletDataContext.isPathNotProcessed(path)) {
162 continue;
163 }
164
165 PollsQuestion question =
166 (PollsQuestion)portletDataContext.getZipEntryAsObject(path);
167
168 PollsPortletDataHandlerImpl.importQuestion(
169 portletDataContext, questionElement, question);
170 }
171
172 Element choicesElement = rootElement.element("choices");
173
174 for (Element choiceElement : choicesElement.elements("choice")) {
175 String path = choiceElement.attributeValue("path");
176
177 if (!portletDataContext.isPathNotProcessed(path)) {
178 continue;
179 }
180
181 PollsChoice choice =
182 (PollsChoice)portletDataContext.getZipEntryAsObject(path);
183
184 PollsPortletDataHandlerImpl.importChoice(
185 portletDataContext, choice);
186 }
187
188 if (portletDataContext.getBooleanParameter(_NAMESPACE, "votes")) {
189 Element votesElement = rootElement.element("votes");
190
191 for (Element voteElement : votesElement.elements("vote")) {
192 String path = voteElement.attributeValue("path");
193
194 if (!portletDataContext.isPathNotProcessed(path)) {
195 continue;
196 }
197
198 PollsVote vote =
199 (PollsVote)portletDataContext.getZipEntryAsObject(path);
200
201 PollsPortletDataHandlerImpl.importVote(
202 portletDataContext, vote);
203 }
204 }
205
206 long questionId = GetterUtil.getLong(
207 portletPreferences.getValue("questionId", StringPool.BLANK));
208
209 if (questionId > 0) {
210 Map<Long, Long> questionIds =
211 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
212 PollsQuestion.class);
213
214 questionId = MapUtil.getLong(questionIds, questionId, questionId);
215
216 portletPreferences.setValue(
217 "questionId", String.valueOf(questionId));
218 }
219
220 return portletPreferences;
221 }
222
223 private static final boolean _DATA_LOCALIZED = true;
224
225 private static final String _NAMESPACE = "polls";
226
227 private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
228
229 private static Log _log = LogFactoryUtil.getLog(
230 PollsDisplayPortletDataHandlerImpl.class);
231
232 private static PortletDataHandlerBoolean _questions =
233 new PortletDataHandlerBoolean(_NAMESPACE, "questions", true, true);
234 private static PortletDataHandlerBoolean _votes =
235 new PortletDataHandlerBoolean(_NAMESPACE, "votes");
236
237 }