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    import com.liferay.portlet.polls.service.persistence.PollsChoiceFinderUtil;
029    
030    import java.util.Map;
031    
032    /**
033     * @author Shinn Lok
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 String[] getClassNames() {
042                    return CLASS_NAMES;
043            }
044    
045            @Override
046            protected void doExportStagedModel(
047                            PortletDataContext portletDataContext, PollsChoice choice)
048                    throws Exception {
049    
050                    PollsQuestion question = PollsQuestionLocalServiceUtil.getQuestion(
051                            choice.getQuestionId());
052    
053                    StagedModelDataHandlerUtil.exportStagedModel(
054                            portletDataContext, question);
055    
056                    Element choiceElement =
057                            portletDataContext.getExportDataStagedModelElement(choice);
058    
059                    portletDataContext.addClassedModel(
060                            choiceElement, ExportImportPathUtil.getModelPath(choice), choice,
061                            PollsPortletDataHandler.NAMESPACE);
062            }
063    
064            @Override
065            protected void doImportStagedModel(
066                            PortletDataContext portletDataContext, PollsChoice choice)
067                    throws Exception {
068    
069                    long userId = portletDataContext.getUserId(choice.getUserUuid());
070    
071                    String questionPath = ExportImportPathUtil.getModelPath(
072                            portletDataContext, PollsQuestion.class.getName(),
073                            choice.getQuestionId());
074    
075                    PollsQuestion question =
076                            (PollsQuestion)portletDataContext.getZipEntryAsObject(questionPath);
077    
078                    StagedModelDataHandlerUtil.importStagedModel(
079                            portletDataContext, question);
080    
081                    Map<Long, Long> questionIds =
082                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
083                                    PollsQuestion.class);
084    
085                    long questionId = MapUtil.getLong(
086                            questionIds, choice.getQuestionId(), choice.getQuestionId());
087    
088                    PollsChoice importedChoice = null;
089    
090                    ServiceContext serviceContext = portletDataContext.createServiceContext(
091                            choice, PollsPortletDataHandler.NAMESPACE);
092    
093                    if (portletDataContext.isDataStrategyMirror()) {
094                            PollsChoice existingChoice = PollsChoiceFinderUtil.fetchByUUID_G(
095                                    choice.getUuid(), portletDataContext.getScopeGroupId());
096    
097                            if (existingChoice == null) {
098                                    serviceContext.setUuid(choice.getUuid());
099    
100                                    importedChoice = PollsChoiceLocalServiceUtil.addChoice(
101                                            userId, questionId, choice.getName(),
102                                            choice.getDescription(), serviceContext);
103                            }
104                            else {
105                                    importedChoice = PollsChoiceLocalServiceUtil.updateChoice(
106                                            existingChoice.getChoiceId(), questionId, choice.getName(),
107                                            choice.getDescription(), serviceContext);
108                            }
109                    }
110                    else {
111                            importedChoice = PollsChoiceLocalServiceUtil.addChoice(
112                                    userId, questionId, choice.getName(), choice.getDescription(),
113                                    serviceContext);
114                    }
115    
116                    portletDataContext.importClassedModel(
117                            choice, importedChoice, PollsPortletDataHandler.NAMESPACE);
118            }
119    
120    }