001
014
015 package com.liferay.portlet.polls.lar;
016
017 import com.liferay.portal.kernel.lar.PortletDataContext;
018 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.MapUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.xml.Element;
025 import com.liferay.portlet.polls.NoSuchQuestionException;
026 import com.liferay.portlet.polls.model.PollsChoice;
027 import com.liferay.portlet.polls.model.PollsQuestion;
028 import com.liferay.portlet.polls.model.PollsVote;
029 import com.liferay.portlet.polls.service.persistence.PollsQuestionUtil;
030
031 import java.util.List;
032 import java.util.Map;
033
034 import javax.portlet.PortletPreferences;
035
036
039 public class PollsDisplayPortletDataHandler extends PollsPortletDataHandler {
040
041 public PollsDisplayPortletDataHandler() {
042 setDataPortletPreferences("questionId");
043 setPublishToLiveByDefault(true);
044 }
045
046 @Override
047 protected PortletPreferences doDeleteData(
048 PortletDataContext portletDataContext, String portletId,
049 PortletPreferences portletPreferences)
050 throws Exception {
051
052 if (portletPreferences == null) {
053 return portletPreferences;
054 }
055
056 portletPreferences.setValue("questionId", StringPool.BLANK);
057
058 return portletPreferences;
059 }
060
061 @Override
062 protected String doExportData(
063 PortletDataContext portletDataContext, String portletId,
064 PortletPreferences portletPreferences)
065 throws Exception {
066
067 long questionId = GetterUtil.getLong(
068 portletPreferences.getValue("questionId", StringPool.BLANK));
069
070 if (questionId <= 0) {
071 if (_log.isWarnEnabled()) {
072 _log.warn(
073 "No question id found in preferences of portlet " +
074 portletId);
075 }
076
077 return StringPool.BLANK;
078 }
079
080 PollsQuestion question = null;
081
082 try {
083 question = PollsQuestionUtil.findByPrimaryKey(questionId);
084 }
085 catch (NoSuchQuestionException nsqe) {
086 if (_log.isWarnEnabled()) {
087 _log.warn(nsqe, nsqe);
088 }
089
090 return StringPool.BLANK;
091 }
092
093 portletDataContext.addPermissions(
094 "com.liferay.portlet.polls", portletDataContext.getScopeGroupId());
095
096 Element rootElement = addExportDataRootElement(portletDataContext);
097
098 rootElement.addAttribute(
099 "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
100
101 StagedModelDataHandlerUtil.exportStagedModel(
102 portletDataContext, question);
103
104 return getExportDataRootElementString(rootElement);
105 }
106
107 @Override
108 protected PortletPreferences doImportData(
109 PortletDataContext portletDataContext, String portletId,
110 PortletPreferences portletPreferences, String data)
111 throws Exception {
112
113 portletDataContext.importPermissions(
114 "com.liferay.portlet.polls", portletDataContext.getSourceGroupId(),
115 portletDataContext.getScopeGroupId());
116
117 Element questionsElement = portletDataContext.getImportDataGroupElement(
118 PollsQuestion.class);
119
120 List<Element> questionElements = questionsElement.elements();
121
122 for (Element questionElement : questionElements) {
123 StagedModelDataHandlerUtil.importStagedModel(
124 portletDataContext, questionElement);
125 }
126
127 Element choicesElement = portletDataContext.getImportDataGroupElement(
128 PollsChoice.class);
129
130 List<Element> choiceElements = choicesElement.elements();
131
132 for (Element choiceElement : choiceElements) {
133 StagedModelDataHandlerUtil.importStagedModel(
134 portletDataContext, choiceElement);
135 }
136
137 if (portletDataContext.getBooleanParameter(
138 PollsPortletDataHandler.NAMESPACE, "votes")) {
139
140 Element votesElement = portletDataContext.getImportDataGroupElement(
141 PollsVote.class);
142
143 List<Element> voteElements = votesElement.elements();
144
145 for (Element voteElement : voteElements) {
146 StagedModelDataHandlerUtil.importStagedModel(
147 portletDataContext, voteElement);
148 }
149 }
150
151 long questionId = GetterUtil.getLong(
152 portletPreferences.getValue("questionId", StringPool.BLANK));
153
154 if (questionId > 0) {
155 Map<Long, Long> questionIds =
156 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
157 PollsQuestion.class);
158
159 questionId = MapUtil.getLong(questionIds, questionId, questionId);
160
161 portletPreferences.setValue(
162 "questionId", String.valueOf(questionId));
163 }
164
165 return portletPreferences;
166 }
167
168 private static Log _log = LogFactoryUtil.getLog(
169 PollsDisplayPortletDataHandler.class);
170
171 }