1
14
15 package com.liferay.portlet.polls.lar;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.util.GetterUtil;
20 import com.liferay.portal.kernel.util.MapUtil;
21 import com.liferay.portal.kernel.util.StringPool;
22 import com.liferay.portal.kernel.util.Validator;
23 import com.liferay.portal.kernel.xml.Document;
24 import com.liferay.portal.kernel.xml.Element;
25 import com.liferay.portal.kernel.xml.SAXReaderUtil;
26 import com.liferay.portal.lar.BasePortletDataHandler;
27 import com.liferay.portal.lar.PortletDataContext;
28 import com.liferay.portal.lar.PortletDataException;
29 import com.liferay.portal.lar.PortletDataHandlerBoolean;
30 import com.liferay.portal.lar.PortletDataHandlerControl;
31 import com.liferay.portlet.polls.NoSuchQuestionException;
32 import com.liferay.portlet.polls.model.PollsChoice;
33 import com.liferay.portlet.polls.model.PollsQuestion;
34 import com.liferay.portlet.polls.model.PollsVote;
35 import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
36
37 import java.util.List;
38 import java.util.Map;
39
40 import javax.portlet.PortletPreferences;
41
42
48 public class PollsDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
49
50 public PortletPreferences deleteData(
51 PortletDataContext context, String portletId,
52 PortletPreferences preferences)
53 throws PortletDataException {
54
55 try {
56 preferences.setValue("question-id", StringPool.BLANK);
57
58 return preferences;
59 }
60 catch (Exception e) {
61 throw new PortletDataException(e);
62 }
63 }
64
65 public String exportData(
66 PortletDataContext context, String portletId,
67 PortletPreferences preferences)
68 throws PortletDataException {
69
70 try {
71 long questionId = GetterUtil.getLong(
72 preferences.getValue("question-id", StringPool.BLANK));
73
74 if (questionId <= 0) {
75 if (_log.isWarnEnabled()) {
76 _log.warn(
77 "No question id found in preferences of portlet " +
78 portletId);
79 }
80
81 return StringPool.BLANK;
82 }
83
84 PollsQuestion question = null;
85
86 try {
87 question = PollsQuestionUtil.findByPrimaryKey(questionId);
88 }
89 catch (NoSuchQuestionException nsqe) {
90 if (_log.isWarnEnabled()) {
91 _log.warn(nsqe);
92 }
93 }
94
95 if (question == null) {
96 return StringPool.BLANK;
97 }
98
99 context.addPermissions(
100 "com.liferay.portlet.polls", context.getScopeGroupId());
101
102 Document doc = SAXReaderUtil.createDocument();
103
104 Element root = doc.addElement("polls-display-data");
105
106 root.addAttribute(
107 "group-id", String.valueOf(context.getScopeGroupId()));
108
109 Element questionsEl = root.addElement("questions");
110 Element choicesEl = root.addElement("choices");
111 Element votesEl = root.addElement("votes");
112
113 PollsPortletDataHandlerImpl.exportQuestion(
114 context, questionsEl, choicesEl, votesEl, question);
115
116 return doc.formattedString();
117 }
118 catch (Exception e) {
119 throw new PortletDataException(e);
120 }
121 }
122
123 public PortletDataHandlerControl[] getExportControls() {
124 return new PortletDataHandlerControl[] {_questions, _votes};
125 }
126
127 public PortletDataHandlerControl[] getImportControls() {
128 return new PortletDataHandlerControl[] {_questions, _votes};
129 }
130
131 public PortletPreferences importData(
132 PortletDataContext context, String portletId,
133 PortletPreferences preferences, String data)
134 throws PortletDataException {
135
136 try {
137 context.importPermissions(
138 "com.liferay.portlet.polls", context.getSourceGroupId(),
139 context.getScopeGroupId());
140
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 = LogFactoryUtil.getLog(
235 PollsDisplayPortletDataHandlerImpl.class);
236
237 }