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