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