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