001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.polls.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
023    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portlet.polls.model.PollsChoice;
026    import com.liferay.portlet.polls.model.PollsQuestion;
027    import com.liferay.portlet.polls.model.PollsVote;
028    import com.liferay.portlet.polls.service.PollsQuestionLocalServiceUtil;
029    import com.liferay.portlet.polls.service.persistence.PollsChoiceActionableDynamicQuery;
030    import com.liferay.portlet.polls.service.persistence.PollsQuestionActionableDynamicQuery;
031    import com.liferay.portlet.polls.service.persistence.PollsVoteActionableDynamicQuery;
032    
033    import java.util.List;
034    
035    import javax.portlet.PortletPreferences;
036    
037    /**
038     * @author Bruno Farache
039     * @author Marcellus Tavares
040     * @author Mate Thurzo
041     */
042    public class PollsPortletDataHandler extends BasePortletDataHandler {
043    
044            public static final String NAMESPACE = "polls";
045    
046            public PollsPortletDataHandler() {
047                    setAlwaysExportable(true);
048                    setDataLocalized(true);
049                    setExportControls(
050                            new PortletDataHandlerBoolean(NAMESPACE, "questions", true, true),
051                            new PortletDataHandlerBoolean(NAMESPACE, "votes"));
052            }
053    
054            @Override
055            protected PortletPreferences doDeleteData(
056                            PortletDataContext portletDataContext, String portletId,
057                            PortletPreferences portletPreferences)
058                    throws Exception {
059    
060                    if (portletDataContext.addPrimaryKey(
061                                    PollsPortletDataHandler.class, "deleteData")) {
062    
063                            return portletPreferences;
064                    }
065    
066                    PollsQuestionLocalServiceUtil.deleteQuestions(
067                            portletDataContext.getScopeGroupId());
068    
069                    return portletPreferences;
070            }
071    
072            @Override
073            protected String doExportData(
074                            final PortletDataContext portletDataContext, String portletId,
075                            PortletPreferences portletPreferences)
076                    throws Exception {
077    
078                    portletDataContext.addPermissions(
079                            "com.liferay.portlet.polls", portletDataContext.getScopeGroupId());
080    
081                    Element rootElement = addExportDataRootElement(portletDataContext);
082    
083                    rootElement.addAttribute(
084                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
085    
086                    ActionableDynamicQuery questionActionableDynamicQuery =
087                            new PollsQuestionActionableDynamicQuery() {
088    
089                            @Override
090                            protected void addCriteria(DynamicQuery dynamicQuery) {
091                                    portletDataContext.addDateRangeCriteria(
092                                            dynamicQuery, "modifiedDate");
093                            }
094    
095                            @Override
096                            protected void performAction(Object object) throws PortalException {
097                                    PollsQuestion pollsQuestion = (PollsQuestion)object;
098    
099                                    StagedModelDataHandlerUtil.exportStagedModel(
100                                            portletDataContext, pollsQuestion);
101                            }
102    
103                    };
104    
105                    questionActionableDynamicQuery.setGroupId(
106                            portletDataContext.getGroupId());
107    
108                    questionActionableDynamicQuery.performActions();
109    
110                    ActionableDynamicQuery choiceActionableDynamicQuery =
111                            new PollsChoiceActionableDynamicQuery() {
112    
113                            @Override
114                            protected void addCriteria(DynamicQuery dynamicQuery) {
115                                    portletDataContext.addDateRangeCriteria(
116                                            dynamicQuery, "modifiedDate");
117                            }
118    
119                            @Override
120                            protected void performAction(Object object) throws PortalException {
121                                    PollsChoice pollsChoice = (PollsChoice)object;
122    
123                                    StagedModelDataHandlerUtil.exportStagedModel(
124                                            portletDataContext, pollsChoice);
125                            }
126    
127                    };
128    
129                    choiceActionableDynamicQuery.setGroupId(
130                            portletDataContext.getGroupId());
131    
132                    choiceActionableDynamicQuery.performActions();
133    
134                    if (portletDataContext.getBooleanParameter(
135                                    PollsPortletDataHandler.NAMESPACE, "votes")) {
136    
137                            ActionableDynamicQuery voteActionableDynamicQuery =
138                                    new PollsVoteActionableDynamicQuery() {
139    
140                                    @Override
141                                    protected void addCriteria(DynamicQuery dynamicQuery) {
142                                            portletDataContext.addDateRangeCriteria(
143                                                    dynamicQuery, "modifiedDate");
144                                    }
145    
146                                    @Override
147                                    protected void performAction(Object object)
148                                            throws PortalException {
149    
150                                            PollsVote pollsVote = (PollsVote)object;
151    
152                                            StagedModelDataHandlerUtil.exportStagedModel(
153                                                    portletDataContext, pollsVote);
154                                    }
155    
156                            };
157    
158                            voteActionableDynamicQuery.setGroupId(
159                                    portletDataContext.getGroupId());
160    
161                            voteActionableDynamicQuery.performActions();
162                    }
163    
164                    return getExportDataRootElementString(rootElement);
165            }
166    
167            @Override
168            protected PortletPreferences doImportData(
169                            PortletDataContext portletDataContext, String portletId,
170                            PortletPreferences portletPreferences, String data)
171                    throws Exception {
172    
173                    portletDataContext.importPermissions(
174                            "com.liferay.portlet.polls", portletDataContext.getSourceGroupId(),
175                            portletDataContext.getScopeGroupId());
176    
177                    Element questionsElement = portletDataContext.getImportDataGroupElement(
178                            PollsQuestion.class);
179    
180                    List<Element> questionElements = questionsElement.elements();
181    
182                    for (Element questionElement : questionElements) {
183                            StagedModelDataHandlerUtil.importStagedModel(
184                                    portletDataContext, questionElement);
185                    }
186    
187                    Element choicesElement = portletDataContext.getImportDataGroupElement(
188                            PollsChoice.class);
189    
190                    List<Element> choiceElements = choicesElement.elements();
191    
192                    for (Element choiceElement : choiceElements) {
193                            StagedModelDataHandlerUtil.importStagedModel(
194                                    portletDataContext, choiceElement);
195                    }
196    
197                    if (portletDataContext.getBooleanParameter(NAMESPACE, "votes")) {
198                            Element votesElement = portletDataContext.getImportDataGroupElement(
199                                    PollsVote.class);
200    
201                            List<Element> voteElements = votesElement.elements();
202    
203                            for (Element voteElement : voteElements) {
204                                    StagedModelDataHandlerUtil.importStagedModel(
205                                            portletDataContext, voteElement);
206                            }
207                    }
208    
209                    return null;
210            }
211    
212    }