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.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.GetterUtil;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
027    import com.liferay.portlet.messageboards.model.MBMessage;
028    import com.liferay.portlet.messageboards.model.MBThread;
029    import com.liferay.portlet.messageboards.model.MBThreadFlag;
030    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
031    import com.liferay.portlet.messageboards.service.MBThreadFlagLocalServiceUtil;
032    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
033    import com.liferay.portlet.messageboards.service.persistence.MBThreadUtil;
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 String[] getClassNames() {
047                    return CLASS_NAMES;
048            }
049    
050            @Override
051            protected void doExportStagedModel(
052                            PortletDataContext portletDataContext, MBThreadFlag threadFlag)
053                    throws Exception {
054    
055                    MBThread thread = MBThreadLocalServiceUtil.getThread(
056                            threadFlag.getThreadId());
057    
058                    MBMessage rootMessage = MBMessageLocalServiceUtil.getMessage(
059                            thread.getRootMessageId());
060    
061                    if ((rootMessage.getStatus() != WorkflowConstants.STATUS_APPROVED) ||
062                            (rootMessage.getCategoryId() ==
063                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
064    
065                            return;
066                    }
067    
068                    StagedModelDataHandlerUtil.exportStagedModel(
069                            portletDataContext, rootMessage);
070    
071                    Element threadFlagElement =
072                            portletDataContext.getExportDataStagedModelElement(threadFlag);
073    
074                    threadFlagElement.addAttribute(
075                            "root-message-id", String.valueOf(rootMessage.getMessageId()));
076    
077                    portletDataContext.addClassedModel(
078                            threadFlagElement, ExportImportPathUtil.getModelPath(threadFlag),
079                            threadFlag, MBPortletDataHandler.NAMESPACE);
080            }
081    
082            @Override
083            protected void doImportStagedModel(
084                            PortletDataContext portletDataContext, MBThreadFlag threadFlag)
085                    throws Exception {
086    
087                    Map<Long, Long> threadIds =
088                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
089                                    MBThread.class);
090    
091                    long threadId = MapUtil.getLong(
092                            threadIds, threadFlag.getThreadId(), threadFlag.getThreadId());
093    
094                    if (threadId == threadFlag.getThreadId()) {
095                            Element element =
096                                    portletDataContext.getImportDataStagedModelElement(threadFlag);
097    
098                            long rootMessageId = GetterUtil.getLong(
099                                    element.attributeValue("root-message-id"));
100    
101                            String rootMessagePath = ExportImportPathUtil.getModelPath(
102                                    portletDataContext, MBMessage.class.getName(), rootMessageId);
103    
104                            MBMessage rootMessage = (MBMessage)portletDataContext.
105                                    getZipEntryAsObject(rootMessagePath);
106    
107                            StagedModelDataHandlerUtil.importStagedModel(
108                                    portletDataContext, rootMessage);
109    
110                            threadId = MapUtil.getLong(
111                                    threadIds, threadFlag.getThreadId(), threadFlag.getThreadId());
112                    }
113    
114                    MBThread thread = MBThreadUtil.fetchByPrimaryKey(threadId);
115    
116                    if (thread == null) {
117                            return;
118                    }
119    
120                    long userId = portletDataContext.getUserId(threadFlag.getUserUuid());
121    
122                    ServiceContext serviceContext = portletDataContext.createServiceContext(
123                            threadFlag, MBPortletDataHandler.NAMESPACE);
124    
125                    MBThreadFlagLocalServiceUtil.addThreadFlag(
126                            userId, thread, serviceContext);
127            }
128    
129    }