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.StagedModelModifiedDateComparator;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.ListUtil;
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.MBBan;
030    import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
031    
032    import java.util.List;
033    
034    /**
035     * @author Daniel Kocsis
036     */
037    public class MBBanStagedModelDataHandler
038            extends BaseStagedModelDataHandler<MBBan> {
039    
040            public static final String[] CLASS_NAMES = {MBBan.class.getName()};
041    
042            @Override
043            public void deleteStagedModel(
044                    String uuid, long groupId, String className, String extraData) {
045    
046                    MBBan ban = fetchStagedModelByUuidAndGroupId(uuid, groupId);
047    
048                    if (ban != null) {
049                            MBBanLocalServiceUtil.deleteBan(ban);
050                    }
051            }
052    
053            @Override
054            public MBBan fetchStagedModelByUuidAndCompanyId(
055                    String uuid, long companyId) {
056    
057                    List<MBBan> bans = MBBanLocalServiceUtil.getMBBansByUuidAndCompanyId(
058                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
059                            new StagedModelModifiedDateComparator<MBBan>());
060    
061                    if (ListUtil.isEmpty(bans)) {
062                            return null;
063                    }
064    
065                    return bans.get(0);
066            }
067    
068            @Override
069            public MBBan fetchStagedModelByUuidAndGroupId(String uuid, long groupId) {
070                    return MBBanLocalServiceUtil.fetchMBBanByUuidAndGroupId(uuid, groupId);
071            }
072    
073            @Override
074            public String[] getClassNames() {
075                    return CLASS_NAMES;
076            }
077    
078            @Override
079            protected void doExportStagedModel(
080                            PortletDataContext portletDataContext, MBBan ban)
081                    throws Exception {
082    
083                    Element userBanElement = portletDataContext.getExportDataElement(ban);
084    
085                    ban.setBanUserUuid(ban.getBanUserUuid());
086    
087                    User bannedUser = UserLocalServiceUtil.getUser(ban.getUserId());
088    
089                    portletDataContext.addReferenceElement(
090                            ban, userBanElement, bannedUser,
091                            PortletDataContext.REFERENCE_TYPE_DEPENDENCY_DISPOSABLE, true);
092    
093                    portletDataContext.addClassedModel(
094                            userBanElement, ExportImportPathUtil.getModelPath(ban), ban);
095            }
096    
097            @Override
098            protected void doImportStagedModel(
099                            PortletDataContext portletDataContext, MBBan ban)
100                    throws Exception {
101    
102                    User user = UserLocalServiceUtil.fetchUserByUuidAndCompanyId(
103                            ban.getBanUserUuid(), portletDataContext.getCompanyId());
104    
105                    if (user == null) {
106                            if (_log.isWarnEnabled()) {
107                                    _log.warn(
108                                            "Unable to find banned user with uuid " +
109                                                    ban.getBanUserUuid());
110                            }
111    
112                            return;
113                    }
114    
115                    long userId = portletDataContext.getUserId(ban.getUserUuid());
116    
117                    ServiceContext serviceContext = portletDataContext.createServiceContext(
118                            ban);
119    
120                    serviceContext.setUuid(ban.getUuid());
121    
122                    MBBanLocalServiceUtil.addBan(userId, user.getUserId(), serviceContext);
123            }
124    
125            @Override
126            protected void importReferenceStagedModels(
127                    PortletDataContext portletDataContext, MBBan ban) {
128            }
129    
130            private static final Log _log = LogFactoryUtil.getLog(
131                    MBBanStagedModelDataHandler.class);
132    
133    }