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            }
067    
068            @Override
069            protected void doImportStagedModel(
070                            PortletDataContext portletDataContext, PollsVote vote)
071                    throws Exception {
072    
073                    String choicePath = ExportImportPathUtil.getModelPath(
074                            portletDataContext, PollsChoice.class.getName(),
075                            vote.getChoiceId());
076    
077                    PollsChoice choice =
078                            (PollsChoice)portletDataContext.getZipEntryAsObject(choicePath);
079    
080                    StagedModelDataHandlerUtil.importReferenceStagedModel(
081                            portletDataContext, choice);
082    
083                    Map<Long, Long> questionIds =
084                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
085                                    PollsQuestion.class);
086    
087                    long questionId = MapUtil.getLong(
088                            questionIds, vote.getQuestionId(), vote.getQuestionId());
089    
090                    Map<Long, Long> choiceIds =
091                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
092                                    PollsChoice.class);
093    
094                    long choiceId = MapUtil.getLong(
095                            choiceIds, vote.getChoiceId(), vote.getChoiceId());
096    
097                    ServiceContext serviceContext = portletDataContext.createServiceContext(
098                            vote);
099    
100                    serviceContext.setCreateDate(vote.getVoteDate());
101    
102                    if (portletDataContext.isDataStrategyMirror()) {
103                            PollsVote existingVote =
104                                    PollsVoteLocalServiceUtil.fetchPollsVoteByUuidAndGroupId(
105                                            vote.getUuid(), portletDataContext.getScopeGroupId());
106    
107                            if (existingVote == null) {
108                                    serviceContext.setUuid(vote.getUuid());
109                            }
110                    }
111    
112                    try {
113                            PollsVoteLocalServiceUtil.addVote(
114                                    vote.getUserId(), questionId, choiceId, serviceContext);
115                    }
116                    catch (DuplicateVoteException dve) {
117                    }
118            }
119    
120    }