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.lar.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.persistence.UserUtil;
026    import com.liferay.portlet.messageboards.model.MBBan;
027    import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
028    
029    /**
030     * @author Daniel Kocsis
031     */
032    public class MBBanStagedModelDataHandler
033            extends BaseStagedModelDataHandler<MBBan> {
034    
035            public static final String[] CLASS_NAMES = {MBBan.class.getName()};
036    
037            @Override
038            public String[] getClassNames() {
039                    return CLASS_NAMES;
040            }
041    
042            @Override
043            protected void doExportStagedModel(
044                            PortletDataContext portletDataContext, MBBan ban)
045                    throws Exception {
046    
047                    Element userBanElement =
048                            portletDataContext.getExportDataStagedModelElement(ban);
049    
050                    ban.setBanUserUuid(ban.getBanUserUuid());
051    
052                    portletDataContext.addClassedModel(
053                            userBanElement, ExportImportPathUtil.getModelPath(ban), ban,
054                            MBPortletDataHandler.NAMESPACE);
055            }
056    
057            @Override
058            protected void doImportStagedModel(
059                            PortletDataContext portletDataContext, MBBan ban)
060                    throws Exception {
061    
062                    User user = UserUtil.fetchByUuid_C_First(
063                            ban.getBanUserUuid(), portletDataContext.getCompanyId(), null);
064    
065                    if (user == null) {
066                            if (_log.isWarnEnabled()) {
067                                    _log.warn(
068                                            "Unable to find banned user with uuid " +
069                                                    ban.getBanUserUuid());
070                            }
071    
072                            return;
073                    }
074    
075                    long userId = portletDataContext.getUserId(ban.getUserUuid());
076    
077                    ServiceContext serviceContext = portletDataContext.createServiceContext(
078                            ban, MBPortletDataHandler.NAMESPACE);
079    
080                    MBBanLocalServiceUtil.addBan(userId, user.getUserId(), serviceContext);
081            }
082    
083            private static Log _log = LogFactoryUtil.getLog(
084                    MBBanStagedModelDataHandler.class);
085    
086    }