@Override protected String doExportData( PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences) throws Exception { long questionId = GetterUtil.getLong(portletPreferences.getValue("questionId", StringPool.BLANK)); if (questionId <= 0) { if (_log.isWarnEnabled()) { _log.warn("No question id found in preferences of portlet " + portletId); } return StringPool.BLANK; } PollsQuestion question = null; try { question = PollsQuestionUtil.findByPrimaryKey(questionId); } catch (NoSuchQuestionException nsqe) { if (_log.isWarnEnabled()) { _log.warn(nsqe, nsqe); } return StringPool.BLANK; } portletDataContext.addPermissions( "com.liferay.portlet.polls", portletDataContext.getScopeGroupId()); Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("polls-display-data"); rootElement.addAttribute("group-id", String.valueOf(portletDataContext.getScopeGroupId())); Element questionsElement = rootElement.addElement("questions"); Element choicesElement = rootElement.addElement("choices"); Element votesElement = rootElement.addElement("votes"); PollsPortletDataHandlerImpl.exportQuestion( portletDataContext, questionsElement, choicesElement, votesElement, question); return document.formattedString(); }
@Override protected PortletPreferences doImportData( PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences, String data) throws Exception { portletDataContext.importPermissions( "com.liferay.portlet.polls", portletDataContext.getSourceGroupId(), portletDataContext.getScopeGroupId()); if (Validator.isNull(data)) { return null; } Document document = SAXReaderUtil.read(data); Element rootElement = document.getRootElement(); Element questionsElement = rootElement.element("questions"); for (Element questionElement : questionsElement.elements("question")) { String path = questionElement.attributeValue("path"); if (!portletDataContext.isPathNotProcessed(path)) { continue; } PollsQuestion question = (PollsQuestion) portletDataContext.getZipEntryAsObject(path); PollsPortletDataHandlerImpl.importQuestion(portletDataContext, questionElement, question); } Element choicesElement = rootElement.element("choices"); for (Element choiceElement : choicesElement.elements("choice")) { String path = choiceElement.attributeValue("path"); if (!portletDataContext.isPathNotProcessed(path)) { continue; } PollsChoice choice = (PollsChoice) portletDataContext.getZipEntryAsObject(path); PollsPortletDataHandlerImpl.importChoice(portletDataContext, choice); } if (portletDataContext.getBooleanParameter(_NAMESPACE, "votes")) { Element votesElement = rootElement.element("votes"); for (Element voteElement : votesElement.elements("vote")) { String path = voteElement.attributeValue("path"); if (!portletDataContext.isPathNotProcessed(path)) { continue; } PollsVote vote = (PollsVote) portletDataContext.getZipEntryAsObject(path); PollsPortletDataHandlerImpl.importVote(portletDataContext, vote); } } long questionId = GetterUtil.getLong(portletPreferences.getValue("questionId", StringPool.BLANK)); if (questionId > 0) { Map<Long, Long> questionPKs = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(PollsQuestion.class); questionId = MapUtil.getLong(questionPKs, questionId, questionId); portletPreferences.setValue("questionId", String.valueOf(questionId)); } return portletPreferences; }