001
014
015 package com.liferay.portlet.journal.lar;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020 import com.liferay.portal.kernel.lar.ExportImportHelper;
021 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
022 import com.liferay.portal.kernel.lar.PortletDataContext;
023 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.MapUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.xml.Element;
033 import com.liferay.portal.model.Group;
034 import com.liferay.portal.service.GroupLocalServiceUtil;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
038 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
039 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
040 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
041 import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
042 import com.liferay.portlet.journal.model.JournalArticle;
043 import com.liferay.portlet.journal.model.JournalFeed;
044 import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
045
046 import java.util.List;
047 import java.util.Map;
048
049
052 public class JournalFeedStagedModelDataHandler
053 extends BaseStagedModelDataHandler<JournalFeed> {
054
055 public static final String[] CLASS_NAMES = {JournalFeed.class.getName()};
056
057 @Override
058 public void deleteStagedModel(
059 String uuid, long groupId, String className, String extraData)
060 throws PortalException, SystemException {
061
062 JournalFeed feed =
063 JournalFeedLocalServiceUtil.fetchJournalFeedByUuidAndGroupId(
064 uuid, groupId);
065
066 if (feed != null) {
067 JournalFeedLocalServiceUtil.deleteFeed(feed);
068 }
069 }
070
071 @Override
072 public String[] getClassNames() {
073 return CLASS_NAMES;
074 }
075
076 @Override
077 protected void doExportStagedModel(
078 PortletDataContext portletDataContext, JournalFeed feed)
079 throws Exception {
080
081 Element feedElement = portletDataContext.getExportDataElement(feed);
082
083 DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
084 feed.getGroupId(), PortalUtil.getClassNameId(JournalArticle.class),
085 feed.getStructureId(), true);
086
087 if (ddmStructure != null) {
088 StagedModelDataHandlerUtil.exportReferenceStagedModel(
089 portletDataContext, feed, ddmStructure,
090 PortletDataContext.REFERENCE_TYPE_STRONG);
091 }
092 else {
093 if (_log.isWarnEnabled()) {
094 _log.warn(
095 "Unable to find DDM structure with key " +
096 feed.getStructureId());
097 }
098 }
099
100 DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
101 feed.getGroupId(), PortalUtil.getClassNameId(DDMStructure.class),
102 feed.getTemplateId());
103
104 if (ddmTemplate != null) {
105 StagedModelDataHandlerUtil.exportReferenceStagedModel(
106 portletDataContext, feed, ddmTemplate,
107 PortletDataContext.REFERENCE_TYPE_STRONG);
108 }
109 else {
110 if (_log.isWarnEnabled()) {
111 _log.warn(
112 "Unable to find DDM template with key " +
113 feed.getTemplateId());
114 }
115 }
116
117 DDMTemplate rendererDDMTemplate =
118 DDMTemplateLocalServiceUtil.fetchTemplate(
119 feed.getGroupId(),
120 PortalUtil.getClassNameId(DDMStructure.class),
121 feed.getRendererTemplateId());
122
123 if (rendererDDMTemplate != null) {
124 Element rendererDDMTemplateElement =
125 StagedModelDataHandlerUtil.exportReferenceStagedModel(
126 portletDataContext, feed, rendererDDMTemplate,
127 PortletDataContext.REFERENCE_TYPE_STRONG);
128
129 rendererDDMTemplateElement.addAttribute(
130 "rendererDDMTemplate", "true");
131 }
132 else {
133 if (_log.isWarnEnabled()) {
134 _log.warn(
135 "Unable to find DDM template with key " +
136 feed.getRendererTemplateId());
137 }
138 }
139
140 Group group = GroupLocalServiceUtil.getGroup(
141 portletDataContext.getScopeGroupId());
142
143 String newGroupFriendlyURL = group.getFriendlyURL().substring(1);
144
145 String[] friendlyURLParts = StringUtil.split(
146 feed.getTargetLayoutFriendlyUrl(), '/');
147
148 String oldGroupFriendlyURL = friendlyURLParts[2];
149
150 if (newGroupFriendlyURL.equals(oldGroupFriendlyURL)) {
151 String targetLayoutFriendlyUrl = StringUtil.replaceFirst(
152 feed.getTargetLayoutFriendlyUrl(),
153 StringPool.SLASH + newGroupFriendlyURL + StringPool.SLASH,
154 StringPool.SLASH +
155 ExportImportHelper.DATA_HANDLER_GROUP_FRIENDLY_URL +
156 StringPool.SLASH);
157
158 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
159 }
160
161 portletDataContext.addClassedModel(
162 feedElement, ExportImportPathUtil.getModelPath(feed), feed,
163 JournalPortletDataHandler.NAMESPACE);
164 }
165
166 @Override
167 protected void doImportStagedModel(
168 PortletDataContext portletDataContext, JournalFeed feed)
169 throws Exception {
170
171 long userId = portletDataContext.getUserId(feed.getUserUuid());
172
173 JournalCreationStrategy creationStrategy =
174 JournalCreationStrategyFactory.getInstance();
175
176 long authorId = creationStrategy.getAuthorUserId(
177 portletDataContext, feed);
178
179 if (authorId != JournalCreationStrategy.USE_DEFAULT_USER_ID_STRATEGY) {
180 userId = authorId;
181 }
182
183 Group group = GroupLocalServiceUtil.getGroup(
184 portletDataContext.getScopeGroupId());
185
186 String newGroupFriendlyURL = group.getFriendlyURL().substring(1);
187
188 String[] friendlyURLParts = StringUtil.split(
189 feed.getTargetLayoutFriendlyUrl(), '/');
190
191 String oldGroupFriendlyURL = friendlyURLParts[2];
192
193 if (oldGroupFriendlyURL.equals(
194 ExportImportHelper.DATA_HANDLER_GROUP_FRIENDLY_URL)) {
195
196 feed.setTargetLayoutFriendlyUrl(
197 StringUtil.replace(
198 feed.getTargetLayoutFriendlyUrl(),
199 ExportImportHelper.DATA_HANDLER_GROUP_FRIENDLY_URL,
200 newGroupFriendlyURL));
201 }
202
203 String feedId = feed.getFeedId();
204
205 boolean autoFeedId = false;
206
207 if (Validator.isNumber(feedId) ||
208 (JournalFeedLocalServiceUtil.fetchFeed(
209 portletDataContext.getScopeGroupId(), feedId) != null)) {
210
211 autoFeedId = true;
212 }
213
214 List<Element> ddmStructureElements =
215 portletDataContext.getReferenceDataElements(
216 feed, DDMStructure.class);
217
218 String parentDDMStructureKey = StringPool.BLANK;
219
220 if (!ddmStructureElements.isEmpty()) {
221 Element ddmStructureElement = ddmStructureElements.get(0);
222
223 String ddmStructurePath = ddmStructureElement.attributeValue(
224 "path");
225
226 DDMStructure ddmStructure =
227 (DDMStructure)portletDataContext.getZipEntryAsObject(
228 ddmStructurePath);
229
230 StagedModelDataHandlerUtil.importReferenceStagedModel(
231 portletDataContext, ddmStructure);
232
233 Map<String, String> ddmStructureKeys =
234 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
235 DDMStructure.class + ".ddmStructureKey");
236
237 parentDDMStructureKey = MapUtil.getString(
238 ddmStructureKeys, ddmStructure.getStructureKey(),
239 ddmStructure.getStructureKey());
240 }
241
242 List<Element> ddmTemplateElements =
243 portletDataContext.getReferenceDataElements(
244 feed, DDMTemplate.class);
245
246 String parentDDMTemplateKey = StringPool.BLANK;
247 String parentRendererDDMTemplateKey = StringPool.BLANK;
248
249 for (Element ddmTemplateElement : ddmTemplateElements) {
250 String ddmTemplatePath = ddmTemplateElement.attributeValue("path");
251
252 DDMTemplate ddmTemplate =
253 (DDMTemplate)portletDataContext.getZipEntryAsObject(
254 ddmTemplatePath);
255
256 StagedModelDataHandlerUtil.importReferenceStagedModel(
257 portletDataContext, ddmTemplate);
258
259 Map<String, String> ddmTemplateKeys =
260 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
261 DDMTemplate.class + ".ddmTemplateKey");
262
263 boolean rendererDDMTemplate = GetterUtil.getBoolean(
264 ddmTemplateElement.attributeValue("rendererDDMTemplate"));
265
266 String ddmTemplateKey = MapUtil.getString(
267 ddmTemplateKeys, ddmTemplate.getTemplateKey(),
268 ddmTemplate.getTemplateKey());
269
270 if (rendererDDMTemplate) {
271 parentDDMTemplateKey = ddmTemplateKey;
272 }
273 else {
274 parentRendererDDMTemplateKey = ddmTemplateKey;
275 }
276 }
277
278 ServiceContext serviceContext = portletDataContext.createServiceContext(
279 feed, JournalPortletDataHandler.NAMESPACE);
280
281 boolean addGroupPermissions = creationStrategy.addGroupPermissions(
282 portletDataContext, feed);
283
284 serviceContext.setAddGroupPermissions(addGroupPermissions);
285
286 boolean addGuestPermissions = creationStrategy.addGuestPermissions(
287 portletDataContext, feed);
288
289 serviceContext.setAddGuestPermissions(addGuestPermissions);
290
291 JournalFeed importedFeed = null;
292
293 try {
294 if (portletDataContext.isDataStrategyMirror()) {
295 JournalFeed existingFeed =
296 JournalFeedLocalServiceUtil.
297 fetchJournalFeedByUuidAndGroupId(
298 feed.getUuid(),
299 portletDataContext.getScopeGroupId());
300
301 if (existingFeed == null) {
302 serviceContext.setUuid(feed.getUuid());
303
304 importedFeed = JournalFeedLocalServiceUtil.addFeed(
305 userId, portletDataContext.getScopeGroupId(), feedId,
306 autoFeedId, feed.getName(), feed.getDescription(),
307 feed.getType(), parentDDMStructureKey,
308 parentDDMTemplateKey, parentRendererDDMTemplateKey,
309 feed.getDelta(), feed.getOrderByCol(),
310 feed.getOrderByType(),
311 feed.getTargetLayoutFriendlyUrl(),
312 feed.getTargetPortletId(), feed.getContentField(),
313 feed.getFeedFormat(), feed.getFeedVersion(),
314 serviceContext);
315 }
316 else {
317 importedFeed = JournalFeedLocalServiceUtil.updateFeed(
318 existingFeed.getGroupId(), existingFeed.getFeedId(),
319 feed.getName(), feed.getDescription(), feed.getType(),
320 parentDDMStructureKey, parentDDMTemplateKey,
321 parentRendererDDMTemplateKey, feed.getDelta(),
322 feed.getOrderByCol(), feed.getOrderByType(),
323 feed.getTargetLayoutFriendlyUrl(),
324 feed.getTargetPortletId(), feed.getContentField(),
325 feed.getFeedFormat(), feed.getFeedVersion(),
326 serviceContext);
327 }
328 }
329 else {
330 importedFeed = JournalFeedLocalServiceUtil.addFeed(
331 userId, portletDataContext.getScopeGroupId(), feedId,
332 autoFeedId, feed.getName(), feed.getDescription(),
333 feed.getType(), parentDDMStructureKey, parentDDMTemplateKey,
334 parentRendererDDMTemplateKey, feed.getDelta(),
335 feed.getOrderByCol(), feed.getOrderByType(),
336 feed.getTargetLayoutFriendlyUrl(),
337 feed.getTargetPortletId(), feed.getContentField(),
338 feed.getFeedFormat(), feed.getFeedVersion(),
339 serviceContext);
340 }
341
342 portletDataContext.importClassedModel(
343 feed, importedFeed, JournalPortletDataHandler.NAMESPACE);
344
345 if (!feedId.equals(importedFeed.getFeedId())) {
346 if (_log.isWarnEnabled()) {
347 StringBundler sb = new StringBundler(5);
348
349 sb.append("A feed with the ID ");
350 sb.append(feedId);
351 sb.append(" already exists. The new generated ID is ");
352 sb.append(importedFeed.getFeedId());
353 sb.append(".");
354
355 _log.warn(sb.toString());
356 }
357 }
358 }
359 catch (FeedTargetLayoutFriendlyUrlException ftlfurle) {
360 if (_log.isWarnEnabled()) {
361 StringBundler sb = new StringBundler(6);
362
363 sb.append("A feed with the ID ");
364 sb.append(feedId);
365 sb.append(" cannot be imported because layout with friendly ");
366 sb.append("URL ");
367 sb.append(feed.getTargetLayoutFriendlyUrl());
368 sb.append(" does not exist");
369
370 _log.warn(sb.toString());
371 }
372 }
373 }
374
375 private static Log _log = LogFactoryUtil.getLog(
376 JournalFeedStagedModelDataHandler.class);
377
378 }