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.lar.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.polls.DuplicateVoteException;
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.PollsVoteLocalServiceUtil;
029    import com.liferay.portlet.polls.service.persistence.PollsVoteUtil;
030    
031    import java.util.Map;
032    
033    /**
034     * @author Shinn Lok
035     * @author Mate Thurzo
036     */
037    public class PollsVoteStagedModelDataHandler
038            extends BaseStagedModelDataHandler<PollsVote> {
039    
040            public static final String[] CLASS_NAMES = {PollsVote.class.getName()};
041    
042            @Override
043            public String[] getClassNames() {
044                    return CLASS_NAMES;
045            }
046    
047            @Override
048            protected void doExportStagedModel(
049                            PortletDataContext portletDataContext, PollsVote vote)
050                    throws Exception {
051    
052                    StagedModelDataHandlerUtil.exportStagedModel(
053                            portletDataContext, vote.getChoice());
054    
055                    Element voteElement =
056                            portletDataContext.getExportDataStagedModelElement(vote);
057    
058                    portletDataContext.addClassedModel(
059                            voteElement, ExportImportPathUtil.getModelPath(vote), vote,
060                            PollsPortletDataHandler.NAMESPACE);
061            }
062    
063            @Override
064            protected void doImportStagedModel(
065                            PortletDataContext portletDataContext, PollsVote vote)
066                    throws Exception {
067    
068                    String choicePath = ExportImportPathUtil.getModelPath(
069                            portletDataContext, PollsChoice.class.getName(),
070                            vote.getChoiceId());
071    
072                    PollsChoice choice =
073                            (PollsChoice)portletDataContext.getZipEntryAsObject(choicePath);
074    
075                    StagedModelDataHandlerUtil.importStagedModel(
076                            portletDataContext, choice);
077    
078                    Map<Long, Long> questionIds =
079                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
080                                    PollsQuestion.class);
081    
082                    long questionId = MapUtil.getLong(
083                            questionIds, vote.getQuestionId(), vote.getQuestionId());
084    
085                    Map<Long, Long> choiceIds =
086                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
087                                    PollsChoice.class);
088    
089                    long choiceId = MapUtil.getLong(
090                            choiceIds, vote.getChoiceId(), vote.getChoiceId());
091    
092                    ServiceContext serviceContext = portletDataContext.createServiceContext(
093                            vote, PollsPortletDataHandler.NAMESPACE);
094    
095                    serviceContext.setCreateDate(vote.getVoteDate());
096    
097                    if (portletDataContext.isDataStrategyMirror()) {
098                            PollsVote existingVote = PollsVoteUtil.fetchByUUID_G(
099                                    vote.getUuid(), portletDataContext.getScopeGroupId());
100    
101                            if (existingVote == null) {
102                                    serviceContext.setUuid(vote.getUuid());
103                            }
104                    }
105    
106                    try {
107                            PollsVoteLocalServiceUtil.addVote(
108                                    vote.getUserId(), questionId, choiceId, serviceContext);
109                    }
110                    catch (DuplicateVoteException dve) {
111                    }
112            }
113    
114    }