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.getAddGroupPermissions() ||
116 serviceContext.getAddGuestPermissions()) {
117
118 addFeedResources(
119 feed, serviceContext.getAddGroupPermissions(),
120 serviceContext.getAddGuestPermissions());
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 long feedId, boolean addGroupPermissions,
139 boolean addGuestPermissions)
140 throws PortalException, SystemException {
141
142 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
143
144 addFeedResources(feed, addGroupPermissions, addGuestPermissions);
145 }
146
147 public void addFeedResources(
148 JournalFeed feed, boolean addGroupPermissions,
149 boolean addGuestPermissions)
150 throws PortalException, SystemException {
151
152 resourceLocalService.addResources(
153 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
154 JournalFeed.class.getName(), feed.getId(), false,
155 addGroupPermissions, addGuestPermissions);
156 }
157
158 public void addFeedResources(
159 long feedId, String[] groupPermissions, String[] guestPermissions)
160 throws PortalException, SystemException {
161
162 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
163
164 addFeedResources(feed, groupPermissions, guestPermissions);
165 }
166
167 public void addFeedResources(
168 JournalFeed feed, String[] groupPermissions,
169 String[] guestPermissions)
170 throws PortalException, SystemException {
171
172 resourceLocalService.addModelResources(
173 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
174 JournalFeed.class.getName(), feed.getId(), groupPermissions,
175 guestPermissions);
176 }
177
178 public void deleteFeed(long feedId)
179 throws PortalException, SystemException {
180
181 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
182
183 deleteFeed(feed);
184 }
185
186 public void deleteFeed(long groupId, String feedId)
187 throws PortalException, SystemException {
188
189 JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
190
191 deleteFeed(feed);
192 }
193
194 public void deleteFeed(JournalFeed feed)
195 throws PortalException, SystemException {
196
197
198
199 expandoValueLocalService.deleteValues(
200 JournalFeed.class.getName(), feed.getId());
201
202
203
204 resourceLocalService.deleteResource(
205 feed.getCompanyId(), JournalFeed.class.getName(),
206 ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
207
208
209
210 journalFeedPersistence.remove(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(
267 companyId, groupId, keywords);
268 }
269
270 public int searchCount(
271 long companyId, long groupId, String feedId, String name,
272 String description, boolean andOperator)
273 throws SystemException {
274
275 return journalFeedFinder.countByC_G_F_N_D(
276 companyId, groupId, feedId, name, description, andOperator);
277 }
278
279 public JournalFeed updateFeed(
280 long groupId, String feedId, String name, String description,
281 String type, String structureId, String templateId,
282 String rendererTemplateId, int delta, String orderByCol,
283 String orderByType, String targetLayoutFriendlyUrl,
284 String targetPortletId, String contentField, String feedType,
285 double feedVersion, ServiceContext serviceContext)
286 throws PortalException, SystemException{
287
288
289
290 JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
291
292 validate(
293 feed.getCompanyId(), groupId, name, structureId,
294 targetLayoutFriendlyUrl, contentField);
295
296 feed.setModifiedDate(serviceContext.getModifiedDate(null));
297 feed.setName(name);
298 feed.setDescription(description);
299 feed.setType(type);
300 feed.setStructureId(structureId);
301 feed.setTemplateId(templateId);
302 feed.setRendererTemplateId(rendererTemplateId);
303 feed.setDelta(delta);
304 feed.setOrderByCol(orderByCol);
305 feed.setOrderByType(orderByType);
306 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
307 feed.setTargetPortletId(targetPortletId);
308 feed.setContentField(contentField);
309
310 if (Validator.isNull(feedType)) {
311 feed.setFeedType(RSSUtil.TYPE_DEFAULT);
312 feed.setFeedVersion(RSSUtil.VERSION_DEFAULT);
313 }
314 else {
315 feed.setFeedType(feedType);
316 feed.setFeedVersion(feedVersion);
317 }
318
319 journalFeedPersistence.update(feed, false);
320
321
322
323 ExpandoBridge expandoBridge = feed.getExpandoBridge();
324
325 expandoBridge.setAttributes(serviceContext);
326
327 return feed;
328 }
329
330 protected boolean isValidStructureField(
331 long groupId, String structureId, String contentField) {
332
333 if (contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION) ||
334 contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) {
335
336 return true;
337 }
338 else {
339 try {
340 JournalStructure structure =
341 journalStructurePersistence.findByG_S(groupId, structureId);
342
343 Document doc = SAXReaderUtil.read(structure.getXsd());
344
345 XPath xpathSelector = SAXReaderUtil.createXPath(
346 "
347
348 Node node = xpathSelector.selectSingleNode(doc);
349
350 if (node != null) {
351 return true;
352 }
353 }
354 catch (Exception e) {
355 _log.error(e, e);
356 }
357 }
358
359 return false;
360 }
361
362 protected void validate(
363 long companyId, long groupId, String feedId, boolean autoFeedId,
364 String name, String structureId, String targetLayoutFriendlyUrl,
365 String contentField)
366 throws PortalException, SystemException {
367
368 if (!autoFeedId) {
369 if ((Validator.isNull(feedId)) || (Validator.isNumber(feedId)) ||
370 (feedId.indexOf(CharPool.SPACE) != -1)) {
371
372 throw new FeedIdException();
373 }
374
375 JournalFeed feed = journalFeedPersistence.fetchByG_F(
376 groupId, feedId);
377
378 if (feed != null) {
379 throw new DuplicateFeedIdException();
380 }
381 }
382
383 validate(
384 companyId, groupId, name, structureId, targetLayoutFriendlyUrl,
385 contentField);
386 }
387
388 protected void validate(
389 long companyId, long groupId, String name, String structureId,
390 String targetLayoutFriendlyUrl, String contentField)
391 throws PortalException {
392
393 if (Validator.isNull(name)) {
394 throw new FeedNameException();
395 }
396
397 long plid = PortalUtil.getPlidFromFriendlyURL(
398 companyId, targetLayoutFriendlyUrl);
399
400 if (plid <= 0) {
401 throw new FeedTargetLayoutFriendlyUrlException();
402 }
403
404 if (!isValidStructureField(groupId, structureId, contentField)) {
405 throw new FeedContentFieldException();
406 }
407 }
408
409 private static Log _log = LogFactoryUtil.getLog(
410 JournalFeedLocalServiceImpl.class);
411
412 }