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