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