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.messageboards.lar;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
019    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
028    import com.liferay.portlet.messageboards.model.MBMessage;
029    import com.liferay.portlet.messageboards.model.MBThread;
030    import com.liferay.portlet.messageboards.model.MBThreadFlag;
031    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
032    import com.liferay.portlet.messageboards.service.MBThreadFlagLocalServiceUtil;
033    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
034    
035    import java.util.Map;
036    
037    /**
038     * @author Daniel Kocsis
039     */
040    public class MBThreadFlagStagedModelDataHandler
041            extends BaseStagedModelDataHandler<MBThreadFlag> {
042    
043            public static final String[] CLASS_NAMES = {MBThreadFlag.class.getName()};
044    
045            @Override
046            public void deleteStagedModel(
047                            String uuid, long groupId, String className, String extraData)
048                    throws SystemException {
049    
050                    MBThreadFlag threadFlag =
051                            MBThreadFlagLocalServiceUtil.fetchMBThreadFlagByUuidAndGroupId(
052                                    uuid, groupId);
053    
054                    if (threadFlag != null) {
055                            MBThreadFlagLocalServiceUtil.deleteThreadFlag(threadFlag);
056                    }
057            }
058    
059            @Override
060            public String[] getClassNames() {
061                    return CLASS_NAMES;
062            }
063    
064            @Override
065            protected void doExportStagedModel(
066                            PortletDataContext portletDataContext, MBThreadFlag threadFlag)
067                    throws Exception {
068    
069                    MBThread thread = MBThreadLocalServiceUtil.getThread(
070                            threadFlag.getThreadId());
071    
072                    MBMessage rootMessage = MBMessageLocalServiceUtil.getMessage(
073                            thread.getRootMessageId());
074    
075                    if ((rootMessage.getStatus() != WorkflowConstants.STATUS_APPROVED) ||
076                            (rootMessage.getCategoryId() ==
077                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
078    
079                            return;
080                    }
081    
082                    StagedModelDataHandlerUtil.exportStagedModel(
083                            portletDataContext, rootMessage);
084    
085                    Element threadFlagElement = portletDataContext.getExportDataElement(
086                            threadFlag);
087    
088                    threadFlagElement.addAttribute(
089                            "root-message-id", String.valueOf(rootMessage.getMessageId()));
090    
091                    portletDataContext.addClassedModel(
092                            threadFlagElement, ExportImportPathUtil.getModelPath(threadFlag),
093                            threadFlag);
094            }
095    
096            @Override
097            protected void doImportStagedModel(
098                            PortletDataContext portletDataContext, MBThreadFlag threadFlag)
099                    throws Exception {
100    
101                    Element element = portletDataContext.getImportDataStagedModelElement(
102                            threadFlag);
103    
104                    long rootMessageId = GetterUtil.getLong(
105                            element.attributeValue("root-message-id"));
106    
107                    String rootMessagePath = ExportImportPathUtil.getModelPath(
108                            portletDataContext, MBMessage.class.getName(), rootMessageId);
109    
110                    MBMessage rootMessage =
111                            (MBMessage)portletDataContext.getZipEntryAsObject(rootMessagePath);
112    
113                    StagedModelDataHandlerUtil.importStagedModel(
114                            portletDataContext, rootMessage);
115    
116                    Map<Long, Long> threadIds =
117                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
118                                    MBThread.class);
119    
120                    long threadId = MapUtil.getLong(
121                            threadIds, threadFlag.getThreadId(), threadFlag.getThreadId());
122    
123                    MBThread thread = MBThreadLocalServiceUtil.fetchThread(threadId);
124    
125                    if (thread == null) {
126                            return;
127                    }
128    
129                    long userId = portletDataContext.getUserId(threadFlag.getUserUuid());
130    
131                    ServiceContext serviceContext = portletDataContext.createServiceContext(
132                            threadFlag);
133    
134                    serviceContext.setUuid(threadFlag.getUuid());
135    
136                    MBThreadFlagLocalServiceUtil.addThreadFlag(
137                            userId, thread, serviceContext);
138            }
139    
140    }