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

/grails-app/services/org/fogbeam/neddick/EntryService.groovy

http://neddick.googlecode.com/
Groovy | 535 lines | 423 code | 83 blank | 29 comment | 16 complexity | 6598421b5104f810ed7dd9b96809aca5 MD5 | raw file
  1. package org.fogbeam.neddick
  2. import org.apache.commons.logging.Log
  3. import org.apache.commons.logging.LogFactory
  4. import org.springframework.transaction.annotation.*
  5. class EntryService {
  6. Log cacheLog = LogFactory.getLog( "logger.special.instrumentation.cache" );
  7. def sessionFactory;
  8. private static final long BEGINNING_OF_TIME = 1230786000000; // 01 01 2009 00:00:00
  9. @Transactional(propagation = Propagation.REQUIRED)
  10. public Entry findById( final String id )
  11. {
  12. Entry entry = Entry.findById( id );
  13. return entry;
  14. }
  15. @Transactional(propagation = Propagation.REQUIRED)
  16. public Entry findByUuid( final String uuid )
  17. {
  18. Entry entry = Entry.findByUuid( uuid );
  19. return entry;
  20. }
  21. @Transactional(propagation = Propagation.REQUIRED)
  22. public List<Entry> findByUrl( final String url )
  23. {
  24. // check if we already have an Entry for this same link
  25. List<Entry> entries = Entry.executeQuery( "select entry from Entry as entry where entry.url = ?", [url] );
  26. return entries;
  27. }
  28. @Transactional(propagation = Propagation.REQUIRED)
  29. public List<Entry> findByUrlAndChannel( final String url, final Channel channel )
  30. {
  31. // check if this channel already has an Entry for this same link
  32. List<Entry> entries = Entry.executeQuery( "select entry from Entry as entry, ChannelEntryLink as clink where entry.url = ? and clink.entry = entry and clink.channel = ?", [url, channel] );
  33. return entries;
  34. }
  35. @Transactional(propagation = Propagation.REQUIRES_NEW)
  36. public boolean saveEntry( final Entry entry )
  37. {
  38. boolean success = false;
  39. if( ! ( success = entry.save(validate:false,flush:true)) )
  40. {
  41. log.error( "Updating entry: ${entry.id} FAILED");
  42. // entry.errors.allErrors.each { p rintln it };
  43. }
  44. return success;
  45. }
  46. // give me everything for this channel, as long this user has not asked to hide it.
  47. @Transactional(propagation = Propagation.REQUIRED)
  48. public List<Entry> getAllNonHiddenEntriesForUser( final User user )
  49. {
  50. List<Entry> entries = new ArrayList<Entry>();
  51. log.debug( "called getAllNonHiddenEntriesForUser( final User user )");
  52. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link where user.userId = ? and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user order by entry.dateCreated desc", [user.userId] );
  53. for( Object o : temp )
  54. {
  55. // object array with Entry and Link
  56. Entry e = o[0];
  57. UserEntryScoreLink link = o[1];
  58. e.link = link;
  59. entries.add( e );
  60. }
  61. return entries;
  62. }
  63. @Transactional(propagation = Propagation.REQUIRED)
  64. public long getCountNonHiddenEntriesForUser( final Channel channel, final User user )
  65. {
  66. long numEntries = 0l;
  67. Object[] o = Entry.executeQuery( "select count(entry) from Entry as entry, User as user, ChannelEntryLink as clink "
  68. + " where user.userId = ? and clink.entry = entry and clink.channel = ? and entry not in elements(user.hiddenEntries)", [user.userId, channel] );
  69. numEntries = ((Long)o[0]).longValue();
  70. return numEntries;
  71. }
  72. @Transactional(propagation = Propagation.REQUIRED)
  73. public List<Entry> getAllNonHiddenEntriesForUser( final Channel channel, final User user )
  74. {
  75. List<Entry> entries = new ArrayList<Entry>();
  76. log.debug( "called getAllNonHiddenEntriesForUser( final Channel channel, final User user )");
  77. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, "
  78. + " ChannelEntryLink as clink where user.userId = ? and clink.entry = entry and clink.channel = ? "
  79. + " and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user "
  80. + " order by entry.dateCreated desc", [user.userId, channel] );
  81. for( Object o : temp )
  82. {
  83. // object array with Entry and Link
  84. Entry e = o[0];
  85. UserEntryScoreLink link = o[1];
  86. e.link = link;
  87. entries.add( e );
  88. }
  89. return entries;
  90. }
  91. // give me everything for this channel, as long this user has not asked to hide it.
  92. @Transactional(propagation = Propagation.REQUIRED)
  93. public List<Entry> getAllNonHiddenEntriesForUser( final User user, final int maxResults, final int offset )
  94. {
  95. List<Entry> entries = new ArrayList<Entry>();
  96. log.debug( "called getAllNonHiddenEntriesForUser( final User user, final int maxResults, final int offset )");
  97. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link where user.userId = ? and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user order by entry.dateCreated desc", [user.userId], [max:maxResults, offset:offset]);
  98. for( Object o : temp )
  99. {
  100. // object array with Entry and Link
  101. Entry e = o[0];
  102. UserEntryScoreLink link = o[1];
  103. e.link = link;
  104. entries.add( e );
  105. }
  106. return entries;
  107. }
  108. @Transactional(propagation = Propagation.REQUIRED)
  109. public List<Entry> getAllNonHiddenEntriesForUser( final Channel channel, final User user, final int maxResults, final int offset )
  110. {
  111. List<Entry> entries = new ArrayList<Entry>();
  112. log.debug( "called getAllNonHiddenEntriesForUser( final Channel channel, final User user, final int maxResults )");
  113. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, "
  114. + "ChannelEntryLink as clink where user.userId = ? and clink.entry = entry and clink.channel = ? "
  115. + " and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user "
  116. + " order by entry.dateCreated desc", [user.userId, channel], [max:maxResults, offset:offset] );
  117. for( Object o : temp )
  118. {
  119. // object array with Entry and Link
  120. Entry e = o[0];
  121. UserEntryScoreLink link = o[1];
  122. e.link = link;
  123. entries.add( e );
  124. }
  125. return entries;
  126. }
  127. @Transactional(propagation = Propagation.REQUIRED)
  128. public List<Entry> getAllEntries()
  129. {
  130. List<Entry> entries = new ArrayList<Entry>();
  131. log.debug( "called getAllEntries()" );
  132. entries.addAll( Entry.executeQuery( "select entry from Entry as entry order by entry.dateCreated desc") );
  133. return entries;
  134. }
  135. @Transactional(propagation = Propagation.REQUIRED)
  136. public List<Entry> getAllEntries(final Channel channel)
  137. {
  138. List<Entry> entries = new ArrayList<Entry>();
  139. log.debug( "called getAllEntries(final Channel channel)");
  140. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? "
  141. + " order by entry.dateCreated desc", [channel] ) );
  142. return entries;
  143. }
  144. @Transactional(propagation = Propagation.REQUIRED)
  145. public List<Entry> getAllEntries( final User user )
  146. {
  147. List<Entry> entries = new ArrayList<Entry>();
  148. log.debug( "called getAllEntries( final User user )" );
  149. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, UserEntryScoreLink as link where entry.submitter = ? and link.entry = entry and link.user = ? order by entry.dateCreated desc", [user, user] );
  150. for( Object o : temp )
  151. {
  152. // object array with Entry and Link
  153. Entry e = o[0];
  154. UserEntryScoreLink link = o[1];
  155. e.link = link;
  156. entries.add( e );
  157. }
  158. return entries;
  159. }
  160. @Transactional(propagation = Propagation.REQUIRED)
  161. public long getCountAllEntries( final Channel channel )
  162. {
  163. long numEntries = 0l;
  164. Object o = Entry.executeQuery( "select count(entry) from Entry as entry, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ?", [channel] );
  165. numEntries = ((Long)o[0]).longValue();
  166. return numEntries;
  167. }
  168. @Transactional(propagation = Propagation.REQUIRED)
  169. public List<Entry> getAllEntries( final int maxResults, final int offset )
  170. {
  171. List<Entry> entries = new ArrayList<Entry>();
  172. log.debug( "called getAllEntries( final int maxResults )" );
  173. entries.addAll( Entry.executeQuery( "select entry from Entry as entry order by entry.dateCreated desc", [], [max:maxResults, offset:offset]) );
  174. return entries;
  175. }
  176. @Transactional(propagation = Propagation.REQUIRED)
  177. public List<Entry> getAllEntries(final Channel channel, final int maxResults, final int offset )
  178. {
  179. List<Entry> entries = new ArrayList<Entry>();
  180. log.debug( "called getAllEntries(final Channel channel, final int maxResults )" );
  181. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? "
  182. + " order by entry.dateCreated desc", [channel], [max:maxResults, offset:offset] ) );
  183. return entries;
  184. }
  185. @Transactional(propagation = Propagation.REQUIRED)
  186. public List<Entry> getAllEntries( final User user, final int maxResults, final int offset )
  187. {
  188. List<Entry> entries = new ArrayList<Entry>();
  189. log.debug( "called getAllEntries( final User user, final int maxResults )" );
  190. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, UserEntryScoreLink as link where entry.submitter = ? and link.entry = entry and link.user = ? order by entry.dateCreated desc", [user, user], [max:maxResults, offset:offset] );
  191. for( Object o : temp )
  192. {
  193. // object array with Entry and Link
  194. Entry e = o[0];
  195. UserEntryScoreLink link = o[1];
  196. e.link = link;
  197. entries.add( e );
  198. }
  199. return entries;
  200. }
  201. /* get hot entries */
  202. @Transactional(propagation = Propagation.REQUIRED)
  203. public List<Entry> getHotEntriesForUser( final Channel channel, final User user )
  204. {
  205. List<Entry> entries = new ArrayList<Entry>();
  206. log.debug( "called getHotEntriesForUser( final Channel channel, final User user )" );
  207. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, ChannelEntryLink as clink where user.userId = ? "
  208. + " and clink.entry = entry and clink.channel = ? and entry not in elements(user.hiddenEntries) and link.entry = entry "
  209. + " and link.user = user order by link.entryHotness desc", [user.userId, channel] );
  210. for( Object o : temp )
  211. {
  212. // object array with Entry and Link
  213. Entry e = o[0];
  214. UserEntryScoreLink link = o[1];
  215. e.link = link;
  216. entries.add( e );
  217. }
  218. return entries;
  219. }
  220. @Transactional(propagation = Propagation.REQUIRED)
  221. public List<Entry> getHotEntries(final Channel channel)
  222. {
  223. List<Entry> entries = new ArrayList<Entry>();
  224. log.debug( "getHotEntries(final Channel channel)" );
  225. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, UserEntryScoreLink as link, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? and link.entry = entry order by link.entryHotness desc", [channel] ) );
  226. return entries;
  227. }
  228. @Transactional(propagation = Propagation.REQUIRED)
  229. public List<Entry> getHotEntriesForUser( final Channel channel, final User user, final int maxResults, final int offset )
  230. {
  231. List<Entry> entries = new ArrayList<Entry>();
  232. log.debug( "called getHotEntriesForUser( final Channel channel, final User user )" );
  233. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, ChannelEntryLink as clink where user.userId = ? "
  234. + " and clink.entry = entry and clink.channel = ? and entry not in elements(user.hiddenEntries) and link.entry = entry "
  235. + " and link.user = user order by link.entryHotness desc", [user.userId, channel], [max:maxResults, offset:offset] );
  236. for( Object o : temp )
  237. {
  238. // object array with Entry and Link
  239. Entry e = o[0];
  240. UserEntryScoreLink link = o[1];
  241. e.link = link;
  242. entries.add( e );
  243. }
  244. return entries;
  245. }
  246. @Transactional(propagation = Propagation.REQUIRED)
  247. public List<Entry> getHotEntries(final Channel channel, final int maxResults, final int offset )
  248. {
  249. List<Entry> entries = new ArrayList<Entry>();
  250. log.debug( "getHotEntries(final Channel channel)" );
  251. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, UserEntryScoreLink as link, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? and link.entry = entry order by link.entryHotness desc", [channel], [max:maxResults, offset:offset] ) );
  252. return entries;
  253. }
  254. /* get new entries */
  255. @Transactional(propagation = Propagation.REQUIRED)
  256. public List<Entry> getNewEntriesForUser(final Channel channel, final User user )
  257. {
  258. List<Entry> entries = new ArrayList<Entry>();
  259. log.debug( "getNewEntriesForUser(final Channel channel, final User user )" );
  260. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, ChannelEntryLink as clink "
  261. + " where user.userId = ? and clink.entry = entry and clink.channel = ? "
  262. + " and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user "
  263. + " order by entry.dateCreated desc", [user.userId, channel] );
  264. for( Object o : temp )
  265. {
  266. // object array with Entry and Link
  267. Entry e = o[0];
  268. UserEntryScoreLink link = o[1];
  269. e.link = link;
  270. entries.add( e );
  271. }
  272. return entries;
  273. }
  274. @Transactional(propagation = Propagation.REQUIRED)
  275. public List<Entry> getNewEntries(final Channel channel)
  276. {
  277. List<Entry> entries = new ArrayList<Entry>();
  278. log.debug( "getNewEntries(final Channel channel)" );
  279. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? order by entry.dateCreated desc", [channel] ) );
  280. return entries;
  281. }
  282. @Transactional(propagation = Propagation.REQUIRED)
  283. public List<Entry> getNewEntriesForUser(final Channel channel, final User user, final int maxResults, final int offset )
  284. {
  285. List<Entry> entries = new ArrayList<Entry>();
  286. log.debug( "getNewEntriesForUser(final Channel channel, final User user )" );
  287. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, ChannelEntryLink as clink "
  288. + " where user.userId = ? and clink.entry = entry and clink.channel = ? "
  289. + " and entry not in elements(user.hiddenEntries) and link.entry = entry "
  290. + " and link.user = user order by entry.dateCreated desc", [user.userId, channel] );
  291. for( Object o : temp )
  292. {
  293. // object array with Entry and Link
  294. Entry e = o[0];
  295. UserEntryScoreLink link = o[1];
  296. e.link = link;
  297. entries.add( e );
  298. }
  299. return entries;
  300. }
  301. @Transactional(propagation = Propagation.REQUIRED)
  302. public List<Entry> getNewEntries(final Channel channel, final int maxResults, final int offset )
  303. {
  304. List<Entry> entries = new ArrayList<Entry>();
  305. log.debug( "getNewEntries(final Channel channel)" );
  306. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? order by entry.dateCreated desc", [channel] ) );
  307. return entries;
  308. }
  309. /* get top entries */
  310. @Transactional(propagation = Propagation.REQUIRED)
  311. public List<Entry> getTopEntriesForUser(final Channel channel, final User user )
  312. {
  313. List<Entry> entries = new ArrayList<Entry>();
  314. log.debug("getTopEntriesForUser(final Channel channel, final User user )");
  315. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link where user.userId = ? and entry.channel = ? and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user order by link.entryBaseScore desc", [user.userId, channel] )
  316. for( Object o : temp )
  317. {
  318. // object array with Entry and Link
  319. Entry e = o[0];
  320. UserEntryScoreLink link = o[1];
  321. e.link = link;
  322. entries.add( e );
  323. }
  324. return entries;
  325. }
  326. // note: get "top" with no user specified basically means that the user defaults to the "anonymous" user. This way we can still use the
  327. // UEL table normally... same for hotness and controversy...
  328. @Transactional(propagation = Propagation.REQUIRED)
  329. public List<Entry> getTopEntries(final Channel channel)
  330. {
  331. List<Entry> entries = new ArrayList<Entry>();
  332. log.debug( "called getTopEntries(final Channel channel)");
  333. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? order by entry.dateCreated desc", [channel] ) );
  334. return entries;
  335. }
  336. @Transactional(propagation = Propagation.REQUIRED)
  337. public List<Entry> getTopEntriesForUser(final Channel channel, final User user, final int maxResults, final int offset )
  338. {
  339. List<Entry> entries = new ArrayList<Entry>();
  340. log.debug( "getTopEntriesForUser(final Channel channel, final User user )");
  341. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, ChannelEntryLink as clink "
  342. + " where user.userId = ? and clink.entry = entry and clink.channel = ? and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user order by link.entryBaseScore desc", [user.userId, channel], [max:maxResults, offset:offset] );
  343. for( Object o : temp )
  344. {
  345. // object array with Entry and Link?
  346. Entry e = o[0];
  347. UserEntryScoreLink link = o[1];
  348. e.link = link;
  349. entries.add( e );
  350. }
  351. return entries;
  352. }
  353. @Transactional(propagation = Propagation.REQUIRED)
  354. public List<Entry> getTopEntries(final Channel channel, final int maxResults, final int offset )
  355. {
  356. List<Entry> entries = new ArrayList<Entry>();
  357. log.debug( "called getTopEntries(final Channel channel)");
  358. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, ChannelEntryLink as clink where clink.entry = entry and clink.channel = ? order by entry.dateCreated desc", [channel], [max:maxResults, offset:offset] ) );
  359. return entries;
  360. }
  361. /* get controversial entries */
  362. @Transactional(propagation = Propagation.REQUIRED)
  363. public List<Entry> getControversialEntriesForUser( final Channel channel, final User user )
  364. {
  365. List<Entry> entries = new ArrayList<Entry>();
  366. log.debug( "called getControversialEntriesForUser( final Channel channel, final User user )" );
  367. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, ChannelEntryLink as clink "
  368. + " where user.userId = ? and clink.entry = entry and clink.channel = ? and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user order by link.entryControversy desc", [user.userId, channel] );
  369. for( Object o : temp )
  370. {
  371. // object array with Entry and Link
  372. Entry e = o[0];
  373. UserEntryScoreLink link = o[1];
  374. e.link = link;
  375. entries.add( e );
  376. }
  377. return entries;
  378. }
  379. @Transactional(propagation = Propagation.REQUIRED)
  380. public List<Entry> getControversialEntries(final Channel channel)
  381. {
  382. List<Entry> entries = new ArrayList<Entry>();
  383. log.debug( "called getControversialEntries(final Channel channel)" );
  384. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, UserEntryScoreLink as link, ChannelEntryLink as clink "
  385. + " where clink.entry = entry and clink.channel = ? and link.entry = entry order by link.entryControversy desc", [channel] ) );
  386. return entries;
  387. }
  388. @Transactional(propagation = Propagation.REQUIRED)
  389. public List<Entry> getControversialEntriesForUser( final Channel channel, final User user, final int maxResults, final int offset )
  390. {
  391. List<Entry> entries = new ArrayList<Entry>();
  392. log.debug( "called getControversialEntriesForUser( final Channel channel, final User user )" );
  393. List<Object> temp = Entry.executeQuery( "select entry, link from Entry as entry, User as user, UserEntryScoreLink as link, ChannelEntryLink as clink "
  394. + " where user.userId = ? and clink.entry = entry and clink.channel = ? and entry not in elements(user.hiddenEntries) and link.entry = entry and link.user = user order by link.entryControversy desc", [user.userId, channel], [max:maxResults, offset:offset] );
  395. for( Object o : temp )
  396. {
  397. // object array with Entry and Link
  398. Entry e = o[0];
  399. UserEntryScoreLink link = o[1];
  400. e.link = link;
  401. entries.add( e );
  402. }
  403. return entries;
  404. }
  405. @Transactional(propagation = Propagation.REQUIRED)
  406. public List<Entry> getControversialEntries(final Channel channel, final int maxResults, final int offset )
  407. {
  408. List<Entry> entries = new ArrayList<Entry>();
  409. log.debug( "called getControversialEntries(final Channel channel)" );
  410. entries.addAll( Entry.executeQuery( "select entry from Entry as entry, UserEntryScoreLink as link, ChannelEntryLink as clink "
  411. + " where clink.entry = entry and clink.channel = ? and link.entry = entry order by link.entryControversy desc", [channel], [max:maxResults, offset:offset] ) );
  412. return entries;
  413. }
  414. /* get saved entries */
  415. @Transactional(propagation = Propagation.REQUIRED)
  416. public List<Entry> getSavedEntriesForUser( final User user )
  417. {
  418. log.debug("called getSavedEntriesForUser( final User user )" );
  419. List<Entry> entries = new ArrayList<Entry>();
  420. def theUser = User.findByUserId( user.userId );
  421. log.debug( "found user: ${theUser}" );
  422. // def tempEntries = theUser.savedEntries;
  423. List<Object> temp = Entry.executeQuery( "select entry, link from User as user inner join user.savedEntries as entry, UserEntryScoreLink as link where user = ? and entry = link.entry and link.user = ?", [user, user] );
  424. for( Object o : temp )
  425. {
  426. // object array with Entry and Link
  427. Entry e = o[0];
  428. UserEntryScoreLink link = o[1];
  429. e.link = link;
  430. entries.add( e );
  431. }
  432. return entries;
  433. }
  434. /* get hidden entries */
  435. @Transactional(propagation = Propagation.REQUIRED)
  436. public List<Entry> getHiddenEntriesForUser( final User user )
  437. {
  438. List<Entry> entries = new ArrayList<Entry>();
  439. log.debug( "called getHiddenEntriesForUser( final User user )" );
  440. def theUser = User.findByUserId( user.userId );
  441. def tempEntries = theUser.hiddenEntries;
  442. entries.addAll( tempEntries );
  443. return entries;
  444. }
  445. @Transactional(propagation = Propagation.REQUIRED)
  446. public List<Entry> getCommentsForUser( final User user )
  447. {
  448. List<Comment> comments = new ArrayList<Comment>();
  449. comments.addAll( Comment.executeQuery( "select comment from Comment as comment where comment.creator = ?", [user] ) );
  450. return comments;
  451. }
  452. }