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     * @author Mate Thurzo
034     */
035    public class PollsChoiceStagedModelDataHandler
036            extends BaseStagedModelDataHandler<PollsChoice> {
037    
038            public static final String[] CLASS_NAMES = {PollsChoice.class.getName()};
039    
040            @Override
041            public void deleteStagedModel(
042                    String uuid, long groupId, String className, String extraData) {
043    
044                    throw new UnsupportedOperationException();
045            }
046    
047            @Override
048            public String[] getClassNames() {
049                    return CLASS_NAMES;
050            }
051    
052            @Override
053            public String getDisplayName(PollsChoice choice) {
054                    return choice.getName();
055            }
056    
057            @Override
058            protected void doExportStagedModel(
059                            PortletDataContext portletDataContext, PollsChoice choice)
060                    throws Exception {
061    
062                    PollsQuestion question = PollsQuestionLocalServiceUtil.getQuestion(
063                            choice.getQuestionId());
064    
065                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
066                            portletDataContext, choice, question,
067                            PortletDataContext.REFERENCE_TYPE_STRONG);
068    
069                    Element choiceElement = portletDataContext.getExportDataElement(choice);
070    
071                    portletDataContext.addClassedModel(
072                            choiceElement, ExportImportPathUtil.getModelPath(choice), choice);
073            }
074    
075            @Override
076            protected void doImportCompanyStagedModel(
077                            PortletDataContext portletDataContext, String uuid, long choiceId)
078                    throws Exception {
079    
080                    PollsChoice existingChoice =
081                            PollsChoiceLocalServiceUtil.fetchPollsChoiceByUuidAndGroupId(
082                                    uuid, portletDataContext.getCompanyGroupId());
083    
084                    Map<Long, Long> choiceIds =
085                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
086                                    PollsChoice.class);
087    
088                    choiceIds.put(choiceId, existingChoice.getChoiceId());
089            }
090    
091            @Override
092            protected void doImportStagedModel(
093                            PortletDataContext portletDataContext, PollsChoice choice)
094                    throws Exception {
095    
096                    long userId = portletDataContext.getUserId(choice.getUserUuid());
097    
098                    StagedModelDataHandlerUtil.importReferenceStagedModel(
099                            portletDataContext, choice, PollsQuestion.class,
100                            choice.getQuestionId());
101    
102                    Map<Long, Long> questionIds =
103                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
104                                    PollsQuestion.class);
105    
106                    long questionId = MapUtil.getLong(
107                            questionIds, choice.getQuestionId(), choice.getQuestionId());
108    
109                    PollsChoice importedChoice = null;
110    
111                    ServiceContext serviceContext = portletDataContext.createServiceContext(
112                            choice);
113    
114                    if (portletDataContext.isDataStrategyMirror()) {
115                            PollsChoice existingChoice =
116                                    PollsChoiceLocalServiceUtil.fetchPollsChoiceByUuidAndGroupId(
117                                            choice.getUuid(), portletDataContext.getScopeGroupId());
118    
119                            if (existingChoice == null) {
120                                    serviceContext.setUuid(choice.getUuid());
121    
122                                    importedChoice = PollsChoiceLocalServiceUtil.addChoice(
123                                            userId, questionId, choice.getName(),
124                                            choice.getDescription(), serviceContext);
125                            }
126                            else {
127                                    importedChoice = PollsChoiceLocalServiceUtil.updateChoice(
128                                            existingChoice.getChoiceId(), questionId, choice.getName(),
129                                            choice.getDescription(), serviceContext);
130                            }
131                    }
132                    else {
133                            importedChoice = PollsChoiceLocalServiceUtil.addChoice(
134                                    userId, questionId, choice.getName(), choice.getDescription(),
135                                    serviceContext);
136                    }
137    
138                    portletDataContext.importClassedModel(choice, importedChoice);
139            }
140    
141            @Override
142            protected boolean validateMissingReference(
143                            String uuid, long companyId, long groupId)
144                    throws Exception {
145    
146                    PollsChoice choice =
147                            PollsChoiceLocalServiceUtil.fetchPollsChoiceByUuidAndGroupId(
148                                    uuid, groupId);
149    
150                    if (choice == null) {
151                            return false;
152                    }
153    
154                    return true;
155            }
156    
157    }