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 doImportCompanyStagedModel(
070                            PortletDataContext portletDataContext, String uuid, long voteId)
071                    throws Exception {
072    
073                    PollsVote existingVote =
074                            PollsVoteLocalServiceUtil.fetchPollsVoteByUuidAndGroupId(
075                                    uuid, portletDataContext.getCompanyGroupId());
076    
077                    Map<Long, Long> voteIds =
078                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
079                                    PollsVote.class);
080    
081                    voteIds.put(voteId, existingVote.getVoteId());
082            }
083    
084            @Override
085            protected void doImportStagedModel(
086                            PortletDataContext portletDataContext, PollsVote vote)
087                    throws Exception {
088    
089                    StagedModelDataHandlerUtil.importReferenceStagedModel(
090                            portletDataContext, vote, PollsChoice.class, vote.getChoiceId());
091    
092                    Map<Long, Long> questionIds =
093                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
094                                    PollsQuestion.class);
095    
096                    long questionId = MapUtil.getLong(
097                            questionIds, vote.getQuestionId(), vote.getQuestionId());
098    
099                    Map<Long, Long> choiceIds =
100                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
101                                    PollsChoice.class);
102    
103                    long choiceId = MapUtil.getLong(
104                            choiceIds, vote.getChoiceId(), vote.getChoiceId());
105    
106                    ServiceContext serviceContext = portletDataContext.createServiceContext(
107                            vote);
108    
109                    serviceContext.setCreateDate(vote.getVoteDate());
110    
111                    if (portletDataContext.isDataStrategyMirror()) {
112                            PollsVote existingVote =
113                                    PollsVoteLocalServiceUtil.fetchPollsVoteByUuidAndGroupId(
114                                            vote.getUuid(), portletDataContext.getScopeGroupId());
115    
116                            if (existingVote == null) {
117                                    serviceContext.setUuid(vote.getUuid());
118                            }
119                    }
120    
121                    try {
122                            PollsVoteLocalServiceUtil.addVote(
123                                    vote.getUserId(), questionId, choiceId, serviceContext);
124                    }
125                    catch (DuplicateVoteException dve) {
126                    }
127            }
128    
129            @Override
130            protected boolean validateMissingReference(
131                            String uuid, long companyId, long groupId)
132                    throws Exception {
133    
134                    PollsVote vote =
135                            PollsVoteLocalServiceUtil.fetchPollsVoteByUuidAndGroupId(
136                                    uuid, groupId);
137    
138                    if (vote == null) {
139                            return false;
140                    }
141    
142                    return true;
143            }
144    
145    }