PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/portal-impl/src/com/liferay/portlet/journal/service/impl/JournalFeedLocalServiceImpl.java

https://github.com/lululiferay/liferay-portal
Java | 411 lines | 294 code | 93 blank | 24 comment | 20 complexity | cc13c20b7d9212b42739b428a6afdc15 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.journal.service.impl;
  15. import com.liferay.portal.kernel.exception.PortalException;
  16. import com.liferay.portal.kernel.exception.SystemException;
  17. import com.liferay.portal.kernel.log.Log;
  18. import com.liferay.portal.kernel.log.LogFactoryUtil;
  19. import com.liferay.portal.kernel.util.CharPool;
  20. import com.liferay.portal.kernel.util.OrderByComparator;
  21. import com.liferay.portal.kernel.util.Validator;
  22. import com.liferay.portal.kernel.xml.Document;
  23. import com.liferay.portal.kernel.xml.Node;
  24. import com.liferay.portal.kernel.xml.SAXReaderUtil;
  25. import com.liferay.portal.kernel.xml.XPath;
  26. import com.liferay.portal.model.ResourceConstants;
  27. import com.liferay.portal.model.User;
  28. import com.liferay.portal.service.ServiceContext;
  29. import com.liferay.portal.util.PortalUtil;
  30. import com.liferay.portlet.expando.model.ExpandoBridge;
  31. import com.liferay.portlet.journal.DuplicateFeedIdException;
  32. import com.liferay.portlet.journal.FeedContentFieldException;
  33. import com.liferay.portlet.journal.FeedIdException;
  34. import com.liferay.portlet.journal.FeedNameException;
  35. import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
  36. import com.liferay.portlet.journal.model.JournalFeed;
  37. import com.liferay.portlet.journal.model.JournalFeedConstants;
  38. import com.liferay.portlet.journal.model.JournalStructure;
  39. import com.liferay.portlet.journal.service.base.JournalFeedLocalServiceBaseImpl;
  40. import com.liferay.util.RSSUtil;
  41. import java.util.Date;
  42. import java.util.List;
  43. /**
  44. * @author Raymond Augé
  45. */
  46. public class JournalFeedLocalServiceImpl
  47. extends JournalFeedLocalServiceBaseImpl {
  48. public JournalFeed addFeed(
  49. long userId, long groupId, String feedId, boolean autoFeedId,
  50. String name, String description, String type, String structureId,
  51. String templateId, String rendererTemplateId, int delta,
  52. String orderByCol, String orderByType,
  53. String targetLayoutFriendlyUrl, String targetPortletId,
  54. String contentField, String feedType, double feedVersion,
  55. ServiceContext serviceContext)
  56. throws PortalException, SystemException {
  57. // Feed
  58. User user = userPersistence.findByPrimaryKey(userId);
  59. feedId = feedId.trim().toUpperCase();
  60. Date now = new Date();
  61. validate(
  62. user.getCompanyId(), groupId, feedId, autoFeedId, name, structureId,
  63. targetLayoutFriendlyUrl, contentField);
  64. if (autoFeedId) {
  65. feedId = String.valueOf(counterLocalService.increment());
  66. }
  67. long id = counterLocalService.increment();
  68. JournalFeed feed = journalFeedPersistence.create(id);
  69. feed.setUuid(serviceContext.getUuid());
  70. feed.setGroupId(groupId);
  71. feed.setCompanyId(user.getCompanyId());
  72. feed.setUserId(user.getUserId());
  73. feed.setUserName(user.getFullName());
  74. feed.setCreateDate(serviceContext.getCreateDate(now));
  75. feed.setModifiedDate(serviceContext.getModifiedDate(now));
  76. feed.setFeedId(feedId);
  77. feed.setName(name);
  78. feed.setDescription(description);
  79. feed.setType(type);
  80. feed.setStructureId(structureId);
  81. feed.setTemplateId(templateId);
  82. feed.setRendererTemplateId(rendererTemplateId);
  83. feed.setDelta(delta);
  84. feed.setOrderByCol(orderByCol);
  85. feed.setOrderByType(orderByType);
  86. feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
  87. feed.setTargetPortletId(targetPortletId);
  88. feed.setContentField(contentField);
  89. if (Validator.isNull(feedType)) {
  90. feed.setFeedType(RSSUtil.TYPE_DEFAULT);
  91. feed.setFeedVersion(RSSUtil.VERSION_DEFAULT);
  92. }
  93. else {
  94. feed.setFeedType(feedType);
  95. feed.setFeedVersion(feedVersion);
  96. }
  97. journalFeedPersistence.update(feed, false);
  98. // Resources
  99. if (serviceContext.isAddGroupPermissions() ||
  100. serviceContext.isAddGuestPermissions()) {
  101. addFeedResources(
  102. feed, serviceContext.isAddGroupPermissions(),
  103. serviceContext.isAddGuestPermissions());
  104. }
  105. else {
  106. addFeedResources(
  107. feed, serviceContext.getGroupPermissions(),
  108. serviceContext.getGuestPermissions());
  109. }
  110. // Expando
  111. ExpandoBridge expandoBridge = feed.getExpandoBridge();
  112. expandoBridge.setAttributes(serviceContext);
  113. return feed;
  114. }
  115. public void addFeedResources(
  116. JournalFeed feed, boolean addGroupPermissions,
  117. boolean addGuestPermissions)
  118. throws PortalException, SystemException {
  119. resourceLocalService.addResources(
  120. feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
  121. JournalFeed.class.getName(), feed.getId(), false,
  122. addGroupPermissions, addGuestPermissions);
  123. }
  124. public void addFeedResources(
  125. JournalFeed feed, String[] groupPermissions,
  126. String[] guestPermissions)
  127. throws PortalException, SystemException {
  128. resourceLocalService.addModelResources(
  129. feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
  130. JournalFeed.class.getName(), feed.getId(), groupPermissions,
  131. guestPermissions);
  132. }
  133. public void addFeedResources(
  134. long feedId, boolean addGroupPermissions,
  135. boolean addGuestPermissions)
  136. throws PortalException, SystemException {
  137. JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
  138. addFeedResources(feed, addGroupPermissions, addGuestPermissions);
  139. }
  140. public void addFeedResources(
  141. long feedId, String[] groupPermissions, String[] guestPermissions)
  142. throws PortalException, SystemException {
  143. JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
  144. addFeedResources(feed, groupPermissions, guestPermissions);
  145. }
  146. public void deleteFeed(JournalFeed feed)
  147. throws PortalException, SystemException {
  148. // Expando
  149. expandoValueLocalService.deleteValues(
  150. JournalFeed.class.getName(), feed.getId());
  151. // Resources
  152. resourceLocalService.deleteResource(
  153. feed.getCompanyId(), JournalFeed.class.getName(),
  154. ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
  155. // Feed
  156. journalFeedPersistence.remove(feed);
  157. }
  158. public void deleteFeed(long feedId)
  159. throws PortalException, SystemException {
  160. JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
  161. deleteFeed(feed);
  162. }
  163. public void deleteFeed(long groupId, String feedId)
  164. throws PortalException, SystemException {
  165. JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
  166. deleteFeed(feed);
  167. }
  168. public JournalFeed getFeed(long feedId)
  169. throws PortalException, SystemException {
  170. return journalFeedPersistence.findByPrimaryKey(feedId);
  171. }
  172. public JournalFeed getFeed(long groupId, String feedId)
  173. throws PortalException, SystemException {
  174. return journalFeedPersistence.findByG_F(groupId, feedId);
  175. }
  176. public List<JournalFeed> getFeeds() throws SystemException {
  177. return journalFeedPersistence.findAll();
  178. }
  179. public List<JournalFeed> getFeeds(long groupId) throws SystemException {
  180. return journalFeedPersistence.findByGroupId(groupId);
  181. }
  182. public List<JournalFeed> getFeeds(long groupId, int start, int end)
  183. throws SystemException {
  184. return journalFeedPersistence.findByGroupId(groupId, start, end);
  185. }
  186. public int getFeedsCount(long groupId) throws SystemException {
  187. return journalFeedPersistence.countByGroupId(groupId);
  188. }
  189. public List<JournalFeed> search(
  190. long companyId, long groupId, String keywords, int start, int end,
  191. OrderByComparator obc)
  192. throws SystemException {
  193. return journalFeedFinder.findByKeywords(
  194. companyId, groupId, keywords, start, end, obc);
  195. }
  196. public List<JournalFeed> search(
  197. long companyId, long groupId, String feedId, String name,
  198. String description, boolean andOperator, int start, int end,
  199. OrderByComparator obc)
  200. throws SystemException {
  201. return journalFeedFinder.findByC_G_F_N_D(
  202. companyId, groupId, feedId, name, description, andOperator, start,
  203. end, obc);
  204. }
  205. public int searchCount(long companyId, long groupId, String keywords)
  206. throws SystemException {
  207. return journalFeedFinder.countByKeywords(companyId, groupId, keywords);
  208. }
  209. public int searchCount(
  210. long companyId, long groupId, String feedId, String name,
  211. String description, boolean andOperator)
  212. throws SystemException {
  213. return journalFeedFinder.countByC_G_F_N_D(
  214. companyId, groupId, feedId, name, description, andOperator);
  215. }
  216. public JournalFeed updateFeed(
  217. long groupId, String feedId, String name, String description,
  218. String type, String structureId, String templateId,
  219. String rendererTemplateId, int delta, String orderByCol,
  220. String orderByType, String targetLayoutFriendlyUrl,
  221. String targetPortletId, String contentField, String feedType,
  222. double feedVersion, ServiceContext serviceContext)
  223. throws PortalException, SystemException {
  224. // Feed
  225. JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
  226. validate(
  227. feed.getCompanyId(), groupId, name, structureId,
  228. targetLayoutFriendlyUrl, contentField);
  229. feed.setModifiedDate(serviceContext.getModifiedDate(null));
  230. feed.setName(name);
  231. feed.setDescription(description);
  232. feed.setType(type);
  233. feed.setStructureId(structureId);
  234. feed.setTemplateId(templateId);
  235. feed.setRendererTemplateId(rendererTemplateId);
  236. feed.setDelta(delta);
  237. feed.setOrderByCol(orderByCol);
  238. feed.setOrderByType(orderByType);
  239. feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
  240. feed.setTargetPortletId(targetPortletId);
  241. feed.setContentField(contentField);
  242. if (Validator.isNull(feedType)) {
  243. feed.setFeedType(RSSUtil.TYPE_DEFAULT);
  244. feed.setFeedVersion(RSSUtil.VERSION_DEFAULT);
  245. }
  246. else {
  247. feed.setFeedType(feedType);
  248. feed.setFeedVersion(feedVersion);
  249. }
  250. journalFeedPersistence.update(feed, false);
  251. // Expando
  252. ExpandoBridge expandoBridge = feed.getExpandoBridge();
  253. expandoBridge.setAttributes(serviceContext);
  254. return feed;
  255. }
  256. protected boolean isValidStructureField(
  257. long groupId, String structureId, String contentField) {
  258. if (contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION) ||
  259. contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) {
  260. return true;
  261. }
  262. else {
  263. try {
  264. JournalStructure structure =
  265. journalStructurePersistence.findByG_S(groupId, structureId);
  266. Document document = SAXReaderUtil.read(structure.getXsd());
  267. XPath xPathSelector = SAXReaderUtil.createXPath(
  268. "//dynamic-element[@name='"+ contentField + "']");
  269. Node node = xPathSelector.selectSingleNode(document);
  270. if (node != null) {
  271. return true;
  272. }
  273. }
  274. catch (Exception e) {
  275. _log.error(e, e);
  276. }
  277. }
  278. return false;
  279. }
  280. protected void validate(
  281. long companyId, long groupId, String feedId, boolean autoFeedId,
  282. String name, String structureId, String targetLayoutFriendlyUrl,
  283. String contentField)
  284. throws PortalException, SystemException {
  285. if (!autoFeedId) {
  286. if (Validator.isNull(feedId) || Validator.isNumber(feedId) ||
  287. (feedId.indexOf(CharPool.SPACE) != -1)) {
  288. throw new FeedIdException();
  289. }
  290. JournalFeed feed = journalFeedPersistence.fetchByG_F(
  291. groupId, feedId);
  292. if (feed != null) {
  293. throw new DuplicateFeedIdException();
  294. }
  295. }
  296. validate(
  297. companyId, groupId, name, structureId, targetLayoutFriendlyUrl,
  298. contentField);
  299. }
  300. protected void validate(
  301. long companyId, long groupId, String name, String structureId,
  302. String targetLayoutFriendlyUrl, String contentField)
  303. throws PortalException {
  304. if (Validator.isNull(name)) {
  305. throw new FeedNameException();
  306. }
  307. long plid = PortalUtil.getPlidFromFriendlyURL(
  308. companyId, targetLayoutFriendlyUrl);
  309. if (plid <= 0) {
  310. throw new FeedTargetLayoutFriendlyUrlException();
  311. }
  312. if (!isValidStructureField(groupId, structureId, contentField)) {
  313. throw new FeedContentFieldException();
  314. }
  315. }
  316. private static Log _log = LogFactoryUtil.getLog(
  317. JournalFeedLocalServiceImpl.class);
  318. }