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