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.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  /**
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              Document doc = SAXReaderUtil.createDocument();
100 
101             Element root = doc.addElement("polls-display-data");
102 
103             root.addAttribute("group-id", String.valueOf(context.getGroupId()));
104 
105             Element questionsEl = root.addElement("questions");
106             Element choicesEl = root.addElement("choices");
107             Element votesEl = root.addElement("votes");
108 
109             PollsPortletDataHandlerImpl.exportQuestion(
110                 context, questionsEl, choicesEl, votesEl, question);
111 
112             return doc.formattedString();
113         }
114         catch (Exception e) {
115             throw new PortletDataException(e);
116         }
117     }
118 
119     public PortletDataHandlerControl[] getExportControls() {
120         return new PortletDataHandlerControl[] {_questions, _votes};
121     }
122 
123     public PortletDataHandlerControl[] getImportControls() {
124         return new PortletDataHandlerControl[] {_questions, _votes};
125     }
126 
127     public PortletPreferences importData(
128             PortletDataContext context, String portletId,
129             PortletPreferences preferences, String data)
130         throws PortletDataException {
131 
132         try {
133             if (Validator.isNull(data)) {
134                 return null;
135             }
136 
137             Document doc = SAXReaderUtil.read(data);
138 
139             Element root = doc.getRootElement();
140 
141             List<Element> questionEls =
142                 root.element("questions").elements("question");
143 
144             Map<Long, Long> questionPKs =
145                 (Map<Long, Long>)context.getNewPrimaryKeysMap(
146                     PollsQuestion.class);
147 
148             for (Element questionEl : questionEls) {
149                 String path = questionEl.attributeValue("path");
150 
151                 if (!context.isPathNotProcessed(path)) {
152                     continue;
153                 }
154 
155                 PollsQuestion question =
156                     (PollsQuestion)context.getZipEntryAsObject(path);
157 
158                 PollsPortletDataHandlerImpl.importQuestion(
159                     context, questionPKs, question);
160             }
161 
162             List<Element> choiceEls = root.element("choices").elements(
163                 "choice");
164 
165             Map<Long, Long> choicePKs =
166                 (Map<Long, Long>)context.getNewPrimaryKeysMap(
167                     PollsChoice.class);
168 
169             for (Element choiceEl : choiceEls) {
170                 String path = choiceEl.attributeValue("path");
171 
172                 if (!context.isPathNotProcessed(path)) {
173                     continue;
174                 }
175 
176                 PollsChoice choice = (PollsChoice)context.getZipEntryAsObject(
177                     path);
178 
179                 PollsPortletDataHandlerImpl.importChoice(
180                     context, questionPKs, choicePKs, choice);
181             }
182 
183             if (context.getBooleanParameter(_NAMESPACE, "votes")) {
184                 List<Element> voteEls = root.element("votes").elements("vote");
185 
186                 for (Element voteEl : voteEls) {
187                     String path = voteEl.attributeValue("path");
188 
189                     if (!context.isPathNotProcessed(path)) {
190                         continue;
191                     }
192 
193                     PollsVote vote = (PollsVote)context.getZipEntryAsObject(
194                         path);
195 
196                     PollsPortletDataHandlerImpl.importVote(
197                         context, questionPKs, choicePKs, vote);
198                 }
199             }
200 
201             long questionId = GetterUtil.getLong(
202                 preferences.getValue("question-id", StringPool.BLANK));
203 
204             if (questionId > 0) {
205                 questionId = MapUtil.getLong(
206                     questionPKs, questionId, questionId);
207 
208                 preferences.setValue("question-id", String.valueOf(questionId));
209             }
210 
211             return preferences;
212         }
213         catch (Exception e) {
214             throw new PortletDataException(e);
215         }
216     }
217 
218     private static final String _NAMESPACE = "polls";
219 
220     private static final PortletDataHandlerBoolean _questions =
221         new PortletDataHandlerBoolean(_NAMESPACE, "questions", true, true);
222 
223     private static final PortletDataHandlerBoolean _votes =
224         new PortletDataHandlerBoolean(_NAMESPACE, "votes");
225 
226     private static Log _log = LogFactoryUtil.getLog(
227         PollsDisplayPortletDataHandlerImpl.class);
228 
229 }