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