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.model.PollsChoice;
025    import com.liferay.portlet.polls.model.PollsQuestion;
026    import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
027    import com.liferay.portlet.polls.service.PollsQuestionLocalServiceUtil;
028    
029    import java.util.Map;
030    
031    /**
032     * @author Shinn Lok
033     */
034    public class PollsChoiceStagedModelDataHandler
035            extends BaseStagedModelDataHandler<PollsChoice> {
036    
037            public static final String[] CLASS_NAMES = {PollsChoice.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(
041                    String uuid, long groupId, String className, String extraData) {
042    
043                    throw new UnsupportedOperationException();
044            }
045    
046            @Override
047            public String[] getClassNames() {
048                    return CLASS_NAMES;
049            }
050    
051            @Override
052            public String getDisplayName(PollsChoice choice) {
053                    return choice.getName();
054            }
055    
056            @Override
057            protected void doExportStagedModel(
058                            PortletDataContext portletDataContext, PollsChoice choice)
059                    throws Exception {
060    
061                    PollsQuestion question = PollsQuestionLocalServiceUtil.getQuestion(
062                            choice.getQuestionId());
063    
064                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
065                            portletDataContext, choice, question,
066                            PortletDataContext.REFERENCE_TYPE_STRONG);
067    
068                    Element choiceElement = portletDataContext.getExportDataElement(choice);
069    
070                    portletDataContext.addClassedModel(
071                            choiceElement, ExportImportPathUtil.getModelPath(choice), choice);
072            }
073    
074            @Override
075            protected void doImportStagedModel(
076                            PortletDataContext portletDataContext, PollsChoice choice)
077                    throws Exception {
078    
079                    long userId = portletDataContext.getUserId(choice.getUserUuid());
080    
081                    String questionPath = ExportImportPathUtil.getModelPath(
082                            portletDataContext, PollsQuestion.class.getName(),
083                            choice.getQuestionId());
084    
085                    PollsQuestion question =
086                            (PollsQuestion)portletDataContext.getZipEntryAsObject(questionPath);
087    
088                    StagedModelDataHandlerUtil.importReferenceStagedModel(
089                            portletDataContext, question);
090    
091                    Map<Long, Long> questionIds =
092                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
093                                    PollsQuestion.class);
094    
095                    long questionId = MapUtil.getLong(
096                            questionIds, choice.getQuestionId(), choice.getQuestionId());
097    
098                    PollsChoice importedChoice = null;
099    
100                    ServiceContext serviceContext = portletDataContext.createServiceContext(
101                            choice);
102    
103                    if (portletDataContext.isDataStrategyMirror()) {
104                            PollsChoice existingChoice =
105                                    PollsChoiceLocalServiceUtil.fetchPollsChoiceByUuidAndGroupId(
106                                            choice.getUuid(), portletDataContext.getScopeGroupId());
107    
108                            if (existingChoice == null) {
109                                    serviceContext.setUuid(choice.getUuid());
110    
111                                    importedChoice = PollsChoiceLocalServiceUtil.addChoice(
112                                            userId, questionId, choice.getName(),
113                                            choice.getDescription(), serviceContext);
114                            }
115                            else {
116                                    importedChoice = PollsChoiceLocalServiceUtil.updateChoice(
117                                            existingChoice.getChoiceId(), questionId, choice.getName(),
118                                            choice.getDescription(), serviceContext);
119                            }
120                    }
121                    else {
122                            importedChoice = PollsChoiceLocalServiceUtil.addChoice(
123                                    userId, questionId, choice.getName(), choice.getDescription(),
124                                    serviceContext);
125                    }
126    
127                    portletDataContext.importClassedModel(choice, importedChoice);
128            }
129    
130    }