001
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
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 }