1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.polls.lar;
16  
17  import com.liferay.portal.kernel.lar.BasePortletDataHandler;
18  import com.liferay.portal.kernel.lar.PortletDataContext;
19  import com.liferay.portal.kernel.lar.PortletDataException;
20  import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
21  import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.MapUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
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  /**
43   * <a href="PollsDisplayPortletDataHandlerImpl.java.html"><b><i>View Source</i>
44   * </b></a>
45   *
46   * @author Marcellus Tavares
47   */
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.getGroupId());
101 
102             Document doc = SAXReaderUtil.createDocument();
103 
104             Element root = doc.addElement("polls-display-data");
105 
106             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
107 
108             Element questionsEl = root.addElement("questions");
109             Element choicesEl = root.addElement("choices");
110             Element votesEl = root.addElement("votes");
111 
112             PollsPortletDataHandlerImpl.exportQuestion(
113                 context, questionsEl, choicesEl, votesEl, question);
114 
115             return doc.formattedString();
116         }
117         catch (Exception e) {
118             throw new PortletDataException(e);
119         }
120     }
121 
122     public PortletDataHandlerControl[] getExportControls() {
123         return new PortletDataHandlerControl[] {_questions, _votes};
124     }
125 
126     public PortletDataHandlerControl[] getImportControls() {
127         return new PortletDataHandlerControl[] {_questions, _votes};
128     }
129 
130     public PortletPreferences importData(
131             PortletDataContext context, String portletId,
132             PortletPreferences preferences, String data)
133         throws PortletDataException {
134 
135         try {
136             context.importPermissions(
137                 "com.liferay.portlet.polls", context.getSourceGroupId(),
138                 context.getGroupId());
139 
140             if (Validator.isNull(data)) {
141                 return null;
142             }
143 
144             Document doc = SAXReaderUtil.read(data);
145 
146             Element root = doc.getRootElement();
147 
148             List<Element> questionEls =
149                 root.element("questions").elements("question");
150 
151             Map<Long, Long> questionPKs =
152                 (Map<Long, Long>)context.getNewPrimaryKeysMap(
153                     PollsQuestion.class);
154 
155             for (Element questionEl : questionEls) {
156                 String path = questionEl.attributeValue("path");
157 
158                 if (!context.isPathNotProcessed(path)) {
159                     continue;
160                 }
161 
162                 PollsQuestion question =
163                     (PollsQuestion)context.getZipEntryAsObject(path);
164 
165                 PollsPortletDataHandlerImpl.importQuestion(
166                     context, questionPKs, question);
167             }
168 
169             List<Element> choiceEls = root.element("choices").elements(
170                 "choice");
171 
172             Map<Long, Long> choicePKs =
173                 (Map<Long, Long>)context.getNewPrimaryKeysMap(
174                     PollsChoice.class);
175 
176             for (Element choiceEl : choiceEls) {
177                 String path = choiceEl.attributeValue("path");
178 
179                 if (!context.isPathNotProcessed(path)) {
180                     continue;
181                 }
182 
183                 PollsChoice choice = (PollsChoice)context.getZipEntryAsObject(
184                     path);
185 
186                 PollsPortletDataHandlerImpl.importChoice(
187                     context, questionPKs, choicePKs, choice);
188             }
189 
190             if (context.getBooleanParameter(_NAMESPACE, "votes")) {
191                 List<Element> voteEls = root.element("votes").elements("vote");
192 
193                 for (Element voteEl : voteEls) {
194                     String path = voteEl.attributeValue("path");
195 
196                     if (!context.isPathNotProcessed(path)) {
197                         continue;
198                     }
199 
200                     PollsVote vote = (PollsVote)context.getZipEntryAsObject(
201                         path);
202 
203                     PollsPortletDataHandlerImpl.importVote(
204                         context, questionPKs, choicePKs, vote);
205                 }
206             }
207 
208             long questionId = GetterUtil.getLong(
209                 preferences.getValue("question-id", StringPool.BLANK));
210 
211             if (questionId > 0) {
212                 questionId = MapUtil.getLong(
213                     questionPKs, questionId, questionId);
214 
215                 preferences.setValue("question-id", String.valueOf(questionId));
216             }
217 
218             return preferences;
219         }
220         catch (Exception e) {
221             throw new PortletDataException(e);
222         }
223     }
224 
225     private static final String _NAMESPACE = "polls";
226 
227     private static Log _log = LogFactoryUtil.getLog(
228         PollsDisplayPortletDataHandlerImpl.class);
229 
230     private static PortletDataHandlerBoolean _questions =
231         new PortletDataHandlerBoolean(_NAMESPACE, "questions", true, true);
232 
233     private static PortletDataHandlerBoolean _votes =
234         new PortletDataHandlerBoolean(_NAMESPACE, "votes");
235 
236 }