001
014
015 package com.liferay.portlet.bookmarks.lar;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
021 import com.liferay.portal.kernel.lar.PortletDataContext;
022 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023 import com.liferay.portal.kernel.lar.StagedModelModifiedDateComparator;
024 import com.liferay.portal.kernel.trash.TrashHandler;
025 import com.liferay.portal.kernel.util.ListUtil;
026 import com.liferay.portal.kernel.util.MapUtil;
027 import com.liferay.portal.kernel.xml.Element;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
030 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
031 import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
032 import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
033
034 import java.util.List;
035 import java.util.Map;
036
037
041 public class BookmarksEntryStagedModelDataHandler
042 extends BaseStagedModelDataHandler<BookmarksEntry> {
043
044 public static final String[] CLASS_NAMES = {BookmarksEntry.class.getName()};
045
046 @Override
047 public void deleteStagedModel(
048 String uuid, long groupId, String className, String extraData)
049 throws PortalException {
050
051 BookmarksEntry entry = fetchStagedModelByUuidAndGroupId(uuid, groupId);
052
053 if (entry != null) {
054 BookmarksEntryLocalServiceUtil.deleteEntry(entry);
055 }
056 }
057
058 @Override
059 public BookmarksEntry fetchStagedModelByUuidAndCompanyId(
060 String uuid, long companyId) {
061
062 List<BookmarksEntry> entries =
063 BookmarksEntryLocalServiceUtil.
064 getBookmarksEntriesByUuidAndCompanyId(
065 uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
066 new StagedModelModifiedDateComparator<BookmarksEntry>());
067
068 if (ListUtil.isEmpty(entries)) {
069 return null;
070 }
071
072 return entries.get(0);
073 }
074
075 @Override
076 public BookmarksEntry fetchStagedModelByUuidAndGroupId(
077 String uuid, long groupId) {
078
079 return BookmarksEntryLocalServiceUtil.
080 fetchBookmarksEntryByUuidAndGroupId(uuid, groupId);
081 }
082
083 @Override
084 public String[] getClassNames() {
085 return CLASS_NAMES;
086 }
087
088 @Override
089 public String getDisplayName(BookmarksEntry entry) {
090 return entry.getName();
091 }
092
093 @Override
094 protected void doExportStagedModel(
095 PortletDataContext portletDataContext, BookmarksEntry entry)
096 throws Exception {
097
098 if (entry.getFolderId() !=
099 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
100
101 StagedModelDataHandlerUtil.exportReferenceStagedModel(
102 portletDataContext, entry, entry.getFolder(),
103 PortletDataContext.REFERENCE_TYPE_PARENT);
104 }
105
106 Element entryElement = portletDataContext.getExportDataElement(entry);
107
108 portletDataContext.addClassedModel(
109 entryElement, ExportImportPathUtil.getModelPath(entry), entry);
110 }
111
112 @Override
113 protected void doImportStagedModel(
114 PortletDataContext portletDataContext, BookmarksEntry entry)
115 throws Exception {
116
117 long userId = portletDataContext.getUserId(entry.getUserUuid());
118
119 Map<Long, Long> folderIds =
120 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
121 BookmarksFolder.class);
122
123 long folderId = MapUtil.getLong(
124 folderIds, entry.getFolderId(), entry.getFolderId());
125
126 ServiceContext serviceContext = portletDataContext.createServiceContext(
127 entry);
128
129 BookmarksEntry importedEntry = null;
130
131 if (portletDataContext.isDataStrategyMirror()) {
132 BookmarksEntry existingEntry = fetchStagedModelByUuidAndGroupId(
133 entry.getUuid(), portletDataContext.getScopeGroupId());
134
135 if (existingEntry == null) {
136 serviceContext.setUuid(entry.getUuid());
137
138 importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
139 userId, portletDataContext.getScopeGroupId(), folderId,
140 entry.getName(), entry.getUrl(), entry.getDescription(),
141 serviceContext);
142 }
143 else {
144 importedEntry = BookmarksEntryLocalServiceUtil.updateEntry(
145 userId, existingEntry.getEntryId(),
146 portletDataContext.getScopeGroupId(), folderId,
147 entry.getName(), entry.getUrl(), entry.getDescription(),
148 serviceContext);
149 }
150 }
151 else {
152 importedEntry = BookmarksEntryLocalServiceUtil.addEntry(
153 userId, portletDataContext.getScopeGroupId(), folderId,
154 entry.getName(), entry.getUrl(), entry.getDescription(),
155 serviceContext);
156 }
157
158 portletDataContext.importClassedModel(entry, importedEntry);
159 }
160
161 @Override
162 protected void doRestoreStagedModel(
163 PortletDataContext portletDataContext, BookmarksEntry entry)
164 throws Exception {
165
166 long userId = portletDataContext.getUserId(entry.getUserUuid());
167
168 BookmarksEntry existingEntry = fetchStagedModelByUuidAndGroupId(
169 entry.getUuid(), portletDataContext.getScopeGroupId());
170
171 if ((existingEntry == null) || !existingEntry.isInTrash()) {
172 return;
173 }
174
175 TrashHandler trashHandler = existingEntry.getTrashHandler();
176
177 if (trashHandler.isRestorable(existingEntry.getEntryId())) {
178 trashHandler.restoreTrashEntry(userId, existingEntry.getEntryId());
179 }
180 }
181
182 }