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