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.ratings.lar;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
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.util.MapUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.ratings.model.RatingsEntry;
027    import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
028    
029    import java.util.Map;
030    
031    /**
032     * @author Daniel Kocsis
033     */
034    public class RatingsEntryStagedModelDataHandler
035            extends BaseStagedModelDataHandler<RatingsEntry> {
036    
037            public static final String[] CLASS_NAMES = {RatingsEntry.class.getName()};
038    
039            @Override
040            public void deleteStagedModel(
041                            String uuid, long groupId, String className, String extraData)
042                    throws PortalException {
043    
044                    Group group = GroupLocalServiceUtil.getGroup(groupId);
045    
046                    RatingsEntry entry = fetchStagedModelByUuidAndCompanyId(
047                            uuid, group.getCompanyId());
048    
049                    if (entry != null) {
050                            RatingsEntryLocalServiceUtil.deleteRatingsEntry(entry);
051                    }
052            }
053    
054            @Override
055            public RatingsEntry fetchStagedModelByUuidAndCompanyId(
056                    String uuid, long companyId) {
057    
058                    return RatingsEntryLocalServiceUtil.fetchRatingsEntryByUuidAndCompanyId(
059                            uuid, companyId);
060            }
061    
062            @Override
063            public String[] getClassNames() {
064                    return CLASS_NAMES;
065            }
066    
067            @Override
068            public String getDisplayName(RatingsEntry entry) {
069                    return entry.getUuid();
070            }
071    
072            @Override
073            protected void doExportStagedModel(
074                            PortletDataContext portletDataContext, RatingsEntry entry)
075                    throws Exception {
076    
077                    Element entryElement = portletDataContext.getExportDataElement(entry);
078    
079                    portletDataContext.addClassedModel(
080                            entryElement, ExportImportPathUtil.getModelPath(entry), entry);
081            }
082    
083            @Override
084            protected void doImportStagedModel(
085                            PortletDataContext portletDataContext, RatingsEntry entry)
086                    throws Exception {
087    
088                    long userId = portletDataContext.getUserId(entry.getUserUuid());
089    
090                    Map<Long, Long> relatedClassPKs =
091                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
092                                    entry.getClassName());
093    
094                    long newClassPK = MapUtil.getLong(
095                            relatedClassPKs, entry.getClassPK(), entry.getClassPK());
096    
097                    ServiceContext serviceContext = portletDataContext.createServiceContext(
098                            entry);
099    
100                    RatingsEntry importedEntry = RatingsEntryLocalServiceUtil.updateEntry(
101                            userId, entry.getClassName(), newClassPK, entry.getScore(),
102                            serviceContext);
103    
104                    portletDataContext.importClassedModel(entry, importedEntry);
105            }
106    
107    }