001
014
015 package com.liferay.portlet.journal.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.systemevent.SystemEvent;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.OrderByComparator;
023 import com.liferay.portal.kernel.util.RSSUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.ResourceConstants;
029 import com.liferay.portal.model.SystemEventConstants;
030 import com.liferay.portal.model.User;
031 import com.liferay.portal.service.ServiceContext;
032 import com.liferay.portal.util.PortalUtil;
033 import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
034 import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
035 import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldOptions;
036 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037 import com.liferay.portlet.journal.DuplicateFeedIdException;
038 import com.liferay.portlet.journal.FeedContentFieldException;
039 import com.liferay.portlet.journal.FeedIdException;
040 import com.liferay.portlet.journal.FeedNameException;
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.model.JournalFeedConstants;
045 import com.liferay.portlet.journal.service.base.JournalFeedLocalServiceBaseImpl;
046
047 import java.util.List;
048 import java.util.Map;
049
050
053 public class JournalFeedLocalServiceImpl
054 extends JournalFeedLocalServiceBaseImpl {
055
056 @Override
057 public JournalFeed addFeed(
058 long userId, long groupId, String feedId, boolean autoFeedId,
059 String name, String description, String ddmStructureKey,
060 String ddmTemplateKey, String ddmRendererTemplateKey, int delta,
061 String orderByCol, String orderByType,
062 String targetLayoutFriendlyUrl, String targetPortletId,
063 String contentField, String feedFormat, double feedVersion,
064 ServiceContext serviceContext)
065 throws PortalException {
066
067
068
069 User user = userPersistence.findByPrimaryKey(userId);
070 feedId = StringUtil.toUpperCase(feedId.trim());
071
072 validate(
073 user.getCompanyId(), groupId, feedId, autoFeedId, name,
074 ddmStructureKey, targetLayoutFriendlyUrl, contentField);
075
076 if (autoFeedId) {
077 feedId = String.valueOf(counterLocalService.increment());
078 }
079
080 long id = counterLocalService.increment();
081
082 JournalFeed feed = journalFeedPersistence.create(id);
083
084 feed.setUuid(serviceContext.getUuid());
085 feed.setGroupId(groupId);
086 feed.setCompanyId(user.getCompanyId());
087 feed.setUserId(user.getUserId());
088 feed.setUserName(user.getFullName());
089 feed.setFeedId(feedId);
090 feed.setName(name);
091 feed.setDescription(description);
092 feed.setDDMStructureKey(ddmStructureKey);
093 feed.setDDMTemplateKey(ddmTemplateKey);
094 feed.setDDMRendererTemplateKey(ddmRendererTemplateKey);
095 feed.setDelta(delta);
096 feed.setOrderByCol(orderByCol);
097 feed.setOrderByType(orderByType);
098 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
099 feed.setTargetPortletId(targetPortletId);
100 feed.setContentField(contentField);
101
102 if (Validator.isNull(feedFormat)) {
103 feed.setFeedFormat(RSSUtil.FORMAT_DEFAULT);
104 feed.setFeedVersion(RSSUtil.VERSION_DEFAULT);
105 }
106 else {
107 feed.setFeedFormat(feedFormat);
108 feed.setFeedVersion(feedVersion);
109 }
110
111 feed.setExpandoBridgeAttributes(serviceContext);
112
113 journalFeedPersistence.update(feed);
114
115
116
117 if (serviceContext.isAddGroupPermissions() ||
118 serviceContext.isAddGuestPermissions()) {
119
120 addFeedResources(
121 feed, serviceContext.isAddGroupPermissions(),
122 serviceContext.isAddGuestPermissions());
123 }
124 else {
125 addFeedResources(
126 feed, serviceContext.getGroupPermissions(),
127 serviceContext.getGuestPermissions());
128 }
129
130 return feed;
131 }
132
133 @Override
134 public void addFeedResources(
135 JournalFeed feed, boolean addGroupPermissions,
136 boolean addGuestPermissions)
137 throws PortalException {
138
139 resourceLocalService.addResources(
140 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
141 JournalFeed.class.getName(), feed.getId(), false,
142 addGroupPermissions, addGuestPermissions);
143 }
144
145 @Override
146 public void addFeedResources(
147 JournalFeed feed, String[] groupPermissions,
148 String[] guestPermissions)
149 throws PortalException {
150
151 resourceLocalService.addModelResources(
152 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
153 JournalFeed.class.getName(), feed.getId(), groupPermissions,
154 guestPermissions);
155 }
156
157 @Override
158 public void addFeedResources(
159 long feedId, boolean addGroupPermissions,
160 boolean addGuestPermissions)
161 throws PortalException {
162
163 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
164
165 addFeedResources(feed, addGroupPermissions, addGuestPermissions);
166 }
167
168 @Override
169 public void addFeedResources(
170 long feedId, String[] groupPermissions, String[] guestPermissions)
171 throws PortalException {
172
173 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
174
175 addFeedResources(feed, groupPermissions, guestPermissions);
176 }
177
178 @Override
179 @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
180 public void deleteFeed(JournalFeed feed) throws PortalException {
181
182
183
184 journalFeedPersistence.remove(feed);
185
186
187
188 resourceLocalService.deleteResource(
189 feed.getCompanyId(), JournalFeed.class.getName(),
190 ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
191
192
193
194 expandoValueLocalService.deleteValues(
195 JournalFeed.class.getName(), feed.getId());
196 }
197
198 @Override
199 public void deleteFeed(long feedId) throws PortalException {
200 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
201
202 journalFeedLocalService.deleteFeed(feed);
203 }
204
205 @Override
206 public void deleteFeed(long groupId, String feedId) throws PortalException {
207 JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
208
209 journalFeedLocalService.deleteFeed(feed);
210 }
211
212 @Override
213 public JournalFeed fetchFeed(long groupId, String feedId) {
214 return journalFeedPersistence.fetchByG_F(groupId, feedId);
215 }
216
217 @Override
218 public JournalFeed getFeed(long feedId) throws PortalException {
219 return journalFeedPersistence.findByPrimaryKey(feedId);
220 }
221
222 @Override
223 public JournalFeed getFeed(long groupId, String feedId)
224 throws PortalException {
225
226 return journalFeedPersistence.findByG_F(groupId, feedId);
227 }
228
229 @Override
230 public List<JournalFeed> getFeeds() {
231 return journalFeedPersistence.findAll();
232 }
233
234 @Override
235 public List<JournalFeed> getFeeds(long groupId) {
236 return journalFeedPersistence.findByGroupId(groupId);
237 }
238
239 @Override
240 public List<JournalFeed> getFeeds(long groupId, int start, int end) {
241 return journalFeedPersistence.findByGroupId(groupId, start, end);
242 }
243
244 @Override
245 public int getFeedsCount(long groupId) {
246 return journalFeedPersistence.countByGroupId(groupId);
247 }
248
249 @Override
250 public List<JournalFeed> search(
251 long companyId, long groupId, String keywords, int start, int end,
252 OrderByComparator<JournalFeed> obc) {
253
254 return journalFeedFinder.findByKeywords(
255 companyId, groupId, keywords, start, end, obc);
256 }
257
258 @Override
259 public List<JournalFeed> search(
260 long companyId, long groupId, String feedId, String name,
261 String description, boolean andOperator, int start, int end,
262 OrderByComparator<JournalFeed> obc) {
263
264 return journalFeedFinder.findByC_G_F_N_D(
265 companyId, groupId, feedId, name, description, andOperator, start,
266 end, obc);
267 }
268
269 @Override
270 public int searchCount(long companyId, long groupId, String keywords) {
271 return journalFeedFinder.countByKeywords(companyId, groupId, keywords);
272 }
273
274 @Override
275 public int searchCount(
276 long companyId, long groupId, String feedId, String name,
277 String description, boolean andOperator) {
278
279 return journalFeedFinder.countByC_G_F_N_D(
280 companyId, groupId, feedId, name, description, andOperator);
281 }
282
283 @Override
284 public JournalFeed updateFeed(
285 long groupId, String feedId, String name, String description,
286 String ddmStructureKey, String ddmTemplateKey,
287 String ddmRendererTemplateKey, int delta, String orderByCol,
288 String orderByType, String targetLayoutFriendlyUrl,
289 String targetPortletId, String contentField, String feedFormat,
290 double feedVersion, ServiceContext serviceContext)
291 throws PortalException {
292
293
294
295 JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
296
297 validate(
298 feed.getCompanyId(), groupId, name, ddmStructureKey,
299 targetLayoutFriendlyUrl, contentField);
300
301 feed.setName(name);
302 feed.setDescription(description);
303 feed.setDDMStructureKey(ddmStructureKey);
304 feed.setDDMTemplateKey(ddmTemplateKey);
305 feed.setDDMRendererTemplateKey(ddmRendererTemplateKey);
306 feed.setDelta(delta);
307 feed.setOrderByCol(orderByCol);
308 feed.setOrderByType(orderByType);
309 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
310 feed.setTargetPortletId(targetPortletId);
311 feed.setContentField(contentField);
312
313 if (Validator.isNull(feedFormat)) {
314 feed.setFeedFormat(RSSUtil.FORMAT_DEFAULT);
315 feed.setFeedVersion(RSSUtil.VERSION_DEFAULT);
316 }
317 else {
318 feed.setFeedFormat(feedFormat);
319 feed.setFeedVersion(feedVersion);
320 }
321
322 feed.setExpandoBridgeAttributes(serviceContext);
323
324 journalFeedPersistence.update(feed);
325
326 return feed;
327 }
328
329 protected boolean isValidStructureField(
330 long groupId, String ddmStructureKey, String contentField) {
331
332 if (contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION) ||
333 contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) {
334
335 return true;
336 }
337
338 try {
339 DDMStructure ddmStructure = ddmStructureLocalService.getStructure(
340 groupId,
341 classNameLocalService.getClassNameId(JournalArticle.class),
342 ddmStructureKey);
343
344 DDMForm ddmForm = ddmStructure.getDDMForm();
345
346 Map<String, DDMFormField> ddmFormFieldsMap =
347 ddmForm.getDDMFormFieldsMap(true);
348
349 if (ddmFormFieldsMap.containsKey(contentField)) {
350 return true;
351 }
352
353 return isValidStructureOptionValue(ddmFormFieldsMap, contentField);
354 }
355 catch (Exception e) {
356 _log.error(e, e);
357 }
358
359 return false;
360 }
361
362 protected boolean isValidStructureOptionValue(
363 Map<String, DDMFormField> ddmFormFieldsMap, String contentField) {
364
365 for (DDMFormField ddmFormField : ddmFormFieldsMap.values()) {
366 String ddmFormFieldType = ddmFormField.getType();
367
368 if (!(ddmFormFieldType.equals("radio") ||
369 ddmFormFieldType.equals("select"))) {
370
371 continue;
372 }
373
374 DDMFormFieldOptions ddmFormFieldOptions =
375 ddmFormField.getDDMFormFieldOptions();
376
377 for (String optionValue : ddmFormFieldOptions.getOptionsValues()) {
378 optionValue =
379 ddmFormField.getName() + StringPool.UNDERLINE + optionValue;
380
381 if (contentField.equals(optionValue)) {
382 return true;
383 }
384 }
385 }
386
387 return false;
388 }
389
390 protected void validate(
391 long companyId, long groupId, String feedId, boolean autoFeedId,
392 String name, String ddmStructureKey, String targetLayoutFriendlyUrl,
393 String contentField)
394 throws PortalException {
395
396 if (!autoFeedId) {
397 if (Validator.isNull(feedId) || Validator.isNumber(feedId) ||
398 (feedId.indexOf(CharPool.COMMA) != -1) ||
399 (feedId.indexOf(CharPool.SPACE) != -1)) {
400
401 throw new FeedIdException();
402 }
403
404 JournalFeed feed = journalFeedPersistence.fetchByG_F(
405 groupId, feedId);
406
407 if (feed != null) {
408 StringBundler sb = new StringBundler(5);
409
410 sb.append("{groupId=");
411 sb.append(groupId);
412 sb.append(", feedId=");
413 sb.append(feedId);
414 sb.append("}");
415
416 throw new DuplicateFeedIdException(sb.toString());
417 }
418 }
419
420 validate(
421 companyId, groupId, name, ddmStructureKey, targetLayoutFriendlyUrl,
422 contentField);
423 }
424
425 protected void validate(
426 long companyId, long groupId, String name, String ddmStructureKey,
427 String targetLayoutFriendlyUrl, String contentField)
428 throws PortalException {
429
430 if (Validator.isNull(name)) {
431 throw new FeedNameException();
432 }
433
434 long plid = PortalUtil.getPlidFromFriendlyURL(
435 companyId, targetLayoutFriendlyUrl);
436
437 if (plid <= 0) {
438 throw new FeedTargetLayoutFriendlyUrlException();
439 }
440
441 if (!isValidStructureField(groupId, ddmStructureKey, contentField)) {
442 throw new FeedContentFieldException();
443 }
444 }
445
446 private static final Log _log = LogFactoryUtil.getLog(
447 JournalFeedLocalServiceImpl.class);
448
449 }