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