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.util.OrderByComparator;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.kernel.xml.Document;
023 import com.liferay.portal.kernel.xml.Element;
024 import com.liferay.portal.kernel.xml.SAXReaderUtil;
025 import com.liferay.portal.kernel.xml.XPath;
026 import com.liferay.portal.model.ResourceConstants;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portlet.expando.model.ExpandoBridge;
031 import com.liferay.portlet.journal.DuplicateFeedIdException;
032 import com.liferay.portlet.journal.FeedContentFieldException;
033 import com.liferay.portlet.journal.FeedDescriptionException;
034 import com.liferay.portlet.journal.FeedIdException;
035 import com.liferay.portlet.journal.FeedNameException;
036 import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
037 import com.liferay.portlet.journal.model.JournalFeed;
038 import com.liferay.portlet.journal.model.JournalFeedConstants;
039 import com.liferay.portlet.journal.model.JournalStructure;
040 import com.liferay.portlet.journal.service.base.JournalFeedLocalServiceBaseImpl;
041 import com.liferay.util.RSSUtil;
042
043 import java.util.Date;
044 import java.util.List;
045
046
049 public class JournalFeedLocalServiceImpl
050 extends JournalFeedLocalServiceBaseImpl {
051
052 public JournalFeed addFeed(
053 long userId, long groupId, String feedId, boolean autoFeedId,
054 String name, String description, String type, String structureId,
055 String templateId, String rendererTemplateId, int delta,
056 String orderByCol, String orderByType,
057 String targetLayoutFriendlyUrl, String targetPortletId,
058 String contentField, String feedType, double feedVersion,
059 ServiceContext serviceContext)
060 throws PortalException, SystemException {
061
062
063
064 User user = userPersistence.findByPrimaryKey(userId);
065 feedId = feedId.trim().toUpperCase();
066 Date now = new Date();
067
068 validate(
069 user.getCompanyId(), groupId, feedId, autoFeedId, name, description,
070 structureId, targetLayoutFriendlyUrl, contentField);
071
072 if (autoFeedId) {
073 feedId = String.valueOf(counterLocalService.increment());
074 }
075
076 long id = counterLocalService.increment();
077
078 JournalFeed feed = journalFeedPersistence.create(id);
079
080 feed.setUuid(serviceContext.getUuid());
081 feed.setGroupId(groupId);
082 feed.setCompanyId(user.getCompanyId());
083 feed.setUserId(user.getUserId());
084 feed.setUserName(user.getFullName());
085 feed.setCreateDate(serviceContext.getCreateDate(now));
086 feed.setModifiedDate(serviceContext.getModifiedDate(now));
087 feed.setFeedId(feedId);
088 feed.setName(name);
089 feed.setDescription(description);
090 feed.setType(type);
091 feed.setStructureId(structureId);
092 feed.setTemplateId(templateId);
093 feed.setRendererTemplateId(rendererTemplateId);
094 feed.setDelta(delta);
095 feed.setOrderByCol(orderByCol);
096 feed.setOrderByType(orderByType);
097 feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
098 feed.setTargetPortletId(targetPortletId);
099 feed.setContentField(contentField);
100
101 if (Validator.isNull(feedType)) {
102 feed.setFeedType(RSSUtil.DEFAULT_TYPE);
103 feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
104 }
105 else {
106 feed.setFeedType(feedType);
107 feed.setFeedVersion(feedVersion);
108 }
109
110 journalFeedPersistence.update(feed, false);
111
112
113
114 if (serviceContext.getAddCommunityPermissions() ||
115 serviceContext.getAddGuestPermissions()) {
116
117 addFeedResources(
118 feed, serviceContext.getAddCommunityPermissions(),
119 serviceContext.getAddGuestPermissions());
120 }
121 else {
122 addFeedResources(
123 feed, serviceContext.getCommunityPermissions(),
124 serviceContext.getGuestPermissions());
125 }
126
127
128
129 ExpandoBridge expandoBridge = feed.getExpandoBridge();
130
131 expandoBridge.setAttributes(serviceContext);
132
133 return feed;
134 }
135
136 public void addFeedResources(
137 long feedId, boolean addCommunityPermissions,
138 boolean addGuestPermissions)
139 throws PortalException, SystemException {
140
141 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
142
143 addFeedResources(feed, addCommunityPermissions, addGuestPermissions);
144 }
145
146 public void addFeedResources(
147 JournalFeed feed, boolean addCommunityPermissions,
148 boolean addGuestPermissions)
149 throws PortalException, SystemException {
150
151 resourceLocalService.addResources(
152 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
153 JournalFeed.class.getName(), feed.getId(), false,
154 addCommunityPermissions, addGuestPermissions);
155 }
156
157 public void addFeedResources(
158 long feedId, String[] communityPermissions,
159 String[] guestPermissions)
160 throws PortalException, SystemException {
161
162 JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
163
164 addFeedResources(feed, communityPermissions, guestPermissions);
165 }
166
167 public void addFeedResources(
168 JournalFeed feed, String[] communityPermissions,
169 String[] guestPermissions)
170 throws PortalException, SystemException {
171
172 resourceLocalService.addModelResources(
173 feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
174 JournalFeed.class.getName(), feed.getId(), communityPermissions,
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, description, 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.DEFAULT_TYPE);
312 feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
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 Element el = (Element)xpathSelector.selectSingleNode(doc);
349
350 if (el != null) {
351 return true;
352 }
353 }
354 catch (Exception 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 description, String structureId,
364 String targetLayoutFriendlyUrl, String contentField)
365 throws PortalException, SystemException {
366
367 if (!autoFeedId) {
368 if ((Validator.isNull(feedId)) || (Validator.isNumber(feedId)) ||
369 (feedId.indexOf(StringPool.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, description, structureId,
384 targetLayoutFriendlyUrl, contentField);
385 }
386
387 protected void validate(
388 long companyId, long groupId, String name, String description,
389 String structureId, String targetLayoutFriendlyUrl,
390 String contentField)
391 throws PortalException {
392
393 if (Validator.isNull(name)) {
394 throw new FeedNameException();
395 }
396
397 if (Validator.isNull(description)) {
398 throw new FeedDescriptionException();
399 }
400
401 long plid = PortalUtil.getPlidFromFriendlyURL(
402 companyId, targetLayoutFriendlyUrl);
403
404 if (plid <= 0) {
405 throw new FeedTargetLayoutFriendlyUrlException();
406 }
407
408 if (!isValidStructureField(groupId, structureId, contentField)) {
409 throw new FeedContentFieldException();
410 }
411 }
412
413 }