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 public PortletDataHandlerControl[] getExportControls() {
046 return new PortletDataHandlerControl[] {_questions, _votes};
047 }
048
049 public PortletDataHandlerControl[] getImportControls() {
050 return new PortletDataHandlerControl[] {_questions, _votes};
051 }
052
053 public boolean isPublishToLiveByDefault() {
054 return _PUBLISH_TO_LIVE_BY_DEFAULT;
055 }
056
057 protected PortletPreferences doDeleteData(
058 PortletDataContext context, String portletId,
059 PortletPreferences preferences)
060 throws Exception {
061
062 preferences.setValue("question-id", StringPool.BLANK);
063
064 return preferences;
065 }
066
067 protected String doExportData(
068 PortletDataContext context, String portletId,
069 PortletPreferences preferences)
070 throws Exception {
071
072 long questionId = GetterUtil.getLong(
073 preferences.getValue("question-id", StringPool.BLANK));
074
075 if (questionId <= 0) {
076 if (_log.isWarnEnabled()) {
077 _log.warn(
078 "No question id found in preferences of portlet " +
079 portletId);
080 }
081
082 return StringPool.BLANK;
083 }
084
085 PollsQuestion question = null;
086
087 try {
088 question = PollsQuestionUtil.findByPrimaryKey(questionId);
089 }
090 catch (NoSuchQuestionException nsqe) {
091 if (_log.isWarnEnabled()) {
092 _log.warn(nsqe, nsqe);
093 }
094
095 return StringPool.BLANK;
096 }
097
098 context.addPermissions(
099 "com.liferay.portlet.polls", context.getScopeGroupId());
100
101 Document document = SAXReaderUtil.createDocument();
102
103 Element rootElement = document.addElement("polls-display-data");
104
105 rootElement.addAttribute(
106 "group-id", String.valueOf(context.getScopeGroupId()));
107
108 Element questionsElement = rootElement.addElement("questions");
109 Element choicesElement = rootElement.addElement("choices");
110 Element votesElement = rootElement.addElement("votes");
111
112 PollsPortletDataHandlerImpl.exportQuestion(
113 context, questionsElement, choicesElement, votesElement, question);
114
115 return document.formattedString();
116 }
117
118 protected PortletPreferences doImportData(
119 PortletDataContext context, String portletId,
120 PortletPreferences preferences, String data)
121 throws Exception {
122
123 context.importPermissions(
124 "com.liferay.portlet.polls", context.getSourceGroupId(),
125 context.getScopeGroupId());
126
127 if (Validator.isNull(data)) {
128 return null;
129 }
130
131 Document document = SAXReaderUtil.read(data);
132
133 Element rootElement = document.getRootElement();
134
135 Element questionsElement = rootElement.element("questions");
136
137 for (Element questionEl : questionsElement.elements("question")) {
138 String path = questionEl.attributeValue("path");
139
140 if (!context.isPathNotProcessed(path)) {
141 continue;
142 }
143
144 PollsQuestion question = (PollsQuestion)context.getZipEntryAsObject(
145 path);
146
147 PollsPortletDataHandlerImpl.importQuestion(context, question);
148 }
149
150 Element choicesElement = rootElement.element("choices");
151
152 for (Element choiceElement : choicesElement.elements("choice")) {
153 String path = choiceElement.attributeValue("path");
154
155 if (!context.isPathNotProcessed(path)) {
156 continue;
157 }
158
159 PollsChoice choice = (PollsChoice)context.getZipEntryAsObject(path);
160
161 PollsPortletDataHandlerImpl.importChoice(context, choice);
162 }
163
164 if (context.getBooleanParameter(_NAMESPACE, "votes")) {
165 Element votesElement = rootElement.element("votes");
166
167 for (Element voteElement : votesElement.elements("vote")) {
168 String path = voteElement.attributeValue("path");
169
170 if (!context.isPathNotProcessed(path)) {
171 continue;
172 }
173
174 PollsVote vote = (PollsVote)context.getZipEntryAsObject(path);
175
176 PollsPortletDataHandlerImpl.importVote(context, vote);
177 }
178 }
179
180 long questionId = GetterUtil.getLong(
181 preferences.getValue("question-id", StringPool.BLANK));
182
183 if (questionId > 0) {
184 Map<Long, Long> questionPKs =
185 (Map<Long, Long>)context.getNewPrimaryKeysMap(
186 PollsQuestion.class);
187
188 questionId = MapUtil.getLong(
189 questionPKs, questionId, questionId);
190
191 preferences.setValue("question-id", String.valueOf(questionId));
192 }
193
194 return preferences;
195 }
196
197 private static final String _NAMESPACE = "polls";
198
199 private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
200
201 private static Log _log = LogFactoryUtil.getLog(
202 PollsDisplayPortletDataHandlerImpl.class);
203
204 private static PortletDataHandlerBoolean _questions =
205 new PortletDataHandlerBoolean(_NAMESPACE, "questions", true, true);
206
207 private static PortletDataHandlerBoolean _votes =
208 new PortletDataHandlerBoolean(_NAMESPACE, "votes");
209
210 }