001
014
015 package com.liferay.portlet.polls.lar;
016
017 import com.liferay.portal.kernel.lar.DataLevel;
018 import com.liferay.portal.kernel.lar.ManifestSummary;
019 import com.liferay.portal.kernel.lar.PortletDataContext;
020 import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
021 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
022 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023 import com.liferay.portal.kernel.lar.StagedModelType;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.MapUtil;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.xml.Element;
030 import com.liferay.portal.util.PropsValues;
031 import com.liferay.portlet.polls.NoSuchQuestionException;
032 import com.liferay.portlet.polls.model.PollsChoice;
033 import com.liferay.portlet.polls.model.PollsQuestion;
034 import com.liferay.portlet.polls.model.PollsVote;
035 import com.liferay.portlet.polls.service.permission.PollsPermission;
036 import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
037
038 import java.util.List;
039 import java.util.Map;
040
041 import javax.portlet.PortletPreferences;
042
043
046 public class PollsDisplayPortletDataHandler extends PollsPortletDataHandler {
047
048 public PollsDisplayPortletDataHandler() {
049 setDataLevel(DataLevel.PORTLET_INSTANCE);
050 setDataPortletPreferences("questionId");
051 setExportControls(
052 new PortletDataHandlerBoolean(
053 NAMESPACE, "selected-question", true, true,
054 new PortletDataHandlerControl[] {
055 new PortletDataHandlerBoolean(
056 NAMESPACE, "votes", true, false, null,
057 PollsVote.class.getName())
058 },
059 PollsQuestion.class.getName()));
060 setPublishToLiveByDefault(PropsValues.POLLS_PUBLISH_TO_LIVE_BY_DEFAULT);
061 }
062
063 @Override
064 protected PortletPreferences doDeleteData(
065 PortletDataContext portletDataContext, String portletId,
066 PortletPreferences portletPreferences)
067 throws Exception {
068
069 if (portletPreferences == null) {
070 return portletPreferences;
071 }
072
073 portletPreferences.setValue("questionId", StringPool.BLANK);
074
075 return portletPreferences;
076 }
077
078 @Override
079 protected String doExportData(
080 PortletDataContext portletDataContext, String portletId,
081 PortletPreferences portletPreferences)
082 throws Exception {
083
084 long questionId = GetterUtil.getLong(
085 portletPreferences.getValue("questionId", StringPool.BLANK));
086
087 if (questionId <= 0) {
088 if (_log.isWarnEnabled()) {
089 _log.warn(
090 "No question id found in preferences of portlet " +
091 portletId);
092 }
093
094 return StringPool.BLANK;
095 }
096
097 PollsQuestion question = null;
098
099 try {
100 question = PollsQuestionUtil.findByPrimaryKey(questionId);
101 }
102 catch (NoSuchQuestionException nsqe) {
103 if (_log.isWarnEnabled()) {
104 _log.warn(nsqe, nsqe);
105 }
106
107 return StringPool.BLANK;
108 }
109
110 portletDataContext.addPermissions(
111 PollsPermission.RESOURCE_NAME,
112 portletDataContext.getScopeGroupId());
113
114 Element rootElement = addExportDataRootElement(portletDataContext);
115
116 rootElement.addAttribute(
117 "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
118
119 StagedModelDataHandlerUtil.exportReferenceStagedModel(
120 portletDataContext, portletId, question);
121
122 for (PollsChoice choice : question.getChoices()) {
123 StagedModelDataHandlerUtil.exportReferenceStagedModel(
124 portletDataContext, portletId, choice);
125 }
126
127 if (portletDataContext.getBooleanParameter(
128 PollsPortletDataHandler.NAMESPACE, "votes")) {
129
130 for (PollsVote vote : question.getVotes()) {
131 StagedModelDataHandlerUtil.exportReferenceStagedModel(
132 portletDataContext, portletId, vote);
133 }
134 }
135
136 return getExportDataRootElementString(rootElement);
137 }
138
139 @Override
140 protected PortletPreferences doImportData(
141 PortletDataContext portletDataContext, String portletId,
142 PortletPreferences portletPreferences, String data)
143 throws Exception {
144
145 portletDataContext.importPermissions(
146 PollsPermission.RESOURCE_NAME,
147 portletDataContext.getSourceGroupId(),
148 portletDataContext.getScopeGroupId());
149
150 Element questionsElement = portletDataContext.getImportDataGroupElement(
151 PollsQuestion.class);
152
153 List<Element> questionElements = questionsElement.elements();
154
155 for (Element questionElement : questionElements) {
156 StagedModelDataHandlerUtil.importStagedModel(
157 portletDataContext, questionElement);
158 }
159
160 Element choicesElement = portletDataContext.getImportDataGroupElement(
161 PollsChoice.class);
162
163 List<Element> choiceElements = choicesElement.elements();
164
165 for (Element choiceElement : choiceElements) {
166 StagedModelDataHandlerUtil.importStagedModel(
167 portletDataContext, choiceElement);
168 }
169
170 if (portletDataContext.getBooleanParameter(
171 PollsPortletDataHandler.NAMESPACE, "votes")) {
172
173 Element votesElement = portletDataContext.getImportDataGroupElement(
174 PollsVote.class);
175
176 List<Element> voteElements = votesElement.elements();
177
178 for (Element voteElement : voteElements) {
179 StagedModelDataHandlerUtil.importStagedModel(
180 portletDataContext, voteElement);
181 }
182 }
183
184 long questionId = GetterUtil.getLong(
185 portletPreferences.getValue("questionId", StringPool.BLANK));
186
187 if (questionId > 0) {
188 Map<Long, Long> questionIds =
189 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
190 PollsQuestion.class);
191
192 questionId = MapUtil.getLong(questionIds, questionId, questionId);
193
194 portletPreferences.setValue(
195 "questionId", String.valueOf(questionId));
196 }
197
198 return portletPreferences;
199 }
200
201 @Override
202 protected void doPrepareManifestSummary(
203 PortletDataContext portletDataContext,
204 PortletPreferences portletPreferences) throws Exception {
205
206 ManifestSummary manifestSummary =
207 portletDataContext.getManifestSummary();
208
209 if ((portletPreferences == null) ||
210 (manifestSummary.getModelAdditionCount(PollsQuestion.class) > -1)) {
211
212 return;
213 }
214
215 long questionId = GetterUtil.getLong(
216 portletPreferences.getValue("questionId", StringPool.BLANK));
217
218 if (questionId > 0) {
219 manifestSummary.addModelAdditionCount(
220 new StagedModelType(PollsQuestion.class), 1);
221 }
222 }
223
224 private static Log _log = LogFactoryUtil.getLog(
225 PollsDisplayPortletDataHandler.class);
226
227 }