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                            PollsPortletDataHandler.NAMESPACE);
073            }
074    
075            @Override
076            protected void doImportStagedModel(
077                            PortletDataContext portletDataContext, PollsChoice choice)
078                    throws Exception {
079    
080                    long userId = portletDataContext.getUserId(choice.getUserUuid());
081    
082                    String questionPath = ExportImportPathUtil.getModelPath(
083                            portletDataContext, PollsQuestion.class.getName(),
084                            choice.getQuestionId());
085    
086                    PollsQuestion question =
087                            (PollsQuestion)portletDataContext.getZipEntryAsObject(questionPath);
088    
089                    StagedModelDataHandlerUtil.importReferenceStagedModel(
090                            portletDataContext, question);
091    
092                    Map<Long, Long> questionIds =
093                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
094                                    PollsQuestion.class);
095    
096                    long questionId = MapUtil.getLong(
097                            questionIds, choice.getQuestionId(), choice.getQuestionId());
098    
099                    PollsChoice importedChoice = null;
100    
101                    ServiceContext serviceContext = portletDataContext.createServiceContext(
102                            choice, PollsPortletDataHandler.NAMESPACE);
103    
104                    if (portletDataContext.isDataStrategyMirror()) {
105                            PollsChoice existingChoice =
106                                    PollsChoiceLocalServiceUtil.fetchPollsChoiceByUuidAndGroupId(
107                                            choice.getUuid(), portletDataContext.getScopeGroupId());
108    
109                            if (existingChoice == null) {
110                                    serviceContext.setUuid(choice.getUuid());
111    
112                                    importedChoice = PollsChoiceLocalServiceUtil.addChoice(
113                                            userId, questionId, choice.getName(),
114                                            choice.getDescription(), serviceContext);
115                            }
116                            else {
117                                    importedChoice = PollsChoiceLocalServiceUtil.updateChoice(
118                                            existingChoice.getChoiceId(), questionId, choice.getName(),
119                                            choice.getDescription(), serviceContext);
120                            }
121                    }
122                    else {
123                            importedChoice = PollsChoiceLocalServiceUtil.addChoice(
124                                    userId, questionId, choice.getName(), choice.getDescription(),
125                                    serviceContext);
126                    }
127    
128                    portletDataContext.importClassedModel(
129                            choice, importedChoice, PollsPortletDataHandler.NAMESPACE);
130            }
131    
132    }