1
22
23 package com.liferay.portlet.polls.lar;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.MapUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.kernel.xml.Document;
32 import com.liferay.portal.kernel.xml.Element;
33 import com.liferay.portal.kernel.xml.SAXReaderUtil;
34 import com.liferay.portal.lar.BasePortletDataHandler;
35 import com.liferay.portal.lar.PortletDataContext;
36 import com.liferay.portal.lar.PortletDataException;
37 import com.liferay.portal.lar.PortletDataHandlerBoolean;
38 import com.liferay.portal.lar.PortletDataHandlerControl;
39 import com.liferay.portlet.polls.NoSuchQuestionException;
40 import com.liferay.portlet.polls.model.PollsChoice;
41 import com.liferay.portlet.polls.model.PollsQuestion;
42 import com.liferay.portlet.polls.model.PollsVote;
43 import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
44
45 import java.util.List;
46 import java.util.Map;
47
48 import javax.portlet.PortletPreferences;
49
50
56 public class PollsDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
57
58 public PortletPreferences deleteData(
59 PortletDataContext context, String portletId,
60 PortletPreferences preferences)
61 throws PortletDataException {
62
63 try {
64 preferences.setValue("question-id", StringPool.BLANK);
65
66 return preferences;
67 }
68 catch (Exception e) {
69 throw new PortletDataException(e);
70 }
71 }
72
73 public String exportData(
74 PortletDataContext context, String portletId,
75 PortletPreferences preferences)
76 throws PortletDataException {
77
78 try {
79 long questionId = GetterUtil.getLong(
80 preferences.getValue("question-id", StringPool.BLANK));
81
82 if (questionId <= 0) {
83 if (_log.isWarnEnabled()) {
84 _log.warn(
85 "No question id found in preferences of portlet " +
86 portletId);
87 }
88
89 return StringPool.BLANK;
90 }
91
92 PollsQuestion question = null;
93
94 try {
95 question = PollsQuestionUtil.findByPrimaryKey(questionId);
96 }
97 catch (NoSuchQuestionException nsqe) {
98 if (_log.isWarnEnabled()) {
99 _log.warn(nsqe);
100 }
101 }
102
103 if (question == null) {
104 return StringPool.BLANK;
105 }
106
107 Document doc = SAXReaderUtil.createDocument();
108
109 Element root = doc.addElement("polls-display-data");
110
111 root.addAttribute("group-id", String.valueOf(context.getGroupId()));
112
113 Element questionsEl = root.addElement("questions");
114 Element choicesEl = root.addElement("choices");
115 Element votesEl = root.addElement("votes");
116
117 PollsPortletDataHandlerImpl.exportQuestion(
118 context, questionsEl, choicesEl, votesEl, question);
119
120 return doc.formattedString();
121 }
122 catch (Exception e) {
123 throw new PortletDataException(e);
124 }
125 }
126
127 public PortletDataHandlerControl[] getExportControls() {
128 return new PortletDataHandlerControl[] {_questions, _votes};
129 }
130
131 public PortletDataHandlerControl[] getImportControls() {
132 return new PortletDataHandlerControl[] {_questions, _votes};
133 }
134
135 public PortletPreferences importData(
136 PortletDataContext context, String portletId,
137 PortletPreferences preferences, String data)
138 throws PortletDataException {
139
140 try {
141 if (Validator.isNull(data)) {
142 return null;
143 }
144
145 Document doc = SAXReaderUtil.read(data);
146
147 Element root = doc.getRootElement();
148
149 List<Element> questionEls =
150 root.element("questions").elements("question");
151
152 Map<Long, Long> questionPKs =
153 (Map<Long, Long>)context.getNewPrimaryKeysMap(
154 PollsQuestion.class);
155
156 for (Element questionEl : questionEls) {
157 String path = questionEl.attributeValue("path");
158
159 if (!context.isPathNotProcessed(path)) {
160 continue;
161 }
162
163 PollsQuestion question =
164 (PollsQuestion)context.getZipEntryAsObject(path);
165
166 PollsPortletDataHandlerImpl.importQuestion(
167 context, questionPKs, question);
168 }
169
170 List<Element> choiceEls = root.element("choices").elements(
171 "choice");
172
173 Map<Long, Long> choicePKs =
174 (Map<Long, Long>)context.getNewPrimaryKeysMap(
175 PollsChoice.class);
176
177 for (Element choiceEl : choiceEls) {
178 String path = choiceEl.attributeValue("path");
179
180 if (!context.isPathNotProcessed(path)) {
181 continue;
182 }
183
184 PollsChoice choice = (PollsChoice)context.getZipEntryAsObject(
185 path);
186
187 PollsPortletDataHandlerImpl.importChoice(
188 context, questionPKs, choicePKs, choice);
189 }
190
191 if (context.getBooleanParameter(_NAMESPACE, "votes")) {
192 List<Element> voteEls = root.element("votes").elements("vote");
193
194 for (Element voteEl : voteEls) {
195 String path = voteEl.attributeValue("path");
196
197 if (!context.isPathNotProcessed(path)) {
198 continue;
199 }
200
201 PollsVote vote = (PollsVote)context.getZipEntryAsObject(
202 path);
203
204 PollsPortletDataHandlerImpl.importVote(
205 context, questionPKs, choicePKs, vote);
206 }
207 }
208
209 long questionId = GetterUtil.getLong(
210 preferences.getValue("question-id", StringPool.BLANK));
211
212 if (questionId > 0) {
213 questionId = MapUtil.getLong(
214 questionPKs, questionId, questionId);
215
216 preferences.setValue("question-id", String.valueOf(questionId));
217 }
218
219 return preferences;
220 }
221 catch (Exception e) {
222 throw new PortletDataException(e);
223 }
224 }
225
226 private static final String _NAMESPACE = "polls";
227
228 private static final PortletDataHandlerBoolean _questions =
229 new PortletDataHandlerBoolean(_NAMESPACE, "questions", true, true);
230
231 private static final PortletDataHandlerBoolean _votes =
232 new PortletDataHandlerBoolean(_NAMESPACE, "votes");
233
234 private static Log _log =
235 LogFactoryUtil.getLog(PollsDisplayPortletDataHandlerImpl.class);
236
237 }