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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.polls.model.PollsQuestion;
026    import com.liferay.portlet.polls.service.PollsQuestionLocalServiceUtil;
027    
028    import java.util.Calendar;
029    import java.util.Date;
030    
031    /**
032     * @author Shinn Lok
033     */
034    public class PollsQuestionStagedModelDataHandler
035            extends BaseStagedModelDataHandler<PollsQuestion> {
036    
037            public static final String[] CLASS_NAMES = {PollsQuestion.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(
041                            String uuid, long groupId, String className, String extraData)
042                    throws PortalException, SystemException {
043    
044                    PollsQuestion question =
045                            PollsQuestionLocalServiceUtil.fetchPollsQuestionByUuidAndGroupId(
046                                    uuid, groupId);
047    
048                    if (question != null) {
049                            PollsQuestionLocalServiceUtil.deleteQuestion(question);
050                    }
051            }
052    
053            @Override
054            public String[] getClassNames() {
055                    return CLASS_NAMES;
056            }
057    
058            @Override
059            public String getDisplayName(PollsQuestion question) {
060                    return question.getTitle();
061            }
062    
063            @Override
064            protected void doExportStagedModel(
065                            PortletDataContext portletDataContext, PollsQuestion question)
066                    throws Exception {
067    
068                    Element questionElement = portletDataContext.getExportDataElement(
069                            question);
070    
071                    portletDataContext.addClassedModel(
072                            questionElement, ExportImportPathUtil.getModelPath(question),
073                            question, PollsPortletDataHandler.NAMESPACE);
074            }
075    
076            @Override
077            protected void doImportStagedModel(
078                            PortletDataContext portletDataContext, PollsQuestion question)
079                    throws Exception {
080    
081                    long userId = portletDataContext.getUserId(question.getUserUuid());
082    
083                    int expirationMonth = 0;
084                    int expirationDay = 0;
085                    int expirationYear = 0;
086                    int expirationHour = 0;
087                    int expirationMinute = 0;
088                    boolean neverExpire = true;
089    
090                    Date expirationDate = question.getExpirationDate();
091    
092                    if (expirationDate != null) {
093                            Calendar expirationCal = CalendarFactoryUtil.getCalendar();
094    
095                            expirationCal.setTime(expirationDate);
096    
097                            expirationMonth = expirationCal.get(Calendar.MONTH);
098                            expirationDay = expirationCal.get(Calendar.DATE);
099                            expirationYear = expirationCal.get(Calendar.YEAR);
100                            expirationHour = expirationCal.get(Calendar.HOUR);
101                            expirationMinute = expirationCal.get(Calendar.MINUTE);
102                            neverExpire = false;
103    
104                            if (expirationCal.get(Calendar.AM_PM) == Calendar.PM) {
105                                    expirationHour += 12;
106                            }
107                    }
108    
109                    ServiceContext serviceContext = portletDataContext.createServiceContext(
110                            question, PollsPortletDataHandler.NAMESPACE);
111    
112                    PollsQuestion importedQuestion = null;
113    
114                    if (portletDataContext.isDataStrategyMirror()) {
115                            PollsQuestion existingQuestion =
116                                    PollsQuestionLocalServiceUtil.
117                                            fetchPollsQuestionByUuidAndGroupId(
118                                                    question.getUuid(),
119                                                    portletDataContext.getScopeGroupId());
120    
121                            if (existingQuestion == null) {
122                                    serviceContext.setUuid(question.getUuid());
123    
124                                    importedQuestion = PollsQuestionLocalServiceUtil.addQuestion(
125                                            userId, question.getTitleMap(),
126                                            question.getDescriptionMap(), expirationMonth,
127                                            expirationDay, expirationYear, expirationHour,
128                                            expirationMinute, neverExpire, null, serviceContext);
129                            }
130                            else {
131                                    importedQuestion = PollsQuestionLocalServiceUtil.updateQuestion(
132                                            userId, existingQuestion.getQuestionId(),
133                                            question.getTitleMap(), question.getDescriptionMap(),
134                                            expirationMonth, expirationDay, expirationYear,
135                                            expirationHour, expirationMinute, neverExpire, null,
136                                            serviceContext);
137                            }
138                    }
139                    else {
140                            importedQuestion = PollsQuestionLocalServiceUtil.addQuestion(
141                                    userId, question.getTitleMap(), question.getDescriptionMap(),
142                                    expirationMonth, expirationDay, expirationYear, expirationHour,
143                                    expirationMinute, neverExpire, null, serviceContext);
144                    }
145    
146                    portletDataContext.importClassedModel(
147                            question, importedQuestion, PollsPortletDataHandler.NAMESPACE);
148            }
149    
150    }