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