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, User.class,
069                            PortletDataContext.REFERENCE_TYPE_DEPENDENCY_DISPOSABLE, true);
070    
071                    portletDataContext.addClassedModel(
072                            userBanElement, ExportImportPathUtil.getModelPath(ban), ban,
073                            MBPortletDataHandler.NAMESPACE);
074            }
075    
076            @Override
077            protected void doImportStagedModel(
078                            PortletDataContext portletDataContext, MBBan ban)
079                    throws Exception {
080    
081                    User user = UserLocalServiceUtil.fetchUserByUuidAndCompanyId(
082                            ban.getBanUserUuid(), portletDataContext.getCompanyId());
083    
084                    if (user == null) {
085                            if (_log.isWarnEnabled()) {
086                                    _log.warn(
087                                            "Unable to find banned user with uuid " +
088                                                    ban.getBanUserUuid());
089                            }
090    
091                            return;
092                    }
093    
094                    long userId = portletDataContext.getUserId(ban.getUserUuid());
095    
096                    ServiceContext serviceContext = portletDataContext.createServiceContext(
097                            ban, MBPortletDataHandler.NAMESPACE);
098    
099                    serviceContext.setUuid(ban.getUuid());
100    
101                    MBBanLocalServiceUtil.addBan(userId, user.getUserId(), serviceContext);
102            }
103    
104            private static Log _log = LogFactoryUtil.getLog(
105                    MBBanStagedModelDataHandler.class);
106    
107    }